/**************************************************************************** * Included Files ****************************************************************************/ #include "cm_iomux.h" #include "cm_gpio.h" #include "stdio.h" #include "stdlib.h" #include "stdarg.h" #include "cm_os.h" #include "cm_mem.h" #include "cm_sys.h" #include "cm_uart.h" #include "app_uart.h" #include "app_common.h" #include "radar.h" #define APP_UART_TASK_PRIORITY osPriorityNormal #define APP_URAT 0 //uart0 #if (APP_URAT == 0) #define APP_UARTTX_IOMUX CM_IOMUX_PIN_18, CM_IOMUX_FUNC_FUNCTION1 #define APP_UARTRX_IOMUX CM_IOMUX_PIN_17, CM_IOMUX_FUNC_FUNCTION1 #endif //uart1 #if (APP_URAT == 1) #define APP_UARTRX_IOMUX CM_IOMUX_PIN_28, CM_IOMUX_FUNC_FUNCTION1 #define APP_UARTTX_IOMUX CM_IOMUX_PIN_29, CM_IOMUX_FUNC_FUNCTION1 #endif #if (APP_URAT == 2) #define APP_UARTTX_IOMUX CM_IOMUX_PIN_50, CM_IOMUX_FUNC_FUNCTION3 #define APP_UARTRX_IOMUX CM_IOMUX_PIN_51, CM_IOMUX_FUNC_FUNCTION3 #endif #define UART_BUF_LEN 1024 // static int rx_rev_len = 0; static char rx_rev_data[UART_BUF_LEN] = {0}; static osThreadId_t os_UART_RX_ThreadId = NULL; //串口数据接收、解析任务Handle static osSemaphoreId_t uart_rx_sem = NULL; // 接收数据信号量 static osThreadId_t os_UART_TX_ThreadId = NULL; //串口数据发送任务Handle static osMessageQueueId_t uart_tx_msg_queue = NULL; // 发送消息队列 static osSemaphoreId_t uart_tx_ack_sem = NULL; // 发送成功应答信号量 typedef struct{ uint16_t msg_data_len; // 发送数据长度 uint8_t *msg_data; // 发送数据缓存 }uart_tx_msg_t; /* 串口接收示例,平时使用信号量挂起,当收到接收事件后,释放信号量以触发读取任务 */ static void Uart_RX_TaskHandle(void *param){ int temp_len = 0; while(1){ if(uart_rx_sem != NULL){ osSemaphoreAcquire(uart_rx_sem, osWaitForever);//阻塞 } temp_len = cm_uart_read(APP_URAT, (void*)&rx_rev_data[0], UART_BUF_LEN, 1000); radar_CheckData(rx_rev_data, temp_len); // 接收数据处理 // temp_len = cm_uart_read(APP_URAT, (void*)&rx_rev_data[rx_rev_len], UART_BUF_LEN, 1000); // rx_rev_len += temp_len; // if (rx_rev_len < UART_BUF_LEN){ // 处理数据 // memset((void*)rx_rev_data, 0, rx_rev_len); // temp_len = 0; // rx_rev_len = 0; // } // app_printf("uart rev data len = %d\n", rx_rev_len); // if (uart_rx_sem != NULL && (strstr(rx_rev_data, "\r\n"))){ // //处理收到数据事件 // cm_uart_write(APP_URAT, rx_rev_data, rx_rev_len, 1000); // memset((void*)rx_rev_data, 0, sizeof(rx_rev_data)); // rx_rev_len = 0; // } } } // 发送数据 int8_t uart0_send_msg(uint8_t *msg_data, uint16_t msg_data_len, uint32_t timeout){ uart_tx_msg_t tx_msg={0}; if(msg_data_len > UART_BUF_LEN || msg_data_len == 0){ cm_uart_write(APP_URAT, "TX too long\r\n", 23, 1000); return -3; //数据长度过长 } uint8_t *p_data =cm_malloc(msg_data_len); if(p_data == NULL){ cm_uart_write(APP_URAT, "TX malloc error\r\n", 14, 1000); return -2;//内存不足 } memcpy(p_data, msg_data, msg_data_len); tx_msg.msg_data_len = msg_data_len; tx_msg.msg_data = p_data; if(osOK == osMessageQueuePut(uart_tx_msg_queue, &tx_msg, 0, 20)){ // 发送消息队列满时,阻塞100ms if(uart_tx_ack_sem != NULL){ osSemaphoreAcquire(uart_tx_ack_sem, timeout); // cm_uart_write(APP_URAT, "TX send ok\r\n", 12, 1000); } }else{ cm_free(tx_msg.msg_data); tx_msg.msg_data = NULL; // cm_uart_write(APP_URAT, "TX msg queue full\r\n", 22, 1000); return -1; } return 0; } static void Uart_TX_TaskHandle(void *param){ uart_tx_msg_t send_msg={0}; uint16_t temp_len = 0; while(1){ if(osOK == osMessageQueueGet(uart_tx_msg_queue, &send_msg, NULL, osWaitForever)){ temp_len =cm_uart_write(APP_URAT, (void*)send_msg.msg_data, send_msg.msg_data_len, 1000); // if(temp_len == send_msg.msg_data_len){ // osSemaphoreRelease(uart_tx_ack_sem); //发送成功,释放信号量 // }else{ // // app_printf("uart tx send error,len=%d\n", temp_len); // } osSemaphoreRelease(uart_tx_ack_sem); //发送成功,释放信号量 send_msg.msg_data_len = 0; if(send_msg.msg_data != NULL){ cm_free(send_msg.msg_data); send_msg.msg_data = NULL; } } } } // 串口事件回调函数// 回调函数中不可输出LOG、串口打印、执行复杂任务或消耗过多资源,建议以信号量或消息队列形式控制其他线程执行任务 static void app_uart_event_callback(void *param, uint32_t type){ if (CM_UART_EVENT_TYPE_RX_ARRIVED & type){ /* 收到接收事件,触发其他线程执行读取数据 */ osSemaphoreRelease(uart_rx_sem); } if (CM_UART_EVENT_TYPE_RX_OVERFLOW & type){ /* 收到溢出事件,触发其他线程处理溢出事件 */ osSemaphoreRelease(uart_rx_sem); // 触发读取任务 } } void app_uart_init(void){ int32_t ret = -1; // 配置引脚复用 cm_iomux_set_pin_func(APP_UARTTX_IOMUX); cm_iomux_set_pin_func(APP_UARTRX_IOMUX); cm_iomux_set_pin_cmd(CM_IOMUX_PIN_17, CM_IOMUX_PINCMD3_PULL, CM_IOMUX_PINCMD3_FUNC2_PULL_HIGH); // cm_iomux_set_pin_cmd(CM_IOMUX_PIN_18, CM_IOMUX_PINCMD3_PULL, CM_IOMUX_PINCMD3_FUNC2_PULL_HIGH); // 事件参数 cm_uart_event_t uart_event = { CM_UART_EVENT_TYPE_RX_ARRIVED | CM_UART_EVENT_TYPE_RX_OVERFLOW, //注册需要上报的事件类型 "uart0", //用户参数 app_uart_event_callback //上报事件的回调函数 }; // 注册事件和回调函数 ret = cm_uart_register_event(APP_URAT, &uart_event); if(ret != RET_SUCCESS){ cm_log_printf(0, "uart register event err,ret=%d\n", ret); return; } // 配置参数 cm_uart_cfg_t uart_cfg = { CM_UART_BYTE_SIZE_8, CM_UART_PARITY_NONE, CM_UART_STOP_BIT_ONE, CM_UART_FLOW_CTRL_NONE, CM_UART_BAUDRATE_9600, 0 //配置为普通串口模式,若要配置为低功耗模式可改为1 }; // 开启串口 ret = cm_uart_open(APP_URAT, &uart_cfg); if (ret != RET_SUCCESS){ cm_log_printf(0, "uart init err,ret=%d\n", ret); return; } // // 配置uart唤醒功能,使能边沿检测才具备唤醒功能,仅主串口具有唤醒功能,用于唤醒的数据并不能被uart接收,请在唤醒后再进行uart数传 // cm_iomux_set_pin_cmd(APP_UARTRX_IOMUX , CM_IOMUX_PINCMD1_LPMEDEG, CM_IOMUX_PINCMD1_FUNC1_LPM_EDGE_RISE); // 串口接收处理任务 osThreadAttr_t uart_rx_task_attr = { .name = "uart_rx_task", .stack_size = 4096 * 4, .priority= APP_UART_TASK_PRIORITY }; os_UART_RX_ThreadId= osThreadNew(Uart_RX_TaskHandle, 0, &uart_rx_task_attr); if (uart_rx_sem == NULL) { uart_rx_sem = osSemaphoreNew(1, 0, NULL); } if(uart_tx_msg_queue == NULL){ uart_tx_msg_queue = osMessageQueueNew(1000, sizeof(uart_tx_msg_t), NULL); } if (uart_tx_ack_sem == NULL) { uart_tx_ack_sem = osSemaphoreNew(1, 0, NULL); } osThreadAttr_t uart_tx_task_attr = { .name = "uart_tx_task", .stack_size = 4096 * 4, .priority= APP_UART_TASK_PRIORITY }; os_UART_TX_ThreadId= osThreadNew(Uart_TX_TaskHandle, 0, &uart_tx_task_attr); } // /* 关闭串口 */ // void app_uart_close(void){ // cm_uart_dev_e dev = CM_UART_DEV_0; // if (0 == cm_uart_close(dev)){ // app_printf("uart%d close is ok\n", dev); // }else{ // app_printf("uart%d close is error\n", dev); // } // } void uart0_printf(char *str, ...){ static char s[1000]; //This needs to be large enough to store the string TODO Change magic number va_list args; int len; if ((str == NULL) || (strlen(str) == 0)){ return; } va_start(args, str); len = vsnprintf((char*)s, 1000, str, args); va_end(args); uart0_send_msg((uint8_t*)s, len, 0); } void app_printf(char *str, ...){ static char s[1000]; //This needs to be large enough to store the string TODO Change magic number va_list args; int len; if ((str == NULL) || (strlen(str) == 0)) { return; } va_start(args, str); len = vsnprintf((char*)s, 1000, str, args); va_end(args); // cm_uart_write(APP_URAT, s, len, 1000); uart0_send_msg((uint8_t*)s, len, osWaitForever); }