Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion radio/src/boards/helloradio-h750/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void getCPUUniqueID(char * s);
#define FLASH_PAGESIZE 256
void unlockFlash();
void lockFlash();
void flashWrite(uint32_t * address, const uint32_t * buffer);
bool flashWrite(uint32_t * address, const uint32_t * buffer);
uint32_t isFirmwareStart(const uint8_t * buffer);
uint32_t isBootloaderStart(const uint8_t * buffer);

Expand Down
2 changes: 1 addition & 1 deletion radio/src/boards/jumper-h750/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void getCPUUniqueID(char * s);
#define FLASH_PAGESIZE 256
void unlockFlash();
void lockFlash();
void flashWrite(uint32_t * address, const uint32_t * buffer);
bool flashWrite(uint32_t * address, const uint32_t * buffer);
uint32_t isFirmwareStart(const uint8_t * buffer);
uint32_t isBootloaderStart(const uint8_t * buffer);

Expand Down
2 changes: 1 addition & 1 deletion radio/src/boards/rm-h750/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void getCPUUniqueID(char * s);
#define FLASH_PAGESIZE 256
void unlockFlash();
void lockFlash();
void flashWrite(uint32_t * address, const uint32_t * buffer);
bool flashWrite(uint32_t * address, const uint32_t * buffer);
uint32_t isFirmwareStart(const uint8_t * buffer);
uint32_t isBootloaderStart(const uint8_t * buffer);

Expand Down
2 changes: 1 addition & 1 deletion radio/src/boards/senduwing-h750/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void getCPUUniqueID(char * s);
#define FLASH_PAGESIZE 256
void unlockFlash();
void lockFlash();
void flashWrite(uint32_t * address, const uint32_t * buffer);
bool flashWrite(uint32_t * address, const uint32_t * buffer);
uint32_t isFirmwareStart(const uint8_t * buffer);
uint32_t isBootloaderStart(const uint8_t * buffer);

Expand Down
23 changes: 16 additions & 7 deletions radio/src/bootloader/bin_fw_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ static FWFileInfo fwFiles[MAX_FW_FILES];
static uint8_t Block_buffer[BLOCK_LEN];
static UINT BlockCount;

static void flashWriteBlock()
static bool flashWriteBlock()
{
// TODO: use some board provided driver instead
uint32_t blockOffset = 0;
#if !defined(SIMU)
while (BlockCount) {
flashWrite((uint32_t *)firmwareAddress, (uint32_t *)&Block_buffer[blockOffset]);
if (!flashWrite((uint32_t *)firmwareAddress, (uint32_t *)&Block_buffer[blockOffset]))
return false;
blockOffset += FLASH_PAGESIZE;
firmwareAddress += FLASH_PAGESIZE;
if (BlockCount > FLASH_PAGESIZE) {
Expand All @@ -70,6 +71,7 @@ static void flashWriteBlock()
}
}
#endif // SIMU
return true;
}

void sdInit(void)
Expand Down Expand Up @@ -267,17 +269,24 @@ void firmwareInitWrite(uint32_t index)
firmwareWritten = 0;
}

bool firmwareWriteBlock(uint32_t* progress)
FlashWriteRes firmwareWriteBlock(uint32_t* progress)
{
flashWriteBlock();
if (!flashWriteBlock()) return FW_ERROR;

firmwareWritten += sizeof(Block_buffer);
*progress = (100 * firmwareWritten) / firmwareSize;

readFirmwareFile();
// a read error or a short file must not be reported as success
if (readFirmwareFile() != FR_OK) {
return FW_ERROR;
}
if (BlockCount == 0 && firmwareWritten < firmwareSize) {
return FW_ERROR;
}
if (BlockCount == 0 || firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE) {
return true;
return FW_DONE;
}

return false;
return FW_IN_PROGRESS;
}

