Skip to content

Commit

Permalink
Watchdog: Move into separate functions. Poll IWDG_SR before updating …
Browse files Browse the repository at this point in the history
…registers.
  • Loading branch information
keirf committed Oct 9, 2019
1 parent e93f91b commit 78733a9
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ static void canary_check(void)
ASSERT(_thread_stackbottom[0] == 0xdeadbeef);
}

static void watchdog_init(void)
{
/* Set up the Watchdog. Based on LSI at 30-60kHz (av. 40kHz). */
iwdg->kr = 0xcccc; /* Enables watchdog, turns on LSI oscillator. */
while (iwdg->sr & 3) {
/* System Memory Bootloader modifies PR. We must wait for that
* to take effect before making our own changes. */
}
iwdg->kr = 0x5555; /* Enables access to PR and RLR. */
iwdg->pr = 3; /* Prescaler: div32 => Ticks at 937-1875Hz (1250Hz) */
iwdg->rlr = 400; /* Reload: 400 => Times out in 213-426ms (320ms) */
iwdg->kr = 0xaaaa; /* Load the new Reload value. */
}

static void watchdog_kick(void)
{
/* Reload the Watchdog. */
iwdg->kr = 0xaaaa;
}

static struct timer button_timer;
static uint8_t rotary;
static volatile uint8_t buttons;
Expand Down Expand Up @@ -533,12 +553,7 @@ int main(void)
time_t frame_time;
bool_t lost_sync, _keyboard_held;

/* Set up the Watchdog. Based on LSI at 30-60kHz (av. 40kHz). */
iwdg->kr = 0xcccc; /* Enables watchdog, turns on LSI oscillator. */
iwdg->kr = 0x5555; /* Enables access to PR and RLR. */
iwdg->pr = 3; /* Prescaler: div32 => Ticks at 937-1875Hz (1250Hz) */
iwdg->rlr = 400; /* Reload: 400 => Times out in 213-426ms (320ms) */
iwdg->kr = 0xaaaa; /* Load the new Reload value. */
watchdog_init();

/* Relocate DATA. Initialise BSS. */
if (_sdat != _ldat)
Expand Down Expand Up @@ -689,8 +704,7 @@ int main(void)

for (;;) {

/* Reload the Watchdog. */
iwdg->kr = 0xaaaa;
watchdog_kick();

canary_check();

Expand Down

0 comments on commit 78733a9

Please sign in to comment.