From 30e3095a74d0d5c27190692e5e8fa705ba171dc9 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Fri, 26 Apr 2019 08:57:19 +0200 Subject: [PATCH] Bluetooth flash firmware continued --- radio/src/bluetooth.cpp | 46 ++++++++++++------- radio/src/bluetooth.h | 3 +- .../common/arm/stm32/bluetooth_driver.cpp | 14 ++++-- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/radio/src/bluetooth.cpp b/radio/src/bluetooth.cpp index 3cf6a79ec..3074e6f99 100644 --- a/radio/src/bluetooth.cpp +++ b/radio/src/bluetooth.cpp @@ -473,28 +473,13 @@ uint8_t Bluetooth::bootloaderChecksum(uint8_t command, const uint8_t * data, uin return sum; } -void Bluetooth::sendBootloaderCommand(uint8_t command, const uint8_t * data, uint8_t size) -{ - uint8_t packet[3] = { - uint8_t(3 + size), - bootloaderChecksum(command, data, size), - command - }; - - write(packet, sizeof(packet)); - - if (size > 0) { - write(data, size); - } -} - -uint8_t Bluetooth::read(uint8_t * data, uint8_t size, uint32_t timeout) +uint8_t Bluetooth::read(uint8_t * data, uint8_t size, uint8_t timeout) { watchdogSuspend(timeout / 10); uint8_t len = 0; while (len < size) { - uint32_t elapsed = 0; + uint8_t elapsed = 0; uint8_t byte; while (!btRxFifo.pop(byte)) { RTOS_WAIT_MS(1); @@ -525,6 +510,31 @@ const char * Bluetooth::waitBootloaderCommandResponse() return "Bluetooth error"; } +const char * Bluetooth::sendBootloaderAutoBaud() +{ + uint8_t packet[2] = { + 0x55, 0x55 + }; + + write(packet, sizeof(packet)); + return waitBootloaderCommandResponse(); +} + +void Bluetooth::sendBootloaderCommand(uint8_t command, const uint8_t *data, uint8_t size) +{ + uint8_t packet[3] = { + uint8_t(3 + size), + bootloaderChecksum(command, data, size), + command + }; + + write(packet, sizeof(packet)); + + if (size > 0) { + write(data, size); + } +} + enum { CMD_GET_CHIP_ID = 0x28, @@ -537,6 +547,8 @@ const char * Bluetooth::doFlashFirmware(const char * filename) // Dummy command sendBootloaderCommand(0); result = waitBootloaderCommandResponse(); + if (result) + result = sendBootloaderAutoBaud(); if (result) return result; diff --git a/radio/src/bluetooth.h b/radio/src/bluetooth.h index 8d4ae23e6..70546f470 100644 --- a/radio/src/bluetooth.h +++ b/radio/src/bluetooth.h @@ -64,7 +64,7 @@ class Bluetooth protected: void pushByte(uint8_t byte); - uint8_t read(uint8_t * data, uint8_t size, uint32_t timeout=100/*ms*/); + uint8_t read(uint8_t * data, uint8_t size, uint8_t timeout=100/*ms*/); void write(const uint8_t * data, uint8_t length); void appendTrainerByte(uint8_t data); void processTrainerFrame(const uint8_t * buffer); @@ -75,6 +75,7 @@ class Bluetooth uint8_t bootloaderChecksum(uint8_t command, const uint8_t * data, uint8_t size); void sendBootloaderCommand(uint8_t command, const uint8_t * data = nullptr, uint8_t size = 0); const char * waitBootloaderCommandResponse(); + const char * sendBootloaderAutoBaud(); const char * doFlashFirmware(const char * filename); diff --git a/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp b/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp index 4acd9a45a..a7fa5a690 100644 --- a/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp +++ b/radio/src/targets/common/arm/stm32/bluetooth_driver.cpp @@ -34,7 +34,11 @@ enum BluetoothWriteState BLUETOOTH_WRITE_INIT, #endif BLUETOOTH_WRITING, +#if defined(BT_BRTS_GPIO_PIN) BLUETOOTH_WRITE_DONE +#else + BLUETOOTH_WRITE_DONE = BLUETOOTH_WRITE_IDLE +#endif }; volatile uint8_t bluetoothWriteState = BLUETOOTH_WRITE_IDLE; @@ -84,8 +88,6 @@ void bluetoothInit(uint32_t baudrate, bool enable) NVIC_SetPriority(BT_USART_IRQn, 6); NVIC_EnableIRQ(BT_USART_IRQn); - btRxFifo.clear(); - btTxFifo.clear(); bluetoothWriteState = BLUETOOTH_WRITE_IDLE; if (enable) { @@ -94,6 +96,9 @@ void bluetoothInit(uint32_t baudrate, bool enable) else { GPIO_SetBits(BT_EN_GPIO, BT_EN_GPIO_PIN); } + + btRxFifo.clear(); + btTxFifo.clear(); } void bluetoothDisable() @@ -108,6 +113,7 @@ extern "C" void BT_USART_IRQHandler(void) USART_ClearITPendingBit(BT_USART, USART_IT_RXNE); uint8_t byte = USART_ReceiveData(BT_USART); btRxFifo.push(byte); + TRACE("BT %02X", byte); #if defined(PCBX7) || defined(PCBXLITE) if (!btChipPresent) { // This is to differentiate X7 and X7S and X-Lite with/without BT @@ -148,13 +154,11 @@ void bluetoothWriteWakeup() bluetoothWriteState = BLUETOOTH_WRITING; USART_ITConfig(BT_USART, USART_IT_TXE, ENABLE); } -#endif 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 } +#endif } uint8_t bluetoothIsWriting(void)