ESP8266 RTOS串口uart的初始代设置

因os_print有时会出现不输出的问题,所以测试时更建议使用user_uart0_print()来输出,


#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "user_uart.h"
#include "driver/uart.h"
#define BUF_SIZE (1024)  //RX环形缓冲区的大小。
//初始化
void user_uart_init(void){
   
    uart_config_t uart_config ={
        .baud_rate = 74880,//波特率
        .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);  //设置为uart端口号0
   if(uart_driver_install(UART_NUM_0, BUF_SIZE * 2, 0, 0, NULL, 0)==ESP_OK){
   user_uart_driver_Status0 = uart_initialized; //安装UART驱动程序     
   }
}

////输出数据到用户定义的串口中(多参数)
void user_uart0_print(char* str,...)
{
  char temp_str[256]; 
  memset(temp_str,0,256);
  va_list args;   //定义一个va_list类型的变量args
  va_start(args, str);//获取函数参数列表中可变参数的首指针,即紧跟fmt后面的参数,故args用来保存函数参数列表中可变参数的首指针
  vsprintf(temp_str,str,args);//转换参数
  va_end(args); 
  //检查驱动状态
  if (user_uart_driver_Status0 == uart_uninitialized)
    {
       user_uart_init(); 
    }
   //发送数据到端口
    uart_write_bytes(UART_NUM_0,(const char *)temp_str,strlen(temp_str));
}

头文件

#ifndef _USER_UART_H_
#define _USER_UART_H_
typedef enum {
   uart_uninitialized   = 0x0,          /* A thread is querying the state of itself, so must be running. */
   uart_initialized     = 0x1,                  /* The thread being queried has been deleted, but its TCB has not yet been freed. */   
   uart_initializedError = 0x7FFFFFFF
} user_uart_driver_Status;
user_uart_driver_Status user_uart_driver_Status0;


/*
使用说明
 char* test_str = "Welcome to www.meanea.com\r\n";
    TickType_t lasttick = xTaskGetTickCount();
    while (1)
    {
           vTaskDelayUntil(&lasttick, 100);
           uart_write_bytes(UART_NUM_0,(const char *)test_str,strlen(test_str));
    }
   vTaskDelete( NULL );
*/
void user_uart_init();
void user_uart0_print(char* str,...);

#endif
#endif
posted @ 2020-04-28 10:13  天祈笨笨  阅读(473)  评论(0编辑  收藏  举报