diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index da037c997..445d5f8fc 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -197,7 +197,7 @@ void perMain() #endif #if defined(REV9E) && !defined(SIMU) - bt_wakeup(); + bluetoothWakeup(); #endif #if defined(PCBTARANIS) diff --git a/radio/src/targets/taranis/bluetooth_driver.cpp b/radio/src/targets/taranis/bluetooth_driver.cpp index 60eb35c6d..0c558fa6c 100755 --- a/radio/src/targets/taranis/bluetooth_driver.cpp +++ b/radio/src/targets/taranis/bluetooth_driver.cpp @@ -12,26 +12,33 @@ #include "string.h" #include "../../fifo.h" -Fifo<200> btTxFifo; -Fifo<200> btRxFifo; +Fifo<64> btTxFifo; +Fifo<64> btRxFifo; -int bt_open() +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_GPIO_PIN_EN; + GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_BRTS; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_Init(BT_GPIO_EN, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_BRTS; GPIO_Init(BT_GPIO_BRTS, &GPIO_InitStructure); + GPIO_SetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS); + + GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_EN; + GPIO_Init(BT_GPIO_EN, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_BCTS; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_Init(BT_GPIO_BCTS, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_TX|BT_GPIO_PIN_RX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; @@ -41,7 +48,7 @@ int bt_open() GPIO_PinAFConfig(BT_GPIO_TXRX, BT_GPIO_PinSource_TX, BT_GPIO_AF); GPIO_PinAFConfig(BT_GPIO_TXRX, BT_GPIO_PinSource_RX, BT_GPIO_AF); - USART_InitStructure.USART_BaudRate = 115200; + 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; @@ -59,25 +66,36 @@ int bt_open() NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); - GPIO_SetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS); GPIO_ResetBits(BT_GPIO_EN, BT_GPIO_PIN_EN); // open bluetooth - - return 0; } -uint8_t bt_state = 0; +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; extern "C" void USART6_IRQHandler(void) { -#if 1 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); } -#endif -#if 1 if (USART_GetITStatus(BT_USART, USART_IT_TXE) != RESET) { uint8_t byte; bool result = btTxFifo.pop(byte); @@ -86,52 +104,116 @@ extern "C" void USART6_IRQHandler(void) } else { USART_ITConfig(BT_USART, USART_IT_TXE, DISABLE); - GPIO_SetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS); - bt_state = 0; + bluetoothWriteState = BLUETOOTH_WRITE_DONE; } } -#endif } -int bt_write(const void *buffer, int len) +void bluetoothWrite(const void * buffer, int len) { - uint8_t *data = (uint8_t *)buffer; + uint8_t * data = (uint8_t *)buffer; for (int i=0; i waitEnd) { + 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) { + bluetoothState = BLUETOOTH_OK; + } + else { + bluetoothInit(9600); + const char btMessage[] = "TTM:BPS-115200"; + bluetoothWriteString(btMessage); + bluetoothState = BLUETOOTH_WAIT_BAUDRATE_CHANGE; + waitEnd = get_tmr10ms() + 250; + } + } + } + else if (bluetoothState == BLUETOOTH_WAIT_BAUDRATE_CHANGE) { + if (get_tmr10ms() > waitEnd) { + bluetoothInit(115200); + bluetoothState = BLUETOOTH_OK; + } + } + } + + bluetoothWriteWakeup(); +} + +uint8_t bluetoothReady() +{ + return (bluetoothState == BLUETOOTH_OK); +} + +int bluetoothRead(void * buffer, int len) { int result = 0; - uint8_t *data = (uint8_t *)buffer; - while (1) { + uint8_t * data = (uint8_t *)buffer; + while (result < len) { uint8_t byte; - if (!btRxFifo.pop(byte)) - return result; + if (!btRxFifo.pop(byte)) { + break; + } data[result++] = byte; - if (result >= len) - return result; } + return result; } -int bt_close() +void bluetoothDone() { GPIO_SetBits(BT_GPIO_EN, BT_GPIO_PIN_EN); // close bluetooth - return 0; } diff --git a/radio/src/targets/taranis/board_taranis.cpp b/radio/src/targets/taranis/board_taranis.cpp index 862b5293f..cd1a518ab 100644 --- a/radio/src/targets/taranis/board_taranis.cpp +++ b/radio/src/targets/taranis/board_taranis.cpp @@ -132,7 +132,7 @@ void boardInit() init2MhzTimer(); init5msTimer(); __enable_irq(); - eepromInit(); + i2cInit(); usbInit(); #if defined(HAPTIC) @@ -140,7 +140,7 @@ void boardInit() #endif #if defined(REV9E) - bt_open(); + bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE); #endif #if defined(DEBUG) diff --git a/radio/src/targets/taranis/board_taranis.h b/radio/src/targets/taranis/board_taranis.h index 44ae02b46..5849ab10b 100644 --- a/radio/src/targets/taranis/board_taranis.h +++ b/radio/src/targets/taranis/board_taranis.h @@ -288,8 +288,8 @@ void usbStop(void); } #endif -// EEPROM driver -void eepromInit(void); +// I2C driver: EEPROM + Audio Volume +void i2cInit(void); void eepromReadBlock(uint8_t * buffer, uint16_t address, uint16_t size); void eepromWriteBlock(uint8_t * buffer, uint16_t address, uint16_t size); @@ -321,7 +321,7 @@ void hapticOff(void); #endif // SERIAL_USART driver -#define DEBUG_BAUDRATE 115200 +#define DEBUG_BAUDRATE 115200 void uart3Init(unsigned int mode, unsigned int protocol); void uart3Putc(const char c); #define telemetrySecondPortInit(protocol) uart3Init(UART_MODE_TELEMETRY, protocol) @@ -329,10 +329,13 @@ void uart3SbusInit(void); void uart3Stop(void); // BT driver -int bt_open(void); -int bt_write(const void *buffer, int len); -int bt_read(void *buffer, int len); -void bt_wakeup(void); +#define BLUETOOTH_DEFAULT_BAUDRATE 115200 +uint8_t bluetoothReady(); +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); // Top LCD driver #if defined(REV9E) diff --git a/radio/src/targets/taranis/bootloader/boot.cpp b/radio/src/targets/taranis/bootloader/boot.cpp index 036b4908c..66612198a 100755 --- a/radio/src/targets/taranis/bootloader/boot.cpp +++ b/radio/src/targets/taranis/bootloader/boot.cpp @@ -123,7 +123,6 @@ TCHAR backupFilename[60]; uint32_t Master_frequency; volatile uint8_t Tenms; -uint8_t EE_timer; FIL FlashFile; DIR Dj; @@ -423,18 +422,12 @@ int main() lcd_putsLeft(0, BOOTLOADER_TITLE); lcd_invert_line(0); lcdRefresh(); -#if defined(PCBSKY9X) - OptrexDisplay = 0; - lcdRefresh(); -#endif -#if defined(PCBTARANIS) #if defined(REVPLUS) turnBacklightOn(0, 0); #endif keysInit(); - eepromInit(); -#endif + i2cInit(); __enable_irq(); init10msTimer(); @@ -444,13 +437,6 @@ int main() init_spi(); #endif -#if defined(PCBSKY9X) - LockBits = readLockBits(); - if (LockBits) { - clearLockBits(); - } -#endif - #if defined(PCBTARANIS) // SD card detect pin sdInit(); @@ -462,15 +448,6 @@ int main() wdt_reset(); if (Tenms) { - - if (EE_timer) { - if (--EE_timer == 0) { -#if defined(PCBSKY9X) - writeBlock(); -#endif - } - } - Tenms = 0; lcdRefreshWait(); diff --git a/radio/src/targets/taranis/hal.h b/radio/src/targets/taranis/hal.h index 07f1af07f..5746e657c 100644 --- a/radio/src/targets/taranis/hal.h +++ b/radio/src/targets/taranis/hal.h @@ -549,8 +549,10 @@ #define BT_GPIO_PIN_RX GPIO_Pin_9 // PG.09 #define BT_GPIO_EN GPIOD #define BT_GPIO_PIN_EN GPIO_Pin_11 // PD.11 - #define BT_GPIO_BRTS GPIOB - #define BT_GPIO_PIN_BRTS GPIO_Pin_10 + #define BT_GPIO_BRTS GPIOE + #define BT_GPIO_PIN_BRTS GPIO_Pin_12 // PE.12 + #define BT_GPIO_BCTS GPIOG + #define BT_GPIO_PIN_BCTS GPIO_Pin_6 // PG.06 #define BT_GPIO_PinSource_TX GPIO_PinSource14 #define BT_GPIO_PinSource_RX GPIO_PinSource9 #endif diff --git a/radio/src/targets/taranis/i2c_driver.cpp b/radio/src/targets/taranis/i2c_driver.cpp index 6cf0ca7c8..747d3a2b4 100644 --- a/radio/src/targets/taranis/i2c_driver.cpp +++ b/radio/src/targets/taranis/i2c_driver.cpp @@ -3,7 +3,7 @@ void eepromPageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite); void eepromWaitEepromStandbyState(void); -void eepromInit(void) +void i2cInit() { I2C_DeInit(I2C); @@ -113,7 +113,7 @@ bool I2C_EE_ReadBlock(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRea void eepromReadBlock(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead) { while (!I2C_EE_ReadBlock(pBuffer, ReadAddr, NumByteToRead)) { - eepromInit(); + i2cInit(); } } @@ -192,7 +192,7 @@ bool I2C_EE_PageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWri void eepromPageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite) { while (!I2C_EE_PageWrite(pBuffer, WriteAddr, NumByteToWrite)) { - eepromInit(); + i2cInit(); } } @@ -218,7 +218,7 @@ bool I2C_EE_WaitEepromStandbyState(void) void eepromWaitEepromStandbyState(void) { while (!I2C_EE_WaitEepromStandbyState()) { - eepromInit(); + i2cInit(); } } diff --git a/radio/src/telemetry/frsky.cpp b/radio/src/telemetry/frsky.cpp index 078106d56..a9fe02453 100644 --- a/radio/src/telemetry/frsky.cpp +++ b/radio/src/telemetry/frsky.cpp @@ -170,6 +170,19 @@ NOINLINE void processSerialData(uint8_t data) } #endif +#if defined(PCBTARANIS) && defined(REV9E) + #define BLUETOOTH_BUFFER_LENGTH 20 + static uint8_t bluetoothBuffer[BLUETOOTH_BUFFER_LENGTH]; + static uint8_t bluetoothIndex = 0; + bluetoothBuffer[bluetoothIndex++] = data; + if (bluetoothIndex == BLUETOOTH_BUFFER_LENGTH) { + if (bluetoothReady()) { + bluetoothWrite(bluetoothBuffer, BLUETOOTH_BUFFER_LENGTH); + } + bluetoothIndex = 0; + } +#endif + switch (dataState) { case STATE_DATA_START: diff --git a/radio/src/telemetry/frsky_sport.cpp b/radio/src/telemetry/frsky_sport.cpp index c0107b006..cc05c7d01 100644 --- a/radio/src/telemetry/frsky_sport.cpp +++ b/radio/src/telemetry/frsky_sport.cpp @@ -511,11 +511,11 @@ void sportFirmwareUpdate(ModuleIndex module, const char *filename) { bool result = sportUpdatePowerOn(module); if (result) - result = sportUpdateReqVersion(); + result = sportUpdateReqVersion(); if (result) - result = sportUpdateUploadFile(filename); + result = sportUpdateUploadFile(filename); if (result) - result = sportUpdateEnd(); + result = sportUpdateEnd(); if (result == false) { POPUP_WARNING("Firmware Update Error");