Skip to content

Commit 01d4308

Browse files
Raffael Rostagnohenrikbrixandersen
authored andcommitted
drivers: counter: rtc: esp32: Fix cancel alarm condition
Disable RTC interrupts when cancelling alarm, and clear callback data, to make sure no ISR/callback is serviced after alarm is cancelled. Also, check programmed ticks in ISR to make sure alarm triggered is not an outdated/cancelled event. Signed-off-by: Raffael Rostagno <[email protected]>
1 parent 1663233 commit 01d4308

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

drivers/counter/counter_esp32_rtc.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id,
129129
ticks = now + alarm_cfg->ticks;
130130
}
131131

132+
data->ticks = (uint32_t)ticks;
133+
132134
rtc_cntl_ll_set_wakeup_timer(ticks);
133135

134136
/* RTC main timer set alarm value */
@@ -145,15 +147,19 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id,
145147

146148
static int counter_esp32_cancel_alarm(const struct device *dev, uint8_t chan_id)
147149
{
148-
ARG_UNUSED(dev);
149150
ARG_UNUSED(chan_id);
151+
struct counter_esp32_data *data = dev->data;
150152

151153
/* RTC main timer set alarm disable */
152154
CLEAR_PERI_REG_MASK(RTC_CNTL_SLP_TIMER1_REG, RTC_CNTL_MAIN_TIMER_ALARM_EN);
153155

154-
/* RTC main timer interrupt disable */
156+
/* RTC main timer interrupt disable, and clear interrupt flag */
157+
REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
155158
SET_PERI_REG_MASK(RTC_CNTL_INT_CLR_REG, RTC_CNTL_MAIN_TIMER_INT_CLR);
156159

160+
data->alarm_cfg.callback = NULL;
161+
data->alarm_cfg.user_data = NULL;
162+
157163
return 0;
158164
}
159165

@@ -228,6 +234,8 @@ static void counter_esp32_isr(void *arg)
228234
{
229235
const struct device *dev = (const struct device *)arg;
230236
struct counter_esp32_data *data = dev->data;
237+
counter_alarm_callback_t cb = data->alarm_cfg.callback;
238+
void *cb_data = data->alarm_cfg.user_data;
231239
uint32_t now;
232240
uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
233241

@@ -238,8 +246,8 @@ static void counter_esp32_isr(void *arg)
238246
counter_esp32_cancel_alarm(dev, 0);
239247
counter_esp32_get_value(dev, &now);
240248

241-
if (data->alarm_cfg.callback) {
242-
data->alarm_cfg.callback(dev, 0, now, data->alarm_cfg.user_data);
249+
if (cb && (now > data->ticks)) {
250+
cb(dev, 0, now, cb_data);
243251
}
244252
}
245253

0 commit comments

Comments
 (0)