Bsongis/x7 bluetooth (#5068)

* Trainer Bluetooth support added
X7S board added (X7 has no bluetooth while X7S has)
X10 HAL updated

* Bluetooth Telemetry

* En fix
Add all languages

* Temporary test for @bsongis

* Missing translations
This commit is contained in:
Bertrand Songis
2017-08-12 00:49:54 +02:00
committed by GitHub
parent 51be7b000e
commit 8d8fef23c9
51 changed files with 972 additions and 636 deletions
+13 -12
View File
@@ -1,5 +1,4 @@
option(DISK_CACHE "Enable SD card disk cache" YES)
option(BLUETOOTH_CLI_PASSTHROUGH "Enable direct communicaton with Bluetooth module from CLI" NO)
option(UNEXPECTED_SHUTDOWN "Enable the Unexpected Shutdown screen" YES)
set(PWR_BUTTON "PRESS" CACHE STRING "Pwr button type (PRESS/SWITCH)")
@@ -103,14 +102,19 @@ if(INTERNAL_GPS)
add_definitions(-DINTERNAL_GPS)
message("Horus: Internal GPS enabled")
endif()
set(SERIAL2_DRIVER ../common/arm/stm32/serial2_driver.cpp)
set(GVAR_SCREEN model_gvars.cpp)
set(TARGET_SRC
${TARGET_SRC}
board.cpp
led_driver.cpp
extmodule_driver.cpp
../common/arm/stm32/bluetooth_driver.cpp
)
set(FIRMWARE_TARGET_SRC
${FIRMWARE_TARGET_SRC}
sdio_sd.c
@@ -120,17 +124,14 @@ set(FIRMWARE_TARGET_SRC
sdram_driver.c
startup_stm32f42_43xxx.s
)
if(PCB STREQUAL X12S)
set(FIRMWARE_TARGET_SRC
${FIRMWARE_TARGET_SRC}
bluetooth_driver.cpp
)
add_definitions(-DBLUETOOTH)
endif()
if(BLUETOOTH_CLI_PASSTHROUGH)
add_definitions(-DBLUETOOTH_CLI_PASSTHROUGH)
message("Horus: Bluetooth pass-trough enabled")
endif()
add_definitions(-DBLUETOOTH)
set(SRC
${SRC}
bluetooth.cpp
)
set(STM32LIB_SRC
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_sdio.c
STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
@@ -1,261 +0,0 @@
/*
* 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;
}
+4 -2
View File
@@ -119,6 +119,7 @@ void boardInit()
SERIAL_RCC_AHB1Periph |
TELEMETRY_RCC_AHB1Periph |
TRAINER_RCC_AHB1Periph |
BT_RCC_AHB1Periph |
AUDIO_RCC_AHB1Periph |
HAPTIC_RCC_AHB1Periph |
INTMODULE_RCC_AHB1Periph |
@@ -145,6 +146,7 @@ void boardInit()
HAPTIC_RCC_APB2Periph |
INTMODULE_RCC_APB2Periph |
EXTMODULE_RCC_APB2Periph |
BT_RCC_APB2Periph |
BL_RCC_APB2Periph,
ENABLE);
@@ -180,10 +182,10 @@ void boardInit()
hapticInit();
#if defined(BLUETOOTH)
bluetoothInit(BLUETOOTH_FACTORY_BAUDRATE);
bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE);
#endif
#if defined(PCBX12S)
#if defined(INTERNAL_GPS)
gpsInit(GPS_USART_BAUDRATE);
#endif
+1 -6
View File
@@ -527,13 +527,8 @@ 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 bluetoothWriteWakeup(void);
void bluetoothDone(void);
extern uint8_t currentTrainerMode;
+58 -32
View File
@@ -39,16 +39,20 @@
#define KEYS_GPIO_REG_RIGHT GPIOC->IDR
#define KEYS_GPIO_PIN_RIGHT GPIO_Pin_4 // PC.04
#elif defined(PCBX10)
#define KEYS_GPIO_REG_PGDN GPIOI->IDR
#define KEYS_GPIO_PIN_PGDN GPIO_Pin_11 // PI.11
#define KEYS_GPIO_REG_LEFT GPIOI->IDR
#define KEYS_GPIO_PIN_LEFT GPIO_Pin_7 // PI.07
#define KEYS_GPIO_REG_PGUP GPIOH->IDR
#define KEYS_GPIO_PIN_PGUP GPIO_Pin_14 // PH.14
#define KEYS_GPIO_REG_PGDN GPIOH->IDR
#define KEYS_GPIO_PIN_PGDN GPIO_Pin_15 // PH.15
#define KEYS_GPIO_REG_ENTER GPIOI->IDR
#define KEYS_GPIO_PIN_ENTER GPIO_Pin_8 // PI.08
#define KEYS_GPIO_PIN_ENTER GPIO_Pin_11 // PI.11
#define KEYS_GPIO_REG_PUSH GPIOI->IDR
#define KEYS_GPIO_PIN_PUSH GPIO_Pin_8 // PI.08
#define KEYS_GPIO_REG_UP GPIOI->IDR
#define KEYS_GPIO_PIN_UP GPIO_Pin_4 // PI.04
#define KEYS_GPIO_REG_DOWN GPIOI->IDR
#define KEYS_GPIO_PIN_DOWN GPIO_Pin_6 // PI.06
#define KEYS_GPIO_REG_LEFT GPIOI->IDR
#define KEYS_GPIO_PIN_LEFT GPIO_Pin_7 // PI.07
#define KEYS_GPIO_REG_RIGHT GPIOI->IDR
#define KEYS_GPIO_PIN_RIGHT GPIO_Pin_5 // PI.05
#endif
@@ -471,13 +475,13 @@
#define HAPTIC_GPIO_AF GPIO_AF_TIM9
#define HAPTIC_GPIO_PinSource GPIO_PinSource2
#elif defined(PCBX10)
#define HAPTIC_RCC_AHB1Periph RCC_AHB1Periph_GPIOB
#define HAPTIC_RCC_APB2Periph RCC_APB2ENR_TIM8EN
#define HAPTIC_GPIO GPIOB
#define HAPTIC_GPIO_PIN GPIO_Pin_0 // PB.00
#define HAPTIC_GPIO_TIMER TIM8
#define HAPTIC_GPIO_AF GPIO_AF_TIM8
#define HAPTIC_GPIO_PinSource GPIO_PinSource0
#define HAPTIC_RCC_AHB1Periph RCC_AHB1Periph_GPIOE
#define HAPTIC_RCC_APB2Periph RCC_APB2ENR_TIM9EN
#define HAPTIC_GPIO GPIOE
#define HAPTIC_GPIO_PIN GPIO_Pin_6 // PE.06
#define HAPTIC_GPIO_TIMER TIM9
#define HAPTIC_GPIO_AF GPIO_AF_TIM9
#define HAPTIC_GPIO_PinSource GPIO_PinSource6
#endif
// Internal Module
@@ -498,6 +502,13 @@
#define INTMODULE_DMA_STREAM_IRQHandler DMA2_Stream7_IRQHandler
#define INTMODULE_DMA_FLAG_TC DMA_IT_TCIF7
#define INTMODULE_DMA_CHANNEL DMA_Channel_4
#if defined(PCBX12S)
#define INTMODULE_BOOT_GPIO GPIOI
#define INTMODULE_BOOT_GPIO_PIN GPIO_PIN_9 // PC.02
#elif defined(PCBX10)
#define INTMODULE_BOOT_GPIO GPIOI
#define INTMODULE_BOOT_GPIO_PIN GPIO_PIN_9 // PI.09
#endif
#if PCBREV >= 13
#define INTMODULE_RCC_APB1Periph RCC_APB1Periph_TIM2
#define INTMODULE_RCC_APB2Periph RCC_APB2Periph_USART1
@@ -553,6 +564,11 @@
#define EXTMODULE_DMA_FLAG_TC DMA_IT_TCIF7
#endif
// Heartbeat (not used)
#define HEARTBEAT_RCC_AHB1Periph RCC_AHB1Periph_GPIOD
#define HEARTBEAT_GPIO GPIOD
#define HEARTBEAT_GPIO_PIN GPIO_Pin_12 // PD.12
// Trainer Port
#define TRAINER_RCC_AHB1Periph (RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA1)
#define TRAINER_RCC_APB1Periph RCC_APB1Periph_TIM3
@@ -586,14 +602,17 @@
#define TIMER_2MHz_TIMER TIM7
// Bluetooth
#define BT_RCC_APB2Periph RCC_APB2Periph_USART6
#define BT_USART USART6
#define BT_GPIO_AF GPIO_AF_USART6
#define BT_USART_IRQn USART6_IRQn
#define BT_GPIO_TXRX GPIOG
#define BT_TX_GPIO_PIN GPIO_Pin_14 // PG.14
#define BT_RX_GPIO_PIN GPIO_Pin_9 // PG.09
#define BT_TX_GPIO_PinSource GPIO_PinSource14
#define BT_RX_GPIO_PinSource GPIO_PinSource9
#define BT_USART_IRQHandler USART6_IRQHandler
#if defined(PCBX12S)
#define BT_RCC_APB2Periph RCC_APB2Periph_USART6
#define BT_USART USART6
#define BT_GPIO_AF GPIO_AF_USART6
#define BT_USART_IRQn USART6_IRQn
#define BT_GPIO_TXRX GPIOG
#define BT_TX_GPIO_PIN GPIO_Pin_14 // PG.14
#define BT_RX_GPIO_PIN GPIO_Pin_9 // PG.09
#if PCBREV >= 13
#define BT_RCC_AHB1Periph (RCC_AHB1Periph_GPIOI | RCC_AHB1Periph_GPIOG)
#define BT_EN_GPIO GPIOI
@@ -607,21 +626,28 @@
#define BT_BRTS_GPIO_PIN GPIO_Pin_10 // PG.10
#define BT_BCTS_GPIO GPIOG
#define BT_BCTS_GPIO_PIN GPIO_Pin_11 // PG.11
#define BT_TX_GPIO_PinSource GPIO_PinSource14
#define BT_RX_GPIO_PinSource GPIO_PinSource9
#elif defined(PCBX10)
#define BT_RCC_AHB1Periph RCC_AHB1Periph_GPIOG
#define BT_EN_GPIO GPIOG
#define BT_EN_GPIO_PIN GPIO_Pin_10 // PG.10
#endif
// GPS
#define GPS_RCC_AHB1Periph RCC_AHB1Periph_GPIOA
#define GPS_RCC_APB1Periph RCC_APB1Periph_UART4
#define GPS_USART UART4
#define GPS_GPIO_AF GPIO_AF_UART4
#define GPS_USART_IRQn UART4_IRQn
#define GPS_USART_IRQHandler UART4_IRQHandler
#define GPS_UART_GPIO GPIOA
#define GPS_TX_GPIO_PIN GPIO_Pin_0 // PA.00
#define GPS_RX_GPIO_PIN GPIO_Pin_1 // PA.01
#define GPS_TX_GPIO_PinSource GPIO_PinSource0
#define GPS_RX_GPIO_PinSource GPIO_PinSource1
#if defined(PCBX12S)
#define GPS_RCC_AHB1Periph RCC_AHB1Periph_GPIOA
#define GPS_RCC_APB1Periph RCC_APB1Periph_UART4
#define GPS_USART UART4
#define GPS_GPIO_AF GPIO_AF_UART4
#define GPS_USART_IRQn UART4_IRQn
#define GPS_USART_IRQHandler UART4_IRQHandler
#define GPS_UART_GPIO GPIOA
#define GPS_TX_GPIO_PIN GPIO_Pin_0 // PA.00
#define GPS_RX_GPIO_PIN GPIO_Pin_1 // PA.01
#define GPS_TX_GPIO_PinSource GPIO_PinSource0
#define GPS_RX_GPIO_PinSource GPIO_PinSource1
#else
#define GPS_RCC_AHB1Periph 0
#define GPS_RCC_APB1Periph 0
#endif
#endif // _HAL_H_