ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

GvTardy
Posts: 23
Joined: Tue Jan 01, 2019 3:26 pm

ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby GvTardy » Wed Oct 07, 2020 12:57 pm

Hello,

I developed a time capture application using interrupts.
My code is very close to the mcpwm capture example.
if I delete line #4, tyhe code works fine,...

I call the interrupt on both edges and figure out about the edge type by calling mcpwm_capture_signal_get_edge.
This works fine with ESP-IDF 4.0.

Yesterday I updated to ESP-IDF4.1. Now the ESP32 panics as soon as the interrupt fires. This is caused by calling mcpwm_capture_signal_get_edge(....

Checking the length of mcpwm.c the newer version is about 80 lines shorter.

So I switched back to 4.0...
Problem gone...
To me, this seems like a bug.

Greetings
Georg

Code: Select all

static void IRAM_ATTR isr_handler(void)
{

    if (mcpwm_capture_signal_get_edge(MCPWM_UNIT_0, MCPWM_SELECT_CAP0) == 1 )  // raising edge
        isr_captured.raising_edge = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, MCPWM_SELECT_CAP0); 
    else{
        isr_falling = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, MCPWM_SELECT_CAP0); 

        if ( (isr_falling - isr_falling_last) > isr_ticks_debounce){
            toggle_22 ^=1;
            gpio_set_level(22,toggle_22);
            isr_captured.falling_edge = isr_falling;
            xQueueSendFromISR(capture_queue, (capture_t *) &isr_captured, NULL);
        };
        isr_falling_last = isr_falling;
    }
    MCPWM0.int_clr.val = CAP0_INT_EN;
}

static void capture_config(void *arg){

    mcpwm_pin_config_t pin_config = {
        .mcpwm_cap0_in_num   = GPIO_CAP0_IN
    };
    mcpwm_set_pin(MCPWM_UNIT_0, &pin_config);

    mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, (MCPWM_POS_EDGE | MCPWM_NEG_EDGE), 0);  
    MCPWM0.int_ena.val = CAP0_INT_EN;  //Enable interrupt on  CAP0 signal
    mcpwm_isr_register(MCPWM_UNIT_0, (void*)&isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL);  //Set ISR Handler

    vTaskDelete(NULL);
}
P.S. (edit to the original post)
I just recompiled an noticed that I forgot to mention 2 modifications I made to
mcpwm.c
mcpwm.h
With these I can trigger both edges and combine them by the OR statement:

mcpwm.h: line 197ff

Code: Select all

typedef enum {
    MCPWM_NEG_EDGE = 0x00,  /*!<Capture starts from negative edge*/
    MCPWM_POS_EDGE = 0x01,      /*!<Capture starts from positive edge*/
    MCPWM_BOTH_EDGE = 0x02,      /*!<Capture both edges*/
} mcpwm_capture_on_edge_t;
mcpwm.c: line 664

Code: Select all

    MCPWM[mcpwm_num]->cap_cfg_ch[cap_sig].mode = cap_edge + 1;

ifynk85
Posts: 7
Joined: Sun Feb 21, 2021 3:19 pm

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ifynk85 » Sun Feb 21, 2021 3:21 pm

I have the same issue with the latest upstream version of framework. How to fix this?

ifynk85
Posts: 7
Joined: Sun Feb 21, 2021 3:19 pm

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ifynk85 » Sun Feb 21, 2021 3:27 pm

Guru Meditation Error: Core 0 panic'ed (StoreProhibited). Exception was unhandled.

Core 0 register dump:
PC : 0x400e2bf3 PS : 0x00060031 A0 : 0x800d0efe A1 : 0x3ffb0770
A2 : 0x3ffb0034 A3 : 0x283e7e80 A4 : 0x00000000 A5 : 0x3ffb07a0
A6 : 0x0000cdcd A7 : 0x00000000 A8 : 0x08000000 A9 : 0x3ff5e000
A10 : 0x00000001 A11 : 0x00060023 A12 : 0x3ffb4840 A13 : 0x3ffb4810
A14 : 0x00000000 A15 : 0x40080e98 SAR : 0x00000005 EXCCAUSE: 0x0000001d
EXCVADDR: 0x00000000 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0xffffffff

