• ESP8266 RTOS 开发笔记(4)串口透传


    串口部分简单,直接copy示例代码稍作修改即可

    static void uart_event_task(void *pvParameters)
    {
        uart_event_t event;
        size_t buffered_size;
        uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
        for(;;) {
            //Waiting for UART event.
            if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
                bzero(dtmp, RD_BUF_SIZE);
                ESP_LOGI(TAG, "uart[%d] event:", UART_NUM_0);
                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, dtmp, event.size, portMAX_DELAY);
                        //ESP_LOGI(TAG, "[DATA EVT]:");
                        //uart_write_bytes(UART_NUM_0, (const char*)dtmp, 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(uart0_queue);
                        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(uart0_queue);
                        break;
                    //Event of UART parity check error
                    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(dtmp);
        dtmp = NULL;
        vTaskDelete(NULL);
    }
    
    
    void serialInit()
    {
        //uart_set_baudrate(UART_NUM_0,params.Uart.baud_rate);//初始化波特率为115200
        /* Configure parameters of an UART driver,
         * communication pins and install the driver */
          //用户参数加载
        uart_config_t uart_config = {
            .baud_rate = params.Uart.baud_rate,
            .data_bits = params.Uart.data_bits,
            .parity = params.Uart.parity,
            .stop_bits = params.Uart.stop_bits,
            .flow_ctrl = params.Uart.flow_ctrl,
            .rx_flow_ctrl_thresh = params.Uart.rx_flow_ctrl_thresh
        };
        //Install UART driver, and get the queue.
        uart_param_config(UART_NUM_0, &uart_config);
        uart_driver_install(UART_NUM_0, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);    
    
        //Set UART log level
        //esp_log_level_set(TAG, ESP_LOG_INFO);    
    
        //Create a task to handler UART event from ISR
        xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
    }
    
  • 相关阅读:
    能飞英语学习软件学习实践
    英语学习方式总结与实践
    Hello World
    centos 7.6中搭建samba共享服务
    PHP漏洞全解(一)PHP网站的安全性问题
    MySQL查询语句练习题
    在PHP中使用CURL实现GET和POST请求的方法
    js数组的操作大全
    php四种基础算法:冒泡,选择,插入和快速排序法
    Linux查看端口使用状态及启动
  • 原文地址:https://www.cnblogs.com/hztd/p/14077883.html
Copyright © 2020-2023  润新知