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 <stdio.h>
#include <stdlib.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 "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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章