MAGIC value for powerReason should be stored in RTC backup on Horus (#7096)

- Allows the radio main application to start correctly when coming from an app in RAM
- Refactoring of the `UNEXPECTED_SHUTDOWN()` process
- Saves the RTC battery as BACKUP RAM is not needed on normal shutdown
- On PWR OFF on an ALERT, the radio won't show the next alert before shutting down
- Bootloader flashable through the SD Manager
This commit is contained in:
Bertrand Songis
2019-11-21 11:50:33 +01:00
committed by GitHub
parent 103534bdcf
commit 4cdd17f0d7
41 changed files with 235 additions and 190 deletions
@@ -8,6 +8,8 @@ if(CPU_TYPE_FULL STREQUAL STM32F429xI)
../f4/system_stm32f4xx.c
../../../../../targets/${TARGET_DIR}/startup_stm32f42_43xxx.s
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rtc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_pwr.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_syscfg.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_exti.c
@@ -25,6 +27,8 @@ elseif(CPU_TYPE_FULL STREQUAL STM32F407xE)
../f4/system_stm32f4xx.c
../../../../../targets/${TARGET_DIR}/startup_stm32f40_41xxx.s
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rtc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_pwr.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_syscfg.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_exti.c
@@ -99,20 +103,16 @@ if(PCB STREQUAL X7 OR PCB STREQUAL XLITE OR PCB STREQUAL XLITES)
)
endif()
if(NOT (PCB STREQUAL X10 OR PCB STREQUAL X12S))
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/${TARGET_DIR}/i2c_driver.cpp
)
remove_definitions(-DDEBUG)
else()
if(PCB STREQUAL X10 OR PCB STREQUAL X12S)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/${TARGET_DIR}/sdram_driver.c
../../../../../targets/common/arm/stm32/sdio_sd.c
../../../../../targets/common/arm/stm32/rtc_driver.cpp
../../../../../targets/${TARGET_DIR}/haptic_driver.cpp
../../../../../gui/${GUI_DIR}/bitmapbuffer.cpp
../../../../../gui/${GUI_DIR}/rle.cpp
../../../../../syscalls.c
)
@@ -123,12 +123,20 @@ else()
../../../../../serial.cpp
)
endif()
else()
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/${TARGET_DIR}/i2c_driver.cpp
)
remove_definitions(-DDEBUG)
endif()
remove_definitions(-DDISK_CACHE)
remove_definitions(-DLUA)
remove_definitions(-DCLI)
remove_definitions(-DUSB_SERIAL)
remove_definitions(-DWATCHDOG)
add_definitions(-DBOOT)
set(CMAKE_EXE_LINKER_FLAGS "-mcpu=${MCU} -mthumb -nostartfiles -lm -T${RADIO_SRC_DIRECTORY}/targets/${TARGET_DIR}/stm32_ramboot.ld -Wl,-Map=bootloader.map,--cref,--no-warn-mismatch,--gc-sections")
@@ -195,7 +195,7 @@ int main()
FRESULT fr;
uint32_t nameCount = 0;
wdt_reset();
WDG_RESET();
RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph | KEYS_RCC_AHB1Periph |
LCD_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph |
@@ -215,7 +215,7 @@ int main()
#if defined(PCBHORUS)
// wait a bit for the inputs to stabilize...
for (uint32_t i = 0; i < 50000; i++) {
wdt_reset();
WDG_RESET();
}
#endif
@@ -265,12 +265,12 @@ int main()
#if defined(PWR_BUTTON_PRESS)
// wait until power button is released
while (pwrPressed()) {
wdt_reset();
WDG_RESET();
}
#endif
for (;;) {
wdt_reset();
WDG_RESET();
if (tenms) {
tenms = 0;
@@ -506,16 +506,12 @@ int main()
lcdRefresh();
lcdRefreshWait();
#if !defined(EEPROM)
// Use jump on radios with emergency mode
// to avoid triggering it with a soft reset
// Jump to proper application address
jumpTo(APP_START_ADDRESS);
#else
// Use software reset everywhere else
NVIC_SystemReset();
#if defined(RTC_BACKUP_RAM)
rtcInit();
RTC->BKP0R = SOFTRESET_REQUEST;
#endif
NVIC_SystemReset();
}
}
@@ -523,5 +519,5 @@ int main()
}
#if defined(PCBHORUS)
void *__dso_handle = 0;
void *__dso_handle = nullptr;
#endif
@@ -20,6 +20,13 @@
#include "opentx.h"
void waitFlashIdle()
{
do {
WDG_RESET();
} while (FLASH->SR & FLASH_FLAG_BSY);
}
//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:
@@ -37,21 +44,16 @@ void unlockFlash()
void lockFlash()
{
while (FLASH->SR & FLASH_SR_BSY);
waitFlashIdle();
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)
{
WDG_ENABLE(3000); // some sectors may take > 1s to erase
waitFlashIdle();
FLASH->CR &= CR_PSIZE_MASK;
@@ -66,9 +68,11 @@ void eraseSector(uint32_t sector)
/* if the erase operation is completed, disable the SER Bit */
FLASH->CR &= (~FLASH_CR_SER);
FLASH->CR &= SECTOR_MASK;
WDG_ENABLE(WDG_DURATION);
}
void flashWrite(uint32_t * address, uint32_t * buffer) // page size is 256 bytes
void flashWrite(uint32_t * address, const uint32_t * buffer) // page size is 256 bytes
{
#define SECTOR_ADDRESS (((uint32_t)address) & 0xFFFFF)
@@ -110,7 +114,6 @@ void flashWrite(uint32_t * address, uint32_t * buffer) // page size is 256 bytes
eraseSector(4 + FLASH_BANK);
}
else if ((((uint32_t)address) & 0x3FFF) == 0) {
// test other 16KB sectors
if (SECTOR_ADDRESS == 0x04000) {
eraseSector(1 + FLASH_BANK);
@@ -141,7 +144,7 @@ void flashWrite(uint32_t * address, uint32_t * buffer) // page size is 256 bytes
/* Wait for operation to be completed */
waitFlashIdle();
FLASH->CR &= (~FLASH_CR_PG);
FLASH->CR &= ~FLASH_CR_PG;
/* Check the written value */
if (*address != *buffer) {
@@ -192,7 +195,7 @@ uint32_t isBootloaderStart(const uint8_t * buffer)
{
const uint32_t * block = (const uint32_t *)buffer;
for (int i=0; i<256; i++) {
for (int i = 0; i < 256; i++) {
if (block[i] == 0x544F4F42/*BOOT*/) {
return 1;
}
@@ -59,17 +59,14 @@ void rtcGetTime(struct gtm * t)
void rtcInit()
{
RTC_InitTypeDef RTC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RCC_LSEConfig(RCC_LSE_ON);
// Prevent lockup in case of 32kHz oscillator failure
uint32_t i = 0;
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
if ( ++i > 1000000 )
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {
if (++i > 1000000)
return;
}
@@ -77,17 +74,21 @@ void rtcInit()
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
#if !defined(BOOT)
RTC_InitTypeDef RTC_InitStruct;
// RTC time base = LSE / ((AsynchPrediv+1) * (SynchPrediv+1)) = 1 Hz*/
RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
RTC_InitStruct.RTC_AsynchPrediv = 127;
RTC_InitStruct.RTC_SynchPrediv = 255;
RTC_Init(&RTC_InitStruct);
struct gtm utm;
rtcGetTime(&utm);
g_rtcTime = gmktime(&utm);
#endif
#if defined(RAMBACKUP)
#if defined(RTC_BACKUP_RAM) && !defined(BOOT)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
PWR_BackupRegulatorCmd(ENABLE);
#endif