void watchdogEnable (uint32_t timeout) { /* this assumes the slow clock is running at 32.768 kHz watchdog frequency is therefore 32768 / 128 = 256 Hz */ timeout = timeout * 256 / 1000; if (timeout == 0) timeout = 1; else if (timeout > 0xFFF) timeout = 0xFFF; timeout = WDT_MR_WDRSTEN | WDT_MR_WDV(timeout) | WDT_MR_WDD(timeout); WDT_Enable (WDT, timeout); }
/** * \brief Get counter value or permitted range value of watchdog timer from the * desired timeout period (in us). * * \note The value returned by this function can be used by wdt_init() if it is * not WDT_INVALID_ARGUMENT. * * \param ul_us The desired timeout period (in us). * \param ul_sclk The slow clock on board (in Hz). * * \return If the desired period is beyond the watchdog period, this function * returns WDT_INVALID_ARGUMENT. Otherwise it returns valid value. */ uint32_t wdt_get_timeout_value(uint32_t ul_us, uint32_t ul_sclk) { uint32_t max, min; min = WDT_SLCK_DIV * 1000000 / ul_sclk; max = min * WDT_MAX_VALUE; if ((ul_us < min) || (ul_us > max)) { return WDT_INVALID_ARGUMENT; } return WDT_MR_WDV(ul_us / min); }
// Configures the watchdog timer to 16s (can only be called once). void wdt_start(void) { if(wdt_has_error()) { // If there is an error in the wdt status register the watchdog // timer reached 0 since last call of start wdt_counter++; } WDT->WDT_MR = WDT_MR_WDV(WDT_TIMEOUT_16S) | WDT_MR_WDD(WDT_TIMEOUT_16S) | WDT_MR_WDFIEN | WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT; }
void watchdog_init(void) { uint32_t timeout = 500 * 32768 / 128 / 1000; // 500ms timeout WDT->WDT_MR = WDT_MR_WDRSTEN | WDT_MR_WDV(timeout) | WDT_MR_WDD(timeout); }
/** * \brief Get the timeout period of the WatchDog Timer in microseconds. * * \param p_wdt Pointer to a WDT instance. * \param ul_sclk The slow clock frequency (in Hz). * * \return The timeout period in microseconds. */ uint32_t wdt_get_us_timeout_period(Wdt *p_wdt, uint32_t ul_sclk) { return WDT_MR_WDV(p_wdt->WDT_MR) * WDT_SLCK_DIV / ul_sclk * 1000000; }
/** * \brief Initialize watchdog timer with the given mode. * * \param p_wdt Pointer to a WDT instance. * \param ul_mode Bitmask of watchdog timer mode. * \param us_counter The value loaded in the 12-bit Watchdog Counter. * \param us_delta The permitted range for reloading the Watchdog Timer. */ void wdt_init(Wdt *p_wdt, uint32_t ul_mode, uint16_t us_counter, uint16_t us_delta) { p_wdt->WDT_MR = ul_mode | WDT_MR_WDV(us_counter) | WDT_MR_WDD(us_delta); }