1
0
Fork 0
mirror of https://github.com/EdgeTX/edgetx.git synced 2025-07-24 16:55:15 +03:00

EEPROM Restore added in Bootloader

This commit is contained in:
bsongis 2014-04-09 11:51:38 +02:00
parent ab7218e5cf
commit 5dab6ef38f
5 changed files with 102 additions and 74 deletions

View file

@ -54,6 +54,7 @@
#include <string.h>
#include "board_taranis.h"
#include "../eeprom_rlc.h"
#include "../pwr.h"
#include "../lcd.h"
#include "../keys.h"
@ -90,11 +91,10 @@ enum BootLoaderStates {
ST_FLASH_CHECK,
ST_FLASHING,
ST_FLASH_DONE,
ST_USB,
ST_REBOOT,
ST_BACKUP_EEPROM,
ST_RESTORE_MENU,
ST_EEPROM_CHECK,
ST_USB,
ST_REBOOT,
};
enum MemoryTypes {
@ -107,12 +107,15 @@ enum MemoryTypes {
*----------------------------------------------------------------------------*/
uint32_t FirmwareSize;
uint32_t firmwareAddress = FIRMWARE_ADDRESS;
uint32_t firmwareWritten = 0;
uint32_t eepromAddress = 0;
uint32_t eepromWritten = 0;
uint32_t Master_frequency;
volatile uint8_t Tenms;
uint8_t EE_timer;
TCHAR FlashFilename[60];
FIL FlashFile;
DIR Dj;
FILINFO Finfo;
@ -129,6 +132,8 @@ uint32_t LockBits;
uint32_t Block_buffer[1024];
UINT BlockCount;
uint32_t memoryType;
#if defined(PCBSKY9X)
extern int32_t EblockAddress;
#endif
@ -344,35 +349,54 @@ uint32_t fillNames(uint32_t index)
return i;
}
FRESULT openFirmwareFile(uint32_t index)
const char *getBinaryPath()
{
cpystr(cpystr((uint8_t *)FlashFilename, (uint8_t *)FIRMWARES_PATH "/"), (uint8_t *) Filenames[index]);
f_open(&FlashFile, FlashFilename, FA_READ);
if (memoryType == MEM_FLASH)
return FIRMWARES_PATH;
else
return EEPROMS_PATH;
}
FRESULT openBinaryFile(uint32_t index)
{
TCHAR filename[60];
cpystr(cpystr(cpystr((uint8_t *)filename, (uint8_t *)getBinaryPath), (uint8_t *)"/"), (uint8_t *)Filenames[index]);
f_open(&FlashFile, filename, FA_READ);
if (memoryType == MEM_FLASH)
f_lseek(&FlashFile, BOOTLOADER_SIZE);
return f_read(&FlashFile, (BYTE *)Block_buffer, 4096, &BlockCount);
}
uint32_t isValidBufferStart(const uint32_t * buffer)
{
if (memoryType == MEM_FLASH)
return isFirmwareStart(buffer);
else
return isEepromStart(buffer);
}
int menuFlashFile(uint32_t index, uint8_t event)
{
FRESULT fr;
// TODO if (memoryType == MEM_EEPROM)
lcd_putsLeft(4*FH, "\012Hold [ENT] to start loading" );
if (Valid == 0) {
// Validate file here
// return 3 if invalid
fr = openFirmwareFile(index);
fr = openBinaryFile(index);
fr = f_close(&FlashFile);
Valid = 1;
if (!isFirmwareStart(Block_buffer)) {
if (!isValidBufferStart(Block_buffer)) {
Valid = 2;
}
}
if (Valid == 2) {
if (memoryType == MEM_FLASH)
lcd_putsLeft(4*FH, "\011No firmware found in the file!");
else
lcd_putsLeft(4*FH, "\011No EEPROM found in the file!");
if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT) || event == EVT_KEY_FIRST(BOOT_KEY_MENU)) {
return 0;
}
@ -380,12 +404,8 @@ int menuFlashFile(uint32_t index, uint8_t event)
}
if (event == EVT_KEY_LONG(BOOT_KEY_MENU)) {
fr = openFirmwareFile(index);
FirmwareSize = FileSize[index];
if (fr != FR_OK) {
return 0; // File open error
}
return 1;
fr = openBinaryFile(index);
return (fr == FR_OK && isValidBufferStart(Block_buffer));
}
else if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) {
return 0;
@ -398,6 +418,27 @@ extern Key keys[];
static uint32_t PowerUpDelay;
void writeFlashBlock()
{
uint32_t blockOffset = 0;
while (BlockCount) {
writeFlash((uint32_t *)firmwareAddress, &Block_buffer[blockOffset]);
blockOffset += FLASH_PAGESIZE/4; // 32-bit words
firmwareAddress += FLASH_PAGESIZE;
if (BlockCount > FLASH_PAGESIZE) {
BlockCount -= FLASH_PAGESIZE;
}
else {
BlockCount = 0;
}
}
}
void writeEepromBlock()
{
eeWriteBlockCmp((uint8_t *)Block_buffer, eepromAddress, BlockCount);
}
int main()
{
uint8_t index = 0;
@ -410,9 +451,6 @@ int main()
uint32_t nameCount = 0;
uint32_t vpos = 0;
uint32_t hpos = 0;
uint32_t firmwareAddress = FIRMWARE_ADDRESS;
uint32_t firmwareWritten = 0;
uint32_t memoryType;
#if defined(PCBTARANIS)
wdt_reset();
@ -459,13 +497,6 @@ int main()
init_spi();
#endif
#if defined(PCBSKY9X)
uint32_t chip_id = CHIPID->CHIPID_CIDR;
FlashSize = ( (chip_id >> 8 ) & 0x000F ) == 9 ? 256 : 512;
#elif defined(PCBTARANIS)
FlashSize = 512;
#endif
#if defined(PCBSKY9X)
LockBits = readLockBits();
if (LockBits) {
@ -553,15 +584,12 @@ int main()
if (state == ST_FLASH_MENU || state == ST_RESTORE_MENU) {
sdInit();
state == ST_RESTORE_MENU ? memoryType = MEM_EEPROM : memoryType = MEM_FLASH;
memoryType = (state == ST_RESTORE_MENU ? MEM_EEPROM : MEM_FLASH);
state = ST_DIR_CHECK;
}
else if (state == ST_DIR_CHECK) {
if (memoryType == MEM_FLASH)
fr = f_chdir(FIRMWARES_PATH);
else
fr = f_chdir(EEPROMS_PATH);
fr = f_chdir(getBinaryPath());
if (fr == FR_OK) {
state = ST_OPEN_DIR;
}
@ -587,7 +615,6 @@ int main()
if (state == ST_BACKUP_EEPROM) {
// TODO Get RTC date/time, dump EEPROM to EEPROMS_PATH/EEPROM_yy-mm-dd-hh-mm-ss.BIN
lcd_putsLeft(2*FH, INDENT "Backup successful!");
// lcd_putsLeft(3*FH, filename);
if (event == EVT_KEY_BREAK(BOOT_KEY_EXIT) || event == EVT_KEY_BREAK(BOOT_KEY_MENU)) {
@ -658,10 +685,7 @@ int main()
#endif
else if (event == EVT_KEY_BREAK(BOOT_KEY_MENU)) {
// Select file to flash
if (memoryType == MEM_FLASH)
state = ST_FLASH_CHECK;
else
state = ST_EEPROM_CHECK;
Valid = 0;
}
else if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) {
@ -683,45 +707,41 @@ int main()
// confirmed
firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE;
firmwareWritten = 0;
eepromAddress = 0;
eepromWritten = 0;
state = ST_FLASHING;
}
}
if (state == ST_FLASHING) {
// Commit to flashing
uint32_t blockOffset = 0;
// commit to flashing
lcd_putsLeft(4*FH, "\032Writing...");
if (firmwareAddress == FIRMWARE_ADDRESS + BOOTLOADER_SIZE) {
if (!isFirmwareStart(Block_buffer)) {
state = ST_FLASH_DONE;
}
}
while (BlockCount) {
writeFlash((uint32_t *)firmwareAddress, &Block_buffer[blockOffset]);
blockOffset += FLASH_PAGESIZE/4; // 32-bit words
firmwareAddress += FLASH_PAGESIZE;
if (BlockCount > FLASH_PAGESIZE) {
BlockCount -= FLASH_PAGESIZE;
int progress;
if (memoryType == MEM_FLASH) {
writeFlashBlock();
firmwareWritten += sizeof(Block_buffer);
progress = (200*firmwareWritten) / FirmwareSize;
}
else {
BlockCount = 0;
writeEepromBlock();
eepromWritten += sizeof(Block_buffer);
progress = (200*eepromWritten) / EESIZE;
}
}
firmwareWritten += 4; // 4K blocks
lcd_rect( 3, 6*FH+4, 204, 7);
lcd_hline(5, 6*FH+6, (200*firmwareWritten*1024)/FirmwareSize, FORCE);
lcd_hline(5, 6*FH+7, (200*firmwareWritten*1024)/FirmwareSize, FORCE);
lcd_hline(5, 6*FH+8, (200*firmwareWritten*1024)/FirmwareSize, FORCE);
lcd_hline(5, 6*FH+6, progress, FORCE);
lcd_hline(5, 6*FH+7, progress, FORCE);
lcd_hline(5, 6*FH+8, progress, FORCE);
fr = f_read(&FlashFile, (BYTE *)Block_buffer, sizeof(Block_buffer), &BlockCount);
if (BlockCount == 0) {
state = ST_FLASH_DONE;
state = ST_FLASH_DONE; // EOF
}
if (firmwareWritten >= FlashSize - 32) {
if (firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE) {
state = ST_FLASH_DONE; // Backstop
}
if (eepromWritten >= EESIZE) {
state = ST_FLASH_DONE; // Backstop
}
}

View file

@ -247,4 +247,13 @@ void loadGeneralSettings();
void loadModel(int index);
#endif
// For EEPROM backup/restore
#if defined(CPUARM)
inline bool isEepromStart(const void * buffer)
{
const EeFs * eeprom = (const EeFs *)buffer;
return (eeprom->version==EEFS_VERS && eeprom->mySize==sizeof(eeFs) && eeprom->bs==BS);
}
#endif
#endif

View file

@ -145,8 +145,8 @@ void delay_01us(uint16_t nb);
#define FLASH_PAGESIZE 256
void unlockFlash();
void writeFlash(uint32_t * address, uint32_t * buffer);
uint32_t isFirmwareStart(uint32_t *block);
uint32_t isBootloaderStart(uint32_t *block);
uint32_t isFirmwareStart(const uint32_t *block);
uint32_t isBootloaderStart(const uint32_t *block);
// Pulses driver
void init_no_pulses(uint32_t port);

View file

@ -98,7 +98,7 @@ void writeFlash(uint32_t *address, uint32_t *buffer) // page size is 256 bytes
}
}
uint32_t isFirmwareStart(uint32_t *block)
uint32_t isFirmwareStart(const uint32_t *block)
{
if ((block[0] & 0xFFFC0000) != 0x20000000) {
return 0;
@ -112,7 +112,7 @@ uint32_t isFirmwareStart(uint32_t *block)
return 1;
}
uint32_t isBootloaderStart(uint32_t *block)
uint32_t isBootloaderStart(const uint32_t *block)
{
for (int i=0; i<256; i++) {
if (block[i] == 0x544F4F42/*BOOT*/) {

View file

@ -724,8 +724,7 @@ int32_t fat12Write(const uint8_t *buffer, uint16_t sector, uint32_t count )
}
else if (sector < 3 + (EESIZE/BLOCKSIZE)) {
while (count) {
const EeFs * test = (const EeFs *)buffer;
if (offset == 0 && test->version==EEFS_VERS && test->mySize==sizeof(eeFs) && test->bs==BS) {
if (offset == 0 && isEepromStart(buffer)) {
TRACE("EEPROM start found in sector %d", sector);
offset = sector;
}