Backtrace:0x400e2bf0:0x3ffb0770 0x400d0efb:0x3ffb0790 0x40080ead:0x3ffb07d0 0x40082025:0x3ffb0800 0x4000bfed:0x3ffb4770 0x40084829:0x3ffb4780 0x400d19ff:0x3ffb47a0 0x400d1a5d:0x3ffb47e0 0x400d0f75:0x3ffb4810 0x400d0aaf:0x3ffb4840 0x400d12b6:0x3ffb4870 0x400845d1:0x3ffb48a0

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ESP_Sprite » Mon Feb 22, 2021 2:02 am

Can you decode that backtrace, by e.g. using 'idf.py monitor' to log the output?

ifynk85
Posts: 7
Joined: Sun Feb 21, 2021 3:19 pm

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ifynk85 » Mon Feb 22, 2021 12:48 pm

How i can do this with vs code and platformio? Does the monitor filter esp32_exception_decoder is the same?

ifynk85
Posts: 7
Joined: Sun Feb 21, 2021 3:19 pm

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ifynk85 » Mon Feb 22, 2021 7:42 pm

I got this info.

Code: Select all

Core  0 register dump:
PC      : 0x400e2bf3  PS      : 0x00060031  A0      : 0x800d0efe  A1      : 0x3ffb0770  
A2      : 0x3ffb0034  A3      : 0x1514e7fd  A4      : 0x00000000  A5      : 0x3ffb07a0  
A6      : 0x0000cdcd  A7      : 0x00000000  A8      : 0x08000000  A9      : 0x3ff5e000  
A10     : 0x00000001  A11     : 0x00060023  A12     : 0x3ffb4840  A13     : 0x3ffb4810  
A14     : 0x00000000  A15     : 0x40080e98  SAR     : 0x00000005  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

Backtrace:0x400e2bf0:0x3ffb0770 0x400d0efb:0x3ffb0790 0x40080ead:0x3ffb07d0 0x40082025:0x3ffb0800 0x4000bfed:0x3ffb4770 0x40084829:0x3ffb4780 0x400d19ff:0x3ffb47a0 0x400d1a5d:0x3ffb47e0 0x400d0f75:0x3ffb4810 0x400d0aaf:0x3ffb4840 0x400d12b6:0x3ffb4870 0x400845d1:0x3ffb48a0
  #0  0x400e2bf0:0x3ffb0770 in mcpwm_ll_get_capture_val at /home/ifynk/.platformio/packages/framework-espidf/components/soc/src/esp32/include/hal/mcpwm_ll.h:671
      (inlined by) mcpwm_hal_capture_get_result at /home/ifynk/.platformio/packages/framework-espidf/components/soc/src/hal/mcpwm_hal.c:217
  #1  0x400d0efb:0x3ffb0790 in mcpwm_capture_signal_get_edge at /home/ifynk/.platformio/packages/framework-espidf/components/driver/mcpwm.c:585 (discriminator 2)
  #2  0x40080ead:0x3ffb07d0 in isr_handler at src/main.c:25
  #3  0x40082025:0x3ffb0800 in _xt_lowint1 at /home/ifynk/.platformio/packages/framework-espidf/components/freertos/xtensa/xtensa_vectors.S:1105
  #4  0x4000bfed:0x3ffb4770 in ?? ??:0
  #5  0x40084829:0x3ffb4780 in vPortExitCritical at /home/ifynk/.platformio/packages/framework-espidf/components/freertos/xtensa/port.c:419
  #6  0x400d19ff:0x3ffb47a0 in esp_intr_alloc_intrstatus at /home/ifynk/.platformio/packages/framework-espidf/components/esp32/intr_alloc.c:673
  #7  0x400d1a5d:0x3ffb47e0 in esp_intr_alloc at /home/ifynk/.platformio/packages/framework-espidf/components/esp32/intr_alloc.c:693
  #8  0x400d0f75:0x3ffb4810 in mcpwm_isr_register at /home/ifynk/.platformio/packages/framework-espidf/components/driver/mcpwm.c:620 (discriminator 2)
  #9  0x400d0aaf:0x3ffb4840 in app_main at src/main.c:62
  #10 0x400d12b6:0x3ffb4870 in main_task at /home/ifynk/.platformio/packages/framework-espidf/components/esp32/cpu_start.c:600 (discriminator 2)
  #11 0x400845d1:0x3ffb48a0 in vPortTaskWrapper at /home/ifynk/.platformio/packages/framework-espidf/components/freertos/xtensa/port.c:143

