summaryrefslogtreecommitdiff
path: root/chip/stm32l/watchdog.c
blob: 4a1cd7f6d2bd5abd364d2fe828e47f05cc828392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Watchdog driver */

#include <stdint.h>

#include "board.h"
#include "common.h"
#include "config.h"
#include "registers.h"
#include "gpio.h"
#include "task.h"
#include "timer.h"
#include "uart.h"
#include "util.h"

/* LSI oscillator frequency is typically 38 kHz
 * but might vary from 28 to 56kHz.
 * So let's pick 56kHz to ensure we reload
 * early enough.
 */
#define LSI_CLOCK 56000

/* Prescaler divider = /256 */
#define IWDG_PRESCALER 6
#define IWDG_PRESCALER_DIV (1 << ((IWDG_PRESCALER) + 2))

void watchdog_reload(void)
{
	/* Reload the watchdog */
	STM32L_IWDG_KR = 0xaaaa;
}

int watchdog_init(int period_ms)
{
	uint32_t watchdog_period;

	/* set the time-out period */
	watchdog_period = period_ms * (LSI_CLOCK / IWDG_PRESCALER_DIV) / 1000;

	/* Unlock watchdog registers */
	STM32L_IWDG_KR = 0x5555;

	/* Set the prescaler between the LSI clock and the watchdog counter */
	STM32L_IWDG_PR = IWDG_PRESCALER & 7;
	/* Set the reload value of the watchdog counter */
	STM32L_IWDG_RLR = watchdog_period & 0x7FF ;

	/* Start the watchdog (and re-lock registers) */
	STM32L_IWDG_KR = 0xcccc;

	return EC_SUCCESS;
}

/* Low priority task to reload the watchdog */
void watchdog_task(void)
{
	while (1) {
#ifdef BOARD_discovery
		gpio_set_level(GPIO_GREEN_LED, 1);
#endif
		usleep(500000);
		watchdog_reload();
#ifdef BOARD_discovery
		gpio_set_level(GPIO_GREEN_LED, 0);
#endif
		usleep(500000);
		watchdog_reload();
	}
}