Browse Source

HAL_ChibiOS: added IWDG watchdog support

this resets the MCU if the main loop stops for 1 second
mission-4.1.18
Andrew Tridgell 6 years ago
parent
commit
c09df1ea7b
  1. 5
      libraries/AP_HAL_ChibiOS/HAL_ChibiOS_Class.cpp
  2. 3
      libraries/AP_HAL_ChibiOS/hwdef/common/chibios_board.mk
  3. 48
      libraries/AP_HAL_ChibiOS/hwdef/common/watchdog.c
  4. 20
      libraries/AP_HAL_ChibiOS/hwdef/common/watchdog.h

5
libraries/AP_HAL_ChibiOS/HAL_ChibiOS_Class.cpp

@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
#include "sdcard.h"
#include "hwdef/common/usbcfg.h"
#include "hwdef/common/stm32_util.h"
#include "hwdef/common/watchdog.h"
#include <hwdef.h>
@ -201,6 +202,9 @@ static void main_loop() @@ -201,6 +202,9 @@ static void main_loop()
*/
chThdSetPriority(APM_MAIN_PRIORITY);
// setup watchdog to reset if main loop stops
stm32_watchdog_init();
while (true) {
g_callbacks->loop();
@ -217,6 +221,7 @@ static void main_loop() @@ -217,6 +221,7 @@ static void main_loop()
hal.scheduler->delay_microseconds(50);
}
#endif
stm32_watchdog_pat();
}
thread_running = false;
}

3
libraries/AP_HAL_ChibiOS/hwdef/common/chibios_board.mk

@ -121,7 +121,8 @@ CSRC += $(HWDEF)/common/stubs.c \ @@ -121,7 +121,8 @@ CSRC += $(HWDEF)/common/stubs.c \
$(HWDEF)/common/malloc.c \
$(HWDEF)/common/hrt.c \
$(HWDEF)/common/stm32_util.c \
$(HWDEF)/common/bouncebuffer.c
$(HWDEF)/common/bouncebuffer.c \
$(HWDEF)/common/watchdog.c
ifeq ($(USE_FATFS),yes)
CSRC += $(HWDEF)/common/posix.c

48
libraries/AP_HAL_ChibiOS/hwdef/common/watchdog.c

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
/*
independent watchdog support
*/
#include "hal.h"
#include "watchdog.h"
#ifndef IWDG_BASE
#if defined(STM32H7)
#define IWDG_BASE 0x58004800
#elif defined(STM32F7) || defined(STM32F4) || defined(STM32F1)
#define IWDG_BASE 0x40003000
#else
#error "Unknown IWDG_BASE"
#endif
#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))
/*
setup the watchdog
*/
void stm32_watchdog_init(void)
{
// setup for 1s reset
IWDGD.KR = 0x5555;
IWDGD.PR = 8;
IWDGD.RLR = 0xFFF;
IWDGD.KR = 0xCCCC;
}
/*
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)
{
IWDGD.KR = 0xAAAA;
}

20
libraries/AP_HAL_ChibiOS/hwdef/common/watchdog.h

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
#ifdef __cplusplus
extern "C" {
#endif
/*
setup the watchdog
*/
void stm32_watchdog_init(void);
/*
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);
#ifdef __cplusplus
}
#endif
Loading…
Cancel
Save