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);
}