Page 1 of 1

Reading PWM using MCPWM

Posted: Wed Jan 22, 2025 4:50 am
by MoRoKo
I'd like to read PWM signal using MCPWM. When I compiled the program on VSC, there was an error about passing incompatible pointer type of the first parameter to mcpwm_new_capture_channel() which is a handle to a capture timer. Need help please :)

Error:
E:/Users/Me/Documents/ESP-IDF/Projects/test_readPWM/main/mymain.c: In function 'app_main':
E:/Users/Me/Documents/ESP-IDF/Projects/test_readPWM/main/mymain.c:44:47: error: passing argument 1 of 'mcpwm_new_capture_channel' from incompatible pointer type [-Wincompatible-pointer-types]
54 | ESP_ERROR_CHECK(mcpwm_new_capture_channel(timer, &cap_config, &cap_channel));
| ^~~~
| |
| mcpwm_timer_handle_t {aka
struct mcpwm_timer_t *}

Here is my code:
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "driver/mcpwm_prelude.h"
  4. #include "esp_timer.h"
  5. #include "esp_log.h"
  6. //#include "driver/mcpwm.h"
  7.  
  8. // Constants
  9. #define GPIO_INPUT_CAPTURE_PIN 18   // GPIO pin connected to the sensor
  10.  
  11. // Globals
  12. static const char *TAG = "Test_Read_PWM";
  13. volatile float read_capture_time = 0;           // store time of reading
  14.  
  15. // ISR Handler for Pulse Capture
  16. bool IRAM_ATTR mcpwm_capture_isr_handler(mcpwm_cap_channel_handle_t cap_channel, const mcpwm_capture_event_data_t *edata, void *user_data) {
  17.     read_capture_time = edata->cap_value;
  18.  
  19.     return false; // No further actions required by the ISR
  20. }
  21.  
  22. // Main Application
  23. void app_main(void) {
  24.    
  25.  
  26.     // Configure MCPWM for input capture
  27.     // Initialize MCPWM timer
  28.     mcpwm_timer_config_t timer_config = {
  29.         .group_id = 0,                         // MCPWM Group 0
  30.         .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, // Default clock source
  31.         .resolution_hz = 1000000,              // 1 MHz resolution (1 µs ticks)
  32.         .count_mode = MCPWM_TIMER_COUNT_MODE_UP
  33.     };
  34.     mcpwm_timer_handle_t timer = NULL;
  35.     ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer));
  36.  
  37.     // Initialize capture channel
  38.     mcpwm_capture_channel_config_t cap_config = {
  39.         .gpio_num = GPIO_INPUT_CAPTURE_PIN,
  40.         .prescale = 1,
  41.         .flags.pos_edge = true, // Capture on rising edge
  42.         .flags.neg_edge = false,
  43.     };
  44.     mcpwm_cap_channel_handle_t cap_channel = NULL;
  45.     ESP_ERROR_CHECK(mcpwm_new_capture_channel(timer, &cap_config, &cap_channel));
  46.  
  47.     // Register the ISR handler for capture events
  48.     mcpwm_capture_event_callbacks_t callbacks = {
  49.         .on_cap = mcpwm_capture_isr_handler
  50.     };
  51.     ESP_ERROR_CHECK(mcpwm_capture_channel_register_event_callbacks(cap_channel, &callbacks, NULL));
  52.  
  53.     ESP_LOGI(TAG, "Enable capture channel");
  54.     ESP_ERROR_CHECK(mcpwm_capture_channel_enable(cap_channel));
  55.  
  56.     // Enable and start MCPWM timer
  57.     ESP_ERROR_CHECK(mcpwm_timer_enable(timer));
  58.     ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP));
  59.  
  60.     // Main task loop
  61.     while (1) {
  62.         // do something with "read_capture_time" variable below
  63.         // ....
  64.  
  65.         vTaskDelay(pdMS_TO_TICKS(1000)); // Sleep for 1 second
  66.     }
  67. }