refactor: IWDG driver and EM trigger (#3846)

This commit is contained in:
Raphael Coeffic
2023-12-04 07:14:25 +10:00
committed by GitHub
parent e18a2a90a4
commit 5153aaeea0
89 changed files with 1204 additions and 3202 deletions
@@ -54,6 +54,8 @@ set(FIRMWARE_TARGET_SRC ${FIRMWARE_TARGET_SRC}
../common/arm/stm32/stm32_exti_driver.cpp
../common/arm/stm32/mixer_scheduler_driver.cpp
../common/arm/stm32/pwr_driver.cpp
../common/arm/stm32/watchdog_driver.cpp
../common/arm/stm32/abnormal_reboot.cpp
)
if(IMU)
@@ -0,0 +1,81 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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 "hal/abnormal_reboot.h"
#include "stm32_hal_ll.h"
#define __REBOOT_DATA __attribute__((section(".rebootdata"), aligned(4)))
// This variable is define at a fixed memory location
// and is used in bootloader and firmware to pass
// commands across reboot.
uint32_t _reboot_cmd __REBOOT_DATA;
constexpr uint32_t _SOFTRESET_REQUEST = 0xCAFEDEAD;
constexpr uint32_t _SHUTDOWN_REQUEST = 0xDEADBEEF;
constexpr uint32_t _REBOOT_CAUSE_DEFAULT = 0xFFFFFFFF;
static uint32_t _reboot_cause = _REBOOT_CAUSE_DEFAULT;
#if defined(DEBUG)
uint32_t _dbg_csr = 0xFFFFFFFF;
#endif
void _init_reboot_cause()
{
if (_reboot_cause != _REBOOT_CAUSE_DEFAULT) return;
#if defined(DEBUG)
_dbg_csr = LL_RCC_ReadReg(CSR);
#endif
if (LL_RCC_IsActiveFlag_IWDGRST()) {
_reboot_cause = ARC_Watchdog;
} else if (LL_RCC_IsActiveFlag_SFTRST()) {
_reboot_cause = ARC_Software;
} else {
_reboot_cause = ARC_None;
}
#if !defined(BOOT)
LL_RCC_ClearResetFlags();
#endif
}
void abnormalRebootEnableDetection()
{
_init_reboot_cause();
_reboot_cmd = 0;
}
void abnormalRebootRequestSoftReset()
{
_reboot_cmd = _SOFTRESET_REQUEST;
}
uint32_t abnormalRebootGetCause()
{
_init_reboot_cause();
return _reboot_cause;
}
@@ -41,6 +41,8 @@ set(BOOTLOADER_SRC
../stm32_gpio_driver.cpp
../stm32_timer.cpp
../stm32_dma.cpp
../watchdog_driver.cpp
../abnormal_reboot.cpp
init.c
boot.cpp
bin_files.cpp
@@ -137,11 +139,17 @@ remove_definitions(-DLUA)
remove_definitions(-DCLI)
remove_definitions(-DSEMIHOSTING)
remove_definitions(-DUSB_SERIAL)
remove_definitions(-DWATCHDOG)
remove_definitions(-DUSE_WATCHDOG)
add_definitions(-DBOOT)
set(CMAKE_EXE_LINKER_FLAGS "-mcpu=${MCU} -mthumb -nostartfiles -lm -T${RADIO_SRC_DIR}/targets/${TARGET_DIR}/stm32_ramboot.ld -Wl,-Map=bootloader.map,--cref,--no-warn-mismatch,--gc-sections")
set(CMAKE_EXE_LINKER_FLAGS "-mcpu=${MCU} -mthumb -nostartfiles -lm")
set(LINKER_DIR ${RADIO_SRC_DIR}/boards/generic_stm32/linker)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T${LINKER_DIR}/bootloader.ld -L${LINKER_DIR} -L${LINKER_DIR}/${TARGET_LINKER_DIR}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=bootloader.map,--cref,--no-warn-mismatch,--gc-sections")
# use smaller nano libc
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --specs=nano.specs")
@@ -35,8 +35,7 @@
#include "debug.h"
#include "timers_driver.h"
#include "watchdog_driver.h"
#include "hal/abnormal_reboot.h"
#include "hal/rotary_encoder.h"
#if defined(DEBUG_SEGGER_RTT)
@@ -80,8 +79,6 @@ const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
{'B', 'O', 'O', 'T', '1', '0'};
#endif
#define SOFTRESET_REQUEST 0xCAFEDEAD
volatile tmr10ms_t g_tmr10ms;
volatile uint8_t tenms = 1;
@@ -309,13 +306,10 @@ int bootloaderMain()
#if defined(PWR_BUTTON_PRESS)
// wait until power button is released
while (pwrPressed()) {
WDG_RESET();
}
while (pwrPressed()) {}
#endif
for (;;) {
WDG_RESET();
if (tenms) {
tenms = 0;
@@ -560,10 +554,6 @@ int bootloaderMain()
if (state == ST_REBOOT) {
sdDone();
#if !defined(SIMU)
#if defined(RTC_BACKUP_RAM)
rtcInit();
RTC->BKP0R = SOFTRESET_REQUEST;
#endif
blExit();
NVIC_SystemReset();
#else
@@ -47,7 +47,7 @@ set(STM32_STDPERIPH_DIR ${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver)
add_library(stm32_stdperiph OBJECT EXCLUDE_FROM_ALL
targets/common/arm/stm32/f2/system_stm32f2xx.c
${STM32LIB_DIR}/CMSIS/Device/ST/STM32F2xx/Source/Templates/gcc_ride7/startup_stm32f2xx.s
boards/generic_stm32/startup/startup_stm32f205xx.s
${STM32_STDPERIPH_DIR}/src/misc.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_gpio.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_dbgmcu.c
@@ -73,17 +73,17 @@ set(STM32_STDPERIPH_SRC
if(CPU_TYPE_FULL STREQUAL STM32F429xI)
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
targets/${TARGET_DIR}/startup_stm32f42_43xxx.s
boards/generic_stm32/startup/startup_stm32f42_43xxx.s
${STM32_STDPERIPH_DIR}/src/stm32f4xx_fmc.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_dma.c
)
elseif (CPU_TYPE_FULL STREQUAL STM32F413xG)
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
targets/${TARGET_DIR}/startup_stm32f413vgtx.s
boards/generic_stm32/startup/startup_stm32f413xx.s
)
else()
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
targets/${TARGET_DIR}/startup_stm32f40_41xxx.s
boards/generic_stm32/startup/startup_stm32f40_41xxx.s
)
endif()
@@ -20,7 +20,7 @@
*/
#include "board.h"
#include "watchdog_driver.h"
#include "hal/watchdog_driver.h"
void waitFlashIdle()
{
@@ -20,7 +20,7 @@
*/
#include "board.h"
#include "watchdog_driver.h"
#include "hal/abnormal_reboot.h"
void pwrInit()
{
@@ -40,6 +40,7 @@ extern "C" {
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_spi.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_iwdg.h"
#elif defined(STM32F2)
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_bus.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_gpio.h"
@@ -51,6 +52,7 @@ extern "C" {
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_rcc.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_adc.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_spi.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_iwdg.h"
#endif
#if defined(__cplusplus)
@@ -24,7 +24,7 @@
#include "stm32_hal_ll.h"
#include "hal.h"
#include "watchdog_driver.h"
#include "hal/watchdog_driver.h"
static volatile uint32_t _ms_ticks;
@@ -0,0 +1,40 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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 "hal/watchdog_driver.h"
#include "stm32_hal_ll.h"
void watchdogInit(unsigned int duration)
{
LL_IWDG_EnableWriteAccess(IWDG);
LL_IWDG_SetPrescaler(IWDG, LL_IWDG_PRESCALER_32);
LL_IWDG_EnableWriteAccess(IWDG);
LL_IWDG_SetReloadCounter(IWDG, duration);
LL_IWDG_ReloadCounter(IWDG);
LL_IWDG_Enable(IWDG);
}
void watchdogReset()
{
LL_IWDG_ReloadCounter(IWDG);
}
@@ -1,54 +0,0 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#define WDG_DURATION 500 /*ms*/
void watchdogInit(unsigned int duration);
#if defined(SIMU)
#define WAS_RESET_BY_WATCHDOG() (false)
#define WAS_RESET_BY_SOFTWARE() (false)
#define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (false)
#define WDG_ENABLE(x)
#define WDG_RESET()
#else // SIMU
#if defined(WATCHDOG)
#define WDG_ENABLE(x) watchdogInit(x)
#define WDG_RESET() IWDG->KR = 0xAAAA
#else
#define WDG_ENABLE(x)
#define WDG_RESET()
#endif
#define WAS_RESET_BY_WATCHDOG() \
(RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF))
#define WAS_RESET_BY_SOFTWARE() (RCC->CSR & RCC_CSR_SFTRSTF)
#define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() \
(RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_SFTRSTF))
#endif