#include "radar_uart.h" static struct device *radar_uart_hdl = NULL;//串口操作句柄 static u8 radar_uart_rx_buf[RADAR_UART_RX_BUF_SIZE]//串口驱动环形缓冲区 __attribute__((aligned(32))); /* UART1初始化 PC6 = TX PC7 = RX */ UART1_PLATFORM_DATA_BEGIN(radar_uart_data) .baudrate = RADAR_UART_BAUDRATE, .port = PORT_REMAP, .tx_pin = IO_PORTC_06, .rx_pin = IO_PORTC_07, .output_channel = OUTPUT_CHANNEL3, .input_channel = INPUT_CHANNEL3, .max_continue_recv_cnt = RADAR_UART_RX_BUF_SIZE, .idle_sys_clk_cnt = 500000, .clk_src = PLL_48M, UART1_PLATFORM_DATA_END(); /* 注册串口设备 */ REGISTER_DEVICES(radar_uart_devices) = { { RADAR_UART_NAME, &uart_dev_ops, (void *)&radar_uart_data }, }; int radar_uart_init(void) { int ret; if (radar_uart_hdl) { return 0; } radar_uart_hdl = dev_open(RADAR_UART_NAME, NULL); if (!radar_uart_hdl) { return -1; }//句柄写入失败跳出返回-1 ret = dev_ioctl( radar_uart_hdl, UART_SET_CIRCULAR_BUFF_ADDR,//配置驱动缓冲区地址 (u32)radar_uart_rx_buf ); if (ret) { goto error; } ret = dev_ioctl( radar_uart_hdl, UART_SET_CIRCULAR_BUFF_LENTH,//配置驱动缓冲区长度 sizeof(radar_uart_rx_buf) ); if (ret) { goto error; } ret = dev_ioctl( radar_uart_hdl, UART_SET_RECV_BLOCK,//配置为阻塞接收模式 1 ); if (ret) { goto error; } ret = dev_ioctl( radar_uart_hdl, UART_SET_RECV_TIMEOUT,//配置接收超时时间 RADAR_UART_RX_TIMEOUT_MS ); if (ret) { goto error; } ret = dev_ioctl( radar_uart_hdl, UART_START,//启动串口 0 ); if (ret) { goto error; } return 0; //goto error时会跳转至此,释放资源返回ret error: dev_close(radar_uart_hdl); radar_uart_hdl = NULL; return ret; } int radar_uart_read(u8 *buf, u32 len)//从驱动缓冲区读取数据并存到buf中,len为读取的最大长度 { if (!radar_uart_hdl || !buf || !len) { return -1; } return dev_read(radar_uart_hdl, buf, len); } int radar_uart_write(const u8 *data, u32 len)//给雷达写命令,data为写入数据,len为实际写入长度 { if (!radar_uart_hdl || !data || !len) { return -1; } return dev_write( radar_uart_hdl, (void *)data, len ); }