Are MCPWM capture callbacks available?

Using #include "driver/mcpwm_cap.h" in the Espressif ESP-IDF we can register callbacks for the capture events e.g.

mcpwm_capture_channel_register_event_callbacks()

Unlike using hardware interrupts, the MCPWM capture module can freeze a timer count with zero latency.

As far as I can tell, the Arduino framework has no equivalent in the MCPWM related library i.e. capture callbacks aren't provided by #include "driver/mcpwm.h"

Have I hit a brick wall with using the Arduino IDE for my project which requires three channels of microsecond accuracy pulse-width measurement or am I missing some information about using this module (I'm only aware of one source of documentation i.e. peripherals api-reference from espressif).

Perhaps someone could help me understand how the Arduino ESP32 libraries are managed?

The source for the mcpwm.h file appears to be held in the espressif/arduino-esp32/repo commited by me-no-dev around 6 years ago. I'm not familiar with the history of the Arduino framework - is it no longer maintained?

Looking in that file, it appears that you specify the callback function in a 'mcpwm_capture_config_t' struct:

/**
 * @brief MCPWM config capture structure
 */
typedef struct {
    mcpwm_capture_on_edge_t cap_edge;      /*!<Set capture edge*/
    uint32_t cap_prescale;                 /*!<Prescale of capture signal, ranging from 1 to 256*/
    cap_isr_cb_t capture_cb;               /*!<User defined capture event callback, running under interrupt context */
    void *user_data;                       /*!<User defined ISR callback function args*/
} mcpwm_capture_config_t;

And then register it with this function:

/**
 * @brief Enable capture channel
 *
 * @param mcpwm_num set MCPWM unit(0-1)
 * @param cap_channel capture channel, which needs to be enabled
 * @param cap_conf capture channel configuration
 *
 * @return
 *     - ESP_OK Success
 *     - ESP_ERR_INVALID_ARG Parameter error
 */
esp_err_t mcpwm_capture_enable_channel(mcpwm_unit_t mcpwm_num, mcpwm_capture_channel_id_t cap_channel, const mcpwm_capture_config_t *cap_conf);

Which file? As far as I can tell, the arduino-esp32 driver is located at:

Which has been loaded by my Arduino IDE library manager to:

C:\Users\xxxxxx\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\tools\sdk\include\driver\driver\mcpwm.h

and searching through that file, there is no occurrence of the structure mcpwm_capture_config_t or the function mcpwm_capture_enable_channel() Have I got the wrong library? I had updated the IDE recently.

Some of the confusion I think comes from the overhaul of the ESP-IDF drivers to version 5. These most recent drivers became incompatible with some legacy calls from version 4.x
I have no idea what version the Arduino libraries are based on but the driver that is
loaded by it has no facility for callback on capture event.

Did you try looking at the version of 'mcpwm.h' that's installed on your PC and not just at the GitHub? What version of the ESP32 Arduino Core are you using? I have v2.0.14 that's based on ESP-IDF v4.4.6. It's the most recent version that's not in an Alpha state. In that version, 'mcpwm.h' contains the struct definition and function prototype that I cited.

When I check the boards manager for my dev board it says version 1.0.6. but It's also offering to upgrade to 2.0.11 however that fails as follows:

Configuring tool.
arduino:dfu-util@0.11.0-arduino5 installed
Replacing platform esp32:esp32@1.0.6 with esp32:esp32@2.0.11
Uninstalling esp32:esp32@1.0.6
Error upgrading platform: removing platform files: remove C:\Users\adria_000\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\tools\sdk\include\driver\driver: The process cannot access the file because it is being used by another process.
Uninstalling esp32:esp32@2.0.11
Platform esp32:esp32@2.0.11 uninstalled
Failed to install platform: 'esp32:esp32:2.0.11'.
Error: 13 INTERNAL: Cannot upgrade platform: removing platform files: remove C:\Users\adria_000\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\tools\sdk\include\driver\driver: The process cannot access the file because it is being used by another process.

v1.0.6 is almost 3 years old. That being said, did you actually look inside 'mcpwm.h' for your installation as I advised? Did it have any provisions for callbacks?

I can't help you with the upgrade problem. Toolchain issues aren't my forte.

Thanks for your help. I reinstalled the arduino IDE and managed to install the most recent library (esp32 by Espressif 2.0.11) I now have the callback control block you outlined although the code example I have taken from the ESP-IDF still isn't compatible. I have to start work converting it over now.

Actually my preferred IDE is platformio in VS code using the arduino framework. This, however, has the same incompatibility. I wonder why the arduino framework has a very different structure - merging the capture callbacks into the driver/mcpwm.h file - whereas the ESP-IDF has a a separate driver/mcpwm_cap.h for the capture stuff?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.