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

Reindentation of boot.cpp

This commit is contained in:
bsongis 2014-03-12 16:18:30 +01:00
parent 68c270760c
commit 278218e7a0

View file

@ -45,7 +45,6 @@
*
****************************************************************************/
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
@ -55,30 +54,17 @@
#include <string.h>
#ifdef PCBTARANIS
#include "stm32f2xx.h"
#include "stm32f2xx_flash.h"
#include "../targets/taranis/i2c.h"
#include "hal.h"
extern "C" {
#include "usb_dcd_int.h"
#include "usb_bsp.h"
#include "../targets/taranis/usbd_desc.h"
#include "usbd_msc_core.h"
#include "usbd_usr.h"
}
#endif
#include "board_taranis.h"
#include "../pwr.h"
#include "../lcd.h"
#include "../keys.h"
#include "../FatFs/ff.h"
#include "../FatFs/diskio.h"
#ifdef PCBTARANIS
#if defined(PCBTARANIS)
#define BOOT_KEY_UP KEY_PLUS
#define BOOT_KEY_DOWN KEY_MINUS
#define BOOT_KEY_LEFT KEY_MENU
@ -89,16 +75,17 @@ extern "C" {
#endif
// states
#define ST_START 0
#define ST_DIR_CHECK 1
#define ST_OPEN_DIR 2
#define ST_FILE_LIST 3
#define ST_FLASH_CHECK 4
#define ST_FLASHING 5
#define ST_FLASH_DONE 6
#define ST_USB 10
#define ST_REBOOT 11
enum BootLoaderStates {
ST_START,
ST_DIR_CHECK,
ST_OPEN_DIR,
ST_FILE_LIST,
ST_FLASH_CHECK,
ST_FLASHING,
ST_FLASH_DONE,
ST_USB = 10,
ST_REBOOT = 11,
};
/*----------------------------------------------------------------------------
* Local variables
@ -122,7 +109,6 @@ uint32_t Valid ;
uint32_t FlashSize;
uint32_t FlashBlocked = 1;
uint32_t LockBits;
@ -132,12 +118,12 @@ UINT BlockCount ;
#ifdef PCBSKY
extern int32_t EblockAddress;
#endif
extern uint32_t EepromBlocked;
extern void init_spi(void);
extern void writeBlock(void);
/*----------------------------------------------------------------------------
* Global functions
*----------------------------------------------------------------------------*/
@ -181,16 +167,13 @@ void delay2ms()
uint32_t isFirmwareStart(uint32_t *block)
{
if ( ( block[0] & 0xFFFC0000 ) != 0x20000000 )
{
if ((block[0] & 0xFFFC0000) != 0x20000000) {
return 0;
}
if ( ( block[1] & 0xFFF00000 ) != 0x08000000 )
{
if ((block[1] & 0xFFF00000) != 0x08000000) {
return 0;
}
if ( ( block[2] & 0xFFF00000 ) != 0x08000000 )
{
if ((block[2] & 0xFFF00000) != 0x08000000) {
return 0;
}
return 1;
@ -216,12 +199,10 @@ uint32_t isFirmwareStart( uint32_t *block )
}
#endif
#ifdef PCBSKY
uint32_t (*IAP_Function)(uint32_t, uint32_t);
uint32_t program( uint32_t *address, uint32_t *buffer ) // size is 256 bytes
{
uint32_t FlashSectorNum;
@ -269,7 +250,6 @@ uint32_t program( uint32_t *address, uint32_t *buffer ) // size is 256 bytes
return i;
}
uint32_t readLockBits()
{
// Always initialise this here, setting a default doesn't seem to work
@ -282,7 +262,6 @@ uint32_t readLockBits()
return EFC->EEFC_FRR;
}
void clearLockBits()
{
uint32_t i;
@ -385,14 +364,12 @@ void init_hw_timer()
TIM13->CR1 = 1;
}
// delay in units of 0.1 uS up to 6.5535 mS
void hw_delay(uint16_t time)
{
TIM13->CNT = 0;
TIM13->EGR = 1; // Re-start counter
while ( TIM13->CNT < time )
{
while ( TIM13->CNT < time) {
// wait
}
}
@ -414,8 +391,7 @@ void unlockFlash()
void waitFlashIdle()
{
while (FLASH->SR & FLASH_FLAG_BSY)
{
while (FLASH->SR & FLASH_FLAG_BSY) {
wdt_reset();
}
}
@ -444,52 +420,41 @@ uint32_t program( uint32_t *address, uint32_t *buffer ) // size is 256 bytes
{
uint32_t i;
if ( (uint32_t) address == 0x08008000 )
{
if ( isFirmwareStart( buffer) )
{
if ((uint32_t) address == 0x08008000) {
if (isFirmwareStart(buffer)) {
FlashBlocked = 0;
}
else
{
else {
FlashBlocked = 1;
}
}
if ( FlashBlocked )
{
if (FlashBlocked) {
return 1;
}
if ( (uint32_t) address == 0x08008000 )
{
if ((uint32_t) address == 0x08008000) {
eraseSector(2);
}
if ( (uint32_t) address == 0x0800C000 )
{
if ((uint32_t) address == 0x0800C000) {
eraseSector(3);
}
if ( (uint32_t) address == 0x08010000 )
{
if ((uint32_t) address == 0x08010000) {
eraseSector(4);
}
if ( (uint32_t) address == 0x08020000 )
{
if ((uint32_t) address == 0x08020000) {
eraseSector(5);
}
if ( (uint32_t) address == 0x08040000 )
{
if ((uint32_t) address == 0x08040000) {
eraseSector(6);
}
if ( (uint32_t) address == 0x08060000 )
{
if ((uint32_t) address == 0x08060000) {
eraseSector(7);
}
// Now program the 256 bytes
for (i = 0 ; i < 64 ; i += 1 )
{
for (i = 0; i < 64; i += 1) {
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
@ -507,8 +472,7 @@ uint32_t program( uint32_t *address, uint32_t *buffer ) // size is 256 bytes
FLASH->CR &= (~FLASH_CR_PG);
/* Check the written value */
if ( *address != *buffer )
{
if (*address != *buffer) {
/* Flash content doesn't match SRAM content */
return 2;
}
@ -521,7 +485,6 @@ uint32_t program( uint32_t *address, uint32_t *buffer ) // size is 256 bytes
#endif
uint8_t *cpystr(uint8_t *dest, uint8_t *source)
{
while ((*dest++ = *source++))
@ -529,43 +492,34 @@ uint8_t *cpystr( uint8_t *dest, uint8_t *source )
return dest - 1;
}
FRESULT readBinDir(DIR *dj, FILINFO *fno)
{
FRESULT fr;
uint32_t loop;
do
{
do {
loop = 0;
fr = f_readdir(dj, fno); // First entry
if ( fr != FR_OK || fno->fname[0] == 0 )
{
if (fr != FR_OK || fno->fname[0] == 0) {
break;
}
if ( *fno->lfname == 0 )
{
if (*fno->lfname == 0) {
cpystr((uint8_t *) fno->lfname, (uint8_t *) fno->fname); // Copy 8.3 name
}
int32_t len = strlen(fno->lfname) - 4;
if ( len < 0 )
{
if (len < 0) {
loop = 1;
}
if ( fno->lfname[len] != '.' )
{
if (fno->lfname[len] != '.') {
loop = 1;
}
if ( ( fno->lfname[len+1] != 'b' ) && ( fno->lfname[len+1] != 'B' ) )
{
if ((fno->lfname[len + 1] != 'b') && (fno->lfname[len + 1] != 'B')) {
loop = 1;
}
if ( ( fno->lfname[len+2] != 'i' ) && ( fno->lfname[len+2] != 'I' ) )
{
if ((fno->lfname[len + 2] != 'i') && (fno->lfname[len + 2] != 'I')) {
loop = 1;
}
if ( ( fno->lfname[len+3] != 'n' ) && ( fno->lfname[len+3] != 'N' ) )
{
if ((fno->lfname[len + 3] != 'n') && (fno->lfname[len + 3] != 'N')) {
loop = 1;
}
@ -573,7 +527,6 @@ FRESULT readBinDir( DIR *dj, FILINFO *fno )
return fr;
}
uint32_t fillNames(uint32_t index)
{
uint32_t i;
@ -584,23 +537,19 @@ uint32_t fillNames( uint32_t index )
fr = f_readdir(&Dj, &Finfo); // Skip .
fr = f_readdir(&Dj, &Finfo); // Skip ..
i = 0;
while ( i <= index )
{
while (i <= index) {
fr = readBinDir(&Dj, &Finfo); // First entry
FileSize[0] = Finfo.fsize;
i += 1;
if ( fr == FR_NO_FILE)
{
if (fr == FR_NO_FILE) {
return 0;
}
}
for ( i = 1 ; i < 7 ; i += 1 )
{
for (i = 1; i < 7; i += 1) {
Finfo.lfname = Filenames[i];
fr = readBinDir(&Dj, &Finfo); // First entry
FileSize[i] = Finfo.fsize;
if ( fr != FR_OK || Finfo.fname[0] == 0 )
{
if (fr != FR_OK || Finfo.fname[0] == 0) {
break;
}
}
@ -609,35 +558,31 @@ uint32_t fillNames( uint32_t index )
FRESULT openFirmwareFile(uint32_t index)
{
cpystr( cpystr( (uint8_t *)FlashFilename, (uint8_t *)"\\firmware\\" ), (uint8_t *)Filenames[index] ) ;
cpystr(cpystr((uint8_t *) FlashFilename, (uint8_t *) "\\firmware\\"),
(uint8_t *) Filenames[index]);
f_open(&FlashFile, FlashFilename, FA_READ);
f_lseek(&FlashFile, 32768);
return f_read(&FlashFile, (BYTE *) Block_buffer, 4096, &BlockCount);
}
uint8_t flashFile(uint32_t index)
{
FRESULT fr;
lcd_clear();
lcd_putsLeft(0, "\005Flash File");
if ( Valid == 0 )
{
if (Valid == 0) {
// Validate file here
// return 3 if invalid
fr = openFirmwareFile(index);
fr = f_close(&FlashFile);
Valid = 1;
if ( isFirmwareStart( Block_buffer ) == 0 )
{
if (isFirmwareStart(Block_buffer) == 0) {
Valid = 2;
}
}
if ( Valid == 2 )
{
if (Valid == 2) {
lcd_putsLeft(3 * FH, "NOT A VALID FIRMWARE");
#ifdef PCBTARANIS
lcd_putsLeft(6 * FH, "\015[EXIT]");
@ -645,8 +590,7 @@ uint8_t flashFile( uint32_t index )
lcd_putsLeft( 6*FH,"\007[EXIT]");
#endif
uint8_t event = getEvent();
if ( event == EVT_KEY_FIRST(BOOT_KEY_EXIT) )
{
if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) {
return 3;
}
return 4; //
@ -663,24 +607,20 @@ uint8_t flashFile( uint32_t index )
uint8_t event = getEvent();
if ( event == EVT_KEY_FIRST(BOOT_KEY_MENU) )
{
if (event == EVT_KEY_FIRST(BOOT_KEY_MENU)) {
fr = openFirmwareFile(index);
FirmwareSize = FileSize[index];
if ( fr != FR_OK )
{
if (fr != FR_OK) {
return 4; // File open error
}
return 2;
}
if ( event == EVT_KEY_FIRST(BOOT_KEY_EXIT) )
{
if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) {
return 1;
}
return 0;
}
extern Key keys[];
static uint32_t PowerUpDelay;
@ -780,22 +720,18 @@ extern uint8_t OptrexDisplay ;
usbStart();
#endif
for(;;)
{
for (;;) {
#ifdef PCBSKY
usbMassStorage();
#endif
wdt_reset();
if ( Tenms )
{
if (Tenms) {
wdt_reset(); // Retrigger hardware watchdog
if ( EE_timer )
{
if ( --EE_timer == 0)
{
if (EE_timer) {
if (--EE_timer == 0) {
#ifdef PCBSKY
writeBlock();
#endif
@ -809,21 +745,18 @@ extern uint8_t OptrexDisplay ;
{
if ( usbPlugged() )
{
if (usbPlugged()) {
state = ST_USB;
}
if ( state == ST_USB )
{
if (state == ST_USB) {
#ifdef PCBSKY
lcd_putsLeft( 3*FH, "\010BUSY" );
#endif
#ifdef PCBTARANIS
lcd_putsLeft(3 * FH, "\016BUSY");
#endif
if ( usbPlugged() == 0 )
{
if (usbPlugged() == 0) {
state = ST_START;
}
#ifdef PCBSKY
@ -834,34 +767,27 @@ extern uint8_t OptrexDisplay ;
#endif
}
if ( state == ST_START )
{
if (state == ST_START) {
sdInit();
fr = FR_OK;
}
else
{
else {
fr = FR_OK;
}
if ( fr == FR_OK)
{
if ( state == ST_START )
{
if (fr == FR_OK) {
if (state == ST_START) {
state = ST_DIR_CHECK;
}
}
if ( state == ST_DIR_CHECK )
{
if (state == ST_DIR_CHECK) {
fr = f_chdir("/firmware");
if ( fr == FR_OK )
{
if (fr == FR_OK) {
state = ST_OPEN_DIR;
index = 0;
}
}
if ( state == ST_DIR_CHECK )
{
if (state == ST_DIR_CHECK) {
#ifdef PCBSKY
lcd_putsLeft( 2*FH, "\005No Firmware" );
#endif
@ -869,11 +795,9 @@ extern uint8_t OptrexDisplay ;
lcd_putsLeft(2 * FH, "\013No Firmware");
#endif
}
if ( state == ST_OPEN_DIR )
{
if (state == ST_OPEN_DIR) {
fr = f_opendir(&Dj, ".");
if ( fr == FR_OK )
{
if (fr == FR_OK) {
state = ST_FILE_LIST;
index = 0;
nameCount = fillNames(0);
@ -881,110 +805,90 @@ extern uint8_t OptrexDisplay ;
vpos = 0;
}
}
if ( state == ST_FILE_LIST )
{
if (state == ST_FILE_LIST) {
uint32_t limit = 6;
if ( nameCount < limit )
{
if (nameCount < limit) {
limit = nameCount;
}
maxhsize = 0;
for ( i = 0 ; i < limit ; i += 1 )
{
for (i = 0; i < limit; i += 1) {
uint32_t x;
x = strlen(Filenames[i]);
if ( x > maxhsize )
{
if (x > maxhsize) {
maxhsize = x;
}
if ( x > DISPLAY_CHAR_WIDTH )
{
if ( ( hpos + DISPLAY_CHAR_WIDTH ) > x )
{
if (x > DISPLAY_CHAR_WIDTH) {
if ((hpos + DISPLAY_CHAR_WIDTH) > x) {
x = x - DISPLAY_CHAR_WIDTH;
}
else
{
else {
x = hpos;
}
}
else
{
else {
x = 0;
}
lcd_putsnAtt( 0, 16+FH*i, &Filenames[i][x], DISPLAY_CHAR_WIDTH, 0 ) ;
lcd_putsnAtt(0, 16 + FH * i, &Filenames[i][x], DISPLAY_CHAR_WIDTH,
0);
}
{
uint8_t event = getEvent();
if ( ( event == EVT_KEY_REPT(BOOT_KEY_DOWN) ) || event == EVT_KEY_FIRST(BOOT_KEY_DOWN) )
{
if ( vpos < limit-1 )
{
if ((event == EVT_KEY_REPT(BOOT_KEY_DOWN))
|| event == EVT_KEY_FIRST(BOOT_KEY_DOWN)) {
if (vpos < limit - 1) {
vpos += 1;
}
else
{
if ( nameCount > limit )
{
else {
if (nameCount > limit) {
index += 1;
nameCount = fillNames(index);
}
}
}
if ( ( event == EVT_KEY_REPT(BOOT_KEY_UP)) || ( event == EVT_KEY_FIRST(BOOT_KEY_UP) ) )
{
if ( vpos > 0 )
{
if ((event == EVT_KEY_REPT(BOOT_KEY_UP))
|| (event == EVT_KEY_FIRST(BOOT_KEY_UP))) {
if (vpos > 0) {
vpos -= 1;
}
else
{
if ( index )
{
else {
if (index) {
index -= 1;
nameCount = fillNames(index);
}
}
}
if ( ( event == EVT_KEY_REPT(BOOT_KEY_RIGHT)) || ( event == EVT_KEY_FIRST(BOOT_KEY_RIGHT) ) )
{
if ( hpos + DISPLAY_CHAR_WIDTH < maxhsize )
{
if ((event == EVT_KEY_REPT(BOOT_KEY_RIGHT))
|| (event == EVT_KEY_FIRST(BOOT_KEY_RIGHT))) {
if (hpos + DISPLAY_CHAR_WIDTH < maxhsize) {
hpos += 1;
}
}
if ( ( event == EVT_KEY_REPT(BOOT_KEY_LEFT)) || ( event == EVT_KEY_FIRST(BOOT_KEY_LEFT) ) )
{
if ( hpos )
{
if ((event == EVT_KEY_REPT(BOOT_KEY_LEFT))
|| (event == EVT_KEY_FIRST(BOOT_KEY_LEFT))) {
if (hpos) {
hpos -= 1;
}
}
if ( event == EVT_KEY_LONG(BOOT_KEY_MENU) )
{
if (event == EVT_KEY_LONG(BOOT_KEY_MENU)) {
// Select file to flash
state = ST_FLASH_CHECK;
Valid = 0;
}
if ( event == EVT_KEY_LONG(BOOT_KEY_EXIT) )
{
if (event == EVT_KEY_LONG(BOOT_KEY_EXIT)) {
state = ST_REBOOT;
}
}
lcd_invert_line(2 + vpos);
// lcd_char_inverse( 0, 2*FH+FH*vpos, DISPLAY_CHAR_WIDTH*FW, 0 ) ;
}
if ( state == ST_FLASH_CHECK )
{
if (state == ST_FLASH_CHECK) {
i = flashFile(vpos);
FirmwareSize = FileSize[vpos] - 32768;
if ( i == 1 )
{
if (i == 1) {
state = ST_FILE_LIST; // Canceled
}
if ( i == 2 )
{
if (i == 2) {
#ifdef PCBSKY
firmwareAddress = 0x00408000;
#endif
@ -994,28 +898,23 @@ extern uint8_t OptrexDisplay ;
firmwareWritten = 0;
state = ST_FLASHING; // confirmed
}
if ( i == 3 )
{
if (i == 3) {
// Invalid file
state = ST_FILE_LIST; // Canceled
}
}
if ( state == ST_FLASHING )
{
if (state == ST_FLASHING) {
// Commit to flashing
uint32_t blockOffset = 0;
lcd_putsLeft(3 * FH, "Flashing");
while ( BlockCount )
{
while (BlockCount) {
program((uint32_t *) firmwareAddress, &Block_buffer[blockOffset]);// size is 256 bytes
blockOffset += 64; // 32-bit words (256 bytes)
firmwareAddress += 256;
if ( BlockCount > 256 )
{
if (BlockCount > 256) {
BlockCount -= 256;
}
else
{
else {
BlockCount = 0;
}
}
@ -1024,13 +923,11 @@ extern uint8_t OptrexDisplay ;
lcd_hline(0, 5 * FH - 1, width + 1);
lcd_hline(0, 6 * FH, width + 1);
lcd_vline(width, 5 * FH, 8);
for ( i = 0 ; i < firmwareWritten ; i += 1 )
{
for (i = 0; i < firmwareWritten; i += 1) {
lcd_vline(i, 5 * FH, 8);
}
fr = f_read(&FlashFile, (BYTE *) Block_buffer, 4096, &BlockCount);
if ( BlockCount == 0 )
{
if (BlockCount == 0) {
state = ST_FLASH_DONE;
}
if (firmwareWritten > FlashSize / 4 - 9)// (127-8, or 63-8) 4K blocks
@ -1038,12 +935,10 @@ extern uint8_t OptrexDisplay ;
state = ST_FLASH_DONE; // Backstop
}
}
if ( state == ST_FLASH_DONE )
{
if (state == ST_FLASH_DONE) {
uint8_t event = getEvent();
lcd_putsLeft(3 * FH, "Flashing Complete");
if ( event == EVT_KEY_FIRST(BOOT_KEY_EXIT) )
{
if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) {
state = ST_FILE_LIST;
}
}
@ -1058,33 +953,26 @@ extern uint8_t OptrexDisplay ;
{
PowerUpDelay += 1;
}
else
{
else {
sdPoll10ms();
}
}
if ((state < ST_FLASH_CHECK) || (state == ST_FLASH_DONE))
{
if ((state < ST_FLASH_CHECK) || (state == ST_FLASH_DONE)) {
if (pwrCheck() == e_power_off) {
pwrOff();
for(;;)
{
for (;;) {
// Wait for power to go off
}
}
}
if ( state < ST_FILE_LIST )
{
if ( getEvent() == EVT_KEY_LONG(BOOT_KEY_EXIT) )
{
if (state < ST_FILE_LIST) {
if (getEvent() == EVT_KEY_LONG(BOOT_KEY_EXIT)) {
state = ST_REBOOT;
}
}
if ( state == ST_REBOOT )
{
if ( (~readKeys() & 0x7E) == 0 )
{
if (state == ST_REBOOT) {
if ((~readKeys() & 0x7E) == 0) {
NVIC_SystemReset();
}
}
@ -1094,4 +982,3 @@ extern uint8_t OptrexDisplay ;
return 0;
}