1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-21 23:35:17 +03:00

[X7] Bootloader fix

This commit is contained in:
Bertrand Songis 2019-05-13 14:20:44 +02:00
parent 8f6fc7cd50
commit 9c04f564df
4 changed files with 166 additions and 180 deletions

View file

@ -47,17 +47,13 @@ typedef void (*voidFunction)(void);
// Bootloader marker: // Bootloader marker:
// -> used to detect valid bootloader files // -> used to detect valid bootloader files
const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) = const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
{ 'B', 'O', 'O', 'T', '1', '0' }; {'B', 'O', 'O', 'T', '1', '0'};
#if defined(ROTARY_ENCODER_NAVIGATION) #if defined(ROTARY_ENCODER_NAVIGATION)
volatile rotenc_t rotencValue = 0; volatile rotenc_t rotencValue = 0;
#endif #endif
/*---------------------------------------------------------------------------- uint32_t firmwareSize;
* Local variables
*----------------------------------------------------------------------------*/
uint32_t FirmwareSize;
uint32_t firmwareAddress = FIRMWARE_ADDRESS; uint32_t firmwareAddress = FIRMWARE_ADDRESS;
uint32_t firmwareWritten = 0; uint32_t firmwareWritten = 0;
@ -66,16 +62,16 @@ uint32_t eepromAddress = 0;
uint32_t eepromWritten = 0; uint32_t eepromWritten = 0;
#endif #endif
volatile uint8_t Tenms = 1; volatile uint8_t tenms = 1;
FlashCheckRes Valid; FlashCheckRes valid;
MemoryType memoryType; MemoryType memoryType;
uint32_t unlocked = 0; uint32_t unlocked = 0;
void interrupt10ms(void) void interrupt10ms(void)
{ {
Tenms |= 1; // 10 mS has passed tenms |= 1; // 10 mS has passed
uint8_t index = 0; uint8_t index = 0;
uint8_t in = readKeys(); uint8_t in = readKeys();
@ -129,26 +125,25 @@ uint32_t isValidBufferStart(const uint8_t * buffer)
FlashCheckRes checkFlashFile(unsigned int index, FlashCheckRes res) FlashCheckRes checkFlashFile(unsigned int index, FlashCheckRes res)
{ {
if (res != FC_UNCHECKED) if (res != FC_UNCHECKED)
return res; return res;
if (openBinFile(memoryType, index) != FR_OK) if (openBinFile(memoryType, index) != FR_OK)
return FC_ERROR; return FC_ERROR;
if (closeBinFile() != FR_OK) if (closeBinFile() != FR_OK)
return FC_ERROR; return FC_ERROR;
if (!isValidBufferStart(Block_buffer)) if (!isValidBufferStart(Block_buffer))
return FC_ERROR; return FC_ERROR;
return FC_OK; return FC_OK;
} }
int menuFlashFile(uint32_t index, event_t event) int menuFlashFile(uint32_t index, event_t event)
{ {
Valid = checkFlashFile(index, Valid); valid = checkFlashFile(index, valid);
if (Valid == FC_ERROR) {
if (valid == FC_ERROR) {
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER))
return 0; return 0;
@ -211,7 +206,7 @@ int main()
SERIAL_RCC_APB1Periph | SERIAL_RCC_APB1Periph |
SD_RCC_APB1Periph, ENABLE); SD_RCC_APB1Periph, ENABLE);
RCC_APB2PeriphClockCmd(LCD_RCC_APB2Periph | BACKLIGHT_RCC_APB2Periph, ENABLE); RCC_APB2PeriphClockCmd(LCD_RCC_APB2Periph | BACKLIGHT_RCC_APB2Periph | RCC_APB2Periph_SYSCFG, ENABLE);
keysInit(); keysInit();
@ -259,7 +254,7 @@ int main()
#if defined(PWR_BUTTON_PRESS) #if defined(PWR_BUTTON_PRESS)
// wait until power button is released // wait until power button is released
while(pwrPressed()) { while (pwrPressed()) {
wdt_reset(); wdt_reset();
} }
#endif #endif
@ -267,8 +262,8 @@ int main()
for (;;) { for (;;) {
wdt_reset(); wdt_reset();
if (Tenms) { if (tenms) {
Tenms = 0; tenms = 0;
if (state != ST_USB) { if (state != ST_USB) {
if (usbPlugged()) { if (usbPlugged()) {
@ -283,16 +278,16 @@ int main()
} }
if (state == ST_USB) { if (state == ST_USB) {
if (usbPlugged() == 0) { if (usbPlugged() == 0) {
vpos = 0; vpos = 0;
usbStop(); usbStop();
if (unlocked) { if (unlocked) {
lockFlash(); lockFlash();
unlocked = 0; unlocked = 0;
}
state = ST_START;
} }
bootloaderDrawScreen(state, 0); state = ST_START;
}
bootloaderDrawScreen(state, 0);
} }
lcdRefreshWait(); lcdRefreshWait();
@ -300,60 +295,58 @@ int main()
if (state == ST_START) { if (state == ST_START) {
bootloaderDrawScreen(state, vpos); bootloaderDrawScreen(state, vpos);
if (event == EVT_KEY_FIRST(KEY_DOWN)) { if (event == EVT_KEY_FIRST(KEY_DOWN)) {
vpos = (vpos + 1) % MAIN_MENU_LEN; vpos = (vpos + 1) % MAIN_MENU_LEN;
continue; continue;
} }
else if (event == EVT_KEY_FIRST(KEY_UP)) { else if (event == EVT_KEY_FIRST(KEY_UP)) {
vpos = (vpos + MAIN_MENU_LEN - 1) % MAIN_MENU_LEN; vpos = (vpos + MAIN_MENU_LEN - 1) % MAIN_MENU_LEN;
continue; continue;
} }
else if (event == EVT_KEY_BREAK(KEY_ENTER)) { else if (event == EVT_KEY_BREAK(KEY_ENTER)) {
switch (vpos) { switch (vpos) {
case 0: case 0:
memoryType = MEM_FLASH; memoryType = MEM_FLASH;
state = ST_DIR_CHECK; state = ST_DIR_CHECK;
break; break;
#if defined(EEPROM) #if defined(EEPROM)
case 1: case 1:
memoryType = MEM_EEPROM; memoryType = MEM_EEPROM;
state = ST_DIR_CHECK; state = ST_DIR_CHECK;
break; break;
#endif #endif
default: default:
state = ST_REBOOT; state = ST_REBOOT;
break; break;
}
// next loop
continue;
} }
// next loop
continue;
}
} }
else if (state == ST_DIR_CHECK) { else if (state == ST_DIR_CHECK) {
fr = openBinDir(memoryType);
fr = openBinDir(memoryType); if (fr == FR_OK) {
index = vpos = 0;
state = ST_FILE_LIST;
nameCount = fetchBinFiles(index);
continue;
}
else {
bootloaderDrawScreen(state, fr);
if (fr == FR_OK) { if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
index = vpos = 0; vpos = 0;
state = ST_FILE_LIST; state = ST_START;
nameCount = fetchBinFiles(index); continue;
continue;
}
else {
bootloaderDrawScreen(state, fr);
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
vpos = 0;
state = ST_START;
continue;
}
} }
}
} }
if (state == ST_FILE_LIST) { if (state == ST_FILE_LIST) {
uint32_t limit = MAX_NAMES_ON_SCREEN; uint32_t limit = MAX_NAMES_ON_SCREEN;
if (nameCount < limit) { if (nameCount < limit) {
limit = nameCount; limit = nameCount;
@ -384,47 +377,46 @@ int main()
bootloaderDrawScreen(state, 0); bootloaderDrawScreen(state, 0);
for (uint32_t i=0; i<limit; i++) { for (uint32_t i = 0; i < limit; i++) {
bootloaderDrawFilename(binFiles[i].name, i, (vpos == i)); bootloaderDrawFilename(binFiles[i].name, i, (vpos == i));
} }
if (event == EVT_KEY_BREAK(KEY_ENTER)) { if (event == EVT_KEY_BREAK(KEY_ENTER)) {
// Select file to flash // Select file to flash
state = ST_FLASH_CHECK; state = ST_FLASH_CHECK;
Valid = FC_UNCHECKED; valid = FC_UNCHECKED;
continue; continue;
} }
else if (event == EVT_KEY_BREAK(KEY_EXIT)) { else if (event == EVT_KEY_BREAK(KEY_EXIT)) {
state = ST_START; state = ST_START;
vpos = 0; vpos = 0;
continue; continue;
} }
} }
else if (state == ST_FLASH_CHECK) { else if (state == ST_FLASH_CHECK) {
bootloaderDrawScreen(state, valid, binFiles[vpos].name);
bootloaderDrawScreen(state, Valid, binFiles[vpos].name); int result = menuFlashFile(vpos, event);
if (result == 0) {
// canceled
state = ST_FILE_LIST;
}
else if (result == 1) {
// confirmed
int result = menuFlashFile(vpos, event); if (memoryType == MEM_FLASH) {
if (result == 0) { firmwareSize = binFiles[vpos].size - BOOTLOADER_SIZE;
// canceled firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE;
state = ST_FILE_LIST; firmwareWritten = 0;
} }
else if (result == 1) {
// confirmed
if (memoryType == MEM_FLASH) {
FirmwareSize = binFiles[vpos].size - BOOTLOADER_SIZE;
firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE;
firmwareWritten = 0;
}
#if defined(EEPROM) #if defined(EEPROM)
else { else {
eepromAddress = 0; eepromAddress = 0;
eepromWritten = 0; eepromWritten = 0;
}
#endif
state = ST_FLASHING;
} }
#endif
state = ST_FLASHING;
}
} }
else if (state == ST_FLASHING) { else if (state == ST_FLASHING) {
@ -438,13 +430,13 @@ int main()
if (memoryType == MEM_FLASH) { if (memoryType == MEM_FLASH) {
flashWriteBlock(); flashWriteBlock();
firmwareWritten += sizeof(Block_buffer); firmwareWritten += sizeof(Block_buffer);
progress = (100*firmwareWritten) / FirmwareSize; progress = (100 * firmwareWritten) / firmwareSize;
} }
#if defined(EEPROM) #if defined(EEPROM)
else { else {
writeEepromBlock(); writeEepromBlock();
eepromWritten += sizeof(Block_buffer); eepromWritten += sizeof(Block_buffer);
progress = (100*eepromWritten) / EEPROM_SIZE; progress = (100 * eepromWritten) / EEPROM_SIZE;
} }
#endif #endif
@ -454,13 +446,11 @@ int main()
if (BlockCount == 0) { if (BlockCount == 0) {
state = ST_FLASH_DONE; // EOF state = ST_FLASH_DONE; // EOF
} }
else if ((memoryType == MEM_FLASH) && else if (memoryType == MEM_FLASH && firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE) {
(firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE)) {
state = ST_FLASH_DONE; // Backstop state = ST_FLASH_DONE; // Backstop
} }
#if defined(EEPROM) #if defined(EEPROM)
else if ((memoryType == MEM_EEPROM) && else if (memoryType == MEM_EEPROM && eepromWritten >= EEPROM_SIZE) {
(eepromWritten >= EEPROM_SIZE)) {
state = ST_FLASH_DONE; // Backstop state = ST_FLASH_DONE; // Backstop
} }
#endif #endif
@ -481,9 +471,8 @@ int main()
} }
if (event == EVT_KEY_LONG(KEY_EXIT)) { if (event == EVT_KEY_LONG(KEY_EXIT)) {
// Start the main application // Start the main application
state = ST_REBOOT;
state = ST_REBOOT;
} }
lcdRefresh(); lcdRefresh();
@ -508,20 +497,19 @@ int main()
} }
if (state == ST_REBOOT) { if (state == ST_REBOOT) {
lcdClear();
lcdClear(); lcdRefresh();
lcdRefresh(); lcdRefreshWait();
lcdRefreshWait();
#if !defined(EEPROM) #if !defined(EEPROM)
// Use jump on radios with emergency mode // Use jump on radios with emergency mode
// to avoid triggering it with a soft reset // to avoid triggering it with a soft reset
// Jump to proper application address // Jump to proper application address
jumpTo(APP_START_ADDRESS); jumpTo(APP_START_ADDRESS);
#else #else
// Use software reset everywhere else // Use software reset everywhere else
NVIC_SystemReset(); NVIC_SystemReset();
#endif #endif
} }
} }

View file

@ -41,7 +41,7 @@ else()
set(TARGET_SRC set(TARGET_SRC
${TARGET_SRC} ${TARGET_SRC}
coproc_driver.cpp coproc_driver.cpp
rotenc_driver.cpp rotary_encoder_driver.cpp
rtc_driver.cpp rtc_driver.cpp
) )
set(OPENRC_BOOTLOADER bootflash4.lbm) set(OPENRC_BOOTLOADER bootflash4.lbm)

View file

@ -8,84 +8,82 @@ void bootloaderInitScreen()
{ {
} }
static void bootloaderDrawMsg(unsigned int x, const char* str, uint8_t line, bool inverted) static void bootloaderDrawMsg(unsigned int x, const char *str, uint8_t line, bool inverted)
{ {
lcdDrawSizedText(x, (line + 2) * FH, str, DISPLAY_CHAR_WIDTH, inverted ? INVERS : 0); lcdDrawSizedText(x, (line + 2) * FH, str, DISPLAY_CHAR_WIDTH, inverted ? INVERS : 0);
} }
void bootloaderDrawFilename(const char* str, uint8_t line, bool selected) void bootloaderDrawFilename(const char *str, uint8_t line, bool selected)
{ {
bootloaderDrawMsg(INDENT_WIDTH, str, line, selected); bootloaderDrawMsg(INDENT_WIDTH, str, line, selected);
} }
void bootloaderDrawScreen(BootloaderState st, int opt, const char* str) void bootloaderDrawScreen(BootloaderState st, int opt, const char *str)
{ {
lcdClear(); lcdClear();
lcdDrawText(0, 0, BOOTLOADER_TITLE, INVERS); lcdDrawText(0, 0, BOOTLOADER_TITLE, INVERS);
if (st == ST_START) { if (st == ST_START) {
lcdDrawTextAlignedLeft(2*FH, "\010Write Firmware"); lcdDrawTextAlignedLeft(2*FH, "\010Write Firmware");
lcdDrawTextAlignedLeft(3*FH, "\010Restore EEPROM"); lcdDrawTextAlignedLeft(3 * FH, "\010Restore EEPROM");
lcdDrawTextAlignedLeft(4*FH, "\010Exit"); lcdDrawTextAlignedLeft(4 * FH, "\010Exit");
#if LCD_W >= 212 #if LCD_W >= 212
lcdDrawTextAlignedLeft(6*FH, "\001Curr FW:"); lcdDrawTextAlignedLeft(6*FH, "\001Curr FW:");
lcdDrawText(50, 6*FH, getOtherVersion(nullptr)); lcdDrawText(50, 6*FH, getOtherVersion(nullptr));
#else #else
lcdDrawTextAlignedLeft(6*FH, "\001FW:"); lcdDrawTextAlignedLeft(6 * FH, "\001FW:");
// Remove opentx- from string // Remove opentx- from string
const char* other_ver = getOtherVersion(nullptr); const char *other_ver = getOtherVersion(nullptr);
if (strstr(other_ver, "opentx-")) if (strstr(other_ver, "opentx-"))
other_ver = other_ver+7; other_ver = other_ver + 7;
lcdDrawText(20, 6*FH, other_ver); lcdDrawText(20, 6 * FH, other_ver);
#endif #endif
lcdInvertLine(2+opt); lcdInvertLine(2 + opt);
lcdDrawTextAlignedLeft(7*FH, STR_OR_PLUGIN_USB_CABLE); lcdDrawTextAlignedLeft(7 * FH, STR_OR_PLUGIN_USB_CABLE);
}
else if (st == ST_USB) {
lcdDrawTextAlignedLeft(4 * FH, STR_USB_CONNECTED);
}
else if (st == ST_DIR_CHECK) {
if (opt == FR_NO_PATH) {
bootloaderDrawMsg(INDENT_WIDTH, "Directory is missing!", 1, false);
bootloaderDrawMsg(INDENT_WIDTH, getBinaryPath(memoryType), 2, false);
} }
else if (st == ST_USB) { else {
lcdDrawTextAlignedLeft(4*FH, STR_USB_CONNECTED); bootloaderDrawMsg(INDENT_WIDTH, "Directory is empty!", 1, false);
} }
else if (st == ST_DIR_CHECK) { }
if (opt == FR_NO_PATH) { else if (st == ST_FLASH_CHECK) {
bootloaderDrawMsg(INDENT_WIDTH, "Directory is missing!", 1, false); if (opt == FC_ERROR) {
bootloaderDrawMsg(INDENT_WIDTH, getBinaryPath(memoryType), 2, false);
}
else {
bootloaderDrawMsg(INDENT_WIDTH, "Directory is empty!", 1, false);
}
}
else if (st == ST_FLASH_CHECK) {
if (opt == FC_ERROR) {
if (memoryType == MEM_FLASH) if (memoryType == MEM_FLASH)
bootloaderDrawMsg(0, STR_INVALID_FIRMWARE, 2, false); bootloaderDrawMsg(0, STR_INVALID_FIRMWARE, 2, false);
else else
bootloaderDrawMsg(0, STR_INVALID_EEPROM, 2, false); bootloaderDrawMsg(0, STR_INVALID_EEPROM, 2, false);
} }
else if (opt == FC_OK) { else if (opt == FC_OK) {
const char *vers = getOtherVersion((char *) Block_buffer);
const char* vers = getOtherVersion((char*)Block_buffer);
#if LCD_W < 212 #if LCD_W < 212
// Remove opentx- from string // Remove opentx- from string
if (strstr(vers, "opentx-")) if (strstr(vers, "opentx-"))
vers = vers+7; vers = vers + 7;
#endif #endif
bootloaderDrawMsg(INDENT_WIDTH, vers, 0, false); bootloaderDrawMsg(INDENT_WIDTH, vers, 0, false);
bootloaderDrawMsg(0, STR_HOLD_ENTER_TO_START, 2, false); bootloaderDrawMsg(0, STR_HOLD_ENTER_TO_START, 2, false);
}
} }
else if (st == ST_FLASHING) { }
lcdDrawTextAlignedLeft(4*FH, CENTER "\015Writing..."); else if (st == ST_FLASHING) {
lcdDrawTextAlignedLeft(4 * FH, CENTER "\015Writing...");
lcdDrawRect(3, 6*FH+4, (LCD_W-8), 7); lcdDrawRect(3, 6 * FH + 4, (LCD_W - 8), 7);
lcdDrawSolidHorizontalLine(5, 6*FH+6, (LCD_W-12) * opt / 100, FORCE); lcdDrawSolidHorizontalLine(5, 6 * FH + 6, (LCD_W - 12) * opt / 100, FORCE);
lcdDrawSolidHorizontalLine(5, 6*FH+7, (LCD_W-12) * opt / 100, FORCE); lcdDrawSolidHorizontalLine(5, 6 * FH + 7, (LCD_W - 12) * opt / 100, FORCE);
lcdDrawSolidHorizontalLine(5, 6*FH+8, (LCD_W-12) * opt / 100, FORCE); lcdDrawSolidHorizontalLine(5, 6 * FH + 8, (LCD_W - 12) * opt / 100, FORCE);
} }
else if (st == ST_FLASH_DONE) { else if (st == ST_FLASH_DONE) {
lcdDrawTextAlignedLeft(4 * FH, CENTER "\007Writing complete");
lcdDrawTextAlignedLeft(4*FH, CENTER "\007Writing complete"); }
}
} }