1
0
Fork 0
mirror of https://github.com/EdgeTX/edgetx.git synced 2025-07-26 01:35:16 +03:00

Bluetooth flash firmware continued

This commit is contained in:
Bertrand Songis 2019-04-25 15:08:38 +02:00
parent 798ee0ffee
commit 3323b0a82f
18 changed files with 112 additions and 45 deletions

View file

@ -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();

View file

@ -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;

View file

@ -1175,7 +1175,7 @@ int cliBlueTooth(const char ** argv)
serialPrint("<BT %s", line);
}
else {
bluetoothDone();
bluetoothDisable();
serialPrint("BT turned off");
}
}

View file

@ -196,10 +196,12 @@ void onSdManagerMenu(const char * result)
DeviceFirmwareUpdate device(SPORT_MODULE);
device.flashFile(lfn);
}
#if defined(BLUETOOTH)
else if (result == STR_FLASH_BLUETOOTH_MODULE) {
getSelectionFullPath(lfn);
bluetooth.flashFirmware(lfn);
}
#endif
else if (result == STR_FLASH_RECEIVER_OTA) {
getSelectionFullPath(lfn);
moduleState[EXTERNAL_MODULE].startBind(&reusableBuffer.sdManager.otaInformation, onUpdateStateChanged);
@ -342,8 +344,8 @@ void menuRadioSdManager(event_t _event)
#endif
}
#endif
#if defined(PCBXLITES)
else if (!READ_ONLY() && !strcasecmp(ext, BLUETOOTH_FIRMWARE_EXT)) {
#if defined(BLUETOOTH)
if (!READ_ONLY() && !strcasecmp(ext, BLUETOOTH_FIRMWARE_EXT)) {
POPUP_MENU_ADD_ITEM(STR_FLASH_BLUETOOTH_MODULE);
}
#endif

View file

@ -39,7 +39,7 @@ enum BluetoothWriteState
volatile uint8_t bluetoothWriteState = BLUETOOTH_WRITE_IDLE;
void bluetoothInit(uint32_t baudrate)
void bluetoothInit(uint32_t baudrate, bool enable)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = BT_EN_GPIO_PIN;
@ -88,10 +88,12 @@ void bluetoothInit(uint32_t baudrate)
btTxFifo.clear();
bluetoothWriteState = BLUETOOTH_WRITE_IDLE;
if (enable) {
GPIO_ResetBits(BT_EN_GPIO, BT_EN_GPIO_PIN); // open bluetooth
}
}
void bluetoothDone()
void bluetoothDisable()
{
GPIO_SetBits(BT_EN_GPIO, BT_EN_GPIO_PIN); // close bluetooth (recent modules will go to bootloader mode)
}
@ -107,7 +109,7 @@ extern "C" void BT_USART_IRQHandler(void)
if (!btChipPresent) {
// This is to differentiate X7 and X7S and X-Lite with/without BT
btChipPresent = 1;
bluetoothDone();
bluetoothDisable();
}
#endif
}

View file

@ -609,12 +609,13 @@ void serial2Stop(void);
#define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
// BT driver
#define BLUETOOTH_BOOTLOADER_BAUDRATE 230400
#define BLUETOOTH_FACTORY_BAUDRATE 57600
#define BLUETOOTH_DEFAULT_BAUDRATE 115200
void bluetoothInit(uint32_t baudrate);
void bluetoothInit(uint32_t baudrate, bool enable = true);
void bluetoothWriteWakeup(void);
uint8_t bluetoothIsWriting(void);
void bluetoothDone(void);
void bluetoothDisable(void);
extern uint8_t currentTrainerMode;
void checkTrainerSettings(void);

View file

@ -719,16 +719,17 @@ void serial2Stop(void);
#endif
// BT driver
#define BLUETOOTH_BOOTLOADER_BAUDRATE 230400
#define BLUETOOTH_DEFAULT_BAUDRATE 115200
#if defined(PCBX9E) && !defined(USEHORUSBT)
#define BLUETOOTH_FACTORY_BAUDRATE 9600
#else
#define BLUETOOTH_FACTORY_BAUDRATE 57600
#endif
void bluetoothInit(uint32_t baudrate);
void bluetoothInit(uint32_t baudrate, bool enable = true);
void bluetoothWriteWakeup(void);
uint8_t bluetoothIsWriting(void);
void bluetoothDone(void);
void bluetoothDisable(void);
#if defined(PCBX3)
#define IS_BLUETOOTH_CHIP_PRESENT() (false)
#elif (defined(PCBX7) || defined(PCBXLITE)) && !defined(SIMU)

