Horus bootloader (#5455)

Horus bootloader
This commit is contained in:
Raphael Coeffic
2017-12-19 12:13:59 +01:00
committed by Bertrand Songis
parent 908adb4d2b
commit 0d7832b208
54 changed files with 1772 additions and 1038 deletions
+5 -6
View File
@@ -25,6 +25,7 @@ if(PCB STREQUAL X9E)
endif()
set(GUI_DIR 212x64)
set(BITMAPS_TARGET taranis_bitmaps)
set(FONTS_TARGET taranis_fonts)
set(LCD_DRIVER lcd_driver_spi.cpp)
set(GVAR_SCREEN model_gvars.cpp)
elseif(PCB STREQUAL X9D+)
@@ -38,6 +39,7 @@ elseif(PCB STREQUAL X9D+)
add_definitions(-DEEPROM_VARIANT=0)
set(GUI_DIR 212x64)
set(BITMAPS_TARGET taranis_bitmaps)
set(FONTS_TARGET taranis_fonts)
set(LCD_DRIVER lcd_driver_spi.cpp)
set(SERIAL2_DRIVER ../common/arm/stm32/serial2_driver.cpp)
set(GVAR_SCREEN model_gvars.cpp)
@@ -52,6 +54,7 @@ elseif(PCB STREQUAL X9D)
add_definitions(-DEEPROM_VARIANT=0)
set(GUI_DIR 212x64)
set(BITMAPS_TARGET taranis_bitmaps)
set(FONTS_TARGET taranis_fonts)
set(LCD_DRIVER lcd_driver_aspi.cpp)
set(SERIAL2_DRIVER ../common/arm/stm32/serial2_driver.cpp)
set(GVAR_SCREEN model_gvars.cpp)
@@ -68,6 +71,7 @@ elseif(PCB STREQUAL X7)
add_definitions(-DPWR_BUTTON_${PWR_BUTTON})
set(GUI_DIR 128x64)
set(BITMAPS_TARGET 9x_bitmaps)
set(FONTS_TARGET 9x_fonts_1bit)
set(LCD_DRIVER lcd_driver_spi.cpp)
endif()
@@ -124,17 +128,12 @@ set(TARGET_SRC
../common/arm/stm32/adc_driver.cpp
)
set(FIRMWARE_SRC
${FIRMWARE_SRC}
loadboot.cpp
)
set(FIRMWARE_TARGET_SRC
${FIRMWARE_TARGET_SRC}
${LCD_DRIVER}
i2c_driver.cpp
pwr_driver.cpp
flash_driver.cpp
../common/arm/loadboot.cpp
)
if(LCD_DUAL_BUFFER)
+34 -16
View File
@@ -51,21 +51,21 @@ void init2MhzTimer()
// Starts TIMER at 200Hz (5ms)
void init5msTimer()
{
INTERRUPT_5MS_TIMER->ARR = 4999 ; // 5mS
INTERRUPT_5MS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1 ; // 1uS from 30MHz
INTERRUPT_5MS_TIMER->CCER = 0 ;
INTERRUPT_5MS_TIMER->CCMR1 = 0 ;
INTERRUPT_5MS_TIMER->EGR = 0 ;
INTERRUPT_5MS_TIMER->CR1 = 5 ;
INTERRUPT_5MS_TIMER->DIER |= 1 ;
NVIC_EnableIRQ(INTERRUPT_5MS_IRQn) ;
NVIC_SetPriority(INTERRUPT_5MS_IRQn, 7);
INTERRUPT_xMS_TIMER->ARR = 4999 ; // 5mS in uS
INTERRUPT_xMS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1 ; // 1uS
INTERRUPT_xMS_TIMER->CCER = 0 ;
INTERRUPT_xMS_TIMER->CCMR1 = 0 ;
INTERRUPT_xMS_TIMER->EGR = 0 ;
INTERRUPT_xMS_TIMER->CR1 = 5 ;
INTERRUPT_xMS_TIMER->DIER |= 1 ;
NVIC_EnableIRQ(INTERRUPT_xMS_IRQn) ;
NVIC_SetPriority(INTERRUPT_xMS_IRQn, 7);
}
void stop5msTimer( void )
{
INTERRUPT_5MS_TIMER->CR1 = 0 ; // stop timer
NVIC_DisableIRQ(INTERRUPT_5MS_IRQn) ;
INTERRUPT_xMS_TIMER->CR1 = 0 ; // stop timer
NVIC_DisableIRQ(INTERRUPT_xMS_IRQn) ;
}
// TODO use the same than board_sky9x.cpp
@@ -93,9 +93,9 @@ void interrupt5ms()
}
#if !defined(SIMU)
extern "C" void INTERRUPT_5MS_IRQHandler()
extern "C" void INTERRUPT_xMS_IRQHandler()
{
INTERRUPT_5MS_TIMER->SR &= ~TIM_SR_UIF ;
INTERRUPT_xMS_TIMER->SR &= ~TIM_SR_UIF ;
interrupt5ms() ;
DEBUG_INTERRUPT(INT_5MS);
}
@@ -147,9 +147,27 @@ void sportUpdatePowerOff()
void boardInit()
{
#if !defined(SIMU)
RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph | PCBREV_RCC_AHB1Periph | KEYS_RCC_AHB1Periph | LCD_RCC_AHB1Periph | AUDIO_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph | ADC_RCC_AHB1Periph | I2C_RCC_AHB1Periph | SD_RCC_AHB1Periph | HAPTIC_RCC_AHB1Periph | INTMODULE_RCC_AHB1Periph | EXTMODULE_RCC_AHB1Periph | TELEMETRY_RCC_AHB1Periph | SPORT_UPDATE_RCC_AHB1Periph | SERIAL_RCC_AHB1Periph | TRAINER_RCC_AHB1Periph | HEARTBEAT_RCC_AHB1Periph | BT_RCC_AHB1Periph, ENABLE);
RCC_APB1PeriphClockCmd(LCD_RCC_APB1Periph | AUDIO_RCC_APB1Periph | BACKLIGHT_RCC_APB1Periph | INTERRUPT_5MS_APB1Periph | TIMER_2MHz_APB1Periph | I2C_RCC_APB1Periph | SD_RCC_APB1Periph | TRAINER_RCC_APB1Periph | TELEMETRY_RCC_APB1Periph | SERIAL_RCC_APB1Periph | BT_RCC_APB1Periph, ENABLE);
RCC_APB2PeriphClockCmd(BACKLIGHT_RCC_APB2Periph | ADC_RCC_APB2Periph | HAPTIC_RCC_APB2Periph | INTMODULE_RCC_APB2Periph | EXTMODULE_RCC_APB2Periph | HEARTBEAT_RCC_APB2Periph | BT_RCC_APB2Periph, ENABLE);
RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph | PCBREV_RCC_AHB1Periph |
KEYS_RCC_AHB1Periph | LCD_RCC_AHB1Periph |
AUDIO_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph |
ADC_RCC_AHB1Periph | I2C_RCC_AHB1Periph |
SD_RCC_AHB1Periph | HAPTIC_RCC_AHB1Periph |
INTMODULE_RCC_AHB1Periph | EXTMODULE_RCC_AHB1Periph |
TELEMETRY_RCC_AHB1Periph | SPORT_UPDATE_RCC_AHB1Periph |
SERIAL_RCC_AHB1Periph | TRAINER_RCC_AHB1Periph |
HEARTBEAT_RCC_AHB1Periph | BT_RCC_AHB1Periph, ENABLE);
RCC_APB1PeriphClockCmd(LCD_RCC_APB1Periph | AUDIO_RCC_APB1Periph |
BACKLIGHT_RCC_APB1Periph | INTERRUPT_xMS_RCC_APB1Periph |
TIMER_2MHz_RCC_APB1Periph | I2C_RCC_APB1Periph |
SD_RCC_APB1Periph | TRAINER_RCC_APB1Periph |
TELEMETRY_RCC_APB1Periph | SERIAL_RCC_APB1Periph |
BT_RCC_APB1Periph, ENABLE);
RCC_APB2PeriphClockCmd(BACKLIGHT_RCC_APB2Periph | ADC_RCC_APB2Periph |
HAPTIC_RCC_APB2Periph | INTMODULE_RCC_APB2Periph |
EXTMODULE_RCC_APB2Periph | HEARTBEAT_RCC_APB2Periph |
BT_RCC_APB2Periph, ENABLE);
#if !defined(PCBX9E)
// some X9E boards need that the pwrInit() is moved a little bit later
@@ -1,3 +0,0 @@
/bootloader_ramBoot.*
/.dep
@@ -1,655 +0,0 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "opentx.h"
#include "stamp.h"
#if defined(PCBX7)
#define BOOTLOADER_TITLE " X7 Bootloader - " VERSION
#elif defined(PCBTARANIS)
#define BOOTLOADER_TITLE " Taranis Bootloader - " VERSION
#else
#error "Not implemented"
#endif
#if defined(PCBX9E) || defined(PCBX7)
#define BOOT_KEY_UP KEY_MINUS
#define BOOT_KEY_DOWN KEY_PLUS
#else
#define BOOT_KEY_UP KEY_PLUS
#define BOOT_KEY_DOWN KEY_MINUS
#endif
#define BOOT_KEY_LEFT KEY_MENU
#define BOOT_KEY_RIGHT KEY_PAGE
#define BOOT_KEY_MENU KEY_ENTER
#define BOOT_KEY_EXIT KEY_EXIT
#define DISPLAY_CHAR_WIDTH 35
#if LCD_W >= 212
#define STR_OR_PLUGIN_USB_CABLE INDENT "Or plug in a USB cable for mass storage"
#define STR_HOLD_ENTER_TO_START "\012Hold [ENT] to start writing"
#define STR_INVALID_FIRMWARE "\011Not a valid firmware file! "
#define STR_INVALID_EEPROM "\011Not a valid EEPROM file! "
#else
#define STR_OR_PLUGIN_USB_CABLE INDENT "Or plug in a USB cable"
#define STR_HOLD_ENTER_TO_START "\006Hold [ENT] to start"
#define STR_INVALID_FIRMWARE "\004Not a valid firmware! "
#define STR_INVALID_EEPROM "\004Not a valid EEPROM! "
#endif
#define STR_USB_CONNECTED CENTER "\011USB Connected"
const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
{
'B', 'O', 'O', 'T', '1', '0'
};
#if defined(ROTARY_ENCODER_NAVIGATION)
volatile rotenc_t rotencValue[1] = {0};
#endif
// states
enum BootLoaderStates {
ST_START,
ST_FLASH_MENU,
ST_DIR_CHECK,
ST_OPEN_DIR,
ST_FILE_LIST,
ST_FLASH_CHECK,
ST_FLASHING,
ST_FLASH_DONE,
ST_RESTORE_MENU,
ST_USB,
ST_REBOOT,
};
enum MemoryTypes {
MEM_FLASH,
MEM_EEPROM
};
/*----------------------------------------------------------------------------
* Local variables
*----------------------------------------------------------------------------*/
uint32_t FirmwareSize;
uint32_t firmwareAddress = FIRMWARE_ADDRESS;
uint32_t firmwareWritten = 0;
uint32_t eepromAddress = 0;
uint32_t eepromWritten = 0;
TCHAR backupFilename[_MAX_LFN+1];
uint32_t Master_frequency;
volatile uint8_t Tenms;
FIL FlashFile;
DIR Dj;
FILINFO Finfo;
TCHAR Filenames[20][_MAX_LFN + 1];
uint32_t FileSize[20];
uint32_t Valid;
#define BLOCK_LEN 4096
uint8_t Block_buffer[BLOCK_LEN];
UINT BlockCount;
uint32_t memoryType;
uint32_t unlocked = 0;
void interrupt10ms(void)
{
Tenms |= 1; // 10 mS has passed
uint8_t index = KEY_MENU;
uint8_t in = readKeys();
for (uint8_t i = 1; i != uint8_t(1 << TRM_BASE); i <<= 1) {
uint8_t value = (in & i);
keys[index].input(value);
++index;
}
#if defined(PCBX9E) || defined(PCBX7)
checkRotaryEncoder();
static rotenc_t rePreviousValue;
rotenc_t reNewValue = (rotencValue[0] / 2);
int8_t scrollRE = reNewValue - rePreviousValue;
if (scrollRE) {
rePreviousValue = reNewValue;
if (scrollRE < 0) {
putEvent(EVT_KEY_FIRST(KEY_MINUS));
}
else {
putEvent(EVT_KEY_FIRST(KEY_PLUS));
}
}
#endif
}
void init10msTimer()
{
INTERRUPT_5MS_TIMER->ARR = 9999; // 10mS
INTERRUPT_5MS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS from 12MHz
INTERRUPT_5MS_TIMER->CCER = 0;
INTERRUPT_5MS_TIMER->CCMR1 = 0;
INTERRUPT_5MS_TIMER->EGR = 0;
INTERRUPT_5MS_TIMER->CR1 = 5;
INTERRUPT_5MS_TIMER->DIER |= 1;
NVIC_EnableIRQ(INTERRUPT_5MS_IRQn);
}
extern "C" void INTERRUPT_5MS_IRQHandler()
{
INTERRUPT_5MS_TIMER->SR &= ~TIM_SR_UIF;
interrupt10ms();
}
FRESULT readBinDir(DIR *dj, FILINFO *fno)
{
FRESULT fr;
uint32_t loop;
do {
loop = 0;
fr = f_readdir(dj, fno); // First entry
if (fr != FR_OK || fno->fname[0] == 0) {
break;
}
int32_t len = strlen(fno->fname) - 4;
if (len < 0) {
loop = 1;
}
if (fno->fname[len] != '.') {
loop = 1;
}
if ((fno->fname[len + 1] != 'b') && (fno->fname[len + 1] != 'B')) {
loop = 1;
}
if ((fno->fname[len + 2] != 'i') && (fno->fname[len + 2] != 'I')) {
loop = 1;
}
if ((fno->fname[len + 3] != 'n') && (fno->fname[len + 3] != 'N')) {
loop = 1;
}
} while (loop);
return fr;
}
uint32_t fillNames(uint32_t index)
{
uint32_t i;
FRESULT fr;
fr = f_readdir(&Dj, 0); // rewind
for (i = 0; i <= index; ++i) {
fr = readBinDir(&Dj, &Finfo); // First entry
if (fr == FR_NO_FILE) {
return 0;
}
}
strAppend(Filenames[0], Finfo.fname);
FileSize[0] = Finfo.fsize;
for (i = 1; i < 7; i += 1) {
fr = readBinDir(&Dj, &Finfo);
if (fr != FR_OK || Finfo.fname[0] == 0) {
break;
}
strAppend(Filenames[i], Finfo.fname);
FileSize[i] = Finfo.fsize;
}
return i;
}
const char *getBinaryPath()
{
if (memoryType == MEM_FLASH)
return FIRMWARES_PATH;
else
return EEPROMS_PATH;
}
FRESULT openBinaryFile(uint32_t index)
{
TCHAR filename[_MAX_LFN+1];
FRESULT fr;
memset(Block_buffer, 0, sizeof(Block_buffer));
strAppend(strAppend(strAppend(filename, getBinaryPath()), "/"), Filenames[index]);
if ((fr = f_open(&FlashFile, filename, FA_READ)) != FR_OK) {
return fr;
}
if (memoryType == MEM_FLASH) {
if ((fr = f_lseek(&FlashFile, BOOTLOADER_SIZE)) != FR_OK) {
return fr;
}
}
fr = f_read(&FlashFile, Block_buffer, BLOCK_LEN, &BlockCount);
if (BlockCount == BLOCK_LEN)
return fr;
else
return FR_INVALID_OBJECT;
}
uint32_t isValidBufferStart(const uint8_t * buffer)
{
if (memoryType == MEM_FLASH)
return isFirmwareStart(buffer);
else
return isEepromStart(buffer);
}
int menuFlashFile(uint32_t index, event_t event)
{
FRESULT fr;
lcdDrawTextAlignedLeft(4*FH, STR_HOLD_ENTER_TO_START);
if (Valid == 0) {
// Validate file here
if ((fr = openBinaryFile(index))) {
Valid = 2;
}
else {
if ((fr = f_close(&FlashFile))) {
Valid = 2;
}
else {
Valid = 1;
}
if (!isValidBufferStart(Block_buffer)) {
Valid = 2;
}
}
}
if (Valid == 2) {
if (memoryType == MEM_FLASH)
lcdDrawTextAlignedLeft(4*FH, STR_INVALID_FIRMWARE);
else
lcdDrawTextAlignedLeft(4*FH, STR_INVALID_EEPROM);
if (event == EVT_KEY_BREAK(BOOT_KEY_EXIT) || event == EVT_KEY_BREAK(BOOT_KEY_MENU)) {
return 0;
}
return -1;
}
if (event == EVT_KEY_LONG(BOOT_KEY_MENU)) {
fr = openBinaryFile(index);
return (fr == FR_OK && isValidBufferStart(Block_buffer));
}
else if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) {
return 0;
}
return -1;
}
extern Key keys[];
static uint32_t PowerUpDelay;
void flashWriteBlock()
{
uint32_t blockOffset = 0;
while (BlockCount) {
flashWrite((uint32_t *)firmwareAddress, (uint32_t *)&Block_buffer[blockOffset]);
blockOffset += FLASH_PAGESIZE;
firmwareAddress += FLASH_PAGESIZE;
if (BlockCount > FLASH_PAGESIZE) {
BlockCount -= FLASH_PAGESIZE;
}
else {
BlockCount = 0;
}
}
}
void writeEepromBlock()
{
eepromWriteBlock(Block_buffer, eepromAddress, BlockCount);
eepromAddress += BlockCount;
}
int main()
{
uint8_t index = 0;
uint8_t maxhsize = DISPLAY_CHAR_WIDTH;
FRESULT fr;
uint32_t state = ST_START;
uint32_t nameCount = 0;
uint32_t vpos = 0;
uint32_t hpos = 0;
#if defined(PCBTARANIS)
wdt_reset();
RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph | KEYS_RCC_AHB1Periph | LCD_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph | I2C_RCC_AHB1Periph | SD_RCC_AHB1Periph, ENABLE);
RCC_APB1PeriphClockCmd(LCD_RCC_APB1Periph | BACKLIGHT_RCC_APB1Periph | INTERRUPT_5MS_APB1Periph | I2C_RCC_APB1Periph | SD_RCC_APB1Periph, ENABLE);
RCC_APB2PeriphClockCmd(BACKLIGHT_RCC_APB2Periph, ENABLE);
#endif
pwrInit();
delaysInit(); // needed for lcdInit()
lcdInit();
backlightInit();
lcdClear();
lcdDrawSizedText(0, 0, (const char *)bootloaderVersion, 0); // trick to avoid bootloaderVersion to be optimized out ...
lcdDrawTextAlignedLeft(0, BOOTLOADER_TITLE);
lcdInvertLine(0);
lcdRefresh();
keysInit();
i2cInit();
__enable_irq();
init10msTimer();
#if defined(PCBTARANIS)
// SD card detect pin
sdInit();
usbInit();
#endif
for (;;) {
wdt_reset();
if (Tenms) {
Tenms = 0;
lcdRefreshWait();
lcdClear();
lcdDrawTextAlignedLeft(0, BOOTLOADER_TITLE);
lcdInvertLine(0);
event_t event = getEvent();
if (state != ST_USB) {
if (usbPlugged()) {
state = ST_USB;
if (!unlocked) {
unlocked = 1;
unlockFlash();
}
usbStart();
usbPluggedIn();
}
}
if (state == ST_START) {
lcdDrawTextAlignedLeft(2*FH, "\010Write Firmware");
lcdDrawTextAlignedLeft(3*FH, "\010Restore EEPROM");
lcdDrawTextAlignedLeft(4*FH, "\010Exit");
#if LCD_W >= 212
lcdDrawTextAlignedLeft(6*FH, "\001Curr FW:");
lcdDrawText(50, 6*FH, getOtherVersion());
#else
lcdDrawTextAlignedLeft(6*FH, "\001FW:");
// Remove opentx- from string
const char* other_ver = getOtherVersion();
if (strstr(other_ver, "opentx-"))
other_ver = other_ver+7;
lcdDrawText(20, 6*FH, other_ver);
#endif
lcdInvertLine(2+vpos);
lcdDrawTextAlignedLeft(7*FH, STR_OR_PLUGIN_USB_CABLE);
if (event == EVT_KEY_FIRST(BOOT_KEY_DOWN)) {
vpos == 2 ? vpos = 0 : vpos = vpos+1;
}
else if (event == EVT_KEY_FIRST(BOOT_KEY_UP)) {
vpos == 0 ? vpos = 2 : vpos = vpos-1;
}
else if (event == EVT_KEY_BREAK(BOOT_KEY_MENU)) {
switch (vpos) {
case 0:
state = ST_FLASH_MENU;
break;
case 1:
state = ST_RESTORE_MENU;
break;
default:
state = ST_REBOOT;
}
}
}
if (state == ST_USB) {
lcdDrawTextAlignedLeft(4*FH, STR_USB_CONNECTED);
if (usbPlugged() == 0) {
vpos = 0;
usbStop();
if (unlocked) {
lockFlash();
unlocked = 0;
}
state = ST_START;
}
}
if (state == ST_FLASH_MENU || state == ST_RESTORE_MENU) {
sdInit();
memoryType = (state == ST_RESTORE_MENU ? MEM_EEPROM : MEM_FLASH);
state = ST_DIR_CHECK;
}
else if (state == ST_DIR_CHECK) {
fr = f_chdir(getBinaryPath());
if (fr == FR_OK) {
state = ST_OPEN_DIR;
}
else {
lcdDrawTextAlignedLeft(2*FH, INDENT "Directory is missing!");
if (event == EVT_KEY_BREAK(BOOT_KEY_EXIT) || event == EVT_KEY_BREAK(BOOT_KEY_MENU)) {
vpos = 0;
state = ST_START;
}
}
}
if (state == ST_OPEN_DIR) {
index = 0;
fr = f_opendir(&Dj, ".");
if (fr == FR_OK) {
state = ST_FILE_LIST;
nameCount = fillNames(0);
hpos = 0;
vpos = 0;
}
}
if (state == ST_FILE_LIST) {
uint32_t limit = 6;
if (nameCount < limit) {
limit = nameCount;
}
maxhsize = 0;
for (uint32_t i=0; i<limit; i++) {
uint32_t x;
x = strlen(Filenames[i]);
if (x > maxhsize) {
maxhsize = x;
}
if (x > DISPLAY_CHAR_WIDTH) {
if (hpos + DISPLAY_CHAR_WIDTH > x) {
x = x - DISPLAY_CHAR_WIDTH;
}
else {
x = hpos;
}
}
else {
x = 0;
}
lcdDrawSizedText(INDENT_WIDTH, 16 + FH * i, &Filenames[i][x], DISPLAY_CHAR_WIDTH, 0);
}
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) {
index += 1;
nameCount = fillNames(index);
}
}
}
else if (event == EVT_KEY_REPT(BOOT_KEY_UP) || event == EVT_KEY_FIRST(BOOT_KEY_UP)) {
if (vpos > 0) {
vpos -= 1;
}
else {
if (index) {
index -= 1;
nameCount = fillNames(index);
}
}
}
#if !defined(PCBTARANIS)
else if (event == EVT_KEY_REPT(BOOT_KEY_RIGHT) || event == EVT_KEY_FIRST(BOOT_KEY_RIGHT)) {
if (hpos + DISPLAY_CHAR_WIDTH < maxhsize) {
hpos += 1;
}
}
else if (event == EVT_KEY_REPT(BOOT_KEY_LEFT) || event == EVT_KEY_FIRST(BOOT_KEY_LEFT)) {
if (hpos) {
hpos -= 1;
}
}
#endif
else if (event == EVT_KEY_BREAK(BOOT_KEY_MENU)) {
// Select file to flash
state = ST_FLASH_CHECK;
Valid = 0;
}
else if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) {
state = ST_START;
vpos = 0;
}
lcdInvertLine(2 + vpos);
}
else if (state == ST_FLASH_CHECK) {
int result = menuFlashFile(vpos, event);
FirmwareSize = FileSize[vpos] - BOOTLOADER_SIZE;
if (result == 0) {
// canceled
state = ST_FILE_LIST;
}
else if (result == 1) {
// confirmed
firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE;
firmwareWritten = 0;
eepromAddress = 0;
eepromWritten = 0;
state = ST_FLASHING;
}
}
else if (state == ST_FLASHING) {
// commit to flashing
lcdDrawTextAlignedLeft(4*FH, CENTER "\015Writing...");
if (!unlocked && (memoryType == MEM_FLASH)) {
unlocked = 1;
unlockFlash();
}
int progress;
if (memoryType == MEM_FLASH) {
flashWriteBlock();
firmwareWritten += sizeof(Block_buffer);
progress = ((LCD_W-12)*firmwareWritten) / FirmwareSize;
}
else {
writeEepromBlock();
eepromWritten += sizeof(Block_buffer);
progress = ((LCD_W-12)*eepromWritten) / EEPROM_SIZE;
}
lcdDrawRect(3, 6*FH+4, (LCD_W-8), 7);
lcdDrawSolidHorizontalLine(5, 6*FH+6, progress, FORCE);
lcdDrawSolidHorizontalLine(5, 6*FH+7, progress, FORCE);
lcdDrawSolidHorizontalLine(5, 6*FH+8, progress, FORCE);
fr = f_read(&FlashFile, Block_buffer, sizeof(Block_buffer), &BlockCount);
if (BlockCount == 0) {
state = ST_FLASH_DONE; // EOF
}
if (firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE) {
state = ST_FLASH_DONE; // Backstop
}
if (eepromWritten >= EEPROM_SIZE) {
state = ST_FLASH_DONE; // Backstop
}
}
if (state == ST_FLASH_DONE) {
if (unlocked) {
lockFlash();
unlocked = 0;
}
lcdDrawTextAlignedLeft(4*FH, CENTER "\007Writing complete");
if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT) || event == EVT_KEY_BREAK(BOOT_KEY_MENU)) {
state = ST_START;
vpos = 0;
}
}
if (event == EVT_KEY_LONG(BOOT_KEY_EXIT)) {
state = ST_REBOOT;
}
lcdRefresh();
if (PowerUpDelay < 20) { // 200 mS
PowerUpDelay += 1;
}
else {
sdPoll10ms();
}
}
if (state != ST_FLASHING && state != ST_USB) {
if (pwrOffPressed()) {
lcdOff(); // this drains LCD caps
pwrOff();
for (;;) {
// Wait for power to go off
}
}
}
if (state == ST_REBOOT) {
if (readKeys() == 0) {
lcdClear();
lcdRefresh();
lcdRefreshWait();
RCC->CSR |= RCC_CSR_RMVF; // clear the reset flags in RCC clock control & status register
NVIC_SystemReset();
}
}
}
return 0;
}
@@ -0,0 +1,91 @@
#include "opentx.h"
#include "../../common/arm/stm32/bootloader/boot.h"
#include "../../common/arm/stm32/bootloader/bin_files.h"
extern MemoryType memoryType;
void bootloaderInitScreen()
{
}
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);
}
void bootloaderDrawFilename(const char* str, uint8_t line, bool selected)
{
bootloaderDrawMsg(INDENT_WIDTH, str, line, selected);
}
void bootloaderDrawScreen(BootloaderState st, int opt, const char* str)
{
lcdClear();
lcdDrawText(0, 0, BOOTLOADER_TITLE, INVERS);
if (st == ST_START) {
lcdDrawTextAlignedLeft(2*FH, "\010Write Firmware");
lcdDrawTextAlignedLeft(3*FH, "\010Restore EEPROM");
lcdDrawTextAlignedLeft(4*FH, "\010Exit");
#if LCD_W >= 212
lcdDrawTextAlignedLeft(6*FH, "\001Curr FW:");
lcdDrawText(50, 6*FH, getOtherVersion(nullptr));
#else
lcdDrawTextAlignedLeft(6*FH, "\001FW:");
// Remove opentx- from string
const char* other_ver = getOtherVersion(nullptr);
if (strstr(other_ver, "opentx-"))
other_ver = other_ver+7;
lcdDrawText(20, 6*FH, other_ver);
#endif
lcdInvertLine(2+opt);
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 {
bootloaderDrawMsg(INDENT_WIDTH, "Directory is empty!", 1, false);
}
}
else if (st == ST_FLASH_CHECK) {
if (opt == FC_ERROR) {
if (memoryType == MEM_FLASH)
bootloaderDrawMsg(0, STR_INVALID_FIRMWARE, 2, false);
else
bootloaderDrawMsg(0, STR_INVALID_EEPROM, 2, false);
}
else if (opt == FC_OK) {
const char* vers = getOtherVersion((char*)Block_buffer);
#if LCD_W < 212
// Remove opentx- from string
if (strstr(vers, "opentx-"))
vers = vers+7;
#endif
bootloaderDrawMsg(INDENT_WIDTH, vers, 0, false);
bootloaderDrawMsg(0, STR_HOLD_ENTER_TO_START, 2, false);
}
}
else if (st == ST_FLASHING) {
lcdDrawTextAlignedLeft(4*FH, CENTER "\015Writing...");
lcdDrawRect(3, 6*FH+4, (LCD_W-8), 7);
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+8, (LCD_W-12) * opt / 100, FORCE);
}
else if (st == ST_FLASH_DONE) {
lcdDrawTextAlignedLeft(4*FH, CENTER "\007Writing complete");
}
}
@@ -1,4 +0,0 @@
void _init (void)
{
}
-164
View File
@@ -1,164 +0,0 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "opentx.h"
//After reset, write is not allowed in the Flash control register (FLASH_CR) to protect the
//Flash memory against possible unwanted operations due, for example, to electric
//disturbances. The following sequence is used to unlock this register:
//1. Write KEY1 = 0x45670123 in the Flash key register (FLASH_KEYR)
//2. Write KEY2 = 0xCDEF89AB in the Flash key register (FLASH_KEYR)
//Any wrong sequence will return a bus error and lock up the FLASH_CR register until the
//next reset.
//The FLASH_CR register can be locked again by software by setting the LOCK bit in the
//FLASH_CR register.
void unlockFlash()
{
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
void lockFlash()
{
while (FLASH->SR & FLASH_SR_BSY);
FLASH->CR |= FLASH_CR_LOCK;
}
void waitFlashIdle()
{
while (FLASH->SR & FLASH_FLAG_BSY) {
wdt_reset();
}
}
#define SECTOR_MASK ((uint32_t)0xFFFFFF07)
void eraseSector(uint32_t sector)
{
waitFlashIdle();
FLASH->CR &= CR_PSIZE_MASK;
FLASH->CR |= FLASH_PSIZE_WORD;
FLASH->CR &= SECTOR_MASK;
FLASH->CR |= FLASH_CR_SER | (sector << 3);
FLASH->CR |= FLASH_CR_STRT;
/* Wait for operation to be completed */
waitFlashIdle();
/* if the erase operation is completed, disable the SER Bit */
FLASH->CR &= (~FLASH_CR_SER);
FLASH->CR &= SECTOR_MASK;
}
void flashWrite(uint32_t * address, uint32_t * buffer) // page size is 256 bytes
{
if ((uint32_t) address == 0x08000000) {
eraseSector(0);
}
else if ((uint32_t) address == 0x08004000) {
eraseSector(1);
}
else if ((uint32_t) address == 0x08008000) {
eraseSector(2);
}
else if ((uint32_t) address == 0x0800C000) {
eraseSector(3);
}
else if ((uint32_t) address == 0x08010000) {
eraseSector(4);
}
else if ((uint32_t) address == 0x08020000) {
eraseSector(5);
}
else if ((uint32_t) address == 0x08040000) {
eraseSector(6);
}
else if ((uint32_t) address == 0x08060000) {
eraseSector(7);
}
for (uint32_t i=0; i<FLASH_PAGESIZE/4; i++) {
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
// Wait for last operation to be completed
waitFlashIdle();
FLASH->CR &= CR_PSIZE_MASK;
FLASH->CR |= FLASH_PSIZE_WORD;
FLASH->CR |= FLASH_CR_PG;
*address = *buffer;
/* Wait for operation to be completed */
waitFlashIdle();
FLASH->CR &= (~FLASH_CR_PG);
/* Check the written value */
if (*address != *buffer) {
/* Flash content doesn't match SRAM content */
return;
}
/* Increment FLASH destination address */
address += 1;
buffer += 1;
}
}
uint32_t isFirmwareStart(const uint8_t * buffer)
{
const uint32_t * block = (const uint32_t *)buffer;
#if defined(PCBX9E)
if ((block[0] & 0xFFFC0000) != 0x10000000 && (block[0] & 0xFFFC0000) != 0x20000000) {
return 0;
}
if ((block[1] & 0xFFF00000) != 0x08000000) {
return 0;
}
if ((block[2] & 0xFFF00000) != 0x08000000) {
return 0;
}
#else
if ((block[0] & 0xFFFC0000) != 0x20000000) {
return 0;
}
if ((block[1] & 0xFFF00000) != 0x08000000) {
return 0;
}
if ((block[2] & 0xFFF00000) != 0x08000000) {
return 0;
}
#endif
return 1;
}
uint32_t isBootloaderStart(const uint8_t * buffer)
{
const uint32_t * block = (const uint32_t *)buffer;
for (int i=0; i<256; i++) {
if (block[i] == 0x544F4F42/*BOOT*/) {
return 1;
}
}
return 0;
}
+13 -6
View File
@@ -433,9 +433,14 @@
#if defined(PCBX9E) || defined(PCBX7)
#define PWR_PRESS_BUTTON
#endif
#define PWR_GPIO GPIOD
#define PWR_SWITCH_GPIO_REG PWR_GPIO->IDR
#define PWR_SWITCH_GPIO_PIN GPIO_Pin_1 // PD.01
#define PWR_ON_GPIO_PIN GPIO_Pin_0 // PD.00
#define PWR_ON_GPIO_MODER GPIO_MODER_MODER0
#define PWR_ON_GPIO_MODER_OUT GPIO_MODER_MODER0_0
#if defined(PCBX7)
#define LED_GREEN_GPIO GPIOC
#define LED_GREEN_GPIO_PIN GPIO_Pin_4 // PC.04
@@ -697,6 +702,8 @@
#define LCD_RST_GPIO GPIOD
#define LCD_RST_GPIO_PIN GPIO_Pin_12 // PD.12
#endif
#define LCD_RCC_APB2Periph 0
// I2C Bus: EEPROM and CAT5137 digital pot for volume control
#define I2C_RCC_AHB1Periph RCC_AHB1Periph_GPIOB
@@ -828,14 +835,14 @@
#define BT_RCC_APB2Periph 0
#endif
// 5ms Interrupt
#define INTERRUPT_5MS_APB1Periph RCC_APB1Periph_TIM14
#define INTERRUPT_5MS_TIMER TIM14
#define INTERRUPT_5MS_IRQn TIM8_TRG_COM_TIM14_IRQn
#define INTERRUPT_5MS_IRQHandler TIM8_TRG_COM_TIM14_IRQHandler
// Xms Interrupt
#define INTERRUPT_xMS_RCC_APB1Periph RCC_APB1Periph_TIM14
#define INTERRUPT_xMS_TIMER TIM14
#define INTERRUPT_xMS_IRQn TIM8_TRG_COM_TIM14_IRQn
#define INTERRUPT_xMS_IRQHandler TIM8_TRG_COM_TIM14_IRQHandler
// 2MHz Timer
#define TIMER_2MHz_APB1Periph RCC_APB1Periph_TIM7
#define TIMER_2MHz_RCC_APB1Periph RCC_APB1Periph_TIM7
#define TIMER_2MHz_TIMER TIM7
#endif // _HAL_H_
+7 -7
View File
@@ -47,7 +47,7 @@ SECTIONS
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
*(.version)
KEEP(*(.bootversiondata))
. = ALIGN(4); /* Align the start of the text part */
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
@@ -62,36 +62,36 @@ SECTIONS
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >RAM
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >RAM
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >RAM
} >FLASH
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >RAM
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >RAM
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(.fini_array*))
KEEP (*(SORT(.fini_array.*)))
PROVIDE_HIDDEN (__fini_array_end = .);
} >RAM
} >FLASH
/* used by the startup to initialize data */
_sidata = .;
+1 -4
View File
@@ -50,10 +50,7 @@ SECTIONS
CREATE_OBJECT_SYMBOLS
KEEP(*(.isr_boot_vector)) /* Startup code */
KEEP(*(.bootversiondata))
*(.bootrodata)
*(.bootrodata.*)
KEEP(*(.bootloader)) /* Bootloader code */
. = 32768; /* Set the start of the main program */
_stext = .; /* Provide the name for the start of this section */
+4 -7
View File
@@ -51,14 +51,11 @@ SECTIONS
CREATE_OBJECT_SYMBOLS
KEEP(*(.isr_boot_vector)) /* Startup code */
KEEP(*(.bootversiondata))
*(.bootrodata)
*(.bootrodata.*)
KEEP(*(.bootloader)) /* Bootloader code */
. = 32768; /* Set the start of the main program */
. = 0x8000; /* Set the start of the main program */
_stext = .; /* Provide the name for the start of this section */
. = ALIGN(4);
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
KEEP(*(.fwversiondata))
*(.text) /* .text sections (code) */
@@ -67,7 +64,7 @@ SECTIONS
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))