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);
    }
    
  • 相关阅读:
    【转】日本留学——修士申请注意事项
    【转】日本留学读研究生和修士有什么区别?申请误区有哪些
    【转】为什么说学一门小语种,就能打开新世界的大门?
    【转】TED:两年一门外语--她总结了学外语的秘诀
    【转】为什么一定要学一门外语?
    【转】学完标准日本语初级至高级,可以过日语n1吗?
    【转】去日本语言学校前,日语应该达到几级呢?
    Cordova学习
    敏捷开发实录(二)
    Mac端博客发布工具推荐
  • 原文地址:https://www.cnblogs.com/hztd/p/14077883.html
Copyright © 2011-2022 走看看