Starting small, I’ve started playing around with the watchdog, and all is working as expected.
However, I’m yet to get the ‘feel’ for how people code around here. Would somebody like to critique this before I go to far? I’m very interested in the right way to define the addresses for the watchdog registers.
#include
#include
#include "platform.h"
#include
#include "encoding.h"
#include
#define RTC_FREQUENCY 32768
/******************************************
* The Watchdog registers and magic values
******************************************/
volatile uint32_t *wdogcfg = (uint32_t *)0x10000000;
volatile uint32_t *wdogcount = (uint32_t *)0x10000008;
volatile uint32_t *wdogs = (uint32_t *)0x10000010;
volatile uint32_t *wdogfeed = (uint32_t *)0x10000018;
volatile uint32_t *wdogkey = (uint32_t *)0x1000001C;
volatile uint32_t *wdogcmp = (uint32_t *)0x10000020;
const uint32_t wdog_key = 0x0051F15E;
const uint32_t wdog_food = 0x0D09F00D;
const char * msg = "Testing the watchdog - it should reset every 5000 milliseconds\n";
static void watchdog_feed() {
*wdogkey = wdog_key; /* Unlock watchdog registers */
*wdogfeed = wdog_food; /* Feed (reset) the watchdog */
}
static void watchdog_enable(uint32_t milliseconds) {
uint8_t scale = 0;
uint32_t cmp_val = (uint64_t)milliseconds*RTC_FREQUENCY/1000;
/* Work out what scale factor to use to get cmp_val < 2^16 */
while(cmp_val > 65535) {
scale++;
cmp_val /= 2;
}
*wdogkey = wdog_key; /* Unlock watchdog registers */
*wdogfeed = wdog_food; /* Feed (reset) the watchdog */
*wdogkey = wdog_key; /* Unlock watchdog registers */
*wdogcmp = cmp_val; /* Set the compare count value */
*wdogkey = wdog_key; /* Unlock watchdog registers */
*wdogcfg = (1<<12) /* wdogenalways - wdog runs always */
| (1<< 8) /* wdogrsten - enable reset */
| (scale << 0); /* wdogscale - depend on timeout */
}
int main(int argc, char *argv[])
{
/* Enable the watchdog reset every 5000ms */
watchdog_enable(5000);
/* Display a message */
write (STDOUT_FILENO, msg, strlen(msg));
/* Loop to do nothing forever */
while (1)
;
/* Never gets here */
return 0;
}