ifynk85
Posts: 7
Joined: Sun Feb 21, 2021 3:19 pm

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ifynk85 » Mon Feb 22, 2021 7:43 pm

And the source code

Code: Select all

#include "driver/mcpwm.h"
#include "soc/mcpwm_periph.h"
#include "freertos/FreeRTOS.h"
#include "soc/rtc.h"
#include "freertos/task.h"
#include "freertos/queue.h"

#define GPIO_CAP0_IN 23
#define CAP0_INT_EN BIT(27)

static mcpwm_dev_t *MCPWM[2] = {&MCPWM0, &MCPWM1};
uint32_t *current_cap_value = NULL;
uint32_t *previous_cap_value = NULL;

xQueueHandle capQueue;
uint32_t captureSignal;

static void IRAM_ATTR isr_handler(void) {
  uint32_t mcpwm_intr_status;
  mcpwm_intr_status = MCPWM[MCPWM_UNIT_0]->int_st.val; //Read interrupt status

  if (mcpwm_intr_status & CAP0_INT_EN) { //Check for interrupt on rising edge on CAP0 signal
    //current_cap_value[0] = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, MCPWM_SELECT_CAP0); //get capture signal counter value

    uint32_t width = mcpwm_capture_signal_get_edge(MCPWM_UNIT_0, MCPWM_SELECT_CAP0);

    // uint32_t width = (current_cap_value[0] - previous_cap_value[0]) / (rtc_clk_apb_freq_get() / 1000000);

    //previous_cap_value[0] = current_cap_value[0];

    xQueueSendFromISR(capQueue, &width, NULL);
  }

  MCPWM[MCPWM_UNIT_0]->int_clr.val = mcpwm_intr_status;
}

static void disp_captured_signal(void *arg) {
  uint32_t width;

  while (1) {
    xQueueReceive(capQueue, &width, portMAX_DELAY);

    printf("Width: %d us\n", width);
  }
}

void app_main() {
  printf("Hello world!\n");

  capQueue = xQueueCreate(1, sizeof(captureSignal));
  xTaskCreate(disp_captured_signal, "mcpwm_config", 4096, NULL, 5, NULL);

  mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, GPIO_CAP0_IN);
  gpio_pulldown_en(GPIO_CAP0_IN);

  current_cap_value = (uint32_t *) malloc(3 * sizeof(uint32_t));
  previous_cap_value = (uint32_t *) malloc(3 * sizeof(uint32_t));

  mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_POS_EDGE, 0);

  MCPWM[MCPWM_UNIT_0]->int_ena.val = CAP0_INT_EN;  //Enable interrupt on CAP0 signal
  mcpwm_isr_register(MCPWM_UNIT_0, isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL);
}


ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ESP_Sprite » Tue Feb 23, 2021 4:43 am

Are you sure that's v4.1? The path components/soc/src/esp32/include/hal/mcpwm_ll.h does not exist in 4.1 (it does in 4.2)

ifynk85
Posts: 7
Joined: Sun Feb 21, 2021 3:19 pm

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ifynk85 » Tue Feb 23, 2021 6:21 am

Yes, sorry, it is 4.2

Code: Select all

PLATFORM: Espressif 32 (3.0.0+sha.55f50dc) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES: 
 - framework-espidf 3.40200.210118 (4.2.0) 
 - tool-cmake 3.16.4 
 - tool-esptoolpy 1.30000.201119 (3.0.0) 
 - tool-ninja 1.7.1 
 - toolchain-esp32ulp 1.22851.191205 (2.28.51) 
 - toolchain-xtensa32 2.80400.210211 (8.4.0)
Can it be fixed?

ifynk85
Posts: 7
Joined: Sun Feb 21, 2021 3:19 pm

Re: ESP-IDF 4.1 Error: mcpwm_capture_signal_get_edge crashes

Postby ifynk85 » Wed Mar 03, 2021 6:57 am

So, any news?

Who is online

Users browsing this forum: No registered users and 119 guests