Read MCPWM timer with DPORT_REG_READ

jcolebaker
Posts: 75
Joined: Thu Mar 18, 2021 12:23 am

Read MCPWM timer with DPORT_REG_READ

Postby jcolebaker » Wed Jul 02, 2025 9:46 pm

I'm using the MCPWM input capture to time the rising edges of a signal:

Code: Select all

    ESP_ERROR_CHECK(
        mcpwm_new_capture_timer(
            &(mcpwm_capture_timer_config_t)
            {
                .clk_src = MCPWM_CAPTURE_CLK_SRC_DEFAULT, // All input capture operations use the APB clock (80 MHz)
                .group_id = 0,
            },
            &m_mcpwm_cap_timer
        )
    );
    
    ESP_ERROR_CHECK(
        mcpwm_new_capture_channel(
            m_mcpwm_cap_timer,
            &(mcpwm_capture_channel_config_t)
            {
                .gpio_num = GPIO_PIN_ICAP,
                .intr_priority = 3,
                .prescale = 1,
                .flags.pos_edge = true,
                .flags.neg_edge = false, // Capture rising edges only.
                .flags.pull_up = false,
                .flags.pull_down = true,
            },
            &m_capture_channel
        )
    );

   TaskHandle_t current_task = xTaskGetCurrentTaskHandle();
    mcpwm_capture_event_callbacks_t capture_callbacks =
    {
        .on_cap = CaptureInterruptHandler,
    };

    ESP_ERROR_CHECK(mcpwm_capture_channel_register_event_callbacks(m_capture_channel, &capture_callbacks, current_task) );    

In my interrupt handler, I get the captured timer value for each rising edge of the signal - so far so good.

What I want to do is profile how much time the CPU is spending in the ISR, by reading a high res timer. This needs to be very efficient due to the high frequency of the interrupts.

I thought maybe I could read the raw (i.e. current) value of the capture timer at the start and end of the ISR, but there is no API function to do this.

I tried read it directly with

Code: Select all

DPORT_REG_READ
:

Code: Select all

uint32_t mcpwm_timer_value = DPORT_REG_READ(MCPWM_TIMER0_STATUS_REG(0));
However, this always returns 0.

I also tried MCPWM_TIMER0_STATUS_REG, MCPWM_TIMER1_STATUS_REG, MCPWM_TIMER2_STATUS_REG and values 0 and 1 for the MCPWM module index, but they always read as 0.

Should this work, or is it a dumb idea?

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Read MCPWM timer with DPORT_REG_READ

Postby MicroController » Thu Jul 03, 2025 7:56 am

What I want to do is profile how much time the CPU is spending in the ISR, by reading a high res timer. This needs to be very efficient due to the high frequency of the interrupts.
esp_cpu_get_cycle_count() is what you want to use for this.

Who is online

Users browsing this forum: ChatGPT-User, dmaxben, Semrush [Bot] and 10 guests