ESP8266学习笔记(21)——UART串口使用(RTOS SDK)

一、简介

ESP8266 有两个UART。UART0有TX、RX作为 系统的打印信息输出接口数据收发口,而UART1只有TX,作为 打印信息输出接口(调试用)。

二、UART0接收

2.1 相关函数

2.1.1 uart_param_config

2.1.2 uart_driver_install

2.1.3 uart_read_bytes

2.2 加入代码

/*********************************************************************
 * INCLUDES
 */
#include 
#include 
#include 
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"

#include "board_uart.h"

static void uartEventTask(void *pvParameters);

/*********************************************************************
 * LOCAL VARIABLES
 */
static QueueHandle_t s_uart0Queue;

static const char *TAG = "board_uart";

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief 串口驱动初始化
 @param 无
 @return 无
*/
void UART_Init(void)
{
    // Configure parameters of an UART driver,
    // communication pins and install the driver
    uart_config_t uart_config = 
    {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };

    uart_param_config(UART_NUM_0, &uart_config);                                            // 配置串口0参数

    // Install UART driver, and get the queue.
    uart_driver_install(UART_NUM_0, BUF_SIZE * 2, BUF_SIZE * 2, 100, &s_uart0Queue, 0);     // 安装UART驱动程序

    // Create a task to handler UART event from ISR
    xTaskCreate(uartEventTask, "uartEventTask", 2048, NULL, 12, NULL);  
}


/*********************************************************************
 * LOCAL FUNCTIONS
 */
static void uartEventTask(void *pvParameters)
{
    uart_event_t event;
    uint8_t *pTempBuf = (uint8_t *)malloc(UART_MAX_NUM_RX_BYTES);

    for(;;)
    {
        // Waiting for UART event.
        if(xQueueReceive(s_uart0Queue, (void *)&event, (portTickType)portMAX_DELAY))
        {
            bzero(pTempBuf, UART_MAX_NUM_RX_BYTES);

            switch(event.type)
            {
                // Event of UART receving data
                // We'd better handler data event fast, there would be much more data events than
                // other types of events. If we take too much time on data event, the queue might be full.
                case UART_DATA:
                    // ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
                    uart_read_bytes(UART_NUM_0, pTempBuf, event.size, portMAX_DELAY);
                    // uart_write_bytes(UART_NUM_0, (const char *)pTempBuf, event.size);
                    break;

                // Event of HW FIFO overflow detected
                case UART_FIFO_OVF:
                    ESP_LOGI(TAG, "hw fifo overflow");
                    // If fifo overflow happened, you should consider adding flow control for your application.
                    // The ISR has already reset the rx FIFO,
                    // As an example, we directly flush the rx buffer here in order to read more data.
                    uart_flush_input(UART_NUM_0);
                    xQueueReset(s_uart0Queue);
                    break;

                // Event of UART ring buffer full
                case UART_BUFFER_FULL:
                    ESP_LOGI(TAG, "ring buffer full");
                    // If buffer full happened, you should consider encreasing your buffer size
                    // As an example, we directly flush the rx buffer here in order to read more data.
                    uart_flush_input(UART_NUM_0);
                    xQueueReset(s_uart0Queue);
                    break;

                case UART_PARITY_ERR:
                    ESP_LOGI(TAG, "uart parity error");
                    break;

                // Event of UART frame error
                case UART_FRAME_ERR:
                    ESP_LOGI(TAG, "uart frame error");
                    break;

                // Others
                default:
                    ESP_LOGI(TAG, "uart event type: %d", event.type);
                    break;
            }
        }
    }

    free(pTempBuf);
    pTempBuf = NULL;
    vTaskDelete(NULL);
}

/****************************************************END OF FILE****************************************************/

2.3 使用流程

①串口初始化
UART_Init(); // 设置串口0
②串口中断处理
uartEventTask(void *pvParameters); // 串口事件处理函数
主要修改下面这个函数
在 case UART_DATA 中使用 uart_read_bytes 读出数据然后进行处理

三、UART0发送

3.1 相关函数

3.1.1 uart_write_bytes

3.2 使用流程

①串口初始化
UART_Init(); // 设置串口0
②串口发送数据
uart_write_bytes(uart_port_t uart_num, const char *src, size_t size); // UART0 发送数据函数

四、UART1打印日志

在 ESP8266_RTOS_SDK-3.2 中任何例程中输入make menuconfig
选择 Component config


选择 Common ESP-related

选择 UART for console output

从默认串口0改为 Customon

选择 UART peripheral to use for console output 改为 UART1 输出


• 由 Leung 写于 2019 年 4 月 30 日

• 参考:ESP8266 RTOS SDK编程指南 - UART

你可能感兴趣的:(ESP8266学习笔记(21)——UART串口使用(RTOS SDK))