From e4969ec52e0392dcd219712b76d77be0c1af5994 Mon Sep 17 00:00:00 2001 From: Sylvio Alves Date: Wed, 14 Sep 2022 10:28:38 -0300 Subject: [PATCH] drivers: esp32: add check for absolute alarm configuration ESP32 counter based on Timer or RTC misses the COUNTER_ALARM_CFG_ABSOLUTE flag check. Signed-off-by: Sylvio Alves --- drivers/counter/counter_esp32_rtc.c | 7 ++++++- drivers/counter/counter_esp32_tmr.c | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/counter/counter_esp32_rtc.c b/drivers/counter/counter_esp32_rtc.c index 02ef539db05..5988c9ed85b 100644 --- a/drivers/counter/counter_esp32_rtc.c +++ b/drivers/counter/counter_esp32_rtc.c @@ -107,8 +107,13 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id, counter_esp32_get_value(dev, &now); + if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) { + WRITE_PERI_REG(RTC_CNTL_SLP_TIMER0_REG, (now + alarm_cfg->ticks)); + } else { + WRITE_PERI_REG(RTC_CNTL_SLP_TIMER0_REG, alarm_cfg->ticks); + } + /* RTC main timer set alarm value */ - WRITE_PERI_REG(RTC_CNTL_SLP_TIMER0_REG, (now + alarm_cfg->ticks)); CLEAR_PERI_REG_MASK(RTC_CNTL_SLP_TIMER1_REG, 0xffffffff); /* RTC main timer interrupt enable */ diff --git a/drivers/counter/counter_esp32_tmr.c b/drivers/counter/counter_esp32_tmr.c index 742840e4778..d9f0c1d919b 100644 --- a/drivers/counter/counter_esp32_tmr.c +++ b/drivers/counter/counter_esp32_tmr.c @@ -140,9 +140,15 @@ static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id, uint32_t now; counter_esp32_get_value(dev, &now); + k_spinlock_key_t key = k_spin_lock(&lock); - timer_hal_set_alarm_value(&data->hal_ctx, (now + alarm_cfg->ticks)); + if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) { + timer_hal_set_alarm_value(&data->hal_ctx, (now + alarm_cfg->ticks)); + } else { + timer_hal_set_alarm_value(&data->hal_ctx, alarm_cfg->ticks); + } + timer_hal_intr_enable(&data->hal_ctx); timer_hal_set_alarm_enable(&data->hal_ctx, TIMER_ALARM_EN); data->alarm_cfg.callback = alarm_cfg->callback;