2019-04-11 05:01:49 -03:00
|
|
|
/*
|
|
|
|
independent watchdog support
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hal.h"
|
|
|
|
#include "watchdog.h"
|
2019-04-19 21:28:15 -03:00
|
|
|
#include "stm32_util.h"
|
2019-04-11 05:01:49 -03:00
|
|
|
|
|
|
|
#ifndef IWDG_BASE
|
|
|
|
#if defined(STM32H7)
|
|
|
|
#define IWDG_BASE 0x58004800
|
2019-04-11 06:50:46 -03:00
|
|
|
#elif defined(STM32F7) || defined(STM32F4)
|
|
|
|
#define IWDG_BASE 0x40003000
|
2019-10-31 07:59:23 -03:00
|
|
|
#elif defined(STM32F1) || defined(STM32F3)
|
2019-04-11 05:01:49 -03:00
|
|
|
#define IWDG_BASE 0x40003000
|
|
|
|
#else
|
2019-04-11 06:50:46 -03:00
|
|
|
#error "Unsupported IWDG MCU config"
|
|
|
|
#endif
|
2019-04-11 05:01:49 -03:00
|
|
|
#endif
|
2019-04-11 06:50:46 -03:00
|
|
|
|
|
|
|
#ifndef RCC_BASE
|
|
|
|
#error "Unsupported IWDG RCC MCU config"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
defines for working out if the reset was from the watchdog
|
|
|
|
*/
|
|
|
|
#if defined(STM32H7)
|
2019-04-12 02:21:58 -03:00
|
|
|
#define WDG_RESET_STATUS (*(__IO uint32_t *)(RCC_BASE + 0xD0))
|
2019-04-11 06:50:46 -03:00
|
|
|
#define WDG_RESET_CLEAR (1U<<16)
|
|
|
|
#define WDG_RESET_IS_IWDG (1U<<26)
|
2022-02-04 21:41:54 -04:00
|
|
|
#define WDG_RESET_IS_SFT (1U<<24)
|
2019-04-11 06:50:46 -03:00
|
|
|
#elif defined(STM32F7) || defined(STM32F4)
|
|
|
|
#define WDG_RESET_STATUS (*(__IO uint32_t *)(RCC_BASE + 0x74))
|
|
|
|
#define WDG_RESET_CLEAR (1U<<24)
|
|
|
|
#define WDG_RESET_IS_IWDG (1U<<29)
|
2022-02-04 21:41:54 -04:00
|
|
|
#define WDG_RESET_IS_SFT (1U<<28)
|
2019-10-31 07:59:23 -03:00
|
|
|
#elif defined(STM32F1) || defined(STM32F3)
|
2019-04-11 06:50:46 -03:00
|
|
|
#define WDG_RESET_STATUS (*(__IO uint32_t *)(RCC_BASE + 0x24))
|
|
|
|
#define WDG_RESET_CLEAR (1U<<24)
|
|
|
|
#define WDG_RESET_IS_IWDG (1U<<29)
|
2022-02-04 21:41:54 -04:00
|
|
|
#define WDG_RESET_IS_SFT (1U<<28)
|
2023-04-12 00:41:35 -03:00
|
|
|
#elif defined(STM32G4) || defined(STM32L4) || defined(STM32L4PLUS)
|
2021-03-07 23:24:38 -04:00
|
|
|
#define WDG_RESET_STATUS (*(__IO uint32_t *)(RCC_BASE + 0x94))
|
|
|
|
#define WDG_RESET_CLEAR (1U<<23)
|
|
|
|
#define WDG_RESET_IS_IWDG (1U<<29)
|
2022-02-04 21:41:54 -04:00
|
|
|
#define WDG_RESET_IS_SFT (1U<<28)
|
2019-04-11 06:50:46 -03:00
|
|
|
#else
|
|
|
|
#error "Unsupported IWDG MCU config"
|
2019-04-11 05:01:49 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
__IO uint32_t KR; /*!< IWDG Key register, Address offset: 0x00 */
|
|
|
|
__IO uint32_t PR; /*!< IWDG Prescaler register, Address offset: 0x04 */
|
|
|
|
__IO uint32_t RLR; /*!< IWDG Reload register, Address offset: 0x08 */
|
|
|
|
__IO uint32_t SR; /*!< IWDG Status register, Address offset: 0x0C */
|
|
|
|
__IO uint32_t WINR; /*!< IWDG Window register, Address offset: 0x10 */
|
|
|
|
} IWDG_Regs;
|
|
|
|
|
|
|
|
#define IWDGD (*(IWDG_Regs *)(IWDG_BASE))
|
|
|
|
|
2022-02-04 21:41:54 -04:00
|
|
|
static uint32_t reset_reason;
|
2019-04-11 08:12:03 -03:00
|
|
|
static bool watchdog_enabled;
|
|
|
|
|
2019-04-11 05:01:49 -03:00
|
|
|
/*
|
|
|
|
setup the watchdog
|
|
|
|
*/
|
|
|
|
void stm32_watchdog_init(void)
|
|
|
|
{
|
2019-04-19 20:29:01 -03:00
|
|
|
// setup for 2s reset
|
2019-04-11 05:01:49 -03:00
|
|
|
IWDGD.KR = 0x5555;
|
2019-04-20 03:30:20 -03:00
|
|
|
IWDGD.PR = 2; // div16
|
2019-04-11 05:01:49 -03:00
|
|
|
IWDGD.RLR = 0xFFF;
|
|
|
|
IWDGD.KR = 0xCCCC;
|
2019-04-11 08:12:03 -03:00
|
|
|
watchdog_enabled = true;
|
2019-04-11 05:01:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
pat the dog, to prevent a reset. If not called for 1s
|
|
|
|
after stm32_watchdog_init() then MCU will reset
|
|
|
|
*/
|
|
|
|
void stm32_watchdog_pat(void)
|
|
|
|
{
|
2019-04-11 08:12:03 -03:00
|
|
|
if (watchdog_enabled) {
|
|
|
|
IWDGD.KR = 0xAAAA;
|
|
|
|
}
|
2019-04-11 05:01:49 -03:00
|
|
|
}
|
2019-04-11 06:50:46 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
save reason code for reset
|
|
|
|
*/
|
|
|
|
void stm32_watchdog_save_reason(void)
|
|
|
|
{
|
2022-02-04 21:41:54 -04:00
|
|
|
if (reset_reason == 0) {
|
|
|
|
reset_reason = WDG_RESET_STATUS;
|
2019-04-11 06:50:46 -03:00
|
|
|
}
|
2019-04-11 08:12:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
clear reason code for reset
|
|
|
|
*/
|
|
|
|
void stm32_watchdog_clear_reason(void)
|
|
|
|
{
|
2019-04-11 06:50:46 -03:00
|
|
|
WDG_RESET_STATUS = WDG_RESET_CLEAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return true if reboot was from a watchdog reset
|
|
|
|
*/
|
|
|
|
bool stm32_was_watchdog_reset(void)
|
|
|
|
{
|
2022-02-04 21:41:54 -04:00
|
|
|
stm32_watchdog_save_reason();
|
|
|
|
return (reset_reason & WDG_RESET_IS_IWDG) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return true if reboot was from a software reset
|
|
|
|
*/
|
|
|
|
bool stm32_was_software_reset(void)
|
|
|
|
{
|
|
|
|
stm32_watchdog_save_reason();
|
|
|
|
return (reset_reason & WDG_RESET_IS_SFT) != 0;
|
2019-04-11 06:50:46 -03:00
|
|
|
}
|
|
|
|
|
2019-04-19 21:28:15 -03:00
|
|
|
/*
|
2019-05-09 04:49:32 -03:00
|
|
|
save persistent watchdog data
|
2019-04-21 00:08:29 -03:00
|
|
|
*/
|
2019-05-09 04:49:32 -03:00
|
|
|
void stm32_watchdog_save(const uint32_t *data, uint32_t nwords)
|
2019-04-21 00:08:29 -03:00
|
|
|
{
|
2019-05-09 04:49:32 -03:00
|
|
|
set_rtc_backup(1, data, nwords);
|
2019-04-21 00:08:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-05-09 04:49:32 -03:00
|
|
|
load persistent watchdog data
|
2019-04-21 00:08:29 -03:00
|
|
|
*/
|
2019-05-09 04:49:32 -03:00
|
|
|
void stm32_watchdog_load(uint32_t *data, uint32_t nwords)
|
2019-04-21 00:08:29 -03:00
|
|
|
{
|
2019-05-09 04:49:32 -03:00
|
|
|
get_rtc_backup(1, data, nwords);
|
2019-04-21 00:08:29 -03:00
|
|
|
}
|