From d9d316b97dd344f89f8d6950efcd75397c90092f Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Mon, 9 Sep 2024 12:43:22 +0800 Subject: [PATCH] refactor(sdm): add check and error info to the clock division --- components/esp_driver_sdm/src/sdm.c | 18 ++++++++++++++++-- components/hal/esp32/include/hal/sdm_ll.h | 4 +++- components/hal/esp32c3/include/hal/sdm_ll.h | 4 +++- components/hal/esp32c5/include/hal/sdm_ll.h | 4 +++- components/hal/esp32c6/include/hal/sdm_ll.h | 4 +++- components/hal/esp32h2/include/hal/sdm_ll.h | 4 +++- components/hal/esp32p4/include/hal/sdm_ll.h | 4 +++- components/hal/esp32s2/include/hal/sdm_ll.h | 4 +++- components/hal/esp32s3/include/hal/sdm_ll.h | 4 +++- 9 files changed, 40 insertions(+), 10 deletions(-) diff --git a/components/esp_driver_sdm/src/sdm.c b/components/esp_driver_sdm/src/sdm.c index 2856604f46..d5300a48ec 100644 --- a/components/esp_driver_sdm/src/sdm.c +++ b/components/esp_driver_sdm/src/sdm.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -25,6 +25,7 @@ #include "hal/gpio_hal.h" #include "hal/sdm_hal.h" #include "hal/sdm_ll.h" +#include "hal/hal_utils.h" #include "soc/sdm_periph.h" #include "esp_private/esp_clk.h" #include "esp_private/io_mux.h" @@ -242,7 +243,20 @@ esp_err_t sdm_new_channel(const sdm_config_t *config, sdm_channel_handle_t *ret_ chan->gpio_num = config->gpio_num; // set prescale based on sample rate - uint32_t prescale = src_clk_hz / config->sample_rate_hz; + uint32_t prescale = 0; + hal_utils_clk_info_t clk_info = { + .src_freq_hz = src_clk_hz, + .exp_freq_hz = config->sample_rate_hz, + .max_integ = SDM_LL_PRESCALE_MAX + 1, + .min_integ = 1, + .round_opt = HAL_DIV_ROUND, + }; + uint32_t actual_freq = hal_utils_calc_clk_div_integer(&clk_info, &prescale); + ESP_GOTO_ON_FALSE(actual_freq, ESP_ERR_INVALID_ARG, err, TAG, + "sample rate out of range [%"PRIu32", %"PRIu32"] Hz", src_clk_hz / SDM_LL_PRESCALE_MAX, src_clk_hz); + if (actual_freq != config->sample_rate_hz) { + ESP_LOGW(TAG, "precision loss, expected sample rate %"PRIu32" Hz runs at %"PRIu32" Hz", config->sample_rate_hz, actual_freq); + } sdm_ll_set_prescale(group->hal.dev, chan_id, prescale); chan->sample_rate_hz = src_clk_hz / prescale; // preset the duty cycle to zero diff --git a/components/hal/esp32/include/hal/sdm_ll.h b/components/hal/esp32/include/hal/sdm_ll.h index 70db7bdc2e..4708a33534 100644 --- a/components/hal/esp32/include/hal/sdm_ll.h +++ b/components/hal/esp32/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], prescale, prescale - 1); } diff --git a/components/hal/esp32c3/include/hal/sdm_ll.h b/components/hal/esp32c3/include/hal/sdm_ll.h index a705f4208b..97cf7669dd 100644 --- a/components/hal/esp32c3/include/hal/sdm_ll.h +++ b/components/hal/esp32c3/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], prescale, prescale - 1); } diff --git a/components/hal/esp32c5/include/hal/sdm_ll.h b/components/hal/esp32c5/include/hal/sdm_ll.h index 62e94affea..4f86a309d4 100644 --- a/components/hal/esp32c5/include/hal/sdm_ll.h +++ b/components/hal/esp32c5/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], sdn_prescale, prescale - 1); } diff --git a/components/hal/esp32c6/include/hal/sdm_ll.h b/components/hal/esp32c6/include/hal/sdm_ll.h index 7bdad37119..621ddf3672 100644 --- a/components/hal/esp32c6/include/hal/sdm_ll.h +++ b/components/hal/esp32c6/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], prescale, prescale - 1); } diff --git a/components/hal/esp32h2/include/hal/sdm_ll.h b/components/hal/esp32h2/include/hal/sdm_ll.h index 7bdad37119..621ddf3672 100644 --- a/components/hal/esp32h2/include/hal/sdm_ll.h +++ b/components/hal/esp32h2/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], prescale, prescale - 1); } diff --git a/components/hal/esp32p4/include/hal/sdm_ll.h b/components/hal/esp32p4/include/hal/sdm_ll.h index 7bdad37119..621ddf3672 100644 --- a/components/hal/esp32p4/include/hal/sdm_ll.h +++ b/components/hal/esp32p4/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], prescale, prescale - 1); } diff --git a/components/hal/esp32s2/include/hal/sdm_ll.h b/components/hal/esp32s2/include/hal/sdm_ll.h index b3f5d9ab22..be0d2d97f8 100644 --- a/components/hal/esp32s2/include/hal/sdm_ll.h +++ b/components/hal/esp32s2/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], prescale, prescale - 1); } diff --git a/components/hal/esp32s3/include/hal/sdm_ll.h b/components/hal/esp32s3/include/hal/sdm_ll.h index de27b3fd9b..c58724ea7a 100644 --- a/components/hal/esp32s3/include/hal/sdm_ll.h +++ b/components/hal/esp32s3/include/hal/sdm_ll.h @@ -15,6 +15,8 @@ extern "C" { #endif +#define SDM_LL_PRESCALE_MAX 256 + /** * @brief Set Sigma-delta enable * @@ -49,7 +51,7 @@ static inline void sdm_ll_set_pulse_density(gpio_sd_dev_t *hw, int channel, int8 */ static inline void sdm_ll_set_prescale(gpio_sd_dev_t *hw, int channel, uint32_t prescale) { - HAL_ASSERT(prescale && prescale <= 256); + HAL_ASSERT(prescale && prescale <= SDM_LL_PRESCALE_MAX); HAL_FORCE_MODIFY_U32_REG_FIELD(hw->channel[channel], prescale, prescale - 1); }