How do you setup a UART Rx interrupt on a ESP32-S2?

Electronics-noob
Posts: 8
Joined: Wed Jul 31, 2019 3:14 pm

How do you setup a UART Rx interrupt on a ESP32-S2?

Postby Electronics-noob » Tue Jul 28, 2020 8:42 am

I have tried moving some code over directly from an ESP32 to a new ESP32-S2 and I ran into an issue with my UART interrupt.

The code used is as follows:

Setup code:

Code: Select all

const uart_port_t uart_num = UART_NUM_0;

void setup {
// Setup UART
	    uart_config_t uart_config = {
	      .baud_rate = 115200,
	      .data_bits = UART_DATA_8_BITS,
	      .parity = UART_PARITY_ODD,
	      .stop_bits = UART_STOP_BITS_1,
	      .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
	    };
	    ESP_ERROR_CHECK(uart_param_config(uart_num , &uart_config));
	    ESP_ERROR_CHECK(uart_set_pin(uart_num , UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));   // Set UART pins (using UART0 default pins ie no changes.)
	    ESP_ERROR_CHECK(uart_driver_install(uart_num , BUF_SIZE * 2, BUF_SIZE * 2, 10, NULL, 0));                    // Install UART driver, and get the queue.        
	    ESP_ERROR_CHECK(uart_isr_free(uart_num ));                                                                  // Release the pre registered UART handler/subroutine
	    ESP_ERROR_CHECK(uart_isr_register(uart_num ,uart_intr_handle, NULL, ESP_INTR_FLAG_IRAM, NULL));             // Register new UART subroutine
	    ESP_ERROR_CHECK(uart_enable_rx_intr(uart_num ));
}
Interrupt handler:

Code: Select all

// UART Handler
static void IRAM_ATTR uart_intr_handle(void *arg) {
  uint16_t rx_fifo_len;
  
  //status = UART0.int_st.val;            // Read UART interrupt Status
  rx_fifo_len = UART0.status.rxfifo_cnt;  // Read number of bytes in UART buffer
  
  while(rx_fifo_len){
   rxbuf[nrBytes++] = UART0.fifo.rw_byte;       // Read all bytes
   rx_fifo_len--;
  }

  UART_RECEIVED = 1;                      // Set flag

  // After reading bytes from buffer clear UART interrupt status
  uart_clear_intr_status(uart_num , UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
}
When I tried to compile this code using esp idf version 4.3 (master branch) compiler it gave me errors regarding the way the Rx buffer is read:

Code: Select all

'uart_dev_t' {aka 'volatile struct <anonymous>'} has no member named 'fifo'
    rxbuf[nrBytes++] = UART0.fifo.rw_byte;       // Read all bytes
I tried rewriting the interrupt handler with this code to see if I could just echo the input:

Code: Select all

static void IRAM_ATTR uart_intr_handle(void *arg) {
	unsigned int rx_fifo_len = UART0.status.rxfifo_cnt;
 	int length = uart_read_bytes(uart_num, &rxbuf, rx_fifo_len, 100);
 	uart_write_bytes(uart_num, (const char *) &rxbuf, length);
 	uart_clear_intr_status(uart_num, UART_RXFIFO_FULL_INT_CLR | UART_RXFIFO_TOUT_INT_CLR | UART_INTR_RXFIFO_FULL | 	UART_INTR_RXFIFO_TOUT);
 }
This was able to compile, but the processor crashes instantly when a UART character is received :(
I feel like there must be an easier way to set this up, but I can't find any good examples of this online (for the ESP32-S2).

Any help would be appreciated!!

PeterR
Posts: 447
Joined: Mon Jun 04, 2018 2:47 pm

Re: How do you setup a UART Rx interrupt on a ESP32-S2?

Postby PeterR » Tue Jul 28, 2020 7:54 pm

Idle scrolling but:

Code: Select all

int length = uart_read_bytes(uart_num, &rxbuf, rx_fifo_len, 100);
Looks weird.
Why would you offer a pointer to a pointer when you just want the client library to return data?
This is an idle post so if the documentatiobn says other then shoot me down. Just seemed a very strange interface (assuming rxbuff is an array).
& I also believe that IDF CAN should be fixed.

Electronics-noob
Posts: 8
Joined: Wed Jul 31, 2019 3:14 pm

Re: How do you setup a UART Rx interrupt on a ESP32-S2?

Postby Electronics-noob » Wed Jul 29, 2020 6:21 am

You can ignore my code completely if you want, it's basically copied from an example I found online. I just want to know how it can be done! Espressif does not explain how to set up the uart interrupt properly:

https://docs.espressif.com/projects/es ... nterrupts

vonnieda
Posts: 133
Joined: Tue Nov 07, 2017 3:42 pm

Re: How do you setup a UART Rx interrupt on a ESP32-S2?

Postby vonnieda » Wed Jul 29, 2020 4:18 pm

I'd recommend looking at the default ISR that the driver installs as a guideline for writing your own: https://github.com/espressif/esp-idf/bl ... art.c#L683

You can also look at the driver install code which sets up the interrupt and the handler to see how to do that: https://github.com/espressif/esp-idf/bl ... rt.c#L1277

Jason

Who is online

Users browsing this forum: Baidu [Spider] and 31 guests