ESP32 UART interrupt handler with ESP-IDF

Semih Kahraman
Posts: 3
Joined: Tue Mar 15, 2022 7:17 am

ESP32 UART interrupt handler with ESP-IDF

Postby Semih Kahraman » Tue Aug 09, 2022 6:01 am

Hello everyone, I'm trying to read data with uart interrupt handler but I got an error like this
I'm using ESP-IDF with Visual Studio Code.
<CODE><s>

Code: Select all

</s>W (35) boot.esp32: PRO CPU has been reset by WDT.
W (40) boot.esp32: WDT reset info: PRO CPU PC=0x400d1aa4
0x400d1aa4: panic_print_char at C:/Users/semih.kahraman/esp/esp-idf/components/esp_system/panic.c:79 (discriminator 1)
W (46) boot.esp32: WDT reset info: APP CPU PC=0x400d155e
0x400d155e: panic_handler at C:/Users/semih.kahraman/esp/esp-idf/components/esp_system/port/panic_handler.c:141 (discriminator 1)<e>

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

Re: ESP32 UART interrupt handler with ESP-IDF

Postby ESP_Sprite » Tue Aug 09, 2022 7:54 am

Seems your shift key got stuck while typing the title; I took the liberty to fix that for you. Wrt the error: can you post the code? You're likely doing something in that ISR that you shouldn't do.

Semih Kahraman
Posts: 3
Joined: Tue Mar 15, 2022 7:17 am

Re: ESP32 UART interrupt handler with ESP-IDF

Postby Semih Kahraman » Tue Aug 09, 2022 8:49 am

Thank you for title.
my aim is to turn a led on and off every time it enters an interrupt and I want to observe it on the oscilloscope. I also want to see the incoming data from the serial screen. but esp resets itself when it spins infinitely.

Code: Select all

/*
 UART Interrupt Example
 */
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "esp_intr_alloc.h"
#include "soc/uart_reg.h"
#include "soc/uart_struct.h"
#include "esp_task_wdt.h"
#define BLINK_GPIO GPIO_NUM_15
#define ACM_CONTROL 12
static const char *TAG = "uart interrupt";
#define EX_UART_NUM UART_NUM_0
#define PATTERN_CHR_NUM    (3)         /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/
#define BUF_SIZE (1024)
#define RD_BUF_SIZE (BUF_SIZE)
#define TX_PIN 26
#define RX_PIN 25

// Both definition are same and valid
//static uart_isr_handle_t *handle_console;
static intr_handle_t handle_console;

// Receive buffer to collect incoming data
uint8_t rxbuf[512];
// Register to collect data length
uint16_t urxlen;
int led_status = 0;
uint16_t rx_fifo_len, status;
uint16_t i = 0;

static void IRAM_ATTR uart_intr_handle(void *arg) {
    gpio_set_level(BLINK_GPIO,1);
	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[i++] = UART0.fifo.rw_byte; // read all bytes
		rx_fifo_len--;
	}
	/* Blink off (output low) */
	// after reading bytes from buffer clear UART interrupt status
	uart_clear_intr_status(EX_UART_NUM,
	UART_RXFIFO_FULL_INT_CLR | UART_RXFIFO_TOUT_INT_CLR);
// a test code or debug code to indicate UART receives successfully,
// you can redirect received byte as echo also
//	uart_write_bytes(EX_UART_NUM, (const char*) "RX Done", 7);
    gpio_set_level(BLINK_GPIO,0);
}

void app_main() {

	gpio_pad_select_gpio(ACM_CONTROL);
	gpio_set_direction(ACM_CONTROL, GPIO_MODE_OUTPUT);
	gpio_set_level(ACM_CONTROL, 0);
	gpio_pad_select_gpio(BLINK_GPIO);
	/* Set the GPIO as a push/pull output */
	gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
	/* Configure parameters of an UART driver,
	 * communication pins and install the driver */
	uart_config_t uart_config = { .baud_rate = 9600, .data_bits =
			UART_DATA_8_BITS, .parity = UART_PARITY_ODD, .stop_bits =
			UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE };
	uart_param_config(EX_UART_NUM, &uart_config);
	//Set UART pins (using UART0 default pins ie no changes.)
	uart_set_pin(EX_UART_NUM, TX_PIN, RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
	//Install UART driver, and get the queue.
	uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0);
	// release the pre registered UART handler/subroutine
	uart_isr_free(EX_UART_NUM);
	// register new UART subroutine
    uart_isr_register(EX_UART_NUM,uart_intr_handle, NULL, ESP_INTR_FLAG_IRAM, &handle_console);
	// enable RX interrupt
	uart_enable_rx_intr(EX_UART_NUM);
}

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

Re: ESP32 UART interrupt handler with ESP-IDF

Postby ESP_Sprite » Wed Aug 10, 2022 12:34 am

You only clear the interrupt status for two specific events (rxfifo full and rx timeout). What do you think happens when another UART event happens that causes an interrupt?

Semih Kahraman
Posts: 3
Joined: Tue Mar 15, 2022 7:17 am

Re: ESP32 UART interrupt handler with ESP-IDF

Postby Semih Kahraman » Fri Aug 19, 2022 4:19 am

ESP_Sprite wrote:
Wed Aug 10, 2022 12:34 am
You only clear the interrupt status for two specific events (rxfifo full and rx timeout). What do you think happens when another UART event happens that causes an interrupt?
Thank you, I was able to read the UART with interrupt. I can control the interrupt condition with UART int_ena registers.

Who is online

Users browsing this forum: No registered users and 112 guests