由 ckdsx.cn 贡献,Ai-Thinker 进行整理
WB2 示例代码入门---外设串口
📜 点击展开完整代码
**#include** <stdio.h>
**#include** <string.h>
**#include** <FreeRTOS.h>
**#include** <task.h>
**#include** <blog.h> //
**#include** "bl_sys.h"
**#include** <stdio.h>
**#include** <cli.h>
**#include** <hosal_uart.h>
**#include** <blog.h> //include 一遍头文件即可
**#include** <hosal_uart.h>
**void** **TaskUart**(**void** *param) //新建无返回函数TaskUart
{
**uint8_t** data[32]; //定义 数组data
**int** ret; //定义变量 rethosal_uart_dev_t uart_dev_echo = { .config = { .uart_id = 0, .tx_pin = 4, // TXD GPIO .rx_pin = 3, // RXD GPIO .cts_pin = 255, .rts_pin = 255, .baud_rate = 9600, //波特率 9600,8,n,1 .data_width = HOSAL_DATA_WIDTH_8BIT, .parity = HOSAL_NO_PARITY, .stop_bits = HOSAL_STOP_BITS_1, .mode = HOSAL_UART_MODE_POLL, }, };
hosal_uart_dev_t uart_dev_log = { .config = { .uart_id = 1, .tx_pin = 16, // TXD GPIO .rx_pin = 7, // RXD GPIO .cts_pin = 255, .rts_pin = 255, .baud_rate = 115200, //波特率9600,8,n,1 .data_width = HOSAL_DATA_WIDTH_8BIT, .parity = HOSAL_NO_PARITY, .stop_bits = HOSAL_STOP_BITS_1, .mode = HOSAL_UART_MODE_POLL, },
📜 点击展开完整代码
};
/* Uart init device */
hosal_uart_init(&uart_dev_log); //初始化串口设备 日志
/* Uart init device */
hosal_uart_init(&uart_dev_echo); //初始化串口设备 回显
blog_info("Uart Demo Start"); //日志输出串口示例开始
**while** (1) //循环
{
/* Uart receive poll */ 串口接收
ret = hosal_uart_receive(&uart_dev_log, data, **sizeof**(data)); //将串口接收到的数据赋值给ret
**if** (ret > 0) //判断 如果ret大于0
{
/* Uart send poll */
hosal_uart_send(&uart_dev_log, data, ret); //串口输出ret 数据
blog_info("Get data "); //日志输出 获得数据
}
}
}
/**- @brief main
*/
📜 点击展开完整代码
**void** **main**(**void**) //主函数
{
xTaskCreate(TaskUart, "TaskUart", 1024, NULL, 15, NULL); //执行串口任务
}代码已添加注释,还有问题请留言! GPIO是对引脚的常规操作方式,点灯等程序都依靠GPIO方式可实现。

