Uart ISR getting trigger and crashing program

Pratikm78
Posts: 13
Joined: Wed Jun 26, 2019 6:03 pm

Uart ISR getting trigger and crashing program

Postby Pratikm78 » Sat Jul 11, 2020 11:11 am

Hi,

I have a fairly large project that has been working quite well. However I am currently struggling with UART ISR that is crashing my program.
  1. static void Initialize_Port(void)
  2. {
  3.     setFlowControlLow();
  4.     if (boot)
  5.     {
  6.         uart_config_t uart_config = {
  7.             .baud_rate = MODBUS_DEFAULT_BAUD,
  8.             .data_bits = UART_DATA_8_BITS,
  9.             .parity = UART_PARITY_DISABLE,
  10.             .stop_bits = UART_STOP_BITS_1,
  11.             .flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
  12.  
  13.         ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart_config));
  14.         ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, RS485_TXD, RS485_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  15.         ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, 2048, 0, 0, NULL, 0));
  16.         ESP_ERROR_CHECK(uart_isr_free(UART_NUM_1));
  17.         ESP_ERROR_CHECK(uart_isr_register(UART_NUM_1, uart_intr_handle, NULL, ESP_INTR_FLAG_LOWMED, &handle_console));
  18.         uart_disable_tx_intr(UART_NUM_1);
  19.         boot = false;
  20.     }
  21.  
  22. void setFlowControlHigh(void)
  23. {
  24.     gpio_set_level(RS485_DIR, 1);
  25. }
  26. /**
  27.  * @brief Set the Flow Control Pin Low
  28.  *
  29.  */
  30. void setFlowControlLow(void)
  31. {
  32.     gpio_set_level(RS485_DIR, 0);
  33. }
  34.     else
  35.     {
  36.         uart_set_baudrate(UART_NUM_1, modbus_port.modbus_baud_rate);
  37.         uart_set_word_length(UART_NUM_1, UART_DATA_8_BITS); /// esp uart does not support 9 bits
  38.         if (modbus_port.modbus_parity == MODBUS_PARITY_NONE)
  39.             uart_set_parity(UART_NUM_1, UART_PARITY_DISABLE);
  40.         if (modbus_port.modbus_parity == MODBUS_PARITY_ODD)
  41.             uart_set_parity(UART_NUM_1, UART_PARITY_ODD);
  42.         if (modbus_port.modbus_parity == MODBUS_PARITY_EVEN)
  43.             uart_set_parity(UART_NUM_1, UART_PARITY_EVEN);
  44.         uart_set_stop_bits(UART_NUM_1, (modbus_port.modbus_stop_bits == MODBUS_STOP_BITS_1) ? UART_STOP_BITS_1 : UART_STOP_BITS_2);
  45.     }
  46. }
  47.  
  48. void esp_transmit_uart(void)
  49. {
  50.     uart_disable_rx_intr(UART_NUM_1);
  51.     char buffer[2];
  52.     modbus_port.master_state = MODBUS_STATE_TRANSMIT;
  53.     while (!FIFO_Empty(&FIFO_MODBUS_Tx))
  54.     {
  55.  
  56.         sprintf(buffer, "%1c", FIFO_Get(&FIFO_MODBUS_Tx));
  57.          if (modbus_debug)
  58.              printf("%02X ", buffer[0]);
  59.         uart_write_bytes(UART_NUM_1, (const char *)buffer, 1);
  60.     }
  61.     if (FIFO_Empty(&FIFO_MODBUS_Tx))
  62.     {
  63.         modbus_port.master_state = MODBUS_STATE_RECEIVE;
  64.         setFlowControlLow();
  65.         uart_enable_rx_intr(UART_NUM_1);
  66.     }
  67. }
  68.  
  69.  
  70. static void IRAM_ATTR uart_intr_handle(void *arg)
  71. {
  72. uint16_t rx_fifo_len, status;
  73.   uint16_t i=0;
  74.  
  75.   status = UART1.int_st.val; // read UART interrupt Status
  76.     FIFO_Flush(&FIFO_MODBUS_Rx);
  77.   rx_fifo_len = UART1.status.rxfifo_cnt; // read number of bytes in UART buffer
  78. //   if (rx_fifo_len > 9){
  79. //       rx_fifo_len = rx_fifo_len -7;
  80. //   }
  81.   modbus_port.master_state = MODBUS_STATE_PROCESS_RX;
  82.    if ( modbus_port.master_state !=2 ){
  83.          while(rx_fifo_len){
  84.          FIFO_Put(&FIFO_MODBUS_Rx, UART1.fifo.rw_byte); // read all bytes
  85.        
  86.          rx_fifo_len--;
  87.          }
  88.   }
  89.  
  90.  
  91.   // after reading bytes from buffer clear UART interrupt status
  92.   uart_clear_intr_status(UART_NUM_1, UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
  93.  
  94.  
  95.   // a test code or debug code to indicate UART receives successfully,
  96.   // you can redirect received byte as echo also
  97.   //uart_write_bytes(UART_NUM_1, (const char*) "RX Done", 7);
  98.  
  99. }
Guru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0)
Core 0 register dump:
PC : 0x40082cea PS : 0x00050034 A0 : 0x4019a19a A1 : 0x3ffbe540
0x40082cea: _xt_lowint1 at D:/esp/esp-IDF/components/freertos/xtensa_vectors.S:1153

