Skip to content

Commit 11def3b

Browse files
author
Raffael Rostagno
committed
drivers: counter: esp32: Fix alarm stops working
Fix condition in which alarm stops working after a certain amount of time. Hardware timer is 54-bit, yet it is treated as 32-bit by the counter driver. To allow alarm event by hardware comparators, counter high word must be loaded into the register along with the lower word managed by the driver. Signed-off-by: Raffael Rostagno <[email protected]>
1 parent 87917a1 commit 11def3b

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

drivers/counter/counter_esp32_tmr.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id,
149149
uint32_t ticks = alarm_cfg->ticks;
150150
uint32_t top = data->top_data.ticks;
151151
uint32_t max_rel_val = data->top_data.ticks;
152-
uint32_t now;
152+
uint64_t now;
153+
uint64_t target;
153154
uint32_t diff;
154155
int err = 0;
155156
bool irq_on_late = 0;
@@ -161,19 +162,19 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id,
161162
data->alarm_cfg.callback = alarm_cfg->callback;
162163
data->alarm_cfg.user_data = alarm_cfg->user_data;
163164

164-
counter_esp32_get_value(dev, &now);
165+
counter_esp32_get_value_64(dev, &now);
165166

166167
if (absolute == 0) {
167-
ticks += now;
168-
timer_ll_set_alarm_value(data->hal_ctx.dev, data->hal_ctx.timer_id, ticks);
168+
target = now + ticks;
169169
} else {
170170
irq_on_late = alarm_cfg->flags & COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE;
171171
max_rel_val = top - data->top_data.guard_period;
172-
timer_ll_set_alarm_value(data->hal_ctx.dev, data->hal_ctx.timer_id,
173-
alarm_cfg->ticks);
172+
target = (now & ~0xFFFFFFFFULL) | ticks;
174173
}
175174

176-
diff = (alarm_cfg->ticks - now);
175+
timer_ll_set_alarm_value(data->hal_ctx.dev, data->hal_ctx.timer_id, target);
176+
177+
diff = (alarm_cfg->ticks - (uint32_t)now);
177178
if (diff > max_rel_val) {
178179
if (absolute) {
179180
err = -ETIME;

0 commit comments

Comments
 (0)