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