Simple example of using independent watchdog timer (IWDG) with Nucleo F401RE board.

Dependencies:   DS1307 mbed-src

Committer:
batrado
Date:
Thu Oct 23 06:45:32 2014 +0000
Revision:
0:8d898804478b
Implementation of watchdog timer for Nucleo F401RE borad.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
batrado 0:8d898804478b 1 #include "mbed.h"
batrado 0:8d898804478b 2 #include "ds1307.h"
batrado 0:8d898804478b 3 #include "stm32f4xx_hal_iwdg.h"
batrado 0:8d898804478b 4
batrado 0:8d898804478b 5 #if defined(TARGET_NUCLEO_F401RE)
batrado 0:8d898804478b 6 DS1307 ds1307(PB_9, PB_8);
batrado 0:8d898804478b 7
batrado 0:8d898804478b 8 IWDG_HandleTypeDef hiwdg;
batrado 0:8d898804478b 9 #endif
batrado 0:8d898804478b 10
batrado 0:8d898804478b 11 /* Initializes the IWDG */
batrado 0:8d898804478b 12 void WatchdogInit(void) {
batrado 0:8d898804478b 13
batrado 0:8d898804478b 14 /*
batrado 0:8d898804478b 15 Watchdog freq. is 32 kHz
batrado 0:8d898804478b 16 Prescaler: Min_Value = 4 and Max_Value = 256
batrado 0:8d898804478b 17 Reload: Min_Data = 0 and Max_Data = 0x0FFF
batrado 0:8d898804478b 18 TimeOut in seconds = (Reload * Prescaler) / Freq.
batrado 0:8d898804478b 19 MinTimeOut = (4 * 1) / 32000 = 0.000125 seconds (125 microseconds)
batrado 0:8d898804478b 20 MaxTimeOut = (256 * 4096) / 32000 = 32.768 seconds
batrado 0:8d898804478b 21 */
batrado 0:8d898804478b 22 hiwdg.Instance = IWDG;
batrado 0:8d898804478b 23 hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
batrado 0:8d898804478b 24 hiwdg.Init.Reload = 625;
batrado 0:8d898804478b 25 HAL_IWDG_Init(&hiwdg);
batrado 0:8d898804478b 26
batrado 0:8d898804478b 27 }
batrado 0:8d898804478b 28
batrado 0:8d898804478b 29 /* Starts the IWDG. */
batrado 0:8d898804478b 30 void WatchdogStart(void) {
batrado 0:8d898804478b 31 HAL_IWDG_Start(&hiwdg);
batrado 0:8d898804478b 32 }
batrado 0:8d898804478b 33
batrado 0:8d898804478b 34 /* Refreshes the IWDG. */
batrado 0:8d898804478b 35 void WatchdogRefresh(void) {
batrado 0:8d898804478b 36 HAL_IWDG_Refresh(&hiwdg);
batrado 0:8d898804478b 37 }
batrado 0:8d898804478b 38
batrado 0:8d898804478b 39 void WatchdogStatus(void) {
batrado 0:8d898804478b 40 switch (HAL_IWDG_GetState(&hiwdg)) {
batrado 0:8d898804478b 41 case HAL_IWDG_STATE_RESET: /*!< IWDG not yet initialized or disabled */
batrado 0:8d898804478b 42 printf("IWDG not yet initialized or disabled\r\n");
batrado 0:8d898804478b 43 break;
batrado 0:8d898804478b 44 case HAL_IWDG_STATE_READY: /*!< IWDG initialized and ready for use */
batrado 0:8d898804478b 45 printf("IIWDG initialized and ready for use\r\n");
batrado 0:8d898804478b 46 break;
batrado 0:8d898804478b 47 case HAL_IWDG_STATE_BUSY: /*!< IWDG internal process is ongoing */
batrado 0:8d898804478b 48 printf("IWDG internal process is ongoing\r\n");
batrado 0:8d898804478b 49 break;
batrado 0:8d898804478b 50 case HAL_IWDG_STATE_TIMEOUT: /*!< IWDG timeout state */
batrado 0:8d898804478b 51 printf("IWDG timeout state\r\n");
batrado 0:8d898804478b 52 break;
batrado 0:8d898804478b 53 case HAL_IWDG_STATE_ERROR: /*!< IWDG error state */
batrado 0:8d898804478b 54 printf("IWDG error state\r\n");
batrado 0:8d898804478b 55 break;
batrado 0:8d898804478b 56 default:
batrado 0:8d898804478b 57 printf("Unknown state\r\n");
batrado 0:8d898804478b 58 break;
batrado 0:8d898804478b 59 }
batrado 0:8d898804478b 60 }
batrado 0:8d898804478b 61
batrado 0:8d898804478b 62 int main() {
batrado 0:8d898804478b 63 int sec = 0;
batrado 0:8d898804478b 64 int min = 0;
batrado 0:8d898804478b 65 int hour = 0;
batrado 0:8d898804478b 66 int day = 0;
batrado 0:8d898804478b 67 int date = 0;
batrado 0:8d898804478b 68 int month = 0;
batrado 0:8d898804478b 69 int year = 0;
batrado 0:8d898804478b 70 int i = 0;
batrado 0:8d898804478b 71
batrado 0:8d898804478b 72 WatchdogInit(); // init a watchdog with 5 seconds timeout
batrado 0:8d898804478b 73 WatchdogStatus();
batrado 0:8d898804478b 74 WatchdogStart();
batrado 0:8d898804478b 75
batrado 0:8d898804478b 76 while (true) {
batrado 0:8d898804478b 77 ds1307.gettime(&sec, &min, &hour, &day, &date, &month, &year);
batrado 0:8d898804478b 78 printf("%02d/%02d/%04d %02d:%02d:%02d\r\n", day, month, year, hour, min, sec);
batrado 0:8d898804478b 79
batrado 0:8d898804478b 80 if (i == 10) {
batrado 0:8d898804478b 81 wait(10);
batrado 0:8d898804478b 82 }
batrado 0:8d898804478b 83 WatchdogRefresh();
batrado 0:8d898804478b 84 i++;
batrado 0:8d898804478b 85 wait(1);
batrado 0:8d898804478b 86 }
batrado 0:8d898804478b 87 }