diff --git a/components/driver/include/driver/mcpwm_cap.h b/components/driver/include/driver/mcpwm_cap.h index d40ef2fd1a..f1bb030720 100644 --- a/components/driver/include/driver/mcpwm_cap.h +++ b/components/driver/include/driver/mcpwm_cap.h @@ -94,6 +94,18 @@ esp_err_t mcpwm_capture_timer_start(mcpwm_cap_timer_handle_t cap_timer); */ esp_err_t mcpwm_capture_timer_stop(mcpwm_cap_timer_handle_t cap_timer); +/** + * @brief Get MCPWM capture timer resolution, in Hz + * + * @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()` + * @param[out] out_resolution Returned capture timer resolution, in Hz + * @return + * - ESP_OK: Get capture timer resolution successfully + * - ESP_ERR_INVALID_ARG: Get capture timer resolution failed because of invalid argument + * - ESP_FAIL: Get capture timer resolution failed because of other error + */ +esp_err_t mcpwm_capture_timer_get_resolution(mcpwm_cap_timer_handle_t cap_timer, uint32_t *out_resolution); + /** * @brief MCPWM Capture timer sync phase configuration */ diff --git a/components/driver/mcpwm/mcpwm_cap.c b/components/driver/mcpwm/mcpwm_cap.c index ef6e3b2c2a..7a80545c54 100644 --- a/components/driver/mcpwm/mcpwm_cap.c +++ b/components/driver/mcpwm/mcpwm_cap.c @@ -185,6 +185,13 @@ esp_err_t mcpwm_capture_timer_stop(mcpwm_cap_timer_handle_t cap_timer) return ESP_OK; } +esp_err_t mcpwm_capture_timer_get_resolution(mcpwm_cap_timer_handle_t cap_timer, uint32_t *out_resolution) +{ + ESP_RETURN_ON_FALSE(cap_timer && out_resolution, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); + *out_resolution = cap_timer->resolution_hz; + return ESP_OK; +} + static esp_err_t mcpwm_capture_channel_register_to_timer(mcpwm_cap_channel_t *cap_channel, mcpwm_cap_timer_t *cap_timer) { int cap_chan_id = -1; diff --git a/components/driver/test_apps/mcpwm/main/test_mcpwm_cap.c b/components/driver/test_apps/mcpwm/main/test_mcpwm_cap.c index 911f2e8dcc..7da9603480 100644 --- a/components/driver/test_apps/mcpwm/main/test_mcpwm_cap.c +++ b/components/driver/test_apps/mcpwm/main/test_mcpwm_cap.c @@ -74,7 +74,7 @@ TEST_CASE("mcpwm_capture_ext_gpio", "[mcpwm]") printf("install mcpwm capture timer\r\n"); mcpwm_cap_timer_handle_t cap_timer = NULL; mcpwm_capture_timer_config_t cap_timer_config = { - .clk_src = MCPWM_CAPTURE_CLK_SRC_APB, + .clk_src = MCPWM_CAPTURE_CLK_SRC_DEFAULT, .group_id = 0, }; TEST_ESP_OK(mcpwm_new_capture_timer(&cap_timer_config, &cap_timer)); @@ -115,8 +115,8 @@ TEST_CASE("mcpwm_capture_ext_gpio", "[mcpwm]") gpio_set_level(cap_gpio, 0); vTaskDelay(pdMS_TO_TICKS(100)); printf("capture value: Pos=%"PRIu32", Neg=%"PRIu32"\r\n", cap_value[0], cap_value[1]); - // Capture timer is clocked from APB by default - uint32_t clk_src_res = esp_clk_apb_freq(); + uint32_t clk_src_res; + mcpwm_capture_timer_get_resolution(cap_timer, &clk_src_res); TEST_ASSERT_UINT_WITHIN(100000, clk_src_res / 10, cap_value[1] - cap_value[0]); printf("uninstall capture channel and timer\r\n"); @@ -183,8 +183,8 @@ TEST_CASE("mcpwm_capture_software_catch", "[mcpwm]") TEST_ASSERT_EQUAL(2, test_callback_data.cap_data_index); uint32_t delta = test_callback_data.cap_data[1] - test_callback_data.cap_data[0]; esp_rom_printf("duration=%u ticks\r\n", delta); - // Capture timer is clocked from APB by default - uint32_t clk_src_res = esp_clk_apb_freq(); + uint32_t clk_src_res; + mcpwm_capture_timer_get_resolution(cap_timer, &clk_src_res); TEST_ASSERT_UINT_WITHIN(80000, clk_src_res / 100, delta); printf("uninstall capture channel and timer\r\n"); diff --git a/docs/en/api-reference/peripherals/mcpwm.rst b/docs/en/api-reference/peripherals/mcpwm.rst index 4760534731..d9e5032f7d 100644 --- a/docs/en/api-reference/peripherals/mcpwm.rst +++ b/docs/en/api-reference/peripherals/mcpwm.rst @@ -806,7 +806,7 @@ The MCPWM capture channel can inform the user when there's a valid edge detected - :cpp:member:`mcpwm_capture_event_callbacks_t::on_cap` sets callback function for the capture channel when a valid edge is detected. -The callback function will provide event specific data of type :cpp:type:`mcpwm_capture_event_data_t`, so that you can get the the edge of the capture signal in :cpp:member:`mcpwm_capture_event_data_t::cap_edge` and the count value of that moment in :cpp:member:`mcpwm_capture_event_data_t::cap_value`. +The callback function will provide event specific data of type :cpp:type:`mcpwm_capture_event_data_t`, so that you can get the edge of the capture signal in :cpp:member:`mcpwm_capture_event_data_t::cap_edge` and the count value of that moment in :cpp:member:`mcpwm_capture_event_data_t::cap_value`. To convert the capture count into timestamp, you need to know the resolution of the capture timer by calling :cpp:func:`mcpwm_capture_timer_get_resolution`. The callback function is called within the ISR context, so is should **not** attempt to block (e.g., make sure that only FreeRTOS APIs with ``ISR`` suffix is called within the function).