Skip to content

Commit b11188d

Browse files
committed
fw/lptim_systick: update wdt feed counter
Signed-off-by: HaiLong Yang <[email protected]>
1 parent 99cca48 commit b11188d

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/fw/drivers/sf32lb52/lptim_systick.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#error "lptim systick not compatible with LXT"
3838
#endif
3939

40+
#define LPTIM_COUNT_MASK 0xFFFF
41+
#define LPTIM_CLUNT_MAX 0x10000
42+
4043
static LPTIM_HandleTypeDef s_lptim1_handle = {0};
4144
static bool s_lptim_systick_initialized = false;
4245
static uint32_t s_last_idle_counter = 0;
@@ -70,7 +73,7 @@ void lptim_systick_enable(void)
7073
{
7174
__HAL_LPTIM_ENABLE(&s_lptim1_handle);
7275
__HAL_LPTIM_COUNTRST_RESET(&s_lptim1_handle);
73-
__HAL_LPTIM_AUTORELOAD_SET(&s_lptim1_handle, 0xFFFF);
76+
__HAL_LPTIM_AUTORELOAD_SET(&s_lptim1_handle, LPTIM_COUNT_MASK);
7477
__HAL_LPTIM_COMPARE_SET(&s_lptim1_handle, SYSTICK_ONE_TICK_HZ);
7578
__HAL_LPTIM_ENABLE_IT(&s_lptim1_handle, LPTIM_IT_OCIE);
7679

@@ -86,8 +89,8 @@ void lptim_systick_tickless_idle(uint32_t ticks_from_now)
8689
s_last_idle_counter = counter;
8790

8891
counter += ticks_from_now * SYSTICK_ONE_TICK_HZ;
89-
if (counter >= 0xFFFF) {
90-
counter -= 0xFFFF;
92+
if (counter >= LPTIM_COUNT_MASK) {
93+
counter -= LPTIM_COUNT_MASK;
9194
}
9295

9396
__HAL_LPTIM_COMPARE_SET(&s_lptim1_handle, counter);
@@ -99,7 +102,7 @@ uint32_t lptim_systick_get_elapsed_ticks(void)
99102
uint32_t counter = LPTIM1->CNT;
100103

101104
if (counter < s_last_idle_counter) {
102-
counter += 0x10000;
105+
counter += LPTIM_CLUNT_MAX;
103106
}
104107

105108
return (counter - s_last_idle_counter) / SYSTICK_ONE_TICK_HZ;
@@ -110,16 +113,17 @@ static inline void lptim_systick_next_tick_setup(void)
110113
uint32_t counter = LPTIM1->CNT;
111114

112115
counter += SYSTICK_ONE_TICK_HZ;
113-
if (counter >= 0xFFFF) {
114-
counter -= 0xFFFF;
116+
if (counter >= LPTIM_COUNT_MASK) {
117+
counter -= LPTIM_COUNT_MASK;
115118
}
116119

117120
__HAL_LPTIM_COMPARE_SET(&s_lptim1_handle, counter);
118121
}
119122

120123
void LPTIM1_IRQHandler(void)
121124
{
122-
static uint32_t wdt_feed_cnt = 0U;
125+
static uint32_t wdt_last_counter = 0U;
126+
static uint32_t wdt_feed_counter = 0U;
123127

124128
if (__HAL_LPTIM_GET_FLAG(&s_lptim1_handle, LPTIM_FLAG_OC) != RESET) {
125129
__HAL_LPTIM_CLEAR_FLAG(&s_lptim1_handle, LPTIM_IT_OCIE);
@@ -129,18 +133,32 @@ void LPTIM1_IRQHandler(void)
129133
if (__HAL_LPTIM_GET_FLAG(&s_lptim1_handle, LPTIM_FLAG_OCWKUP) == RESET) {
130134
extern void SysTick_Handler();
131135
SysTick_Handler();
132-
}
133136

134-
wdt_feed_cnt++;
135-
if (wdt_feed_cnt >= (RTC_TICKS_HZ * TASK_WATCHDOG_FEED_PERIOD_MS) / 1000) {
136-
wdt_feed_cnt = 0U;
137-
task_watchdog_feed();
137+
uint32_t current_counter = LPTIM1->CNT;
138+
if (current_counter < wdt_last_counter) {
139+
current_counter += LPTIM_CLUNT_MAX;
140+
}
141+
wdt_feed_counter += (current_counter - wdt_last_counter);
142+
wdt_last_counter = current_counter & LPTIM_COUNT_MASK;
143+
if (wdt_feed_counter >= (TASK_WATCHDOG_FEED_PERIOD_MS * SYSTICK_ONE_TICK_HZ)) {
144+
wdt_feed_counter = 0U;
145+
task_watchdog_feed();
146+
}
138147
}
139148
}
140149

141150
if (__HAL_LPTIM_GET_FLAG(&s_lptim1_handle, LPTIM_FLAG_OCWKUP) != RESET) {
142151
__HAL_LPTIM_DISABLE_IT(&s_lptim1_handle, LPTIM_IT_OCWE);
143152
__HAL_LPTIM_CLEAR_FLAG(&s_lptim1_handle, LPTIM_ICR_WKUPCLR);
153+
154+
// Force a watchdog refresh immediately after wakeup. The LPTIM SysTick requires
155+
// time to restart; if the system re-enters Stop mode during this latency, a watchdog
156+
// timeout may occur.
157+
task_watchdog_bit_set_all();
158+
task_watchdog_feed();
159+
// refresh wdt feed counter
160+
wdt_feed_counter = 0;
161+
wdt_last_counter = LPTIM1->CNT;
144162
}
145163
}
146164

0 commit comments

Comments
 (0)