2021-02-16 03:40:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Espressif Systems (Shanghai) Co., Ltd.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT espressif_esp32_timer
|
|
|
|
|
|
|
|
/* Include esp-idf headers first to avoid redefining BIT() macro */
|
|
|
|
#include <soc/rtc_cntl_reg.h>
|
|
|
|
#include <soc/timer_group_reg.h>
|
|
|
|
#include <driver/periph_ctrl.h>
|
|
|
|
#include <soc/periph_defs.h>
|
|
|
|
#include <hal/timer_types.h>
|
|
|
|
#include <hal/timer_hal.h>
|
|
|
|
#include <string.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/counter.h>
|
2022-10-12 20:25:33 +08:00
|
|
|
#include <zephyr/spinlock.h>
|
|
|
|
#include <zephyr/kernel.h>
|
2021-11-02 08:25:57 +08:00
|
|
|
#ifndef CONFIG_SOC_ESP32C3
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/interrupt_controller/intc_esp32.h>
|
2021-11-02 08:25:57 +08:00
|
|
|
#else
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/interrupt_controller/intc_esp32c3.h>
|
2021-11-02 08:25:57 +08:00
|
|
|
#endif
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
2021-02-16 03:40:38 +08:00
|
|
|
LOG_MODULE_REGISTER(esp32_counter, CONFIG_COUNTER_LOG_LEVEL);
|
|
|
|
|
2021-11-02 08:25:57 +08:00
|
|
|
#ifdef CONFIG_SOC_ESP32C3
|
|
|
|
#define ISR_HANDLER isr_handler_t
|
|
|
|
#else
|
|
|
|
#define ISR_HANDLER intr_handler_t
|
|
|
|
#endif
|
|
|
|
|
2021-04-17 05:15:11 +08:00
|
|
|
static void counter_esp32_isr(void *arg);
|
|
|
|
|
|
|
|
typedef bool (*timer_isr_t)(void *);
|
2021-02-16 03:40:38 +08:00
|
|
|
|
|
|
|
struct timer_isr_func_t {
|
|
|
|
timer_isr_t fn;
|
|
|
|
void *args;
|
2021-04-17 05:15:11 +08:00
|
|
|
struct intr_handle_data_t *timer_isr_handle;
|
2021-02-16 03:40:38 +08:00
|
|
|
timer_group_t isr_timer_group;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct counter_esp32_config {
|
|
|
|
struct counter_config_info counter_info;
|
|
|
|
timer_config_t config;
|
|
|
|
timer_group_t group;
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_idx_t index;
|
2021-04-17 05:15:11 +08:00
|
|
|
int irq_source;
|
2021-02-16 03:40:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct counter_esp32_data {
|
|
|
|
struct counter_alarm_cfg alarm_cfg;
|
|
|
|
uint32_t ticks;
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_context_t hal_ctx;
|
|
|
|
struct timer_isr_func_t timer_isr_fun;
|
2021-02-16 03:40:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct k_spinlock lock;
|
|
|
|
|
|
|
|
static int counter_esp32_init(const struct device *dev)
|
|
|
|
{
|
2022-01-18 21:01:39 +08:00
|
|
|
const struct counter_esp32_config *cfg = dev->config;
|
|
|
|
struct counter_esp32_data *data = dev->data;
|
2021-02-16 03:40:38 +08:00
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
switch (cfg->group) {
|
|
|
|
case TIMER_GROUP_0:
|
2021-02-16 03:40:38 +08:00
|
|
|
periph_module_enable(PERIPH_TIMG0_MODULE);
|
2022-07-07 05:21:28 +08:00
|
|
|
break;
|
|
|
|
case TIMER_GROUP_1:
|
2021-02-16 03:40:38 +08:00
|
|
|
periph_module_enable(PERIPH_TIMG1_MODULE);
|
2022-07-07 05:21:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
2021-02-16 03:40:38 +08:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_init(&data->hal_ctx, cfg->group, cfg->index);
|
2022-01-18 21:01:39 +08:00
|
|
|
data->alarm_cfg.callback = NULL;
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_intr_disable(&data->hal_ctx);
|
|
|
|
timer_hal_clear_intr_status(&data->hal_ctx);
|
|
|
|
timer_hal_set_auto_reload(&data->hal_ctx, cfg->config.auto_reload);
|
|
|
|
timer_hal_set_divider(&data->hal_ctx, cfg->config.divider);
|
|
|
|
timer_hal_set_counter_increase(&data->hal_ctx, cfg->config.counter_dir);
|
|
|
|
timer_hal_set_alarm_enable(&data->hal_ctx, cfg->config.alarm_en);
|
2021-02-16 03:40:38 +08:00
|
|
|
if (cfg->config.intr_type == TIMER_INTR_LEVEL) {
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_set_level_int_enable(&data->hal_ctx, true);
|
2021-02-16 03:40:38 +08:00
|
|
|
}
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_set_counter_value(&data->hal_ctx, 0);
|
|
|
|
timer_hal_set_counter_enable(&data->hal_ctx, cfg->config.counter_en);
|
2022-01-18 21:01:39 +08:00
|
|
|
esp_intr_alloc(cfg->irq_source,
|
2021-11-02 08:25:57 +08:00
|
|
|
0,
|
|
|
|
(ISR_HANDLER)counter_esp32_isr,
|
|
|
|
(void *)dev,
|
|
|
|
NULL);
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int counter_esp32_start(const struct device *dev)
|
|
|
|
{
|
2022-07-07 05:21:28 +08:00
|
|
|
struct counter_esp32_data *data = dev->data;
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_set_counter_enable(&data->hal_ctx, TIMER_START);
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int counter_esp32_stop(const struct device *dev)
|
|
|
|
{
|
2022-07-07 05:21:28 +08:00
|
|
|
struct counter_esp32_data *data = dev->data;
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_set_counter_enable(&data->hal_ctx, TIMER_PAUSE);
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int counter_esp32_get_value(const struct device *dev, uint32_t *ticks)
|
|
|
|
{
|
2022-07-07 05:21:28 +08:00
|
|
|
struct counter_esp32_data *data = dev->data;
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_get_counter_value(&data->hal_ctx, (uint64_t *)ticks);
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int counter_esp32_set_alarm(const struct device *dev, uint8_t chan_id,
|
|
|
|
const struct counter_alarm_cfg *alarm_cfg)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(chan_id);
|
2022-01-20 22:04:35 +08:00
|
|
|
struct counter_esp32_data *data = dev->data;
|
2021-02-16 03:40:38 +08:00
|
|
|
uint32_t now;
|
|
|
|
|
|
|
|
counter_esp32_get_value(dev, &now);
|
2022-09-14 21:28:38 +08:00
|
|
|
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
|
2022-09-14 21:28:38 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_intr_enable(&data->hal_ctx);
|
|
|
|
timer_hal_set_alarm_enable(&data->hal_ctx, TIMER_ALARM_EN);
|
2022-01-18 21:01:39 +08:00
|
|
|
data->alarm_cfg.callback = alarm_cfg->callback;
|
|
|
|
data->alarm_cfg.user_data = alarm_cfg->user_data;
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int counter_esp32_cancel_alarm(const struct device *dev, uint8_t chan_id)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(chan_id);
|
2022-07-07 05:21:28 +08:00
|
|
|
struct counter_esp32_data *data = dev->data;
|
2021-02-16 03:40:38 +08:00
|
|
|
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_intr_disable(&data->hal_ctx);
|
|
|
|
timer_hal_set_alarm_enable(&data->hal_ctx, TIMER_ALARM_DIS);
|
2021-02-16 03:40:38 +08:00
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int counter_esp32_set_top_value(const struct device *dev,
|
|
|
|
const struct counter_top_cfg *cfg)
|
|
|
|
{
|
2022-01-18 21:01:39 +08:00
|
|
|
const struct counter_esp32_config *config = dev->config;
|
|
|
|
|
|
|
|
if (cfg->ticks != config->counter_info.max_top_value) {
|
2021-02-16 03:40:38 +08:00
|
|
|
return -ENOTSUP;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t counter_esp32_get_pending_int(const struct device *dev)
|
|
|
|
{
|
2022-07-07 05:21:28 +08:00
|
|
|
struct counter_esp32_data *data = dev->data;
|
|
|
|
|
|
|
|
timer_hal_get_intr_status_reg(&data->hal_ctx);
|
2021-02-16 03:40:38 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t counter_esp32_get_top_value(const struct device *dev)
|
|
|
|
{
|
2022-01-18 21:01:39 +08:00
|
|
|
const struct counter_esp32_config *config = dev->config;
|
|
|
|
|
|
|
|
return config->counter_info.max_top_value;
|
2021-02-16 03:40:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct counter_driver_api counter_api = {
|
|
|
|
.start = counter_esp32_start,
|
|
|
|
.stop = counter_esp32_stop,
|
|
|
|
.get_value = counter_esp32_get_value,
|
|
|
|
.set_alarm = counter_esp32_set_alarm,
|
|
|
|
.cancel_alarm = counter_esp32_cancel_alarm,
|
|
|
|
.set_top_value = counter_esp32_set_top_value,
|
|
|
|
.get_pending_int = counter_esp32_get_pending_int,
|
|
|
|
.get_top_value = counter_esp32_get_top_value,
|
|
|
|
};
|
|
|
|
|
2021-04-17 05:15:11 +08:00
|
|
|
static void counter_esp32_isr(void *arg)
|
2021-02-16 03:40:38 +08:00
|
|
|
{
|
2022-01-06 21:10:19 +08:00
|
|
|
const struct device *dev = (const struct device *)arg;
|
2022-01-18 21:01:39 +08:00
|
|
|
struct counter_esp32_data *data = dev->data;
|
2021-02-16 03:40:38 +08:00
|
|
|
uint32_t now;
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
counter_esp32_cancel_alarm(dev, 0);
|
2021-02-16 03:40:38 +08:00
|
|
|
counter_esp32_get_value(dev, &now);
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
if (data->alarm_cfg.callback) {
|
|
|
|
data->alarm_cfg.callback(dev, 0, now, data->alarm_cfg.user_data);
|
2021-02-16 03:40:38 +08:00
|
|
|
}
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
timer_hal_clear_intr_status(&data->hal_ctx);
|
2021-02-16 03:40:38 +08:00
|
|
|
}
|
|
|
|
|
2022-07-07 05:21:28 +08:00
|
|
|
#define ESP32_COUNTER_GET_CLK_DIV(idx) \
|
|
|
|
(((DT_INST_PROP(idx, prescaler) & UINT16_MAX) < 2) ? \
|
|
|
|
2 : (DT_INST_PROP(idx, prescaler) & UINT16_MAX))
|
|
|
|
|
|
|
|
#define ESP32_COUNTER_INIT(idx) \
|
2021-02-16 03:40:38 +08:00
|
|
|
\
|
2022-07-07 05:21:28 +08:00
|
|
|
static struct counter_esp32_data counter_data_##idx; \
|
2021-02-16 03:40:38 +08:00
|
|
|
\
|
2022-07-07 05:21:28 +08:00
|
|
|
static const struct counter_esp32_config counter_config_##idx = { \
|
2021-02-16 03:40:38 +08:00
|
|
|
.counter_info = { \
|
|
|
|
.max_top_value = UINT32_MAX, \
|
2022-07-07 05:21:28 +08:00
|
|
|
.freq = (APB_CLK_FREQ / ESP32_COUNTER_GET_CLK_DIV(idx)), \
|
2021-02-16 03:40:38 +08:00
|
|
|
.flags = COUNTER_CONFIG_INFO_COUNT_UP, \
|
|
|
|
.channels = 1 \
|
|
|
|
}, \
|
|
|
|
.config = { \
|
|
|
|
.alarm_en = TIMER_ALARM_DIS, \
|
|
|
|
.counter_en = TIMER_START, \
|
|
|
|
.intr_type = TIMER_INTR_LEVEL, \
|
|
|
|
.counter_dir = TIMER_COUNT_UP, \
|
|
|
|
.auto_reload = TIMER_AUTORELOAD_DIS, \
|
2022-07-07 05:21:28 +08:00
|
|
|
.divider = ESP32_COUNTER_GET_CLK_DIV(idx), \
|
2021-02-16 03:40:38 +08:00
|
|
|
}, \
|
2022-07-07 05:21:28 +08:00
|
|
|
.group = DT_INST_PROP(idx, group), \
|
|
|
|
.index = DT_INST_PROP(idx, index), \
|
|
|
|
.irq_source = DT_INST_IRQN(idx), \
|
2021-02-16 03:40:38 +08:00
|
|
|
}; \
|
|
|
|
\
|
|
|
|
\
|
2022-07-07 05:21:28 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(idx, \
|
2021-02-16 03:40:38 +08:00
|
|
|
counter_esp32_init, \
|
2022-07-07 05:21:28 +08:00
|
|
|
NULL, \
|
|
|
|
&counter_data_##idx, \
|
|
|
|
&counter_config_##idx, \
|
|
|
|
PRE_KERNEL_1, \
|
|
|
|
CONFIG_COUNTER_INIT_PRIORITY, \
|
|
|
|
&counter_api);
|
2021-02-16 03:40:38 +08:00
|
|
|
|
2021-11-02 08:25:57 +08:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(ESP32_COUNTER_INIT);
|