From 59f8008e22ee354e6f0dd9e0eb0b01ac6dc60f64 Mon Sep 17 00:00:00 2001 From: "Planck (Lu Zeyu)" Date: Tue, 12 Sep 2023 12:28:56 +0800 Subject: [PATCH] feat(pcnt): replace periph_module func with new ll func --- components/driver/deprecated/pcnt_legacy.c | 13 +++++-- components/driver/pcnt/pulse_cnt.c | 16 +++++++-- components/hal/esp32/include/hal/pcnt_ll.h | 35 +++++++++++++++++++ components/hal/esp32c6/include/hal/pcnt_ll.h | 22 ++++++++++++ components/hal/esp32h2/include/hal/pcnt_ll.h | 22 ++++++++++++ components/hal/esp32p4/include/hal/pcnt_ll.h | 30 ++++++++++++++++ components/hal/esp32s2/include/hal/pcnt_ll.h | 36 ++++++++++++++++++++ components/hal/esp32s3/include/hal/pcnt_ll.h | 30 ++++++++++++++++ 8 files changed, 199 insertions(+), 5 deletions(-) diff --git a/components/driver/deprecated/pcnt_legacy.c b/components/driver/deprecated/pcnt_legacy.c index b0ecdbf9bf..630fced4f3 100644 --- a/components/driver/deprecated/pcnt_legacy.c +++ b/components/driver/deprecated/pcnt_legacy.c @@ -31,6 +31,13 @@ #define PCNT_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux) #define PCNT_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux) +#if !SOC_RCC_IS_INDEPENDENT +#define PCNT_RCC_ATOMIC() PERIPH_RCC_ATOMIC() +#else +#define PCNT_RCC_ATOMIC() +#endif + + static const char *TAG = "pcnt(legacy)"; #define PCNT_CHECK(a, str, ret_val) ESP_RETURN_ON_FALSE(a, ret_val, TAG, "%s", str) @@ -365,10 +372,12 @@ static inline esp_err_t _pcnt_unit_config(pcnt_port_t pcnt_port, const pcnt_conf /*Enalbe hardware module*/ static bool pcnt_enable = false; if (pcnt_enable == false) { - periph_module_reset(pcnt_periph_signals.groups[pcnt_port].module); + PCNT_RCC_ATOMIC() { + pcnt_ll_reset_register(pcnt_port); + pcnt_ll_enable_bus_clock(pcnt_port, true); + } pcnt_enable = true; } - periph_module_enable(pcnt_periph_signals.groups[pcnt_port].module); /*Set counter range*/ _pcnt_set_event_value(pcnt_port, unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim); _pcnt_set_event_value(pcnt_port, unit, PCNT_EVT_L_LIM, pcnt_config->counter_l_lim); diff --git a/components/driver/pcnt/pulse_cnt.c b/components/driver/pcnt/pulse_cnt.c index c8dee2346e..fc2c0a709e 100644 --- a/components/driver/pcnt/pulse_cnt.c +++ b/components/driver/pcnt/pulse_cnt.c @@ -50,6 +50,12 @@ #define PCNT_PM_LOCK_NAME_LEN_MAX 16 +#if !SOC_RCC_IS_INDEPENDENT +#define PCNT_RCC_ATOMIC() PERIPH_RCC_ATOMIC() +#else +#define PCNT_RCC_ATOMIC() +#endif + static const char *TAG = "pcnt"; typedef struct pcnt_platform_t pcnt_platform_t; @@ -763,8 +769,10 @@ static pcnt_group_t *pcnt_acquire_group_handle(int group_id) group->intr_priority = -1; group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED; // enable APB access pcnt registers - periph_module_enable(pcnt_periph_signals.groups[group_id].module); - periph_module_reset(pcnt_periph_signals.groups[group_id].module); + PCNT_RCC_ATOMIC() { + pcnt_ll_enable_bus_clock(group_id, true); + pcnt_ll_reset_register(group_id); + } // initialize HAL context pcnt_hal_init(&group->hal, group_id); } @@ -795,7 +803,9 @@ static void pcnt_release_group_handle(pcnt_group_t *group) assert(s_platform.groups[group_id]); do_deinitialize = true; s_platform.groups[group_id] = NULL; // deregister from platform - periph_module_disable(pcnt_periph_signals.groups[group_id].module); + PCNT_RCC_ATOMIC() { + pcnt_ll_enable_bus_clock(group_id, false); + } } _lock_release(&s_platform.mutex); diff --git a/components/hal/esp32/include/hal/pcnt_ll.h b/components/hal/esp32/include/hal/pcnt_ll.h index 5e6a8b3660..e54e97b050 100644 --- a/components/hal/esp32/include/hal/pcnt_ll.h +++ b/components/hal/esp32/include/hal/pcnt_ll.h @@ -21,6 +21,8 @@ #include "soc/pcnt_struct.h" #include "hal/pcnt_types.h" #include "hal/misc.h" +#include "soc/dport_access.h" +#include "soc/dport_reg.h" #ifdef __cplusplus extern "C" { @@ -437,6 +439,39 @@ static inline volatile void *pcnt_ll_get_intr_status_reg(pcnt_dev_t *hw) return &hw->int_st.val; } +/** + * @brief Enable or disable the bus clock for the PCNT module + * + * @param set_bit True to set bit, false to clear bit + */ +static inline void pcnt_ll_enable_bus_clock(int group_id, bool enable) +{ + (void)group_id; + if (enable) { + DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_PCNT_CLK_EN); + } else { + DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_PCNT_CLK_EN); + } +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the PCNT module + */ +static inline void pcnt_ll_reset_register(int group_id) +{ + (void)group_id; + DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_PCNT_RST); + DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_PCNT_RST); +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_reset_register(__VA_ARGS__) + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32c6/include/hal/pcnt_ll.h b/components/hal/esp32c6/include/hal/pcnt_ll.h index b597087fb4..ade48358ae 100644 --- a/components/hal/esp32c6/include/hal/pcnt_ll.h +++ b/components/hal/esp32c6/include/hal/pcnt_ll.h @@ -19,6 +19,7 @@ #include #include "soc/pcnt_struct.h" #include "hal/pcnt_types.h" +#include "soc/pcr_struct.h" #ifdef __cplusplus extern "C" { @@ -435,6 +436,27 @@ static inline volatile void *pcnt_ll_get_intr_status_reg(pcnt_dev_t *hw) return &hw->int_st.val; } +/** + * @brief Enable or disable the bus clock for the PCNT module + * + * @param set_bit True to set bit, false to clear bit + */ +static inline void pcnt_ll_enable_bus_clock(int group_id, bool enable) +{ + (void)group_id; + PCR.pcnt_conf.pcnt_clk_en = enable; +} + +/** + * @brief Reset the PCNT module + */ +static inline void pcnt_ll_reset_register(int group_id) +{ + (void)group_id; + PCR.pcnt_conf.pcnt_rst_en = 1; + PCR.pcnt_conf.pcnt_rst_en = 0; +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32h2/include/hal/pcnt_ll.h b/components/hal/esp32h2/include/hal/pcnt_ll.h index 0b393aee67..17c4b96212 100644 --- a/components/hal/esp32h2/include/hal/pcnt_ll.h +++ b/components/hal/esp32h2/include/hal/pcnt_ll.h @@ -19,6 +19,7 @@ #include #include "soc/pcnt_struct.h" #include "hal/pcnt_types.h" +#include "soc/pcr_struct.h" #ifdef __cplusplus extern "C" { @@ -435,6 +436,27 @@ static inline volatile void *pcnt_ll_get_intr_status_reg(pcnt_dev_t *hw) return &hw->int_st.val; } +/** + * @brief Enable or disable the bus clock for the PCNT module + * + * @param set_bit True to set bit, false to clear bit + */ +static inline void pcnt_ll_enable_bus_clock(int group_id, bool enable) +{ + (void)group_id; + PCR.pcnt_conf.pcnt_clk_en = enable; +} + +/** + * @brief Reset the PCNT module + */ +static inline void pcnt_ll_reset_register(int group_id) +{ + (void)group_id; + PCR.pcnt_conf.pcnt_rst_en = 1; + PCR.pcnt_conf.pcnt_rst_en = 0; +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32p4/include/hal/pcnt_ll.h b/components/hal/esp32p4/include/hal/pcnt_ll.h index bc62612308..610dae51e5 100644 --- a/components/hal/esp32p4/include/hal/pcnt_ll.h +++ b/components/hal/esp32p4/include/hal/pcnt_ll.h @@ -20,6 +20,7 @@ #include "soc/pcnt_struct.h" #include "hal/pcnt_types.h" #include "hal/misc.h" +#include "soc/hp_sys_clkrst_struct.h" #ifdef __cplusplus extern "C" { @@ -476,6 +477,35 @@ static inline volatile void *pcnt_ll_get_intr_status_reg(pcnt_dev_t *hw) return &hw->int_st.val; } +/** + * @brief Enable or disable the bus clock for the PCNT module + * + * @param set_bit True to set bit, false to clear bit + */ +static inline void pcnt_ll_enable_bus_clock(int group_id, bool enable) +{ + (void)group_id; + HP_SYS_CLKRST.soc_clk_ctrl2.reg_pcnt_apb_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the PCNT module + */ +static inline void pcnt_ll_reset_register(int group_id) +{ + (void)group_id; + HP_SYS_CLKRST.hp_rst_en1.reg_rst_en_pcnt = 1; + HP_SYS_CLKRST.hp_rst_en1.reg_rst_en_pcnt = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_reset_register(__VA_ARGS__) + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32s2/include/hal/pcnt_ll.h b/components/hal/esp32s2/include/hal/pcnt_ll.h index 4ba835fbb2..fecf814652 100644 --- a/components/hal/esp32s2/include/hal/pcnt_ll.h +++ b/components/hal/esp32s2/include/hal/pcnt_ll.h @@ -19,6 +19,8 @@ #include #include "soc/pcnt_struct.h" #include "hal/pcnt_types.h" +#include "soc/dport_reg.h" +#include "soc/dport_access.h" #ifdef __cplusplus extern "C" { @@ -435,6 +437,40 @@ static inline volatile void *pcnt_ll_get_intr_status_reg(pcnt_dev_t *hw) return &hw->int_st.val; } +/** + * @brief Enable or disable the bus clock for the PCNT module + * + * @param set_bit True to set bit, false to clear bit + */ +static inline void pcnt_ll_enable_bus_clock(int group_id, bool enable) +{ + (void)group_id; + if (enable) { + DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_PCNT_CLK_EN); + } else { + DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_PCNT_CLK_EN); + } +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the PCNT module + */ +static inline void pcnt_ll_reset_register(int group_id) +{ + (void)group_id; + DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_PCNT_RST); + DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_PCNT_RST); +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_reset_register(__VA_ARGS__) + + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32s3/include/hal/pcnt_ll.h b/components/hal/esp32s3/include/hal/pcnt_ll.h index 34b2f87413..e9ec7e1a1c 100644 --- a/components/hal/esp32s3/include/hal/pcnt_ll.h +++ b/components/hal/esp32s3/include/hal/pcnt_ll.h @@ -19,6 +19,7 @@ #include #include "soc/pcnt_struct.h" #include "hal/pcnt_types.h" +#include "soc/system_struct.h" #ifdef __cplusplus extern "C" { @@ -435,6 +436,35 @@ static inline volatile void *pcnt_ll_get_intr_status_reg(pcnt_dev_t *hw) return &hw->int_st.val; } +/** + * @brief Enable or disable the bus clock for the PCNT module + * + * @param set_bit True to set bit, false to clear bit + */ +static inline void pcnt_ll_enable_bus_clock(int group_id, bool enable) +{ + (void)group_id; + SYSTEM.perip_clk_en0.pcnt_clk_en = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_enable_bus_clock(__VA_ARGS__) + +/** + * @brief Reset the PCNT module + */ +static inline void pcnt_ll_reset_register(int group_id) +{ + (void)group_id; + SYSTEM.perip_rst_en0.pcnt_rst = 1; + SYSTEM.perip_rst_en0.pcnt_rst = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define pcnt_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_reset_register(__VA_ARGS__) + #ifdef __cplusplus } #endif