From 47bf0ef21295f673d516a63409c5edbf1c607575 Mon Sep 17 00:00:00 2001 From: morris Date: Mon, 29 Aug 2022 15:18:06 +0800 Subject: [PATCH] mcpwm: fix multiplication overflow in converting us to compare ticks Closes https://github.com/espressif/esp-idf/issues/9648 --- components/driver/deprecated/mcpwm_legacy.c | 6 ++++-- components/driver/include/driver/mcpwm_types.h | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/components/driver/deprecated/mcpwm_legacy.c b/components/driver/deprecated/mcpwm_legacy.c index c7aeaa944c..f2e52fa4b3 100644 --- a/components/driver/deprecated/mcpwm_legacy.c +++ b/components/driver/deprecated/mcpwm_legacy.c @@ -298,9 +298,11 @@ esp_err_t mcpwm_set_duty_in_us(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_critical_enter(mcpwm_num); int real_group_prescale = mcpwm_ll_group_get_clock_prescale(hal->dev); - unsigned long int real_timer_clk_hz = + // to avid multiplication overflow, use uint64_t here + uint64_t real_timer_clk_hz = MCPWM_GROUP_CLK_SRC_HZ / real_group_prescale / mcpwm_ll_timer_get_clock_prescale(hal->dev, timer_num); - mcpwm_ll_operator_set_compare_value(hal->dev, op, cmp, duty_in_us * real_timer_clk_hz / 1000000); + uint64_t compare_val = real_timer_clk_hz * duty_in_us / 1000000; + mcpwm_ll_operator_set_compare_value(hal->dev, op, cmp, (uint32_t)compare_val); mcpwm_ll_operator_enable_update_compare_on_tez(hal->dev, op, cmp, true); mcpwm_critical_exit(mcpwm_num); return ESP_OK; diff --git a/components/driver/include/driver/mcpwm_types.h b/components/driver/include/driver/mcpwm_types.h index d50ba3c36b..fcd736ae3e 100644 --- a/components/driver/include/driver/mcpwm_types.h +++ b/components/driver/include/driver/mcpwm_types.h @@ -98,11 +98,11 @@ typedef struct { * @brief MCPWM fault event callback function * * @param fault MCPWM fault handle - * @param ev_data MCPWM fault event data, fed by driver + * @param edata MCPWM fault event data, fed by driver * @param user_ctx User data, set in `mcpwm_fault_register_event_callbacks()` * @return whether a task switch is needed after the callback returns */ -typedef bool (*mcpwm_fault_event_cb_t)(mcpwm_fault_handle_t fault, const mcpwm_fault_event_data_t *ev_data, void *user_ctx); +typedef bool (*mcpwm_fault_event_cb_t)(mcpwm_fault_handle_t fault, const mcpwm_fault_event_data_t *edata, void *user_ctx); /** * @brief MCPWM compare event data @@ -134,7 +134,7 @@ typedef struct { * @brief MCPWM capture event callback function * * @param cap_channel MCPWM capture channel handle - * @param ev_data MCPWM capture event data, fed by driver + * @param edata MCPWM capture event data, fed by driver * @param user_ctx User data, set in `mcpwm_capture_channel_register_event_callbacks()` * @return Whether a high priority task has been waken up by this function */