Skip to content

Commit f190ac0

Browse files
committed
mb9bf566 added basic watchdog driver (init == wip)
1 parent 0b75989 commit f190ac0

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef KLIB_MB9BF566K_WATCHDOG_HPP
2+
#define KLIB_MB9BF566K_WATCHDOG_HPP
3+
4+
#include <cstdint>
5+
6+
#include <targets/core/cypress/mb9bf560l/watchdog.hpp>
7+
8+
namespace klib::mb9bf566k::io::periph {
9+
struct wdt0 {
10+
// peripheral id (e.g wdt0, wdt1)
11+
constexpr static uint32_t id = 0;
12+
13+
// peripheral interrupt position (uses the NMI interrupt for the watchdog)
14+
constexpr static uint32_t interrupt_id = 2;
15+
16+
// port to the wdt hardware
17+
static inline HWWDT_Type *const port = HWWDT;
18+
};
19+
}
20+
21+
namespace klib::mb9bf566k::io {
22+
template <typename Wdt>
23+
using watchdog = core::mb9bf560l::io::watchdog<Wdt>;
24+
}
25+
26+
#endif
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef KLIB_CYPRESS_MB9BF560L_WATCHDOG_HPP
2+
#define KLIB_CYPRESS_MB9BF560L_WATCHDOG_HPP
3+
4+
#include <cstdint>
5+
6+
#include <klib/klib.hpp>
7+
8+
namespace klib::core::mb9bf560l::io {
9+
template <typename Wdt>
10+
class watchdog {
11+
protected:
12+
template <bool Control = false>
13+
static void unlock() {
14+
// unlock the watchdog registers
15+
Wdt::port->WDG_LCK = 0x1acce551;
16+
17+
// if we need to enable the control
18+
// we need to do another write
19+
if constexpr (Control) {
20+
Wdt::port->WDG_LCK = 0xe5331aae;
21+
}
22+
}
23+
24+
public:
25+
/**
26+
* @brief Disable the watchdog timer
27+
*
28+
*/
29+
static void disable() {
30+
// unlock the registers + control register
31+
unlock<true>();
32+
33+
// set the disable bit
34+
Wdt::port->WDG_CTL = 0x00;
35+
}
36+
37+
/**
38+
* @brief Feed the watchdog timer to prevent the watchdog from triggering
39+
*
40+
*/
41+
static void feed() {
42+
// write a arbitrary 8-bit value
43+
Wdt::port->WDG_ICL = 0xf0;
44+
45+
// write the reversal value of the arbirary value
46+
Wdt::port->WDG_ICL = 0x0f;
47+
}
48+
49+
/**
50+
* @brief Init the watchdog timer
51+
*
52+
* @tparam Irq enable/disable the watchdog timer interrupt
53+
* @tparam RstCPU enable/disable if a processor reset gets generated
54+
* @tparam Period reset period (refer to the datasheet for the timing)
55+
*/
56+
template<bool Irq = true, bool RstCPU = false, uint32_t Period = 0xffff>
57+
static void init() {
58+
// make sure the input is valid
59+
static_assert(Period > 0, "Minimum period value is 1");
60+
static_assert((Irq && RstCPU) || (!RstCPU), "Reset does not work when Irq is not enabled");
61+
62+
// unlock the registers
63+
unlock();
64+
65+
// setup the parameters of the watchdog
66+
Wdt::port->WDG_LDR = Period;
67+
68+
// unlock the control register
69+
unlock<true>();
70+
71+
// enable or disable the interrupt bit
72+
Wdt::port->WDG_CTL = (RstCPU << 1) | Irq;
73+
74+
// feed the watchdog
75+
feed();
76+
}
77+
};
78+
}
79+
80+
#endif

0 commit comments

Comments
 (0)