发送数据
Code: Select all
0x61 0x61 0x61
Code: Select all
0x61 0x61 0x61 0x4F 0x4B 0x01 0x4F 0x4B 0x02
Code: Select all
#include <stdio.h>
#include <string.h>
#include "soc/uart_periph.h"
#include "driver/uart.h"
#include "soc/uhci_reg.h"
#include "driver/gpio.h"
#include "esp_event.h"
#include "esp_log.h"
TaskHandle_t rx_task_handle;
#define UART_LOG "uart_log" //串口TAG
#define UART1_RX_IO GPIO_NUM_9 //UART1_RX引脚 -> GPIO9
#define UART1_TX_IO GPIO_NUM_10 //UART1_TX引脚 -> GPIO10
#define BUFF_SIZE 1024
uart_isr_handle_t uart1_handle;
uint8_t rxbuf[128];
uint16_t urxlen;
static void IRAM_ATTR uart_intr_handle(void *arg)
{
volatile uart_dev_t *uart = &UART1;
static uint8_t count = 0;
count++;
volatile uint8_t flag = 0; //标志位判断是否触发rxfifo_tout中断
if(uart->int_st.rxfifo_tout == 1)
{
uart_clear_intr_status(UART_NUM_1, UART_RXFIFO_TOUT_INT_CLR);
flag = 1; //触发rxfifo_tout中断,标志位置1
}
while (uart->status.rxfifo_cnt) {
uint8_t c = uart->ahb_fifo.rw_byte;
uart_tx_chars(UART_NUM_1, (char *)&c, 1); //返回数据
}
if(flag)
uart_tx_chars(UART_NUM_1, "OK", 2); //进入rxfifo_tout中断返回OK
uart_tx_chars(UART_NUM_1,(char *)&count,1); //返回当前进入中断次数
}
void app_main(void)
{
uart_config_t uart_conf = {
.baud_rate = 115200, //波特率:115200
.data_bits = UART_DATA_8_BITS, //数据位:八位
.parity = UART_PARITY_DISABLE, //校验位:不校验
.stop_bits = UART_STOP_BITS_1, //停止位:1
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, //硬件控制流:不启用
.source_clk = UART_SCLK_APB, //时钟总线:APB总线
};
uart_port_t uart_port = UART_NUM_1; //UART1
uart_param_config(uart_port, &uart_conf);
uart_set_pin(uart_port, UART1_TX_IO, UART1_RX_IO, GPIO_NUM_NC, GPIO_NUM_NC);
uart_driver_install(uart_port, BUFF_SIZE*2, 0, 0, NULL, 0);
//关闭预先创建的uart中断
uart_isr_free(uart_port);
uart_isr_register(uart_port, uart_intr_handle,NULL, ESP_INTR_FLAG_IRAM, &uart1_handle);
uart_disable_intr_mask(uart_port, 0x001FFFFF); //关闭预先的串口中断
uart_enable_intr_mask(uart_port, UART_RXFIFO_TOUT_INT_ENA); //开启RXFIFO_TOUT中断
while(1)
{
vTaskDelay(1);
}
}