hmi_r.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * 程序清单:这是一个串口设备 DMA 接收使用例程
  3. * 例程导出了 uart_dma_sample 命令到控制终端
  4. * 命令调用格式:uart_dma_sample uart3
  5. * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
  6. * 程序功能:通过串口输出字符串"hello RT-Thread!",并通过串口输出接收到的数据,然后打印接收到的数据。
  7. */
  8. #define LOG_TAG "hmi.r" // 该模块对应的标签。不定义时,默认:NO_TAG
  9. #define LOG_LVL LOG_LVL_DBG// ELOG_LVL_WARN // 该模块对应的日志输出级别。不定义时,默认:调试级别
  10. #include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #define SAMPLE_UART_NAME "uart4" /* 串口设备名称 */
  14. struct rt_ringbuffer recv_rb;// 定义一个环形队列
  15. uint8_t HMI_Decode(unsigned char *Read_CMD);
  16. /* 串口接收消息结构*/
  17. struct rx_msg
  18. {
  19. rt_device_t dev;
  20. rt_size_t size;
  21. };
  22. /* 串口设备句柄 */
  23. static rt_device_t serial;
  24. /* 消息队列控制块 */
  25. static struct rt_messagequeue rx_mq;//接收数据消息队列
  26. static char cachebuf[128] = {0};
  27. /* 接收数据回调函数 */
  28. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  29. {
  30. struct rx_msg msg;
  31. rt_err_t result;
  32. msg.dev = dev;
  33. msg.size = size;
  34. //rt_kprintf("rt_mq_send MARK C\n");
  35. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  36. //rt_kprintf("uart_input:%d \n", dev->device_id);
  37. if ( result == -RT_EFULL)
  38. {
  39. /* 消息队列满 */
  40. rt_kprintf("rx message queue full!\n");
  41. rt_mq_recv(&rx_mq, cachebuf, sizeof(cachebuf), RT_WAITING_NO);
  42. }
  43. return result;
  44. }
  45. static void serial_thread_entry(void *parameter)
  46. {
  47. struct rx_msg msg;
  48. rt_err_t result;
  49. rt_uint32_t rx_length;
  50. static unsigned char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
  51. //rt_kprintf("serial_thread_entry \n\n");
  52. while (1)
  53. {
  54. rt_memset(&msg, 0, sizeof(msg));
  55. /* 从消息队列中读取消息*/
  56. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
  57. if (result == RT_EOK)
  58. {
  59. /* 从串口读取数据*/
  60. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  61. rt_ringbuffer_put(&recv_rb, rx_buffer, rx_length);// 存入环形队列
  62. // rt_kprintf("rx_buffer %d >> " , rx_length);
  63. // for (uint8_t a = 0; a < rx_length; a++)
  64. // {
  65. // rt_kprintf("%X " ,rx_buffer[a]);
  66. // }
  67. // rt_kprintf("\n");
  68. // if( (rx_buffer[rx_length-1] == 0xff ) && (rx_buffer[rx_length-2] == 0xff) && (rx_buffer[rx_length-3] == 0xff) )
  69. // {
  70. // HMI_Decode(rx_buffer);
  71. // }
  72. }
  73. //rt_thread_mdelay(10);
  74. }
  75. }
  76. //从接收缓冲区取出一个字节消息
  77. uint8_t Get_Byte_CLI(uint8_t *dat)
  78. {
  79. return(rt_ringbuffer_getchar(&recv_rb,dat));
  80. }
  81. unsigned char Read_CMD[256]; //一帧信息的缓冲区
  82. static unsigned char CMD_Count=0;//计数器
  83. //检测数据流中是否有一帧信息 读取的CMD;数据流的缓冲区
  84. unsigned char Check_CMD(void)
  85. {
  86. uint16_t i;
  87. CMD_Count =0;
  88. for( i=0;i<=255;i++ )Read_CMD[i] = 0;/*清空CMD缓冲区*/
  89. // if(Get_Mess_Count_CLI() == 0)return 0;
  90. if( rt_ringbuffer_data_len(&recv_rb) == 0)return 0;
  91. //执行到这里表示缓冲区有信息
  92. while(Get_Byte_CLI(&Read_CMD[CMD_Count++]))
  93. {
  94. if(CMD_Count >=254 )//表示超出了数据处理的范围了
  95. {
  96. CMD_Count = 0;
  97. rt_ringbuffer_reset(&recv_rb);//清空接收队列
  98. log_i("Error:CMD Too Long!\r\n");
  99. return 0;
  100. }
  101. if( Read_CMD[0] == 0xff ) return 0;
  102. if( (Read_CMD[CMD_Count-1] == 0xff ) && (Read_CMD[CMD_Count-2] == 0xff) && (Read_CMD[CMD_Count-3] == 0xff) )
  103. {
  104. // for(i = 0;i < CMD_Count;i ++){log_i("%x ",Read_CMD[i]);}log_i("\n");
  105. return 1;
  106. }
  107. }
  108. // Clear_CLI();//清空接收队列
  109. return 0;
  110. }
  111. static char msg_pool[256];
  112. static uint8_t rb_pool[1024];
  113. static int HMI_dma_r(int argc, char *argv[])
  114. {
  115. rt_err_t ret = RT_EOK;
  116. char uart_name[RT_NAME_MAX];
  117. rt_ringbuffer_init(&recv_rb, rb_pool, sizeof(rb_pool));//初始化环形队列
  118. if (argc == 2)
  119. {
  120. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  121. }
  122. else
  123. {
  124. rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
  125. }
  126. /* 查找串口设备 */
  127. serial = rt_device_find(uart_name);
  128. if (!serial)
  129. {
  130. rt_kprintf("find %s failed!\n", uart_name);
  131. return RT_ERROR;
  132. }
  133. // else
  134. // {
  135. // rt_kprintf("HMI_dma_r name > %s %d \n" , uart_name , serial->device_id);
  136. // }
  137. /* 初始化消息队列 */
  138. rt_mq_init(&rx_mq, "rx_mq",
  139. msg_pool, /* 存放消息的缓冲区 */
  140. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  141. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  142. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  143. /* 以 DMA 接收及轮询发送方式打开串口设备 */
  144. rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  145. /* 设置接收回调函数 */
  146. rt_device_set_rx_indicate(serial, uart_input);
  147. /* 创建 serial 线程 */
  148. rt_thread_t thread = rt_thread_create("rx", serial_thread_entry, RT_NULL, 256, 20, 10);
  149. /* 创建成功则启动线程 */
  150. if (thread != RT_NULL)
  151. {
  152. rt_thread_startup(thread);
  153. }
  154. else
  155. {
  156. ret = RT_ERROR;
  157. }
  158. return ret;
  159. }
  160. INIT_APP_EXPORT(HMI_dma_r);
  161. /* 导出到 msh 命令列表中 */