How do you setup a UART Rx interrupt on a ESP32-S2?
Posted: 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:
Interrupt handler:
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:
I tried rewriting the interrupt handler with this code to see if I could just echo the input:
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!!
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 ));
}
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);
}
Code: Select all
'uart_dev_t' {aka 'volatile struct <anonymous>'} has no member named 'fifo'
rxbuf[nrBytes++] = UART0.fifo.rw_byte; // Read all bytes
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);
}
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!!