watchdog to reboot the board

Post here first, or if you can't find a relevant section!
Post Reply
svyatokha
Posts: 5
Joined: Thu Feb 27, 2020 1:21 am

watchdog to reboot the board

Post by svyatokha »

Hi.
I'm trying to configure watchdog to reboot the board in case of its freeze or lag. But I can’t find how this is done, maybe you have an example of such use? stm32f103c8t6

sorry for broken english :)
by stas2z » Thu Feb 27, 2020 7:08 am

Code: Select all

#define LIBMAPPLE_CORE //comment it for HAL based core

#define IWDG_PR_DIV_4 0x0
#define IWDG_PR_DIV_8 0x1
#define IWDG_PR_DIV_16 0x2
#define IWDG_PR_DIV_32 0x3
#define IWDG_PR_DIV_64 0x4
#define IWDG_PR_DIV_128 0x5
#define IWDG_PR_DIV_256 0x6

typedef enum iwdg_prescaler {
  IWDG_PRE_4 = IWDG_PR_DIV_4,     /**< Divide by 4 */
  IWDG_PRE_8 = IWDG_PR_DIV_8,     /**< Divide by 8 */
  IWDG_PRE_16 = IWDG_PR_DIV_16,   /**< Divide by 16 */
  IWDG_PRE_32 = IWDG_PR_DIV_32,   /**< Divide by 32 */
  IWDG_PRE_64 = IWDG_PR_DIV_64,   /**< Divide by 64 */
  IWDG_PRE_128 = IWDG_PR_DIV_128, /**< Divide by 128 */
  IWDG_PRE_256 = IWDG_PR_DIV_256  /**< Divide by 256 */
} iwdg_prescaler;

#if defined(LIBMAPPLE_CORE)
typedef struct iwdg_reg_map {
  volatile uint32_t KR;  /**< Key register. */
  volatile uint32_t PR;  /**< Prescaler register. */
  volatile uint32_t RLR; /**< Reload register. */
  volatile uint32_t SR;  /**< Status register */
} iwdg_reg_map;

#define IWDG ((struct iwdg_reg_map *)0x40003000)
#endif

void iwdg_feed(void) { IWDG->KR = 0xAAAA; }

void iwdg_init(iwdg_prescaler prescaler, uint16_t reload) {
  IWDG->KR = 0x5555;
  IWDG->PR = prescaler;
  IWDG->RLR = reload;
  IWDG->KR = 0xCCCC;
  IWDG->KR = 0xAAAA;
}
to use it call iwdg_init() at setup with divider value and reload value you need, divider is for LSI clock, always check your mcu for actual value (for F103 LSI is 40khz)
for example with 32khz LSI clock and divider 32, reload value 1000 will be one second to avoid reboot by calling iwdg_feed in the main loop
Go to full post
User avatar
fpiSTM
Posts: 1758
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: watchdog to reboot the board

Post by fpiSTM »

The IWDG can help you.
If you use the STM32 core, there is a library to ease the use:
https://github.com/stm32duino/Arduino_C ... /IWatchdog

For Roger's core, I don't know if there is such API, anyway, it is simple to configure using register.
svyatokha
Posts: 5
Joined: Thu Feb 27, 2020 1:21 am

Re: watchdog to reboot the board

Post by svyatokha »

Yes, I tried this library, and this is the core. but My projects, almost all, are not compiled by that core(. Can you help to do this with registers? Unfortunately, I haven’t figured it out yet.
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: watchdog to reboot the board

Post by stas2z »

Code: Select all

#define LIBMAPPLE_CORE //comment it for HAL based core

#define IWDG_PR_DIV_4 0x0
#define IWDG_PR_DIV_8 0x1
#define IWDG_PR_DIV_16 0x2
#define IWDG_PR_DIV_32 0x3
#define IWDG_PR_DIV_64 0x4
#define IWDG_PR_DIV_128 0x5
#define IWDG_PR_DIV_256 0x6

typedef enum iwdg_prescaler {
  IWDG_PRE_4 = IWDG_PR_DIV_4,     /**< Divide by 4 */
  IWDG_PRE_8 = IWDG_PR_DIV_8,     /**< Divide by 8 */
  IWDG_PRE_16 = IWDG_PR_DIV_16,   /**< Divide by 16 */
  IWDG_PRE_32 = IWDG_PR_DIV_32,   /**< Divide by 32 */
  IWDG_PRE_64 = IWDG_PR_DIV_64,   /**< Divide by 64 */
  IWDG_PRE_128 = IWDG_PR_DIV_128, /**< Divide by 128 */
  IWDG_PRE_256 = IWDG_PR_DIV_256  /**< Divide by 256 */
} iwdg_prescaler;

#if defined(LIBMAPPLE_CORE)
typedef struct iwdg_reg_map {
  volatile uint32_t KR;  /**< Key register. */
  volatile uint32_t PR;  /**< Prescaler register. */
  volatile uint32_t RLR; /**< Reload register. */
  volatile uint32_t SR;  /**< Status register */
} iwdg_reg_map;

#define IWDG ((struct iwdg_reg_map *)0x40003000)
#endif

void iwdg_feed(void) { IWDG->KR = 0xAAAA; }

void iwdg_init(iwdg_prescaler prescaler, uint16_t reload) {
  IWDG->KR = 0x5555;
  IWDG->PR = prescaler;
  IWDG->RLR = reload;
  IWDG->KR = 0xCCCC;
  IWDG->KR = 0xAAAA;
}
to use it call iwdg_init() at setup with divider value and reload value you need, divider is for LSI clock, always check your mcu for actual value (for F103 LSI is 40khz)
for example with 32khz LSI clock and divider 32, reload value 1000 will be one second to avoid reboot by calling iwdg_feed in the main loop
svyatokha
Posts: 5
Joined: Thu Feb 27, 2020 1:21 am

Re: watchdog to reboot the board

Post by svyatokha »

It is working! It works!) Thank you very much) I surfed the Internet for several hours in search of a working solution. Thanks)
Post Reply

Return to “General discussion”