1 change: 1 addition & 0 deletions radio/src/bootloader/boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum BootloaderState {
ST_FLASH_CHECK,
ST_FLASHING,
ST_FLASH_DONE,
ST_FLASH_ERROR,
ST_RESTORE_MENU,
ST_USB,
#if defined(SPI_FLASH)
Expand Down
18 changes: 12 additions & 6 deletions radio/src/bootloader/boot_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void bootloaderMenu()
uint8_t index = 0;
FRESULT fr;
uint32_t nameCount = 0;
uint32_t progress = 0;

sdInit();

Expand All @@ -107,7 +108,8 @@ void bootloaderMenu()
next_frame += FRAME_INTERVAL_MS;

if (state != ST_USB && state != ST_FLASHING
&& state != ST_FLASH_DONE && state != ST_RADIO_MENU) {
&& state != ST_FLASH_DONE && state != ST_FLASH_ERROR
&& state != ST_RADIO_MENU) {
if (usbPlugged()) {
state = ST_USB;
#if !defined(SIMU)
Expand Down Expand Up @@ -244,14 +246,16 @@ void bootloaderMenu()
} else if (result == 1) {
// confirmed
firmwareInitWrite(vpos);
progress = 0;
state = ST_FLASHING;
}
} else if (state == ST_FLASHING) {
uint32_t progress = 0;
bool done = firmwareWriteBlock(&progress);
FlashWriteRes res = firmwareWriteBlock(&progress);
bootloaderDrawScreen(state, progress);
if(done) {
if (res == FW_DONE) {
state = ST_FLASH_DONE;
} else if (res == FW_ERROR) {
state = ST_FLASH_ERROR;
}
#if defined(SPI_FLASH)
} else if (state == ST_CLEAR_FLASH_CHECK) {
Expand Down Expand Up @@ -282,13 +286,15 @@ void bootloaderMenu()
}
}

if (state == ST_FLASH_DONE) {
if (state == ST_FLASH_DONE || state == ST_FLASH_ERROR) {
BootloaderState drawn = state;

if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
state = ST_START;
vpos = 0;
}

bootloaderDrawScreen(state, 100);
bootloaderDrawScreen(drawn, drawn == ST_FLASH_DONE ? 100 : progress);
}

if (event == EVT_KEY_LONG(KEY_EXIT)) {
Expand Down
10 changes: 9 additions & 1 deletion radio/src/bootloader/firmware_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ enum FlashCheckRes {
};
FlashCheckRes checkFirmwareFile(unsigned int index, FlashCheckRes res);

enum FlashWriteRes {
FW_IN_PROGRESS=0,
FW_DONE,
FW_ERROR
};

void firmwareInitWrite(uint32_t index);
bool firmwareEraseBlock(uint32_t* progress);
bool firmwareWriteBlock(uint32_t* progress);

// Write the block currently in the buffer, then fetch the next one
FlashWriteRes firmwareWriteBlock(uint32_t* progress);

11 changes: 9 additions & 2 deletions radio/src/gui/colorlcd/boot_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,20 @@ void bootloaderDrawScreen(BootloaderState st, int opt, const char* str)
lcd.drawText(USB_TXT_X, y + USB_PLG_TXT_YO, TR_BL_USB_CONNECTED, USB_TXT_ALIGN | BL_FOREGROUND);
}
else if (st == ST_FILE_LIST || st == ST_DIR_CHECK || st == ST_FLASH_CHECK ||
st == ST_FLASHING || st == ST_FLASH_DONE) {
st == ST_FLASHING || st == ST_FLASH_DONE || st == ST_FLASH_ERROR) {

bootloaderDrawTitle(LV_SYMBOL_SD_CARD " /FIRMWARE");

if (st == ST_FLASHING || st == ST_FLASH_DONE) {
if (st == ST_FLASHING || st == ST_FLASH_DONE || st == ST_FLASH_ERROR) {
LcdFlags color = BL_RED;

if (st == ST_FLASH_DONE) {
color = BL_GREEN;
opt = 100; // Completed > 100%
} else if (st == ST_FLASH_ERROR) {
// the bar stays where the write gave up
lcd.drawText(LCD_W / 2, (LCD_H - PROGRESS_H) / 2 - EdgeTxStyles::STD_FONT_HEIGHT,
LV_SYMBOL_CLOSE " " TR_BL_WRITING_FAILED, CENTERED | BL_FOREGROUND);
}

lcd.drawRect(PROGRESS_X, (LCD_H - PROGRESS_H) / 2, PROGRESS_W, PROGRESS_H, LINE_H, SOLID, BL_SELECTED);
Expand Down Expand Up @@ -405,6 +409,9 @@ void bootloaderDrawScreen(BootloaderState st, int opt, const char* str)
else if (st == ST_FLASH_DONE) {
pos = lcd.drawText(FOOTER_X1, LCD_H - FOOTER_Y1, LV_SYMBOL_CHARGE " " TR_BL_WRITING_COMPL, FOOTER_ALIGN1 | BL_FOREGROUND);
}
else if (st == ST_FLASH_ERROR) {
pos = lcd.drawText(FOOTER_X1, LCD_H - FOOTER_Y1, LV_SYMBOL_CHARGE " " TR_BL_RETRY_OR_DFU, FOOTER_ALIGN1 | BL_FOREGROUND);
}
}
#if LANDSCAPE
pos = 0;
Expand Down
4 changes: 4 additions & 0 deletions radio/src/gui/common/stdlcd/boot_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ void bootloaderDrawScreen(BootloaderState st, int opt, const char *str)
else if (st == ST_FLASH_DONE) {
lcdDrawCenteredText(4 * FH, TR_BL_WRITING_COMPL);
}
else if (st == ST_FLASH_ERROR) {
lcdDrawCenteredText(4 * FH, TR_BL_WRITING_FAILED);
lcdDrawCenteredText(6 * FH, TR_BL_RETRY_OR_DFU);
}
}

uint32_t bootloaderGetMenuItemCount(int baseCount)
Expand Down
14 changes: 11 additions & 3 deletions radio/src/io/bootloader_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void BootloaderFirmwareUpdate::flashFirmware(const char * filename, ProgressHand
FIL file;
uint8_t buffer[1024];
UINT count;
bool success = true;

pulsesStop();

Expand All @@ -81,22 +82,29 @@ void BootloaderFirmwareUpdate::flashFirmware(const char * filename, ProgressHand

if (f_read(&file, buffer, sizeof(buffer), &count) != FR_OK) {
POPUP_WARNING(STR_SDCARD_ERROR);
success = false;
break;
}
if (count != sizeof(buffer)
&& !f_eof(&file)) {
POPUP_WARNING(STR_SDCARD_ERROR);
success = false;
break;
}
if (i == 0 && !isBootloaderStart(buffer)) {
POPUP_WARNING(STR_INCOMPATIBLE);
success = false;
break;
}
for (UINT j = 0; j < count; j += FLASH_PAGESIZE) {
for (UINT j = 0; j < count && success; j += FLASH_PAGESIZE) {
WDG_ENABLE(3000);
flashWrite(CONVERT_UINT_PTR(BOOTLOADER_ADDRESS + i + j), CONVERT_UINT_PTR(buffer + j));
success = flashWrite(CONVERT_UINT_PTR(BOOTLOADER_ADDRESS + i + j), CONVERT_UINT_PTR(buffer + j));
WDG_ENABLE(WDG_DURATION);
}
if (!success) {
POPUP_WARNING(STR_FIRMWARE_UPDATE_ERROR);
break;
}
progressHandler("Bootloader", STR_WRITING, i, flash_size);

// Reached end-of-file
Expand All @@ -107,7 +115,7 @@ void BootloaderFirmwareUpdate::flashFirmware(const char * filename, ProgressHand
#endif
}

POPUP_INFORMATION(STR_FIRMWARE_UPDATE_SUCCESS);
if (success) POPUP_INFORMATION(STR_FIRMWARE_UPDATE_SUCCESS);

watchdogSuspend(0);
WDG_RESET();
Expand Down
2 changes: 1 addition & 1 deletion radio/src/targets/c14/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void getCPUUniqueID(char * s);
#define FLASH_PAGESIZE 256
void unlockFlash();
void lockFlash();
void flashWrite(uint32_t * address, const uint32_t * buffer);
bool flashWrite(uint32_t * address, const uint32_t * buffer);
uint32_t isFirmwareStart(const uint8_t * buffer);
uint32_t isBootloaderStart(const uint8_t * buffer);

Expand Down
Loading
Loading