| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- /*
- * 程序清单:这是一个串口设备 DMA 接收使用例程
- * 例程导出了 uart_dma_sample 命令到控制终端
- * 命令调用格式:uart_dma_sample uart3
- * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
- * 程序功能:通过串口输出字符串"hello RT-Thread!",并通过串口输出接收到的数据,然后打印接收到的数据。
- */
- #define LOG_TAG "hmi.r" // 该模块对应的标签。不定义时,默认:NO_TAG
- #define LOG_LVL LOG_LVL_DBG// ELOG_LVL_WARN // 该模块对应的日志输出级别。不定义时,默认:调试级别
- #include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
- #include <rtthread.h>
- #include <rtdevice.h>
- #define SAMPLE_UART_NAME "uart4" /* 串口设备名称 */
- struct rt_ringbuffer recv_rb;// 定义一个环形队列
- uint8_t HMI_Decode(unsigned char *Read_CMD);
- /* 串口接收消息结构*/
- struct rx_msg
- {
- rt_device_t dev;
- rt_size_t size;
- };
- /* 串口设备句柄 */
- static rt_device_t serial;
- /* 消息队列控制块 */
- static struct rt_messagequeue rx_mq;//接收数据消息队列
- static char cachebuf[128] = {0};
- /* 接收数据回调函数 */
- static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
- {
- struct rx_msg msg;
- rt_err_t result;
- msg.dev = dev;
- msg.size = size;
- //rt_kprintf("rt_mq_send MARK C\n");
- result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
- //rt_kprintf("uart_input:%d \n", dev->device_id);
- if ( result == -RT_EFULL)
- {
- /* 消息队列满 */
- rt_kprintf("rx message queue full!\n");
- rt_mq_recv(&rx_mq, cachebuf, sizeof(cachebuf), RT_WAITING_NO);
- }
- return result;
- }
- static void serial_thread_entry(void *parameter)
- {
- struct rx_msg msg;
- rt_err_t result;
- rt_uint32_t rx_length;
- static unsigned char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
- //rt_kprintf("serial_thread_entry \n\n");
- while (1)
- {
- rt_memset(&msg, 0, sizeof(msg));
- /* 从消息队列中读取消息*/
- result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
- if (result == RT_EOK)
- {
- /* 从串口读取数据*/
- rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
- rt_ringbuffer_put(&recv_rb, rx_buffer, rx_length);// 存入环形队列
- // rt_kprintf("rx_buffer %d >> " , rx_length);
- // for (uint8_t a = 0; a < rx_length; a++)
- // {
- // rt_kprintf("%X " ,rx_buffer[a]);
- // }
- // rt_kprintf("\n");
- // if( (rx_buffer[rx_length-1] == 0xff ) && (rx_buffer[rx_length-2] == 0xff) && (rx_buffer[rx_length-3] == 0xff) )
- // {
- // HMI_Decode(rx_buffer);
- // }
- }
- //rt_thread_mdelay(10);
- }
- }
-
- //从接收缓冲区取出一个字节消息
- uint8_t Get_Byte_CLI(uint8_t *dat)
- {
- return(rt_ringbuffer_getchar(&recv_rb,dat));
- }
- unsigned char Read_CMD[256]; //一帧信息的缓冲区
- static unsigned char CMD_Count=0;//计数器
- //检测数据流中是否有一帧信息 读取的CMD;数据流的缓冲区
- unsigned char Check_CMD(void)
- {
- uint16_t i;
- CMD_Count =0;
-
- for( i=0;i<=255;i++ )Read_CMD[i] = 0;/*清空CMD缓冲区*/
- // if(Get_Mess_Count_CLI() == 0)return 0;
- if( rt_ringbuffer_data_len(&recv_rb) == 0)return 0;
- //执行到这里表示缓冲区有信息
- while(Get_Byte_CLI(&Read_CMD[CMD_Count++]))
- {
- if(CMD_Count >=254 )//表示超出了数据处理的范围了
- {
- CMD_Count = 0;
- rt_ringbuffer_reset(&recv_rb);//清空接收队列
- log_i("Error:CMD Too Long!\r\n");
- return 0;
- }
- if( Read_CMD[0] == 0xff ) return 0;
- if( (Read_CMD[CMD_Count-1] == 0xff ) && (Read_CMD[CMD_Count-2] == 0xff) && (Read_CMD[CMD_Count-3] == 0xff) )
- {
- // for(i = 0;i < CMD_Count;i ++){log_i("%x ",Read_CMD[i]);}log_i("\n");
- return 1;
- }
- }
- // Clear_CLI();//清空接收队列
- return 0;
- }
- static char msg_pool[256];
- static uint8_t rb_pool[1024];
- static int HMI_dma_r(int argc, char *argv[])
- {
- rt_err_t ret = RT_EOK;
- char uart_name[RT_NAME_MAX];
-
- rt_ringbuffer_init(&recv_rb, rb_pool, sizeof(rb_pool));//初始化环形队列
- if (argc == 2)
- {
- rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
- }
- else
- {
- rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
- }
- /* 查找串口设备 */
- serial = rt_device_find(uart_name);
- if (!serial)
- {
- rt_kprintf("find %s failed!\n", uart_name);
- return RT_ERROR;
- }
- // else
- // {
- // rt_kprintf("HMI_dma_r name > %s %d \n" , uart_name , serial->device_id);
- // }
- /* 初始化消息队列 */
- rt_mq_init(&rx_mq, "rx_mq",
- msg_pool, /* 存放消息的缓冲区 */
- sizeof(struct rx_msg), /* 一条消息的最大长度 */
- sizeof(msg_pool), /* 存放消息的缓冲区大小 */
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
- /* 以 DMA 接收及轮询发送方式打开串口设备 */
- rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
- /* 设置接收回调函数 */
- rt_device_set_rx_indicate(serial, uart_input);
- /* 创建 serial 线程 */
- rt_thread_t thread = rt_thread_create("rx", serial_thread_entry, RT_NULL, 256, 20, 10);
- /* 创建成功则启动线程 */
- if (thread != RT_NULL)
- {
- rt_thread_startup(thread);
- }
- else
- {
- ret = RT_ERROR;
- }
- return ret;
- }
- INIT_APP_EXPORT(HMI_dma_r);
- /* 导出到 msh 命令列表中 */
|