0x4019a19a: esp_pm_impl_waiti at D:/esp/esp-IDF/components/esp32/pm_esp32.c:486

A2 : 0x00002068 A3 : 0x3ffbbc10 A4 : 0xc0000000 A5 : 0x3ffbe520
A6 : 0x00000000 A7 : 0x00000001 A8 : 0x800d3f16 A9 : 0x40099ed4
0x40099ed4: _frxt_int_enter at D:/esp/esp-IDF/components/freertos/portasm.S:119

A10 : 0x00000000 A11 : 0x00000070 A12 : 0x800831c4 A13 : 0x3ffbe4f0
A14 : 0x00000000 A15 : 0x00000101 SAR : 0x00000017 EXCCAUSE: 0x00000005
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Core 0 was running in ISR context:
EPC1 : 0x4019a19a EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x40082cea
0x4019a19a: esp_pm_impl_waiti at D:/esp/esp-IDF/components/esp32/pm_esp32.c:486

0x40082cea: _xt_lowint1 at D:/esp/esp-IDF/components/freertos/xtensa_vectors.S:1153


ELF file SHA256: e96210a03f2b310cfefc2adbda811d12c5f3081d16d7db0b41cc836038a8e0d3

Backtrace: 0x40082ce7:0x3ffbe540 0x4019a197:0x3ffbbc10 0x400d44ca:0x3ffbbc30 0x4009b911:0x3ffbbc50 0x40099ca9:0x3ffbbc70
0x40082ce7: _xt_lowint1 at D:/esp/esp-IDF/components/freertos/xtensa_vectors.S:1153

0x4019a197: esp_pm_impl_waiti at D:/esp/esp-IDF/components/esp32/pm_esp32.c:484

0x400d44ca: esp_vApplicationIdleHook at D:/esp/esp-IDF/components/esp_common/src/freertos_hooks.c:63

0x4009b911: prvIdleTask at D:/esp/esp-IDF/components/freertos/tasks.c:3382 (discriminator 1)

0x40099ca9: vPortTaskWrapper at D:/esp/esp-IDF/components/freertos/port.c:143


Core 1 register dump:
PC : 0x4019a19a PS : 0x00060f34 A0 : 0x800d44cd A1 : 0x3ffbc380
0x4019a19a: esp_pm_impl_waiti at D:/esp/esp-IDF/components/esp32/pm_esp32.c:486

A2 : 0x00000000 A3 : 0x00000001 A4 : 0x80099eae A5 : 0x3ffbc2b0
A6 : 0x00000003 A7 : 0x00060023 A8 : 0x800d3f16 A9 : 0x3ffbc350
A10 : 0x00000000 A11 : 0x00000001 A12 : 0x00060620 A13 : 0x00000001
A14 : 0x00060620 A15 : 0x3ffbd2b0 SAR : 0x00000000 EXCCAUSE: 0x00000005
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000

ELF file SHA256: e96210a03f2b310cfefc2adbda811d12c5f3081d16d7db0b41cc836038a8e0d3

Backtrace: 0x4019a197:0x3ffbc380 0x400d44ca:0x3ffbc3a0 0x4009b911:0x3ffbc3c0 0x40099ca9:0x3ffbc3e0
0x4019a197: esp_pm_impl_waiti at D:/esp/esp-IDF/components/esp32/pm_esp32.c:484

0x400d44ca: esp_vApplicationIdleHook at D:/esp/esp-IDF/components/esp_common/src/freertos_hooks.c:63

0x4009b911: prvIdleTask at D:/esp/esp-IDF/components/freertos/tasks.c:3382 (discriminator 1)

0x40099ca9: vPortTaskWrapper at D:/esp/esp-IDF/components/freertos/port.c:143


Rebooting...


I am transmitting data using the esp_transmit_uart function which works as intended when the FIFO buffer is empty i change the flow control to LOW and enable the rx intr. I currently have nothing connected and the ISR is still being triggered and then system crashes. Not sure what is triggering the interrupt. I have tried different interrupt flag however no luck.

