zoukankan      html  css  js  c++  java
  • 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);
    }
    
  • 相关阅读:
    MCU软件最佳实践——独立按键
    MCU软件最佳实践——矩阵键盘驱动
    MCU软件最佳实践——使用printf打印数据
    CAP定理图解证明
    类型和变量
    数字ID过长 精度丢失 (已解决:后端方案)
    Springboot 异步线程池配置(小型应用)
    Java 数字 字符串 简单操作
    Java 网络请求
    Java 时间 日 周 月 季 年
  • 原文地址:https://www.cnblogs.com/hztd/p/14077883.html
Copyright © 2011-2022 走看看