From 7b2d8b1f0a1bb7058509c8f08c795cd709008b23 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Fri, 11 Nov 2016 19:56:36 +0100 Subject: [PATCH] Bluetooth driver added to Horus, BT_EN_GPIO_PIN was wrong on beta board (#4005) CLI interface for Bluetooth --- radio/src/cli.cpp | 44 +++- radio/src/debug.cpp | 3 +- radio/src/debug.h | 1 + radio/src/main_arm.cpp | 2 +- radio/src/targets/horus/CMakeLists.txt | 6 + radio/src/targets/horus/bluetooth_driver.cpp | 261 +++++++++++++++++++ radio/src/targets/horus/board.cpp | 4 +- radio/src/targets/horus/board.h | 11 + radio/src/targets/horus/hal.h | 6 +- radio/src/telemetry/frsky.cpp | 2 +- 10 files changed, 330 insertions(+), 10 deletions(-) create mode 100644 radio/src/targets/horus/bluetooth_driver.cpp diff --git a/radio/src/cli.cpp b/radio/src/cli.cpp index 3a196e55b..bf8f8cc8a 100644 --- a/radio/src/cli.cpp +++ b/radio/src/cli.cpp @@ -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, "[] []" }, { "ls", cliLs, "" }, @@ -818,7 +857,10 @@ const CliCommand cliCommands[] = { { "jitter", cliShowJitter, "" }, #endif #if defined(INTERNAL_GPS) - { "gps", cliGps, "" }, + { "gps", cliGps, "|$|trace" }, +#endif +#if defined(PCBX9E) || defined(PCBHORUS) + { "bt", cliBlueTooth, "|$|read" }, #endif { NULL, NULL, NULL } /* sentinel */ }; diff --git a/radio/src/debug.cpp b/radio/src/debug.cpp index da30be14e..5f28c57ae 100644 --- a/radio/src/debug.cpp +++ b/radio/src/debug.cpp @@ -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] = { diff --git a/radio/src/debug.h b/radio/src/debug.h index e5cb615cc..96c5d5ff6 100644 --- a/radio/src/debug.h +++ b/radio/src/debug.h @@ -211,6 +211,7 @@ enum InterruptNames { INT_TIM2, INT_TRAINER, INT_OTG_FS, + INT_BLUETOOTH, INT_LAST }; #elif defined(PCBTARANIS) diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index c902d4dd1..963e6ff92 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -477,7 +477,7 @@ void perMain() toplcdRefreshEnd(); #endif -#if defined(PCBX9E) && !defined(SIMU) +#if (defined(PCBX9E) || defined(PCBHORUS)) && !defined(SIMU) bluetoothWakeup(); #endif diff --git a/radio/src/targets/horus/CMakeLists.txt b/radio/src/targets/horus/CMakeLists.txt index 97a92866e..04fc1ae35 100644 --- a/radio/src/targets/horus/CMakeLists.txt +++ b/radio/src/targets/horus/CMakeLists.txt @@ -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 diff --git a/radio/src/targets/horus/bluetooth_driver.cpp b/radio/src/targets/horus/bluetooth_driver.cpp new file mode 100644 index 000000000..af7ad255a --- /dev/null +++ b/radio/src/targets/horus/bluetooth_driver.cpp @@ -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 btTxFifo; +Fifo 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 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; +} diff --git a/radio/src/targets/horus/board.cpp b/radio/src/targets/horus/board.cpp index efb4afa92..1a4de516e 100644 --- a/radio/src/targets/horus/board.cpp +++ b/radio/src/targets/horus/board.cpp @@ -174,9 +174,7 @@ void boardInit() init1msTimer(); usbInit(); hapticInit(); - - // bt_open(); - + bluetoothInit(BLUETOOTH_FACTORY_BAUDRATE); gpsInit(GPS_USART_BAUDRATE); #if defined(DEBUG) diff --git a/radio/src/targets/horus/board.h b/radio/src/targets/horus/board.h index b368531a3..441c48c43 100644 --- a/radio/src/targets/horus/board.h +++ b/radio/src/targets/horus/board.h @@ -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 diff --git a/radio/src/targets/horus/hal.h b/radio/src/targets/horus/hal.h index 5883f5dd3..2e00e7f7d 100644 --- a/radio/src/targets/horus/hal.h +++ b/radio/src/targets/horus/hal.h @@ -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 diff --git a/radio/src/telemetry/frsky.cpp b/radio/src/telemetry/frsky.cpp index 26c1bded2..abec941aa 100644 --- a/radio/src/telemetry/frsky.cpp +++ b/radio/src/telemetry/frsky.cpp @@ -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;