hmi_t.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * 程序清单:这是一个串口设备 DMA 接收使用例程
  3. * 例程导出了 uart_dma_sample 命令到控制终端
  4. * 命令调用格式:uart_dma_sample uart3
  5. * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
  6. * 程序功能:通过串口输出字符串"hello RT-Thread!",并通过串口输出接收到的数据,然后打印接收到的数据。
  7. */
  8. #include <rtthread.h>
  9. #define HMI_UART_NAME "uart4" /* 串口设备名称 */
  10. /*刷新时钟数据*/
  11. void Display_RTC(void);
  12. /* 串口发送消息结构*/
  13. struct tx_msg
  14. {
  15. rt_uint8_t *data;
  16. rt_uint32_t size;
  17. };
  18. /* 串口设备句柄 */
  19. static rt_device_t serial;
  20. /* 消息队列控制块 */
  21. static struct rt_messagequeue tx_mq;//发送数据消息队列
  22. /* HMI显示功能函数 */
  23. rt_err_t HMI_Display(void *Data, rt_size_t length)
  24. {
  25. struct tx_msg msg;
  26. rt_err_t result;
  27. msg.data = Data;
  28. msg.size = length + 3;
  29. *(msg.data + length ) = 0xff;
  30. *(msg.data + length + 1) = 0xff;
  31. *(msg.data + length + 2) = 0xff;
  32. result = rt_mq_send(&tx_mq, (void*)&msg, sizeof(struct tx_msg));
  33. if ( result == -RT_EFULL)
  34. {
  35. /* 消息队列满 */
  36. rt_kprintf("message queue full!\n");
  37. }rt_thread_mdelay(10);//必须加延时
  38. return result;
  39. }
  40. static void serial_out_thread_entry(void *parameter)
  41. {
  42. struct tx_msg msg;
  43. rt_err_t result;
  44. rt_uint32_t tx_length;
  45. while (1)
  46. {
  47. result = rt_mq_recv(&tx_mq, (void*)&msg, sizeof(struct tx_msg), RT_WAITING_FOREVER);
  48. if (result == RT_EOK)
  49. {
  50. /* 发送数据 */
  51. //rt_kprintf("uart4 TX %d >> %s \n" ,msg.size , msg.data);
  52. rt_device_write(serial, 0, msg.data,(uint32_t)msg.size);
  53. }
  54. rt_thread_mdelay(10);
  55. }
  56. }
  57. static char msg_pool[1024];
  58. // 串口发送数据线程
  59. static int HMI_dma_t(int argc, char *argv[])
  60. {
  61. rt_err_t ret = RT_EOK;
  62. char uart_name[RT_NAME_MAX];
  63. char str[] = "hello RT-Thread!\r\n";
  64. if (argc == 2)
  65. {
  66. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  67. }
  68. else
  69. {
  70. rt_strncpy(uart_name, HMI_UART_NAME, RT_NAME_MAX);
  71. }
  72. /* 查找串口设备 */
  73. serial = rt_device_find(uart_name);
  74. if (!serial)
  75. {
  76. rt_kprintf("find %s failed!\n", uart_name);
  77. return RT_ERROR;
  78. }
  79. /* 初始化消息队列 */
  80. rt_mq_init(&tx_mq, "tx_mq",
  81. msg_pool, /* 存放消息的缓冲区 */
  82. 50,
  83. // sizeof(struct tx_msg), /* 一条消息的最大长度 */
  84. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  85. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  86. /* 以 DMA 接收及轮询发送方式打开串口设备 */
  87. // rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
  88. /* 发送字符串 */
  89. rt_device_write(serial, 0, str, (sizeof(str) - 1));
  90. /* 创建 serial 线程 */
  91. rt_thread_t thread1 = rt_thread_create("tx", serial_out_thread_entry, RT_NULL, 256, 20, 10);//任务优先级必须大于"Hmi_flash"线程
  92. /* 创建成功则启动线程 */
  93. if (thread1 != RT_NULL)
  94. {
  95. rt_thread_startup(thread1);
  96. }
  97. else
  98. {
  99. ret = RT_ERROR;
  100. }
  101. /* 创建 serial 线程 */
  102. return ret;
  103. }
  104. INIT_COMPONENT_EXPORT(HMI_dma_t);