Bluetooth driver added to Horus, BT_EN_GPIO_PIN was wrong on beta board (#4005)

CLI interface for Bluetooth
This commit is contained in:
Damjan Adamic
2016-11-11 19:56:36 +01:00
committed by Bertrand Songis
parent 5869b81e9c
commit 7b2d8b1f0a
10 changed files with 330 additions and 10 deletions
+43 -1
View File
@@ -793,6 +793,45 @@ int cliGps(const char ** argv)
}
#endif
#if defined(PCBX9E) || defined(PCBHORUS)
int cliBlueTooth(const char ** argv)
{
int baudrate = 0;
if (argv[1][0] == '$') {
// send command to GPS
bluetoothWriteString(argv[1] + 1);
bluetoothWriteString("\r\n");
serialPrint("bt sent: %s", argv[1] + 1);
CoTickDelay(100); // 200ms
char buff[100];
int len = bluetoothRead(buff, 100);
buff[len] = 0;
serialPrint("bt read: %s", buff);
}
else if (!strcmp(argv[1], "read")) {
char buff[100];
int len = bluetoothRead(buff, 100);
buff[len] = 0;
serialPrint("bt read: %s", buff);
}
else if (toInt(argv, 1, &baudrate) > 0) {
if (baudrate > 0) {
bluetoothInit(baudrate);
serialPrint("BT baudrate set to %d", baudrate);
}
else {
bluetoothDone();
serialPrint("BT turned off");
}
}
else {
serialPrint("%s: Invalid arguments", argv[0]);
}
return 0;
}
#endif // #if defined(PCBX9E) || defined(PCBHORUS)
const CliCommand cliCommands[] = {
{ "beep", cliBeep, "[<frequency>] [<duration>]" },
{ "ls", cliLs, "<directory>" },
@@ -818,7 +857,10 @@ const CliCommand cliCommands[] = {
{ "jitter", cliShowJitter, "" },
#endif
#if defined(INTERNAL_GPS)
{ "gps", cliGps, "<baudrate>" },
{ "gps", cliGps, "<baudrate>|$<command>|trace" },
#endif
#if defined(PCBX9E) || defined(PCBHORUS)
{ "bt", cliBlueTooth, "<baudrate>|$<command>|read" },
#endif
{ NULL, NULL, NULL } /* sentinel */
};
+2 -1
View File
@@ -118,7 +118,8 @@ void dumpTraceBuffer()
"Tim1 ", // INT_TIM1CC,
"Tim2 ", // INT_TIM2,
"Tim3 ", // INT_TIM3,
"Usb " // INT_OTG_FS,
"Usb " // INT_OTG_FS,
"BlueT", // INT_BLUETOOTH,
};
#elif defined(PCBTARANIS)
const char * interruptNames[INT_LAST] = {
+1
View File
@@ -211,6 +211,7 @@ enum InterruptNames {
INT_TIM2,
INT_TRAINER,
INT_OTG_FS,
INT_BLUETOOTH,
INT_LAST
};
#elif defined(PCBTARANIS)
+1 -1
View File
@@ -477,7 +477,7 @@ void perMain()
toplcdRefreshEnd();
#endif
#if defined(PCBX9E) && !defined(SIMU)
#if (defined(PCBX9E) || defined(PCBHORUS)) && !defined(SIMU)
bluetoothWakeup();
#endif
+6
View File
@@ -9,6 +9,7 @@ else()
message("Internal GPS is optional, use INTERNAL_GPS=YES option to enable it")
endif()
endif()
option(BLUETOOTH_CLI_PASSTHROUGH "Enable direct communicaton with Bluetooth module from CLI" NO)
set(CPU_TYPE STM32F4)
set(HSE_VALUE 12000000)
@@ -76,10 +77,15 @@ set(FIRMWARE_TARGET_SRC
sdio_sd.c
lcd_driver.cpp
backlight_driver.cpp
bluetooth_driver.cpp
pwr_driver.cpp
sdram_driver.c
startup_stm32f40_41xxx.s
)
if(BLUETOOTH_CLI_PASSTHROUGH)
add_definitions(-DBLUETOOTH_CLI_PASSTHROUGH)
message("Bluetooth pass-trough enabled")
endif()
set(STM32LIB_SRC
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_sdio.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
@@ -0,0 +1,261 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "opentx.h"
Fifo<uint8_t, 64> btTxFifo;
Fifo<uint8_t, 64> btRxFifo;
enum BluetoothState
{
BLUETOOTH_INIT,
BLUETOOTH_WAIT_TTM,
BLUETOOTH_WAIT_BAUDRATE_CHANGE,
BLUETOOTH_OK,
};
enum BluetoothWriteState
{
BLUETOOTH_WRITE_IDLE,
BLUETOOTH_WRITE_INIT,
BLUETOOTH_WRITING,
BLUETOOTH_WRITE_DONE
};
volatile uint8_t bluetoothState = BLUETOOTH_INIT;
volatile uint8_t bluetoothWriteState = BLUETOOTH_WRITE_IDLE;
void bluetoothInit(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_DeInit(BT_USART);
RCC_AHB1PeriphClockCmd(BT_RCC_AHB1Periph, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
GPIO_InitStructure.GPIO_Pin = BT_EN_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(BT_EN_GPIO, &GPIO_InitStructure);
#if defined(BT_BRTS_GPIO_PIN)
GPIO_InitStructure.GPIO_Pin = BT_BRTS_GPIO_PIN;
GPIO_Init(BT_BRTS_GPIO, &GPIO_InitStructure);
GPIO_SetBits(BT_BRTS_GPIO, BT_BRTS_GPIO_PIN);
#endif
#if defined(BT_BCTS_GPIO_PIN)
GPIO_InitStructure.GPIO_Pin = BT_BCTS_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(BT_BCTS_GPIO, &GPIO_InitStructure);
#endif
GPIO_InitStructure.GPIO_Pin = BT_TX_GPIO_PIN|BT_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(BT_GPIO_TXRX, &GPIO_InitStructure);
GPIO_PinAFConfig(BT_GPIO_TXRX, BT_TX_GPIO_PinSource, BT_GPIO_AF);
GPIO_PinAFConfig(BT_GPIO_TXRX, BT_RX_GPIO_PinSource, BT_GPIO_AF);
USART_DeInit(BT_USART);
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(BT_USART, &USART_InitStructure);
USART_Cmd(BT_USART, ENABLE);
USART_ITConfig(BT_USART, USART_IT_RXNE, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = BT_USART_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_ResetBits(BT_EN_GPIO, BT_EN_GPIO_PIN); // open bluetooth
}
void bluetoothDone()
{
GPIO_SetBits(BT_EN_GPIO, BT_EN_GPIO_PIN); // close bluetooth
}
extern "C" void USART6_IRQHandler(void)
{
DEBUG_INTERRUPT(INT_BLUETOOTH);
if (USART_GetITStatus(BT_USART, USART_IT_RXNE) != RESET) {
USART_ClearITPendingBit(BT_USART, USART_IT_RXNE);
uint8_t byte = USART_ReceiveData(BT_USART);
btRxFifo.push(byte);
}
if (USART_GetITStatus(BT_USART, USART_IT_TXE) != RESET) {
uint8_t byte;
bool result = btTxFifo.pop(byte);
if (result) {
USART_SendData(BT_USART, byte);
}
else {
USART_ITConfig(BT_USART, USART_IT_TXE, DISABLE);
bluetoothWriteState = BLUETOOTH_WRITE_DONE;
}
}
}
void bluetoothWrite(const void * buffer, int len)
{
uint8_t * data = (uint8_t *)buffer;
for (int i=0; i<len; ++i) {
btTxFifo.push(data[i]);
}
}
void bluetoothWriteString(const char * str)
{
while (*str != 0) {
btTxFifo.push(*str++);
}
}
void bluetoothWriteWakeup(void)
{
if (bluetoothWriteState == BLUETOOTH_WRITE_IDLE) {
if (!btTxFifo.isEmpty()) {
bluetoothWriteState = BLUETOOTH_WRITE_INIT;
#if defined(BT_BRTS_GPIO_PIN)
GPIO_ResetBits(BT_BRTS_GPIO, BT_BRTS_GPIO_PIN);
#endif
}
}
else if (bluetoothWriteState == BLUETOOTH_WRITE_INIT) {
bluetoothWriteState = BLUETOOTH_WRITING;
USART_ITConfig(BT_USART, USART_IT_TXE, ENABLE);
}
else if (bluetoothWriteState == BLUETOOTH_WRITE_DONE) {
bluetoothWriteState = BLUETOOTH_WRITE_IDLE;
#if defined(BT_BRTS_GPIO_PIN)
GPIO_SetBits(BT_BRTS_GPIO, BT_BRTS_GPIO_PIN);
#endif
}
}
void bluetoothWakeup(void)
{
#if defined(BLUETOOTH_CLI_PASSTHROUGH)
bluetoothWriteWakeup();
return;
#else // #if defined(BLUETOOTH_CLI_PASSTHROUGH)
// NOTICE: code below doesn't work on Horus beta!
if (!g_eeGeneral.bluetoothEnable) {
if (bluetoothState != BLUETOOTH_INIT) {
bluetoothDone();
bluetoothState = BLUETOOTH_INIT;
}
}
else {
if (bluetoothState != BLUETOOTH_OK) {
static tmr10ms_t waitEnd = 0;
if (bluetoothState == BLUETOOTH_INIT) {
TRACE("BLUETOOTH_INIT");
bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE);
const char btMessage[] = "TTM:REN-";
bluetoothWriteString(btMessage);
uint8_t len = ZLEN(g_eeGeneral.bluetoothName);
for (int i=0; i<len; i++) {
btTxFifo.push(idx2char(g_eeGeneral.bluetoothName[i]));
}
bluetoothState = BLUETOOTH_WAIT_TTM;
waitEnd = get_tmr10ms() + 25; // 250ms
}
else if (bluetoothState == BLUETOOTH_WAIT_TTM) {
if (get_tmr10ms() > waitEnd) {
TRACE("bt rename");
char ttm[] = "TTM:REN";
int index = 0;
uint8_t c;
bool found = false;
while (btRxFifo.pop(c)) {
if (c == ttm[index]) {
index++;
if (index == sizeof(ttm)-1) {
found = true;
break;
}
}
else {
index = 0;
}
}
if (found) {
TRACE("bt OK");
bluetoothState = BLUETOOTH_OK;
}
else {
TRACE("bt failure");
bluetoothInit(BLUETOOTH_FACTORY_BAUDRATE);
const char btMessage[] = "TTM:BPS-115200";
bluetoothWriteString(btMessage);
bluetoothState = BLUETOOTH_WAIT_BAUDRATE_CHANGE;
waitEnd = get_tmr10ms() + 250; // 2.5s
}
}
}
else if (bluetoothState == BLUETOOTH_WAIT_BAUDRATE_CHANGE) {
if (get_tmr10ms() > waitEnd) {
bluetoothState = BLUETOOTH_INIT;
}
}
}
bluetoothWriteWakeup();
}
#endif // #if defined(BLUETOOTH_CLI_PASSTHROUGH)
}
uint8_t bluetoothReady()
{
return (bluetoothState == BLUETOOTH_OK);
}
int bluetoothRead(void * buffer, int len)
{
int result = 0;
uint8_t * data = (uint8_t *)buffer;
while (result < len) {
uint8_t byte;
if (!btRxFifo.pop(byte)) {
break;
}
data[result++] = byte;
}
return result;
}
+1 -3
View File
@@ -174,9 +174,7 @@ void boardInit()
init1msTimer();
usbInit();
hapticInit();
// bt_open();
bluetoothInit(BLUETOOTH_FACTORY_BAUDRATE);
gpsInit(GPS_USART_BAUDRATE);
#if defined(DEBUG)
+11
View File
@@ -431,6 +431,17 @@ void serial2Stop(void);
#define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
int sbusGetByte(uint8_t * byte);
// BT driver
#define BLUETOOTH_DEFAULT_BAUDRATE 115200
#define BLUETOOTH_FACTORY_BAUDRATE 9600
uint8_t bluetoothReady(void);
void bluetoothInit(uint32_t baudrate);
void bluetoothWrite(const void * buffer, int len);
void bluetoothWriteString(const char * str);
int bluetoothRead(void * buffer, int len);
void bluetoothWakeup(void);
void bluetoothDone(void);
#if defined(USB_JOYSTICK) && !defined(SIMU)
void usbJoystickUpdate(void);
#endif
+3 -3
View File
@@ -422,9 +422,9 @@
#define BT_EN_GPIO GPIOI
#define BT_EN_GPIO_PIN GPIO_Pin_10 // PI.10
#else
#define BT_RCC_AHB1Periph (RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOG)
#define BT_EN_GPIO GPIOB
#define BT_EN_GPIO_PIN GPIO_Pin_10 // PB.10
#define BT_RCC_AHB1Periph (RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOG)
#define BT_EN_GPIO GPIOA
#define BT_EN_GPIO_PIN GPIO_Pin_6 // PA.06
#endif
#define BT_BRTS_GPIO GPIOG
#define BT_BRTS_GPIO_PIN GPIO_Pin_10 // PG.10
+1 -1
View File
@@ -63,7 +63,7 @@ NOINLINE void processFrskyTelemetryData(uint8_t data)
}
#endif
#if defined(PCBX9E) && !defined(SIMU)
#if defined(PCBX9E) && !defined(SIMU) && !defined(BLUETOOTH_CLI_PASSTHROUGH)
#define BLUETOOTH_BUFFER_LENGTH 20
static uint8_t bluetoothBuffer[BLUETOOTH_BUFFER_LENGTH];
static uint8_t bluetoothIndex = 0;