uart_dma_sample.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-16 misonyo first implementation.
  9. */
  10. /*
  11. * 程序清单:这是一个串口设备 DMA 接收使用例程
  12. * 例程导出了 uart_dma_sample 命令到控制终端
  13. * 命令调用格式:uart_dma_sample uart3
  14. * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
  15. * 程序功能:通过串口输出字符串"hello RT-Thread!",并通过串口输出接收到的数据,然后打印接收到的数据。
  16. */
  17. #include <rtthread.h>
  18. #define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */
  19. /* 串口接收消息结构*/
  20. struct rx_msg
  21. {
  22. rt_device_t dev;
  23. rt_size_t size;
  24. };
  25. /* 串口设备句柄 */
  26. static rt_device_t serial;
  27. /* 消息队列控制块 */
  28. static struct rt_messagequeue rx_mq;
  29. /* 接收数据回调函数 */
  30. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  31. {
  32. struct rx_msg msg;
  33. rt_err_t result;
  34. msg.dev = dev;
  35. msg.size = size;
  36. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  37. if ( result == -RT_EFULL)
  38. {
  39. /* 消息队列满 */
  40. rt_kprintf("message queue full!\n");
  41. }
  42. return result;
  43. }
  44. static void serial_thread_entry(void *parameter)
  45. {
  46. struct rx_msg msg;
  47. rt_err_t result;
  48. rt_uint32_t rx_length;
  49. static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
  50. while (1)
  51. {
  52. rt_memset(&msg, 0, sizeof(msg));
  53. /* 从消息队列中读取消息*/
  54. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
  55. if (result == RT_EOK)
  56. {
  57. /* 从串口读取数据*/
  58. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  59. rx_buffer[rx_length] = '\0';
  60. /* 通过串口设备 serial 输出读取到的消息 */
  61. rt_device_write(serial, 0, rx_buffer, rx_length);
  62. /* 打印数据 */
  63. rt_kprintf("%s\n",rx_buffer);
  64. }
  65. }
  66. }
  67. static int uart_dma_sample(int argc, char *argv[])
  68. {
  69. rt_err_t ret = RT_EOK;
  70. char uart_name[RT_NAME_MAX];
  71. static char msg_pool[256];
  72. char str[] = "hello RT-Thread!\r\n";
  73. if (argc == 2)
  74. {
  75. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  76. }
  77. else
  78. {
  79. rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
  80. }
  81. /* 查找串口设备 */
  82. serial = rt_device_find(uart_name);
  83. if (!serial)
  84. {
  85. rt_kprintf("find %s failed!\n", uart_name);
  86. return RT_ERROR;
  87. }
  88. /* 初始化消息队列 */
  89. rt_mq_init(&rx_mq, "rx_mq",
  90. msg_pool, /* 存放消息的缓冲区 */
  91. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  92. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  93. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  94. /* 以 DMA 接收及轮询发送方式打开串口设备 */
  95. rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
  96. /* 设置接收回调函数 */
  97. rt_device_set_rx_indicate(serial, uart_input);
  98. /* 发送字符串 */
  99. rt_device_write(serial, 0, str, (sizeof(str) - 1));
  100. /* 创建 serial 线程 */
  101. rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
  102. /* 创建成功则启动线程 */
  103. if (thread != RT_NULL)
  104. {
  105. rt_thread_startup(thread);
  106. }
  107. else
  108. {
  109. ret = RT_ERROR;
  110. }
  111. return ret;
  112. }
  113. /* 导出到 msh 命令列表中 */
  114. MSH_CMD_EXPORT(uart_dma_sample, uart device dma sample);