|
34 | 34 |
|
35 | 35 | #include "component/wdt.h"
|
36 | 36 |
|
| 37 | +#define SYNC_CTRL_WRITE while (WDT->SYNCBUSY.reg) {} |
| 38 | + |
| 39 | +static void watchdog_disable(void) { |
| 40 | + // disable watchdog |
| 41 | + WDT->CTRLA.reg = 0; |
| 42 | + SYNC_CTRL_WRITE |
| 43 | +} |
| 44 | + |
| 45 | +static void watchdog_enable(watchdog_watchdogtimer_obj_t *self) { |
| 46 | + // disable watchdog for config |
| 47 | + watchdog_disable(); |
| 48 | + |
| 49 | + int wdt_cycles = (int)(self->timeout * 1024); |
| 50 | + if (wdt_cycles < 8) { |
| 51 | + wdt_cycles = 8; |
| 52 | + } |
| 53 | + |
| 54 | + // ceil(log2(n)) = 32 - __builtin_clz(n - 1) when n > 1 (if int is 32 bits) |
| 55 | + int log2_wdt_cycles = (sizeof(int) * CHAR_BIT) - __builtin_clz(wdt_cycles - 1); |
| 56 | + int setting = log2_wdt_cycles - 3; // CYC8_Val is 0 |
| 57 | + |
| 58 | + OSC32KCTRL->OSCULP32K.bit.EN1K = 1; // Enable out 1K (for WDT) |
| 59 | + |
| 60 | + WDT->INTENCLR.reg = WDT_INTENCLR_EW; // Disable early warning interrupt |
| 61 | + WDT->CONFIG.bit.PER = setting; // Set period for chip reset |
| 62 | + WDT->CTRLA.bit.WEN = 0; // Disable window mode |
| 63 | + SYNC_CTRL_WRITE |
| 64 | + common_hal_watchdog_feed(self); // Clear watchdog interval |
| 65 | + WDT->CTRLA.bit.ENABLE = 1; // Start watchdog now! |
| 66 | + SYNC_CTRL_WRITE |
| 67 | +} |
| 68 | + |
37 | 69 | void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
|
38 | 70 | WDT->CLEAR.reg = WDT_CLEAR_CLEAR_KEY;
|
39 | 71 | }
|
40 | 72 |
|
41 | 73 | void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
|
42 |
| - if (self->mode == WATCHDOGMODE_RESET) { |
43 |
| - mp_raise_RuntimeError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET")); |
44 |
| - } else { |
45 |
| - self->mode = WATCHDOGMODE_NONE; |
| 74 | + if (self->mode == WATCHDOGMODE_NONE) { |
| 75 | + return; |
46 | 76 | }
|
| 77 | + watchdog_disable(); |
| 78 | + self->mode = WATCHDOGMODE_NONE; |
47 | 79 | }
|
48 | 80 |
|
49 |
| -mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { |
50 |
| - return self->timeout; |
| 81 | +void watchdog_reset(void) { |
| 82 | + common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj); |
51 | 83 | }
|
52 | 84 |
|
53 |
| -STATIC void setup_wdt(watchdog_watchdogtimer_obj_t *self, int setting) { |
54 |
| - OSC32KCTRL->OSCULP32K.bit.EN1K = 1; // Enable out 1K (for WDT) |
55 |
| - |
56 |
| - // disable watchdog for config |
57 |
| - WDT->CTRLA.reg = 0; |
58 |
| - while (WDT->SYNCBUSY.reg) { // Sync CTRL write |
59 |
| - } |
60 |
| - |
61 |
| - WDT->INTENCLR.reg = WDT_INTENCLR_EW; // Disable early warning interrupt |
62 |
| - WDT->CONFIG.bit.PER = setting; // Set period for chip reset |
63 |
| - WDT->CTRLA.bit.WEN = 0; // Disable window mode |
64 |
| - while (WDT->SYNCBUSY.reg) { // Sync CTRL write |
65 |
| - } |
66 |
| - common_hal_watchdog_feed(self); // Clear watchdog interval |
67 |
| - WDT->CTRLA.bit.ENABLE = 1; // Start watchdog now! |
68 |
| - while (WDT->SYNCBUSY.reg) { |
69 |
| - } |
| 85 | +mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) { |
| 86 | + return self->timeout; |
70 | 87 | }
|
71 | 88 |
|
72 | 89 | void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) {
|
73 |
| - int wdt_cycles = (int)(new_timeout * 1024); |
74 |
| - if (wdt_cycles < 8) { |
75 |
| - wdt_cycles = 8; |
76 |
| - } |
77 |
| - if (wdt_cycles > 16384) { |
78 |
| - mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value")); |
| 90 | + if (!(self->timeout < new_timeout || self->timeout > new_timeout)) { |
| 91 | + return; |
79 | 92 | }
|
80 |
| - // ceil(log2(n)) = 32 - __builtin_clz(n - 1) when n > 1 (if int is 32 bits) |
81 |
| - int log2_wdt_cycles = (sizeof(int) * CHAR_BIT) - __builtin_clz(wdt_cycles - 1); |
82 |
| - int setting = log2_wdt_cycles - 3; // CYC8_Val is 0 |
83 |
| - float timeout = (8 << setting) / 1024.f; |
| 93 | + |
| 94 | + mp_arg_validate_int_max(new_timeout, 16, MP_QSTR_timeout); |
| 95 | + self->timeout = new_timeout; |
84 | 96 |
|
85 | 97 | if (self->mode == WATCHDOGMODE_RESET) {
|
86 |
| - setup_wdt(self, setting); |
| 98 | + watchdog_enable(self); |
87 | 99 | }
|
88 |
| - self->timeout = timeout; |
89 | 100 | }
|
90 | 101 |
|
91 | 102 | watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
|
92 | 103 | return self->mode;
|
93 | 104 | }
|
94 | 105 |
|
95 | 106 | void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
|
96 |
| - if (self->mode != new_mode) { |
97 |
| - if (new_mode == WATCHDOGMODE_RAISE) { |
98 |
| - mp_raise_NotImplementedError(translate("RAISE mode is not implemented")); |
99 |
| - } else if (new_mode == WATCHDOGMODE_NONE) { |
| 107 | + if (self->mode == new_mode) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + switch (new_mode) { |
| 112 | + case WATCHDOGMODE_NONE: |
100 | 113 | common_hal_watchdog_deinit(self);
|
101 |
| - } |
102 |
| - self->mode = new_mode; |
103 |
| - common_hal_watchdog_set_timeout(self, self->timeout); |
| 114 | + break; |
| 115 | + case WATCHDOGMODE_RAISE: |
| 116 | + mp_raise_NotImplementedError(NULL); |
| 117 | + break; |
| 118 | + case WATCHDOGMODE_RESET: |
| 119 | + watchdog_enable(self); |
| 120 | + break; |
| 121 | + default: |
| 122 | + return; |
104 | 123 | }
|
| 124 | + |
| 125 | + self->mode = new_mode; |
105 | 126 | }
|
0 commit comments