mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-27 19:13:21 +00:00
feat(ana_cmpr): refactor driver implemnetation for new ip
esp32h4 and esp32s31 has a new IP design WRT the ana cmpr module
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -8,7 +8,6 @@
|
||||
#include "esp_clk_tree.h"
|
||||
#include "esp_types.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_pm.h"
|
||||
#include "esp_memory_utils.h"
|
||||
#include "driver/ana_cmpr.h"
|
||||
#include "esp_private/gpio.h"
|
||||
@@ -16,34 +15,6 @@
|
||||
#include "esp_private/esp_clk.h"
|
||||
#include "ana_cmpr_private.h"
|
||||
|
||||
struct ana_cmpr_t {
|
||||
ana_cmpr_unit_t unit; /*!< Analog comparator unit id */
|
||||
analog_cmpr_dev_t *dev; /*!< Analog comparator unit device address */
|
||||
ana_cmpr_ref_source_t ref_src; /*!< Analog comparator reference source, internal or external */
|
||||
_Atomic ana_cmpr_fsm_t fsm; /*!< The state machine of the Analog Comparator unit */
|
||||
ana_cmpr_event_callbacks_t cbs; /*!< The callback group that set by user */
|
||||
void *user_data; /*!< User data that passed to the callbacks */
|
||||
intr_handle_t intr_handle; /*!< Interrupt handle */
|
||||
uint32_t intr_mask; /*!< Interrupt mask */
|
||||
int intr_priority; /*!< Interrupt priority */
|
||||
uint32_t src_clk_freq_hz; /*!< Source clock frequency of the Analog Comparator unit */
|
||||
#if CONFIG_PM_ENABLE
|
||||
esp_pm_lock_handle_t pm_lock; /*!< The Power Management lock that used to avoid unexpected power down of the clock domain */
|
||||
#endif
|
||||
};
|
||||
|
||||
/* Helper macros */
|
||||
#define ANA_CMPR_NULL_POINTER_CHECK(p) ESP_RETURN_ON_FALSE((p), ESP_ERR_INVALID_ARG, TAG, "input parameter '" #p "' is NULL")
|
||||
#define ANA_CMPR_NULL_POINTER_CHECK_SAFE(p) \
|
||||
do { \
|
||||
if (unlikely(!(p))) { \
|
||||
ESP_EARLY_LOGE(TAG, "input parameter '" #p "' is NULL"); \
|
||||
return ESP_ERR_INVALID_ARG; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define ANA_CMPR_UNIT_CHECK(unit) ESP_RETURN_ON_FALSE((unit) >= 0 && (unit) < ANALOG_CMPR_LL_GET(INST_NUM), ESP_ERR_INVALID_ARG, TAG, "invalid unit number")
|
||||
|
||||
/* Global static object of the Analog Comparator unit */
|
||||
static ana_cmpr_handle_t s_ana_cmpr[ANALOG_CMPR_LL_GET(INST_NUM)] = {
|
||||
[0 ...(ANALOG_CMPR_LL_GET(INST_NUM) - 1)] = NULL,
|
||||
@@ -62,33 +33,38 @@ void ana_cmpr_default_intr_handler(void *usr_data)
|
||||
analog_cmpr_ll_clear_intr(cmpr_handle->dev, status);
|
||||
|
||||
/* Call the user callback function if it is specified and the corresponding event triggers*/
|
||||
if (cmpr_handle->cbs.on_cross && (status & cmpr_handle->intr_mask)) {
|
||||
ana_cmpr_cross_cb_t on_cross = cmpr_handle->cbs.on_cross;
|
||||
if (on_cross) {
|
||||
// some chip can distinguish the edge of the cross event
|
||||
#if ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
if (status & ANALOG_CMPR_LL_POS_CROSS_MASK(cmpr_handle->unit, 0)) {
|
||||
for (int i = 0; i < ANALOG_CMPR_LL_GET(SRC_CHANNEL_NUM); i++) {
|
||||
evt_data.src_chan_id = i;
|
||||
if (status & ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(cmpr_handle->unit_id, i)) {
|
||||
evt_data.cross_type = ANA_CMPR_CROSS_POS;
|
||||
} else if (status & ANALOG_CMPR_LL_NEG_CROSS_MASK(cmpr_handle->unit, 0)) {
|
||||
need_yield |= on_cross(cmpr_handle, &evt_data, cmpr_handle->user_data);
|
||||
} else if (status & ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(cmpr_handle->unit_id, i)) {
|
||||
evt_data.cross_type = ANA_CMPR_CROSS_NEG;
|
||||
need_yield |= on_cross(cmpr_handle, &evt_data, cmpr_handle->user_data);
|
||||
}
|
||||
#endif // ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
need_yield = cmpr_handle->cbs.on_cross(cmpr_handle, &evt_data, cmpr_handle->user_data);
|
||||
}
|
||||
#else
|
||||
need_yield = on_cross(cmpr_handle, &evt_data, cmpr_handle->user_data);
|
||||
#endif
|
||||
}
|
||||
if (need_yield) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t s_ana_cmpr_init_gpio(ana_cmpr_handle_t cmpr, bool is_external_ref)
|
||||
{
|
||||
esp_err_t err = gpio_config_as_analog(ana_cmpr_periph[cmpr->unit].src_gpio);
|
||||
if (err == ESP_OK && is_external_ref) {
|
||||
err = gpio_config_as_analog(ana_cmpr_periph[cmpr->unit].ext_ref_gpio);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static void ana_cmpr_destroy_unit(ana_cmpr_handle_t cmpr)
|
||||
{
|
||||
int unit_id = cmpr->unit_id;
|
||||
|
||||
// Disable function clock first
|
||||
analog_cmpr_ll_enable_function_clock(unit_id, false);
|
||||
// Disable bus clock last
|
||||
analog_cmpr_ll_enable_bus_clock(unit_id, false);
|
||||
|
||||
#if CONFIG_PM_ENABLE
|
||||
if (cmpr->pm_lock) {
|
||||
esp_pm_lock_delete(cmpr->pm_lock);
|
||||
@@ -100,15 +76,79 @@ static void ana_cmpr_destroy_unit(ana_cmpr_handle_t cmpr)
|
||||
free(cmpr);
|
||||
}
|
||||
|
||||
#if ANALOG_CMPR_LL_GET(IP_VERSION) > 1
|
||||
static int _ana_cmpr_gpio_to_pad_id(ana_cmpr_handle_t cmpr, int gpio_num)
|
||||
{
|
||||
int pad_id = -1;
|
||||
for (int i = 0; i < ANALOG_CMPR_LL_GET(PAD_NUM); i++) {
|
||||
if (ana_cmpr_periph[cmpr->unit_id].pad_gpios[i] == gpio_num) {
|
||||
pad_id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pad_id;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void _ana_cmpr_init_default_channels(ana_cmpr_handle_t cmpr, const ana_cmpr_config_t *config)
|
||||
{
|
||||
int unit_id = cmpr->unit_id;
|
||||
cmpr->ref_chan.ref_src = config->ref_src;
|
||||
#if ANALOG_CMPR_LL_GET(IP_VERSION) > 1
|
||||
// GPIO number of external reference channel is configurable
|
||||
// cmpr->ref_chan.gpio_num =
|
||||
cmpr->ref_chan.pad_id = _ana_cmpr_gpio_to_pad_id(cmpr, cmpr->ref_chan.gpio_num);
|
||||
#else
|
||||
cmpr->ref_chan.gpio_num = ana_cmpr_periph[unit_id].ext_ref_gpio;
|
||||
#endif
|
||||
|
||||
cmpr->src_chans[0].chan_id = 0;
|
||||
cmpr->src_chans[0].cross_type = config->cross_type;
|
||||
#if ANALOG_CMPR_LL_GET(IP_VERSION) > 1
|
||||
// GPIO number of source channel is configurable
|
||||
// cmpr->src_chans[0].gpio_num =
|
||||
cmpr->src_chans[0].pad_id = _ana_cmpr_gpio_to_pad_id(cmpr, cmpr->src_chans[0].gpio_num);
|
||||
#else
|
||||
cmpr->src_chans[0].gpio_num = ana_cmpr_periph[unit_id].src_gpio;
|
||||
#endif
|
||||
|
||||
analog_cmpr_ll_set_ref_source(cmpr->dev, config->ref_src);
|
||||
|
||||
#if !ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
// set which cross type can trigger the interrupt
|
||||
analog_cmpr_ll_set_intr_cross_type(cmpr->dev, config->cross_type);
|
||||
#endif // !ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
// each source channel's cross type can contribute different mask to the unit's intr_mask, so set it here according to the config
|
||||
cmpr->intr_mask |= analog_cmpr_ll_get_intr_mask_by_type(unit_id, 0, config->cross_type);
|
||||
|
||||
// setup the gpio pad for the source and reference signal
|
||||
gpio_config_as_analog(cmpr->src_chans[0].gpio_num);
|
||||
#if ANALOG_CMPR_LL_GET(IP_VERSION) > 1
|
||||
analog_cmpr_ll_set_src_pad(cmpr->dev, 0, cmpr->src_chans[0].pad_id);
|
||||
#endif
|
||||
if (config->ref_src == ANA_CMPR_REF_SRC_EXTERNAL) {
|
||||
gpio_config_as_analog(cmpr->ref_chan.gpio_num);
|
||||
#if ANALOG_CMPR_LL_GET(IP_VERSION) > 1
|
||||
analog_cmpr_ll_set_ext_ref_pad(cmpr->dev, cmpr->ref_chan.pad_id);
|
||||
#endif
|
||||
ESP_LOGD(TAG, "unit %d: source0 signal from GPIO %d, reference signal from GPIO %d",
|
||||
unit_id, cmpr->src_chans[0].gpio_num, cmpr->ref_chan.gpio_num);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "unit %d: source0 signal from GPIO %d, reference signal from internal",
|
||||
unit_id, cmpr->src_chans[0].gpio_num);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t ana_cmpr_new_unit(const ana_cmpr_config_t *config, ana_cmpr_handle_t *ret_cmpr)
|
||||
{
|
||||
if (config == NULL || ret_cmpr == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_err_t ret = ESP_OK;
|
||||
ana_cmpr_handle_t ana_cmpr_hdl = NULL;
|
||||
ANA_CMPR_NULL_POINTER_CHECK(config);
|
||||
ANA_CMPR_NULL_POINTER_CHECK(ret_cmpr);
|
||||
ana_cmpr_unit_t unit = config->unit;
|
||||
ANA_CMPR_UNIT_CHECK(unit);
|
||||
ESP_RETURN_ON_FALSE(!s_ana_cmpr[unit], ESP_ERR_INVALID_STATE, TAG, "unit has been allocated already");
|
||||
int unit_id = config->unit;
|
||||
ESP_RETURN_ON_FALSE(unit_id >= 0 && unit_id < ANALOG_CMPR_LL_GET(INST_NUM), ESP_ERR_INVALID_ARG, TAG, "invalid unit id");
|
||||
ESP_RETURN_ON_FALSE(!s_ana_cmpr[unit_id], ESP_ERR_INVALID_STATE, TAG, "unit has been allocated already");
|
||||
if (config->intr_priority) {
|
||||
ESP_RETURN_ON_FALSE(1 << (config->intr_priority) & ANA_CMPR_ALLOW_INTR_PRIORITY_MASK, ESP_ERR_INVALID_ARG,
|
||||
TAG, "invalid interrupt priority:%d", config->intr_priority);
|
||||
@@ -119,69 +159,52 @@ esp_err_t ana_cmpr_new_unit(const ana_cmpr_config_t *config, ana_cmpr_handle_t *
|
||||
ESP_RETURN_ON_FALSE(ana_cmpr_hdl, ESP_ERR_NO_MEM, TAG, "no memory for analog comparator object");
|
||||
|
||||
/* Assign analog comparator unit */
|
||||
ana_cmpr_hdl->dev = ANALOG_CMPR_LL_GET_HW(unit);
|
||||
ana_cmpr_hdl->unit = unit;
|
||||
ana_cmpr_hdl->dev = ANALOG_CMPR_LL_GET_HW(unit_id);
|
||||
ana_cmpr_hdl->unit_id = unit_id;
|
||||
ana_cmpr_hdl->intr_priority = config->intr_priority;
|
||||
atomic_init(&ana_cmpr_hdl->fsm, ANA_CMPR_FSM_INIT);
|
||||
|
||||
// Enable bus clock
|
||||
analog_cmpr_ll_enable_bus_clock(unit, true);
|
||||
analog_cmpr_ll_enable_bus_clock(unit_id, true);
|
||||
// Reset register
|
||||
analog_cmpr_ll_reset_register(unit);
|
||||
analog_cmpr_ll_reset_register(unit_id);
|
||||
// Reset core
|
||||
analog_cmpr_ll_reset_core(unit_id);
|
||||
|
||||
// Set clock source (use default if not specified in config)
|
||||
ana_cmpr_clk_src_t clk_src = config->clk_src ? config->clk_src : ANA_CMPR_CLK_SRC_DEFAULT;
|
||||
#if ANALOG_CMPR_LL_GET(IP_VERSION) > 1
|
||||
// Reset core
|
||||
analog_cmpr_ll_reset_core(unit);
|
||||
// Set clock source (use default if not specified in config)
|
||||
analog_cmpr_ll_set_clk_src(unit, clk_src);
|
||||
analog_cmpr_ll_set_clk_src(unit_id, clk_src);
|
||||
// Set clock divider to 1
|
||||
analog_cmpr_ll_set_clk_div(unit, 1);
|
||||
analog_cmpr_ll_set_clk_div(unit_id, 1);
|
||||
// Enable function clock
|
||||
analog_cmpr_ll_enable_function_clock(unit, true);
|
||||
analog_cmpr_ll_enable_function_clock(unit_id, true);
|
||||
#else
|
||||
// Analog comparator located in the IO MUX module in older chips, so the clock source is shared with IO MUX.
|
||||
// TODO: Check if the clock source conflicts with other IOMUX consumers
|
||||
ESP_GOTO_ON_ERROR(io_mux_set_clock_source((soc_module_clk_t)clk_src), err, TAG, "clock source conflicts with other IOMUX consumers");
|
||||
#endif
|
||||
ESP_GOTO_ON_ERROR(esp_clk_tree_src_get_freq_hz((soc_module_clk_t)clk_src, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &ana_cmpr_hdl->src_clk_freq_hz),
|
||||
err, TAG, "get source clock frequency failed");
|
||||
|
||||
// init the default source and reference channels according to the config
|
||||
_ana_cmpr_init_default_channels(ana_cmpr_hdl, config);
|
||||
|
||||
#if CONFIG_PM_ENABLE
|
||||
// Create PM lock, because the light sleep may disable the clock and power domain used by the analog comparator
|
||||
// TODO: IDF-12818
|
||||
ret = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, ana_cmpr_periph[unit].module_name, &ana_cmpr_hdl->pm_lock);
|
||||
ret = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, ana_cmpr_periph[unit_id].module_name, &ana_cmpr_hdl->pm_lock);
|
||||
ESP_GOTO_ON_ERROR(ret, err, TAG, "create NO_LIGHT_SLEEP lock failed");
|
||||
#endif
|
||||
|
||||
/* Configure the register */
|
||||
analog_cmpr_ll_set_ref_source(ana_cmpr_hdl->dev, config->ref_src);
|
||||
ana_cmpr_hdl->ref_src = config->ref_src;
|
||||
#if !ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
// set which cross type can trigger the interrupt
|
||||
analog_cmpr_ll_set_intr_cross_type(ana_cmpr_hdl->dev, config->cross_type);
|
||||
#endif // !ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
// record the interrupt mask, the interrupt will be lazy installed when register user callbacks
|
||||
// different cross type means different interrupt mask
|
||||
ana_cmpr_hdl->intr_mask = analog_cmpr_ll_get_intr_mask_by_type(unit, 0, config->cross_type);
|
||||
|
||||
// different unit share the same interrupt register, so using a spin lock to protect it
|
||||
portENTER_CRITICAL(&s_spinlock);
|
||||
// disable the interrupt by default, and clear pending status
|
||||
analog_cmpr_ll_enable_intr(ana_cmpr_hdl->dev, ANALOG_CMPR_LL_ALL_INTR_MASK(unit, 0), false);
|
||||
analog_cmpr_ll_clear_intr(ana_cmpr_hdl->dev, ANALOG_CMPR_LL_ALL_INTR_MASK(unit, 0));
|
||||
// disable the interrupt by default, and clear all pending status
|
||||
analog_cmpr_ll_enable_intr(ana_cmpr_hdl->dev, ANALOG_CMPR_LL_ALL_INTR_MASK(unit_id), false);
|
||||
analog_cmpr_ll_clear_intr(ana_cmpr_hdl->dev, ANALOG_CMPR_LL_ALL_INTR_MASK(unit_id));
|
||||
portEXIT_CRITICAL(&s_spinlock);
|
||||
|
||||
// GPIO configuration
|
||||
ESP_GOTO_ON_ERROR(s_ana_cmpr_init_gpio(ana_cmpr_hdl, config->ref_src == ANA_CMPR_REF_SRC_EXTERNAL), err, TAG, "failed to initialize GPIO");
|
||||
if (config->ref_src == ANA_CMPR_REF_SRC_INTERNAL) {
|
||||
ESP_LOGD(TAG, "unit %d allocated, source signal: GPIO %d, reference signal: internal",
|
||||
(int)unit, ana_cmpr_periph[unit].src_gpio);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "unit %d allocated, source signal: GPIO %d, reference signal: GPIO %d",
|
||||
(int)unit, ana_cmpr_periph[unit].src_gpio, ana_cmpr_periph[unit].ext_ref_gpio);
|
||||
}
|
||||
|
||||
// register the analog comparator unit to the global object array
|
||||
s_ana_cmpr[unit] = ana_cmpr_hdl;
|
||||
s_ana_cmpr[unit_id] = ana_cmpr_hdl;
|
||||
*ret_cmpr = ana_cmpr_hdl;
|
||||
return ESP_OK;
|
||||
|
||||
@@ -194,38 +217,36 @@ err:
|
||||
|
||||
esp_err_t ana_cmpr_del_unit(ana_cmpr_handle_t cmpr)
|
||||
{
|
||||
ANA_CMPR_NULL_POINTER_CHECK(cmpr);
|
||||
if (cmpr == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
/* Search the global object array to check if the input handle is valid */
|
||||
int unit = -1;
|
||||
int unit_id = -1;
|
||||
for (int i = 0; i < ANALOG_CMPR_LL_GET(INST_NUM); i++) {
|
||||
if (s_ana_cmpr[i] == cmpr) {
|
||||
unit = i;
|
||||
unit_id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ESP_RETURN_ON_FALSE(unit != -1, ESP_ERR_INVALID_ARG, TAG, "unregistered unit handle");
|
||||
ESP_RETURN_ON_FALSE(unit_id != -1, ESP_ERR_INVALID_ARG, TAG, "unregistered unit handle");
|
||||
ESP_RETURN_ON_FALSE(atomic_load(&cmpr->fsm) == ANA_CMPR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "not in init state");
|
||||
|
||||
// Disable function clock first
|
||||
analog_cmpr_ll_enable_function_clock(unit, false);
|
||||
// Disable bus clock last
|
||||
analog_cmpr_ll_enable_bus_clock(unit, false);
|
||||
|
||||
ana_cmpr_destroy_unit(cmpr);
|
||||
// unregister it from the global object array
|
||||
s_ana_cmpr[unit] = NULL;
|
||||
ESP_LOGD(TAG, "unit %d deleted", (int)unit);
|
||||
s_ana_cmpr[unit_id] = NULL;
|
||||
ESP_LOGD(TAG, "unit %d deleted", (int)unit_id);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ana_cmpr_set_internal_reference(ana_cmpr_handle_t cmpr, const ana_cmpr_internal_ref_config_t *ref_cfg)
|
||||
{
|
||||
ANA_CMPR_NULL_POINTER_CHECK_SAFE(cmpr);
|
||||
ANA_CMPR_NULL_POINTER_CHECK_SAFE(ref_cfg);
|
||||
if (unlikely(cmpr->ref_src != ANA_CMPR_REF_SRC_INTERNAL)) {
|
||||
ESP_EARLY_LOGE(TAG, "the reference voltage does not come from internal");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
if (cmpr == NULL || ref_cfg == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
// external reference channel doesn't support it
|
||||
if (cmpr->ref_chan.ref_src != ANA_CMPR_REF_SRC_INTERNAL) {
|
||||
return ESP_ERR_NOT_ALLOWED;
|
||||
}
|
||||
|
||||
// the underlying register may be accessed by different threads at the same time, so use spin lock to protect it
|
||||
@@ -238,8 +259,9 @@ esp_err_t ana_cmpr_set_internal_reference(ana_cmpr_handle_t cmpr, const ana_cmpr
|
||||
|
||||
esp_err_t ana_cmpr_set_debounce(ana_cmpr_handle_t cmpr, const ana_cmpr_debounce_config_t *dbc_cfg)
|
||||
{
|
||||
ANA_CMPR_NULL_POINTER_CHECK_SAFE(cmpr);
|
||||
ANA_CMPR_NULL_POINTER_CHECK_SAFE(dbc_cfg);
|
||||
if (cmpr == NULL || dbc_cfg == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* Transfer the time to clock cycles */
|
||||
uint32_t wait_cycle = dbc_cfg->wait_us * (cmpr->src_clk_freq_hz / 1000000);
|
||||
@@ -251,34 +273,11 @@ esp_err_t ana_cmpr_set_debounce(ana_cmpr_handle_t cmpr, const ana_cmpr_debounce_
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ana_cmpr_set_cross_type(ana_cmpr_handle_t cmpr, ana_cmpr_cross_type_t cross_type)
|
||||
{
|
||||
#if ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
/* Not support to set the cross type after initialized, because it relies on the interrupt types to distinguish the edge,
|
||||
* i.e. have to re-allocate the interrupt to change the cross type */
|
||||
(void)cmpr;
|
||||
(void)cross_type;
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
ANA_CMPR_NULL_POINTER_CHECK_SAFE(cmpr);
|
||||
if (unlikely(cross_type < ANA_CMPR_CROSS_DISABLE || cross_type > ANA_CMPR_CROSS_ANY)) {
|
||||
ESP_EARLY_LOGE(TAG, "invalid cross type");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
portENTER_CRITICAL_SAFE(&s_spinlock);
|
||||
analog_cmpr_ll_set_intr_cross_type(cmpr->dev, cross_type);
|
||||
cmpr->intr_mask = analog_cmpr_ll_get_intr_mask_by_type(cmpr->unit, 0, cross_type);
|
||||
portEXIT_CRITICAL_SAFE(&s_spinlock);
|
||||
|
||||
return ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
esp_err_t ana_cmpr_register_event_callbacks(ana_cmpr_handle_t cmpr, const ana_cmpr_event_callbacks_t *cbs, void *user_data)
|
||||
{
|
||||
ANA_CMPR_NULL_POINTER_CHECK(cmpr);
|
||||
ANA_CMPR_NULL_POINTER_CHECK(cbs);
|
||||
if (cmpr == NULL || cbs == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ESP_RETURN_ON_FALSE(atomic_load(&cmpr->fsm) == ANA_CMPR_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "not in init state");
|
||||
#if CONFIG_ANA_CMPR_ISR_CACHE_SAFE
|
||||
if (cbs->on_cross) {
|
||||
@@ -289,10 +288,13 @@ esp_err_t ana_cmpr_register_event_callbacks(ana_cmpr_handle_t cmpr, const ana_cm
|
||||
}
|
||||
#endif
|
||||
|
||||
// the interrupt service is lazy installed.
|
||||
if (!cmpr->intr_handle) {
|
||||
int intr_flags = ANA_CMPR_INTR_FLAG | ((cmpr->intr_priority > 0) ? BIT(cmpr->intr_priority) : ESP_INTR_FLAG_LOWMED);
|
||||
ESP_RETURN_ON_ERROR(esp_intr_alloc_intrstatus(ana_cmpr_periph[cmpr->unit].intr_src, intr_flags, (uint32_t)analog_cmpr_ll_get_intr_status_reg(cmpr->dev),
|
||||
cmpr->intr_mask, ana_cmpr_default_intr_handler, cmpr, &cmpr->intr_handle),
|
||||
ESP_RETURN_ON_ERROR(esp_intr_alloc_intrstatus(ana_cmpr_periph[cmpr->unit_id].intr_src, intr_flags,
|
||||
(uint32_t)analog_cmpr_ll_get_intr_status_reg(cmpr->dev),
|
||||
ANALOG_CMPR_LL_ALL_INTR_MASK(cmpr->unit_id), ana_cmpr_default_intr_handler,
|
||||
cmpr, &cmpr->intr_handle),
|
||||
TAG, "allocate interrupt failed");
|
||||
}
|
||||
|
||||
@@ -300,13 +302,15 @@ esp_err_t ana_cmpr_register_event_callbacks(ana_cmpr_handle_t cmpr, const ana_cm
|
||||
memcpy(&(cmpr->cbs), cbs, sizeof(ana_cmpr_event_callbacks_t));
|
||||
cmpr->user_data = user_data;
|
||||
|
||||
ESP_LOGV(TAG, "unit %d event callback registered", (int)cmpr->unit);
|
||||
ESP_LOGV(TAG, "unit %d event callback registered", cmpr->unit_id);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ana_cmpr_enable(ana_cmpr_handle_t cmpr)
|
||||
{
|
||||
ANA_CMPR_NULL_POINTER_CHECK(cmpr);
|
||||
if (cmpr == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ana_cmpr_fsm_t expected_fsm = ANA_CMPR_FSM_INIT;
|
||||
if (atomic_compare_exchange_strong(&cmpr->fsm, &expected_fsm, ANA_CMPR_FSM_WAIT)) {
|
||||
#if CONFIG_PM_ENABLE
|
||||
@@ -323,7 +327,7 @@ esp_err_t ana_cmpr_enable(ana_cmpr_handle_t cmpr)
|
||||
|
||||
// switch the state machine to enable state
|
||||
atomic_store(&cmpr->fsm, ANA_CMPR_FSM_ENABLE);
|
||||
ESP_LOGD(TAG, "unit %d enabled", (int)cmpr->unit);
|
||||
ESP_LOGD(TAG, "unit %d enabled", (int)cmpr->unit_id);
|
||||
} else {
|
||||
ESP_RETURN_ON_FALSE(false, ESP_ERR_INVALID_STATE, TAG, "not in init state");
|
||||
}
|
||||
@@ -333,7 +337,9 @@ esp_err_t ana_cmpr_enable(ana_cmpr_handle_t cmpr)
|
||||
|
||||
esp_err_t ana_cmpr_disable(ana_cmpr_handle_t cmpr)
|
||||
{
|
||||
ANA_CMPR_NULL_POINTER_CHECK(cmpr);
|
||||
if (cmpr == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ana_cmpr_fsm_t expected_fsm = ANA_CMPR_FSM_ENABLE;
|
||||
if (atomic_compare_exchange_strong(&cmpr->fsm, &expected_fsm, ANA_CMPR_FSM_WAIT)) {
|
||||
// the underlying register may be accessed by different threads at the same time, so use spin lock to protect it
|
||||
@@ -350,7 +356,7 @@ esp_err_t ana_cmpr_disable(ana_cmpr_handle_t cmpr)
|
||||
|
||||
// switch the state machine to init state
|
||||
atomic_store(&cmpr->fsm, ANA_CMPR_FSM_INIT);
|
||||
ESP_LOGD(TAG, "unit %d disabled", (int)cmpr->unit);
|
||||
ESP_LOGD(TAG, "unit %d disabled", (int)cmpr->unit_id);
|
||||
} else {
|
||||
ESP_RETURN_ON_FALSE(false, ESP_ERR_INVALID_STATE, TAG, "not enabled yet");
|
||||
}
|
||||
@@ -358,10 +364,62 @@ esp_err_t ana_cmpr_disable(ana_cmpr_handle_t cmpr)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#if CONFIG_ANA_CMPR_ENABLE_DEBUG_LOG
|
||||
__attribute__((constructor))
|
||||
static void ana_cmpr_override_default_log_level(void)
|
||||
{
|
||||
esp_log_level_set(TAG, ESP_LOG_DEBUG);
|
||||
}
|
||||
#endif
|
||||
|
||||
/////////////////////////////// Legacy API for backward compatibility, will be removed in the future ///////////////////
|
||||
// These APIs are implemented based on the "legacy" ref and src channel objects in the analog comparator unit,
|
||||
// which are designed for the old version driver that only support one ref and src channel and directly configured in the analog comparator unit.
|
||||
// The legacy channels are still used in the new version driver for backward compatibility, but they are not recommended for new use
|
||||
// because they have some limitations, such as source channel can only support one GPIO input.
|
||||
// New APIs with more flexible channel configuration are provided in the new version driver,
|
||||
// which are implemented based on the new ref and src channel objects and are recommended for new use.
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
esp_err_t ana_cmpr_set_cross_type(ana_cmpr_handle_t cmpr, ana_cmpr_cross_type_t cross_type)
|
||||
{
|
||||
#if ANALOG_CMPR_LL_SUPPORT(EDGE_TYPE)
|
||||
/* Not support to set the cross type after initialized, because it relies on the interrupt types to distinguish the edge,
|
||||
* i.e. have to re-allocate the interrupt to change the cross type */
|
||||
(void)cmpr;
|
||||
(void)cross_type;
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
if (cmpr == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (cross_type < ANA_CMPR_CROSS_DISABLE || cross_type > ANA_CMPR_CROSS_ANY) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
portENTER_CRITICAL_SAFE(&s_spinlock);
|
||||
analog_cmpr_ll_set_intr_cross_type(cmpr->dev, cross_type);
|
||||
// each source channel's cross type can contribute different mask to the unit's intr_mask
|
||||
cmpr->intr_mask |= analog_cmpr_ll_get_intr_mask_by_type(cmpr->unit_id, 0, cross_type);
|
||||
portEXIT_CRITICAL_SAFE(&s_spinlock);
|
||||
|
||||
return ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
esp_err_t ana_cmpr_get_gpio(ana_cmpr_unit_t unit, ana_cmpr_channel_type_t chan_type, int *gpio_num)
|
||||
{
|
||||
ANA_CMPR_NULL_POINTER_CHECK(gpio_num);
|
||||
ANA_CMPR_UNIT_CHECK(unit);
|
||||
#if ANALOG_CMPR_LL_GET(IP_VERSION) > 1
|
||||
(void)unit;
|
||||
(void)chan_type;
|
||||
(void)gpio_num;
|
||||
// the source channel and reference channel GPIO number are configurable in the new analog comparator IP.
|
||||
// we can't get the GPIO number from a fixed mapping like in the old version driver
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
if (gpio_num == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ESP_RETURN_ON_FALSE(unit >= 0 && unit < ANALOG_CMPR_LL_GET(INST_NUM), ESP_ERR_INVALID_ARG, TAG, "invalid unit id");
|
||||
|
||||
/* Get the gpio number according to the channel type */
|
||||
switch (chan_type) {
|
||||
@@ -377,20 +435,5 @@ esp_err_t ana_cmpr_get_gpio(ana_cmpr_unit_t unit, ana_cmpr_channel_type_t chan_t
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
ana_cmpr_unit_t ana_cmpr_get_unit_id(ana_cmpr_handle_t cmpr)
|
||||
{
|
||||
if (!cmpr) {
|
||||
return -1;
|
||||
}
|
||||
return cmpr->unit;
|
||||
}
|
||||
|
||||
#if CONFIG_ANA_CMPR_ENABLE_DEBUG_LOG
|
||||
__attribute__((constructor))
|
||||
static void ana_cmpr_override_default_log_level(void)
|
||||
{
|
||||
esp_log_level_set(TAG, ESP_LOG_DEBUG);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -23,17 +23,16 @@ static esp_err_t ana_cmpr_del_etm_event(esp_etm_event_handle_t base_event)
|
||||
|
||||
esp_err_t ana_cmpr_new_etm_event(ana_cmpr_handle_t cmpr, const ana_cmpr_etm_event_config_t *config, esp_etm_event_handle_t *ret_event)
|
||||
{
|
||||
ana_cmpr_unit_t unit = ana_cmpr_get_unit_id(cmpr);
|
||||
ESP_RETURN_ON_FALSE(((int)unit) >= 0, ESP_ERR_INVALID_ARG, TAG, "invalid analog comparator handle");
|
||||
ESP_RETURN_ON_FALSE(config && ret_event, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
||||
ESP_RETURN_ON_FALSE(cmpr && config && ret_event, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
||||
ana_cmpr_unit_t unit_id = cmpr->unit_id;
|
||||
ana_cmpr_etm_event_t *event = heap_caps_calloc(1, sizeof(ana_cmpr_etm_event_t), ETM_MEM_ALLOC_CAPS);
|
||||
ESP_RETURN_ON_FALSE(event, ESP_ERR_NO_MEM, TAG, "no mem for analog comparator event");
|
||||
|
||||
uint32_t event_id = ANALOG_CMPR_LL_ETM_SOURCE(unit, 0, config->event_type);
|
||||
uint32_t event_id = ANALOG_CMPR_LL_ETM_SOURCE(unit_id, 0, config->event_type);
|
||||
event->base.del = ana_cmpr_del_etm_event;
|
||||
event->base.event_id = event_id;
|
||||
event->base.trig_periph = ETM_TRIG_PERIPH_ANA_CMPR;
|
||||
ESP_LOGD(TAG, "new event @%p, event_id=%"PRIu32", unit_id=%d", event, event_id, unit);
|
||||
ESP_LOGD(TAG, "new event @%p, event_id=%"PRIu32", unit_id=%d", event, event_id, unit_id);
|
||||
*ret_event = &event->base;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "esp_check.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "esp_pm.h"
|
||||
#include "driver/ana_cmpr_types.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "hal/ana_cmpr_ll.h"
|
||||
@@ -55,13 +56,41 @@ typedef enum {
|
||||
ANA_CMPR_FSM_WAIT, // Comparator is in the middle of state change, so busy, other operations should wait
|
||||
} ana_cmpr_fsm_t;
|
||||
|
||||
/**
|
||||
* @brief Get the analog comparator unit id from the handle
|
||||
*
|
||||
* @param cmpr The handle of the analog comparator unit
|
||||
* @return The id of the analog comparator unit
|
||||
*/
|
||||
ana_cmpr_unit_t ana_cmpr_get_unit_id(ana_cmpr_handle_t cmpr);
|
||||
typedef struct ana_cmpr_t ana_cmpr_t;
|
||||
|
||||
typedef struct ana_cmpr_ref_chan_t {
|
||||
ana_cmpr_ref_source_t ref_src; // the reference source type of this reference channel, internal or external
|
||||
// for internal reference channel
|
||||
ana_cmpr_ref_voltage_t ref_volt;
|
||||
ana_cmpr_ref_hys_t ref_hys_level;
|
||||
// for external reference channel
|
||||
int gpio_num;
|
||||
uint32_t pad_id; // the pad id corresponding to the gpio_num
|
||||
} ana_cmpr_ref_chan_t;
|
||||
|
||||
typedef struct ana_cmpr_src_chan_t {
|
||||
uint8_t chan_id; // there're multiple source channels in the analog comparator unit
|
||||
ana_cmpr_cross_type_t cross_type;
|
||||
int gpio_num;
|
||||
uint32_t pad_id; // the pad id corresponding to the gpio_num
|
||||
} ana_cmpr_src_chan_t;
|
||||
|
||||
struct ana_cmpr_t {
|
||||
int unit_id; // Analog comparator unit id
|
||||
analog_cmpr_dev_t *dev; // Analog comparator unit device address
|
||||
_Atomic ana_cmpr_fsm_t fsm; // The state machine of the Analog Comparator unit
|
||||
ana_cmpr_event_callbacks_t cbs; // The callback group that set by user
|
||||
void *user_data; // User data that passed to the callbacks
|
||||
intr_handle_t intr_handle; // Interrupt handle
|
||||
uint32_t intr_mask; // Interrupt mask
|
||||
int intr_priority; // Interrupt priority
|
||||
uint32_t src_clk_freq_hz; // Source clock frequency of the Analog Comparator unit
|
||||
ana_cmpr_src_chan_t src_chans[ANALOG_CMPR_LL_GET(SRC_CHANNEL_NUM)]; // The source channel objects in the unit
|
||||
ana_cmpr_ref_chan_t ref_chan; // The reference channel object in the unit
|
||||
#if CONFIG_PM_ENABLE
|
||||
esp_pm_lock_handle_t pm_lock; // The Power Management lock that used to avoid unexpected power down of the clock domain
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -50,16 +50,6 @@ typedef struct {
|
||||
otherwise the next cross event may be missed. */
|
||||
} ana_cmpr_debounce_config_t;
|
||||
|
||||
/**
|
||||
* @brief Group of Analog Comparator callbacks
|
||||
* @note The callbacks are all running under ISR environment
|
||||
* @note When CONFIG_ANA_CMPR_ISR_CACHE_SAFE is enabled, the callback itself and functions called by it should be placed in IRAM.
|
||||
* The variables used in the function should be in the SRAM as well.
|
||||
*/
|
||||
typedef struct {
|
||||
ana_cmpr_cross_cb_t on_cross; /*!< The callback function on cross interrupt */
|
||||
} ana_cmpr_event_callbacks_t;
|
||||
|
||||
/**
|
||||
* @brief Allocating a new analog comparator unit handle
|
||||
*
|
||||
@@ -94,9 +84,9 @@ esp_err_t ana_cmpr_del_unit(ana_cmpr_handle_t cmpr);
|
||||
* @param[in] cmpr The handle of analog comparator unit
|
||||
* @param[in] ref_cfg Internal reference configuration
|
||||
* @return
|
||||
* - ESP_OK Set denounce configuration success
|
||||
* - ESP_OK Set internal reference configuration success
|
||||
* - ESP_ERR_INVALID_ARG NULL pointer of the parameters
|
||||
* - ESP_ERR_INVALID_STATE The reference source is not `ANA_CMPR_REF_SRC_INTERNAL`
|
||||
* - ESP_ERR_NOT_ALLOWED Set the reference voltage for external reference channel is not allowed
|
||||
*/
|
||||
esp_err_t ana_cmpr_set_internal_reference(ana_cmpr_handle_t cmpr, const ana_cmpr_internal_ref_config_t *ref_cfg);
|
||||
|
||||
@@ -109,7 +99,7 @@ esp_err_t ana_cmpr_set_internal_reference(ana_cmpr_handle_t cmpr, const ana_cmpr
|
||||
* @param[in] cmpr The handle of analog comparator unit
|
||||
* @param[in] dbc_cfg Debounce configuration
|
||||
* @return
|
||||
* - ESP_OK Set denounce configuration success
|
||||
* - ESP_OK Set debounce configuration success
|
||||
* - ESP_ERR_INVALID_ARG NULL pointer of the parameters
|
||||
*/
|
||||
esp_err_t ana_cmpr_set_debounce(ana_cmpr_handle_t cmpr, const ana_cmpr_debounce_config_t *dbc_cfg);
|
||||
@@ -123,7 +113,7 @@ esp_err_t ana_cmpr_set_debounce(ana_cmpr_handle_t cmpr, const ana_cmpr_debounce_
|
||||
* @param[in] cmpr The handle of analog comparator unit
|
||||
* @param[in] cross_type The source signal cross type that can trigger the interrupt
|
||||
* @return
|
||||
* - ESP_OK Set denounce configuration success
|
||||
* - ESP_OK Set cross type configuration success
|
||||
* - ESP_ERR_INVALID_ARG NULL pointer of the parameters
|
||||
*/
|
||||
esp_err_t ana_cmpr_set_cross_type(ana_cmpr_handle_t cmpr, ana_cmpr_cross_type_t cross_type);
|
||||
|
||||
@@ -42,6 +42,7 @@ typedef struct {
|
||||
ana_cmpr_cross_type_t cross_type; /*!< The cross type of the target signal to the reference signal.
|
||||
* Will either be ANA_CMPR_CROSS_POS or ANA_CMPR_CROSS_NEG
|
||||
* Always be ANA_CMPR_CROSS_ANY if target does not support independent interrupt (like ESP32H2) */
|
||||
int src_chan_id; /*!< The source channel index that triggers the interrupt, valid when the target supports multiple source channels */
|
||||
} ana_cmpr_cross_event_data_t;
|
||||
|
||||
/**
|
||||
@@ -55,6 +56,16 @@ typedef struct {
|
||||
*/
|
||||
typedef bool (*ana_cmpr_cross_cb_t)(ana_cmpr_handle_t cmpr, const ana_cmpr_cross_event_data_t *edata, void *user_ctx);
|
||||
|
||||
/**
|
||||
* @brief Group of Analog Comparator callbacks
|
||||
* @note The callbacks are all running under ISR environment
|
||||
* @note When CONFIG_ANA_CMPR_ISR_CACHE_SAFE is enabled, the callback itself and functions called by it should be placed in IRAM.
|
||||
* The variables used in the function should be in the SRAM as well.
|
||||
*/
|
||||
typedef struct {
|
||||
ana_cmpr_cross_cb_t on_cross; /*!< The callback function on cross interrupt */
|
||||
} ana_cmpr_event_callbacks_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -44,7 +44,7 @@ TEST_CASE("ana_cmpr unit install/uninstall", "[ana_cmpr]")
|
||||
/* Try to set internal reference for a external unit */
|
||||
config.ref_src = ANA_CMPR_REF_SRC_EXTERNAL;
|
||||
TEST_ESP_OK(ana_cmpr_new_unit(&config, &cmpr));
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
|
||||
TEST_ESP_ERR(ESP_ERR_NOT_ALLOWED, ana_cmpr_set_internal_reference(cmpr, &ref_cfg));
|
||||
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, ana_cmpr_del_unit(NULL));
|
||||
TEST_ESP_OK(ana_cmpr_del_unit(cmpr));
|
||||
}
|
||||
|
||||
@@ -37,11 +37,10 @@ extern "C" {
|
||||
|
||||
#define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit])
|
||||
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 2))
|
||||
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) 0x01
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan) 0x02
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) 0x07
|
||||
|
||||
#define ANALOG_CMPR_LL_ETM_SOURCE(unit, src_chan, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type))
|
||||
|
||||
@@ -59,13 +58,13 @@ static inline uint32_t analog_cmpr_ll_get_intr_mask_by_type(uint32_t unit_id, ui
|
||||
uint32_t mask = 0;
|
||||
switch (type) {
|
||||
case ANA_CMPR_CROSS_POS:
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_NEG:
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_ANY:
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -160,7 +159,7 @@ static inline void analog_cmpr_ll_enable_intr(analog_cmpr_dev_t *hw, uint32_t ma
|
||||
__attribute__((always_inline))
|
||||
static inline uint32_t analog_cmpr_ll_get_intr_status(analog_cmpr_dev_t *hw)
|
||||
{
|
||||
return hw->int_st->val;
|
||||
return (hw->int_st->val) & ANALOG_CMPR_LL_ALL_INTR_MASK(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,11 +37,10 @@ extern "C" {
|
||||
|
||||
#define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit])
|
||||
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 2))
|
||||
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) 0x01
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan) 0x02
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) 0x07
|
||||
|
||||
#define ANALOG_CMPR_LL_ETM_SOURCE(unit, src_chan, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type))
|
||||
|
||||
@@ -59,13 +58,13 @@ static inline uint32_t analog_cmpr_ll_get_intr_mask_by_type(uint32_t unit_id, ui
|
||||
uint32_t mask = 0;
|
||||
switch (type) {
|
||||
case ANA_CMPR_CROSS_POS:
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_NEG:
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_ANY:
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -160,7 +159,7 @@ static inline void analog_cmpr_ll_enable_intr(analog_cmpr_dev_t *hw, uint32_t ma
|
||||
__attribute__((always_inline))
|
||||
static inline uint32_t analog_cmpr_ll_get_intr_status(analog_cmpr_dev_t *hw)
|
||||
{
|
||||
return hw->int_st->val;
|
||||
return hw->int_st->val & ANALOG_CMPR_LL_ALL_INTR_MASK(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,9 +28,9 @@
|
||||
#define ANALOG_CMPR_LL_SRC_CHANNEL_NUM 1
|
||||
|
||||
#define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit])
|
||||
#define ANALOG_CMPR_LL_EVENT_CROSS (1 << 0)
|
||||
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_EVENT_CROSS)
|
||||
#define ANALOG_CMPR_LL_CROSS_INTR_MASK 0x01
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) ANALOG_CMPR_LL_CROSS_INTR_MASK
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -50,7 +50,7 @@ static inline uint32_t analog_cmpr_ll_get_intr_mask_by_type(uint32_t unit_id, ui
|
||||
(void)type;
|
||||
(void)unit_id;
|
||||
(void)src_chan;
|
||||
return ANALOG_CMPR_LL_EVENT_CROSS;
|
||||
return ANALOG_CMPR_LL_CROSS_INTR_MASK;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,7 +156,7 @@ static inline void analog_cmpr_ll_enable_intr(analog_cmpr_dev_t *hw, uint32_t ma
|
||||
__attribute__((always_inline))
|
||||
static inline uint32_t analog_cmpr_ll_get_intr_status(analog_cmpr_dev_t *hw)
|
||||
{
|
||||
return hw->int_st->val;
|
||||
return hw->int_st->val & ANALOG_CMPR_LL_ALL_INTR_MASK(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -36,11 +36,10 @@ extern "C" {
|
||||
|
||||
#define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit])
|
||||
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 2))
|
||||
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) 0x01
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan) 0x02
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) 0x07
|
||||
|
||||
/**
|
||||
* @brief Get the interrupt mask from the given cross type
|
||||
@@ -56,13 +55,13 @@ static inline uint32_t analog_cmpr_ll_get_intr_mask_by_type(uint32_t unit_id, ui
|
||||
uint32_t mask = 0;
|
||||
switch (type) {
|
||||
case ANA_CMPR_CROSS_POS:
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_NEG:
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_ANY:
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -157,7 +156,7 @@ static inline void analog_cmpr_ll_enable_intr(analog_cmpr_dev_t *hw, uint32_t ma
|
||||
__attribute__((always_inline))
|
||||
static inline uint32_t analog_cmpr_ll_get_intr_status(analog_cmpr_dev_t *hw)
|
||||
{
|
||||
return hw->int_st->val;
|
||||
return hw->int_st->val & ANALOG_CMPR_LL_ALL_INTR_MASK(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,11 +41,10 @@ typedef zero_dev_t analog_cmpr_dev_t;
|
||||
|
||||
#define ANALOG_CMPR_LL_GET_HW(unit) (&ZERO_DET)
|
||||
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 2))
|
||||
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) 0x1FF
|
||||
|
||||
#define ANALOG_CMPR_LL_ETM_SOURCE(unit, src_chan, type) (ZERO_DET_EVT_CHANNEL_1_POS + (src_chan) + ((type) * ANALOG_CMPR_LL_SRC_CHANNEL_NUM))
|
||||
|
||||
@@ -143,13 +142,13 @@ static inline uint32_t analog_cmpr_ll_get_intr_mask_by_type(uint32_t unit_id, ui
|
||||
uint32_t mask = 0;
|
||||
switch (type) {
|
||||
case ANA_CMPR_CROSS_POS:
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_NEG:
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_ANY:
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -244,7 +243,7 @@ static inline void analog_cmpr_ll_enable_intr(analog_cmpr_dev_t *hw, uint32_t ma
|
||||
__attribute__((always_inline))
|
||||
static inline uint32_t analog_cmpr_ll_get_intr_status(analog_cmpr_dev_t *hw)
|
||||
{
|
||||
return hw->det_int_st.val;
|
||||
return hw->det_int_st.val & ANALOG_CMPR_LL_ALL_INTR_MASK(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,11 +38,10 @@ extern "C" {
|
||||
#define ANALOG_CMPR_LL_GET_HW(unit) (&ANALOG_CMPR[unit])
|
||||
#define ANALOG_CMPR_LL_GET_UNIT(hw) ((hw) == (&ANALOG_CMPR[0]) ? 0 : 1)
|
||||
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan) (1UL << ((unit) * 3 + 2))
|
||||
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) (1UL << ((unit) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan) (1UL << ((unit) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) (0x07 << ((unit) * 3))
|
||||
|
||||
#define ANALOG_CMPR_LL_ETM_SOURCE(unit, src_chan, type) (GPIO_EVT_ZERO_DET_POS0 + (unit) * 2 + (type))
|
||||
|
||||
@@ -60,13 +59,13 @@ static inline uint32_t analog_cmpr_ll_get_intr_mask_by_type(uint32_t unit_id, ui
|
||||
uint32_t mask = 0;
|
||||
switch (type) {
|
||||
case ANA_CMPR_CROSS_POS:
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_NEG:
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_ANY:
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -161,7 +160,8 @@ static inline void analog_cmpr_ll_enable_intr(analog_cmpr_dev_t *hw, uint32_t ma
|
||||
__attribute__((always_inline))
|
||||
static inline uint32_t analog_cmpr_ll_get_intr_status(analog_cmpr_dev_t *hw)
|
||||
{
|
||||
return hw->int_st->val;
|
||||
int unit_id = ANALOG_CMPR_LL_GET_UNIT(hw);
|
||||
return hw->int_st->val & ANALOG_CMPR_LL_ALL_INTR_MASK(unit_id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,11 +41,10 @@ typedef zero_det_dev_t analog_cmpr_dev_t;
|
||||
|
||||
#define ANALOG_CMPR_LL_GET_HW(unit) (&ZERO_DET)
|
||||
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 2))
|
||||
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_MASK(unit, src_chan) | ANALOG_CMPR_LL_ANY_CROSS_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 0))
|
||||
#define ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan) (1UL << ((2 - (src_chan)) * 3 + 1))
|
||||
#define ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit, src_chan) (ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit, src_chan) | ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit, src_chan))
|
||||
#define ANALOG_CMPR_LL_ALL_INTR_MASK(unit) 0x1FF
|
||||
|
||||
#define ANALOG_CMPR_LL_ETM_SOURCE(unit, src_chan, type) (ZERO_DET_EVT_CHANNEL_1_POS + (src_chan) + ((type) * ANALOG_CMPR_LL_SRC_CHANNEL_NUM))
|
||||
|
||||
@@ -143,13 +142,13 @@ static inline uint32_t analog_cmpr_ll_get_intr_mask_by_type(uint32_t unit_id, ui
|
||||
uint32_t mask = 0;
|
||||
switch (type) {
|
||||
case ANA_CMPR_CROSS_POS:
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_POS_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_NEG:
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_NEG_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
case ANA_CMPR_CROSS_ANY:
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_MASK(unit_id, src_chan);
|
||||
mask |= ANALOG_CMPR_LL_ANY_CROSS_INTR_MASK(unit_id, src_chan);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -244,7 +243,7 @@ static inline void analog_cmpr_ll_enable_intr(analog_cmpr_dev_t *hw, uint32_t ma
|
||||
__attribute__((always_inline))
|
||||
static inline uint32_t analog_cmpr_ll_get_intr_status(analog_cmpr_dev_t *hw)
|
||||
{
|
||||
return hw->int_st.val;
|
||||
return hw->int_st.val & ANALOG_CMPR_LL_ALL_INTR_MASK(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user