Any assistance will be highly appreciated.

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

Re: Uart ISR getting trigger and crashing program

Postby ESP_Sprite » Sat Jul 11, 2020 11:29 am

What do FIFO_flush/FIFO_put look like? Any potentially blocking code in there?

Pratikm78
Posts: 13
Joined: Wed Jun 26, 2019 6:03 pm

Re: Uart ISR getting trigger and crashing program

Postby Pratikm78 » Sat Jul 11, 2020 12:24 pm

  1. /**
  2. * Flushes any data in the FIFO buffer
  3. *
  4. * @param  b - pointer to fifo_buffer_t structure
  5. *
  6. * @return none
  7. */
  8. void FIFO_Flush(
  9.     fifo_buffer_t * b)
  10. {
  11.     unsigned head;      /* used to avoid volatile decision */
  12.  
  13.     if (b) {
  14.         head = b->head;
  15.         b->tail = head;
  16.     }
  17. }
  18.  
  19.  
  20. /**
  21. * Adds a byte of data to the FIFO
  22. *
  23. * @param  b - pointer to fifo_buffer_t structure
  24. * @param  data_byte [in] - data to put into the FIFO
  25. *
  26. * @return true on successful add, false if not added
  27. */
  28. bool FIFO_Put(
  29.     fifo_buffer_t * b,
  30.     uint8_t data_byte)
  31. {
  32.     bool status = false;        /* return value */
  33.     unsigned index;
  34.  
  35.     if (b) {
  36.         /* limit the buffer to prevent overwriting */
  37.         if (!FIFO_Full(b)) {
  38.             index = b->head % b->buffer_len;
  39.             b->buffer[index] = data_byte;
  40.             b->head++;
  41.             status = true;
  42.         }
  43.     }
  44.  
  45.     return status;
  46. }
Note the uart is connected to RS485 transceiver. (SP3485)

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

Re: Uart ISR getting trigger and crashing program

Postby ESP_Sprite » Sun Jul 12, 2020 8:02 am

Hold up, I think I see the issue. You're using the UART driver (which needs the ISR hooked to work), then forcefully free the ISR and install your own, while still expecting the UART driver to be able to do its job (as you still use it to write data to the UART. You can't do that: either use the driver fully and leave its ISR alone, or don't install the driver and handle everything manually.

Pratikm78
Posts: 13
Joined: Wed Jun 26, 2019 6:03 pm

Re: Uart ISR getting trigger and crashing program

Postby Pratikm78 » Sun Jul 12, 2020 11:28 am

OK so if don't use the install of the driver can I set up the interrupt to receive. Do I need separate isr to manually write to the unit?

Pratikm78
Posts: 13
Joined: Wed Jun 26, 2019 6:03 pm

Re: Uart ISR getting trigger and crashing program

Postby Pratikm78 » Sun Jul 12, 2020 11:44 am

I want to write data on the uart. Then data is received trigger an interrupt to fill buffer with incoming data. Don't want poll with uart read function. Is this possible. Note I am NOT using Freertos create tasks.

vinimac
Posts: 24
Joined: Sun May 06, 2018 1:04 pm

Re: Uart ISR getting trigger and crashing program

Postby vinimac » Sat Oct 17, 2020 2:30 am

I had same issue, so I wrote my own transmit function and that solved the issue.

Code: Select all

#include "esp32/rom/uart.h" // for WRITE_PERI_REG
int uart_write(uint8_t uartN, char *datarx, int size)
{
    int i;

    for (i = 0; i < size; i++)
        WRITE_PERI_REG(UART_FIFO_REG(uartN), datarx[i]);
    return i;
}

gargmukul121
Posts: 1
Joined: Tue Oct 20, 2020 7:49 am

Re: Uart ISR getting trigger and crashing program

Postby gargmukul121 » Tue Oct 20, 2020 7:59 am

Damm even I am having a similar kind of issue, I have searched all over the internet and even have posted on number of threads on different forum, no solution seems to work. I am really frustrated, can anyone of you here help me resolve this issue, I am very much tired now.

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

Re: Uart ISR getting trigger and crashing program

Postby ESP_Sprite » Wed Oct 21, 2020 2:27 am

gargmukul121 wrote:
Tue Oct 20, 2020 7:59 am
Damm even I am having a similar kind of issue, I have searched all over the internet and even have posted on number of threads on different forum, no solution seems to work. I am really frustrated, can anyone of you here help me resolve this issue, I am very much tired now.
Not when you don't post what 'similar kind' of issue you have. Suggest starting your own thread and putting the details there.

Who is online

Users browsing this forum: username and 93 guests