From 3323b0a82fbd4b9db992d7709ce5158a9c27c391 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Thu, 25 Apr 2019 15:08:38 +0200 Subject: [PATCH] Bluetooth flash firmware continued --- radio/src/bluetooth.cpp | 85 +++++++++++++++---- radio/src/bluetooth.h | 20 +++-- radio/src/cli.cpp | 2 +- .../src/gui/common/stdlcd/radio_sdmanager.cpp | 6 +- .../common/arm/stm32/bluetooth_driver.cpp | 10 ++- radio/src/targets/horus/board.h | 5 +- radio/src/targets/taranis/board.h | 7 +- radio/src/translations/cz.h.txt | 2 +- radio/src/translations/de.h.txt | 2 +- radio/src/translations/en.h.txt | 2 +- radio/src/translations/es.h.txt | 2 +- radio/src/translations/fi.h.txt | 2 +- radio/src/translations/fr.h.txt | 2 +- radio/src/translations/it.h.txt | 2 +- radio/src/translations/nl.h.txt | 2 +- radio/src/translations/pl.h.txt | 2 +- radio/src/translations/pt.h.txt | 2 +- radio/src/translations/se.h.txt | 2 +- 18 files changed, 112 insertions(+), 45 deletions(-) diff --git a/radio/src/bluetooth.cpp b/radio/src/bluetooth.cpp index 2afefb9ca..c8e6bbb13 100644 --- a/radio/src/bluetooth.cpp +++ b/radio/src/bluetooth.cpp @@ -78,7 +78,7 @@ char * Bluetooth::readline(bool error_reset) #else TRACE("BT Reset..."); bufferIndex = 0; - bluetoothDone(); + bluetoothDisable(); state = BLUETOOTH_STATE_OFF; wakeupTime = get_tmr10ms() + 100; /* 1s */ #endif @@ -97,7 +97,7 @@ char * Bluetooth::readline(bool error_reset) TRACE("BT Error..."); #else TRACE("BT Reset..."); - bluetoothDone(); + bluetoothDisable(); state = BLUETOOTH_STATE_OFF; wakeupTime = get_tmr10ms() + 100; /* 1s */ #endif @@ -284,7 +284,7 @@ void Bluetooth::wakeup(void) #if !defined(SIMU) if (!g_eeGeneral.bluetoothMode) { if (state != BLUETOOTH_INIT) { - bluetoothDone(); + bluetoothDisable(); state = BLUETOOTH_INIT; } } @@ -356,7 +356,7 @@ void Bluetooth::wakeup() if (g_eeGeneral.bluetoothMode == BLUETOOTH_OFF || (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && !IS_BLUETOOTH_TRAINER())) { if (state != BLUETOOTH_STATE_OFF) { - bluetoothDone(); + bluetoothDisable(); state = BLUETOOTH_STATE_OFF; } wakeupTime = now + 10; /* 100ms */ @@ -464,22 +464,77 @@ void Bluetooth::wakeup() } #endif +uint8_t Bluetooth::bootloaderChecksum(uint8_t command, const uint8_t * data, uint8_t size) +{ + uint8_t sum = command; + for (uint8_t i = 0; i < size; i++) { + sum += data[i]; + } + 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 len = 0; + while (len < size) { + uint32_t elapsed = 0; + uint8_t byte; + while (!btRxFifo.pop(byte)) { + RTOS_WAIT_MS(1); + if (elapsed++ >= timeout) { + return len; + } + } + } + return len; +} + +const char * Bluetooth::waitBootloaderResponse() +{ + uint8_t response[2]; + if (read(response, 2) != 2) { + return "Bluetooth timeout"; + } + + if (response[0] != 0x00) { + return "Bluetooth error"; + } + + if (response[1] == 0xCC || response[1] == 0x33) { + return nullptr; + } + + return "Bluetooth error"; +} + void Bluetooth::flashFirmware(const char * filename) { state = BLUETOOTH_STATE_FLASH_FIRMWARE; pausePulses(); - /* go to bootloader mode */ - bluetoothDone(); + bluetoothInit(BLUETOOTH_BOOTLOADER_BAUDRATE); + bluetoothDisable(); /* go to bootloader mode */ - drawProgressScreen(getBasename(filename), "Device reset...", 0, 0); + drawProgressScreen(getBasename(filename), "Bluetooth reset...", 0, 0); - /* wait 100ms */ - watchdogSuspend(100); - RTOS_WAIT_MS(100); - - const char * result = nullptr; + sendBootloaderCommand(0); + const char * result = waitBootloaderResponse(); AUDIO_PLAY(AU_SPECIAL_SOUND_BEEP1 ); BACKLIGHT_ENABLE(); @@ -492,9 +547,9 @@ void Bluetooth::flashFirmware(const char * filename) POPUP_INFORMATION(STR_FIRMWARE_UPDATE_SUCCESS); } - /* wait 2s off */ - watchdogSuspend(2000); - RTOS_WAIT_MS(2000); + /* wait 1s off */ + watchdogSuspend(1000); + RTOS_WAIT_MS(1000); state = BLUETOOTH_STATE_OFF; resumePulses(); diff --git a/radio/src/bluetooth.h b/radio/src/bluetooth.h index 816ef492d..75c935c90 100644 --- a/radio/src/bluetooth.h +++ b/radio/src/bluetooth.h @@ -51,17 +51,10 @@ enum BluetoothStates { class Bluetooth { public: - void write(const uint8_t * data, uint8_t length); void writeString(const char * str); char * readline(bool error_reset = true); - void processTrainerFrame(const uint8_t * buffer); - void appendTrainerByte(uint8_t data); - void processTrainerByte(uint8_t data); - void pushByte(uint8_t byte); - void sendTrainer(); void forwardTelemetry(const uint8_t * packet); - void receiveTrainer(); void wakeup(); void flashFirmware(const char * filename); @@ -70,6 +63,19 @@ class Bluetooth char distantAddr[LEN_BLUETOOTH_ADDR+1]; protected: + void pushByte(uint8_t byte); + uint8_t read(uint8_t * data, uint8_t size, uint32_t timeout=1000/*ms*/); + void write(const uint8_t * data, uint8_t length); + void appendTrainerByte(uint8_t data); + void processTrainerFrame(const uint8_t * buffer); + void processTrainerByte(uint8_t data); + void sendTrainer(); + void receiveTrainer(); + + 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 * waitBootloaderResponse(); + uint8_t buffer[BLUETOOTH_LINE_LENGTH+1]; uint8_t bufferIndex = 0; tmr10ms_t wakeupTime = 0; diff --git a/radio/src/cli.cpp b/radio/src/cli.cpp index bfc345922..c550f50b0 100644 --- a/radio/src/cli.cpp +++ b/radio/src/cli.cpp @@ -1175,7 +1175,7 @@ int cliBlueTooth(const char ** argv) serialPrint("