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:
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/mcpwm_prelude.h"
- #include "esp_timer.h"
- #include "esp_log.h"
- //#include "driver/mcpwm.h"
- // Constants
- #define GPIO_INPUT_CAPTURE_PIN 18 // GPIO pin connected to the sensor
- // Globals
- static const char *TAG = "Test_Read_PWM";
- volatile float read_capture_time = 0; // store time of reading
- // ISR Handler for Pulse Capture
- bool IRAM_ATTR mcpwm_capture_isr_handler(mcpwm_cap_channel_handle_t cap_channel, const mcpwm_capture_event_data_t *edata, void *user_data) {
- read_capture_time = edata->cap_value;
- return false; // No further actions required by the ISR
- }
- // Main Application
- void app_main(void) {
- // Configure MCPWM for input capture
- // Initialize MCPWM timer
- mcpwm_timer_config_t timer_config = {
- .group_id = 0, // MCPWM Group 0
- .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, // Default clock source
- .resolution_hz = 1000000, // 1 MHz resolution (1 µs ticks)
- .count_mode = MCPWM_TIMER_COUNT_MODE_UP
- };
- mcpwm_timer_handle_t timer = NULL;
- ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer));
- // Initialize capture channel
- mcpwm_capture_channel_config_t cap_config = {
- .gpio_num = GPIO_INPUT_CAPTURE_PIN,
- .prescale = 1,
- .flags.pos_edge = true, // Capture on rising edge
- .flags.neg_edge = false,
- };
- mcpwm_cap_channel_handle_t cap_channel = NULL;
- ESP_ERROR_CHECK(mcpwm_new_capture_channel(timer, &cap_config, &cap_channel));
- // Register the ISR handler for capture events
- mcpwm_capture_event_callbacks_t callbacks = {
- .on_cap = mcpwm_capture_isr_handler
- };
- ESP_ERROR_CHECK(mcpwm_capture_channel_register_event_callbacks(cap_channel, &callbacks, NULL));
- ESP_LOGI(TAG, "Enable capture channel");
- ESP_ERROR_CHECK(mcpwm_capture_channel_enable(cap_channel));
- // Enable and start MCPWM timer
- ESP_ERROR_CHECK(mcpwm_timer_enable(timer));
- ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP));
- // Main task loop
- while (1) {
- // do something with "read_capture_time" variable below
- // ....
- vTaskDelay(pdMS_TO_TICKS(1000)); // Sleep for 1 second
- }
- }