View file

@ -935,7 +935,7 @@
#define TR_FLASH_BOOTLOADER "Flash BootLoaderu"
#define TR_FLASH_EXTERNAL_DEVICE TR("Flash ext. port", "Flash externího port")
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE TR("Flash vnitř. modulu", "Flash vnitřního modulu")
#define TR_FLASH_EXTERNAL_MODULE TR("Flash ext. modulu", "Flash externího zařízení")

View file

@ -944,7 +944,7 @@
#define TR_FLASH_BOOTLOADER TR("Flash Bootloader","Flash BootLoader selbst") //
#define TR_FLASH_EXTERNAL_DEVICE TR("Flash ext. Gerät","Flash externes Gerät")
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE TR("Flash int. XJT","Flash internes XJT-Modul")
#define TR_FLASH_EXTERNAL_MODULE TR("Flash ext. mod","Flash extern module")

View file

@ -938,7 +938,7 @@
#define TR_FLASH_BOOTLOADER "Flash bootLoader"
#define TR_FLASH_EXTERNAL_DEVICE TR("Flash S.Port", "Flash S.Port device")
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE TR("Flash int. module", "Flash internal module")
#define TR_FLASH_EXTERNAL_MODULE TR("Flash ext. module", "Flash external module")

View file

@ -946,7 +946,7 @@
#define TR_FLASH_BOOTLOADER "Flash BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE "Flash External Device"
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE "Flash Internal Module"
#define TR_FLASH_EXTERNAL_MODULE "Flash external module"

View file

@ -938,7 +938,7 @@
#define TR_FLASH_BOOTLOADER "Flash BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE "Flash External Device"
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE "Flash Internal Module"
#define TR_FLASH_EXTERNAL_MODULE "Flash external module"

View file

@ -950,7 +950,7 @@
#define TR_FLASH_BOOTLOADER "Flasher BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE TR("Flasher S.Port", "Flasher S.Port externe")
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION "Version courante :"
#define TR_FLASH_INTERNAL_MODULE TR("Flasher module int.", "Flasher module interne")
#define TR_FLASH_EXTERNAL_MODULE TR("Flasher module ext.", "Flasher module externe")

View file

@ -940,7 +940,7 @@
#define TR_FLASH_BOOTLOADER "Flash BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE "Progr. Dispositivo Esterno"
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE "Progr. Modulo Interno"
#define TR_FLASH_EXTERNAL_MODULE "Flash external module"

View file

@ -941,7 +941,7 @@ TR_GYR_VSRCRAW
#define TR_FLASH_BOOTLOADER "Flash BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE "Flash extern Apparaat"
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE "Flash interne XJT-Module"
#define TR_FLASH_EXTERNAL_MODULE "Flash external module"

View file

@ -941,7 +941,7 @@
#define TR_FLASH_BOOTLOADER "Flash BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE "Sflashuj Moduł Zewnętrzny"
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE "Sflashuj Moduł Wewnętrzny"
#define TR_FLASH_EXTERNAL_MODULE "Flash external module"

View file

@ -945,7 +945,7 @@
#define TR_FLASH_BOOTLOADER "Flash BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE "Flash External Device"
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE "Flash Internal Module"
#define TR_FLASH_EXTERNAL_MODULE "Flash external module"

View file

@ -954,7 +954,7 @@
#define TR_FLASH_BOOTLOADER "Skriv BootLoader"
#define TR_FLASH_EXTERNAL_DEVICE "Flash External Device"
#define TR_FLASH_RECEIVER_OTA "Flash receiver OTA"
#define TR_FLASH_BLUETOOTH_MODULE "Flash Bluetooth module"
#define TR_FLASH_BLUETOOTH_MODULE TR("Flash BT module", "Flash Bluetooth module")
#define TR_CURRENT_VERSION TR("Current vers. ", "Current version: ")
#define TR_FLASH_INTERNAL_MODULE "Flash Internal Module"
#define TR_FLASH_EXTERNAL_MODULE "Flash external module"