|
19 | 19 |
|
20 | 20 | #include "WDT.h"
|
21 | 21 |
|
22 |
| -WDTimer::WDTimer() { } |
| 22 | +WDTimer::WDTimer() |
| 23 | +: _is_initialized {false} |
| 24 | +, _p_ctrl {0} |
| 25 | +, _timeout {0} |
| 26 | +{ |
| 27 | + |
| 28 | +} |
| 29 | + |
23 | 30 | WDTimer::~WDTimer() { }
|
24 | 31 |
|
25 | 32 | int WDTimer::begin(wdt_cfg_t config)
|
26 | 33 | {
|
27 |
| - if(_is_initialized) { |
28 |
| - return FSP_SUCCESS; |
| 34 | + if (_is_initialized) { |
| 35 | + return 1; |
29 | 36 | }
|
30 | 37 |
|
31 | 38 | fsp_err_t err = R_WDT_Open(&_p_ctrl, &config);
|
32 |
| - if(FSP_SUCCESS == err ) { |
33 |
| - R_WDT_Refresh(&_p_ctrl); |
34 |
| - _is_initialized = true; |
| 39 | + if (err != FSP_SUCCESS) { |
| 40 | + return 0; |
35 | 41 | }
|
| 42 | + R_WDT_Refresh(&_p_ctrl); |
| 43 | + _is_initialized = true; |
36 | 44 |
|
37 |
| - return (int)err; |
| 45 | + return 1; |
38 | 46 | }
|
39 | 47 |
|
40 |
| -void WDTimer::refresh() |
| 48 | +int WDTimer::begin(uint32_t timeout_ms) |
41 | 49 | {
|
| 50 | + if (_is_initialized) { |
| 51 | + return 1; |
| 52 | + } |
| 53 | + |
| 54 | + const uint8_t pr = getPrescaler(timeout_ms); |
| 55 | + if (pr == WDT_INVALID_TIMEOUT) { |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + const uint8_t rl = getReload(pr, timeout_ms); |
| 60 | + |
| 61 | + wdt_cfg_t p_cfg; |
| 62 | + p_cfg.timeout = (wdt_timeout_t)rl; |
| 63 | + p_cfg.clock_division = (wdt_clock_division_t)pr; |
| 64 | + p_cfg.window_start = WDT_WINDOW_START_100; |
| 65 | + p_cfg.window_end = WDT_WINDOW_END_0; |
| 66 | + p_cfg.reset_control = WDT_RESET_CONTROL_RESET; |
| 67 | + p_cfg.stop_control = WDT_STOP_CONTROL_ENABLE; |
| 68 | + |
| 69 | + fsp_err_t err = R_WDT_Open(&_p_ctrl, &p_cfg); |
| 70 | + if (err != FSP_SUCCESS) { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
42 | 74 | R_WDT_Refresh(&_p_ctrl);
|
| 75 | + _is_initialized = true; |
| 76 | + |
| 77 | + return 1; |
| 78 | +} |
| 79 | + |
| 80 | +void WDTimer::refresh() |
| 81 | +{ |
| 82 | + if (_is_initialized) { |
| 83 | + R_WDT_Refresh(&_p_ctrl); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +uint32_t WDTimer::getTimeout() |
| 88 | +{ |
| 89 | + if (_is_initialized) { |
| 90 | + return _timeout; |
| 91 | + } |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +uint32_t WDTimer::getCounter() |
| 96 | +{ |
| 97 | + uint32_t count = 0; |
| 98 | + |
| 99 | + if (_is_initialized) { |
| 100 | + R_WDT_CounterGet (&_p_ctrl, &count); |
| 101 | + } |
| 102 | + return count; |
| 103 | +} |
| 104 | + |
| 105 | +uint32_t WDTimer::getTicksMs() |
| 106 | +{ |
| 107 | + return (R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKB) / 1000); |
| 108 | +} |
| 109 | + |
| 110 | +uint32_t WDTimer::getMaxTimeout() |
| 111 | +{ |
| 112 | + return ((WDT_RL_VALUES::RL_16384 * WDT_PR_VALUES::PR_8192) / getTicksMs()); |
| 113 | +} |
| 114 | + |
| 115 | +uint32_t WDTimer::getPrescalerMaxTimeout(WDT_PR_VALUES pr) |
| 116 | +{ |
| 117 | + return ((WDT_RL_VALUES::RL_16384 * pr) / getTicksMs()); |
| 118 | +} |
| 119 | + |
| 120 | +uint8_t WDTimer::getPrescaler(uint32_t timeout_ms) |
| 121 | +{ |
| 122 | + const uint32_t max_timeout_ms = getMaxTimeout(); |
| 123 | + |
| 124 | + if (timeout_ms > max_timeout_ms) { |
| 125 | + return WDT_INVALID_TIMEOUT; |
| 126 | + } |
| 127 | + |
| 128 | + uint32_t pr; |
| 129 | + for (pr = 0; pr < WDT_PR_ARRAY_SIZE; pr++) { |
| 130 | + if (!prescaler_values[pr]) { |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + const uint32_t pr_max_timeout_ms = getPrescalerMaxTimeout(prescaler_values[pr]); |
| 135 | + if (timeout_ms <= pr_max_timeout_ms) { |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + const uint32_t pr_max_timeout_ms = getPrescalerMaxTimeout(WDT_PR_VALUES::PR_128); |
| 141 | + if (pr > WDT_CLOCK_DIVISION_64 && timeout_ms <= pr_max_timeout_ms) { |
| 142 | + return WDT_CLOCK_DIVISION_128; |
| 143 | + } |
| 144 | + return pr; |
| 145 | +} |
| 146 | + |
| 147 | +uint8_t WDTimer::getReload(uint8_t pr, uint32_t timeout_ms) |
| 148 | +{ |
| 149 | + uint32_t rl; |
| 150 | + for (rl = 0; rl < WDT_RL_ARRAY_SIZE; rl++) { |
| 151 | + const uint32_t pr_max_timeout_ms = (reload_values[rl] * prescaler_values[pr]) / (getTicksMs()); |
| 152 | + if (timeout_ms <= pr_max_timeout_ms) { |
| 153 | + _timeout = pr_max_timeout_ms; |
| 154 | + return rl; |
| 155 | + } |
| 156 | + } |
| 157 | + return rl; |
43 | 158 | }
|
44 | 159 |
|
45 | 160 | WDTimer WDT;
|
0 commit comments