dma_sample.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-12-10 thread-liu first version
  9. */
  10. #include <board.h>
  11. #if defined(BSP_UART3_RX_USING_DMA) || defined(BSP_USING_UART3)
  12. #include <rtthread.h>
  13. #define SAMPLE_UART_NAME "uart3" /* serial device name */
  14. struct rx_msg
  15. {
  16. rt_device_t dev;
  17. rt_size_t size;
  18. };
  19. static rt_device_t serial;
  20. static struct rt_messagequeue rx_mq;
  21. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  22. {
  23. struct rx_msg msg;
  24. rt_err_t result;
  25. msg.dev = dev;
  26. msg.size = size;
  27. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  28. if ( result == -RT_EFULL)
  29. {
  30. rt_kprintf("message queue full!\n");
  31. }
  32. return result;
  33. }
  34. static void serial_thread_entry(void *parameter)
  35. {
  36. struct rx_msg msg;
  37. rt_err_t result;
  38. rt_uint32_t rx_length;
  39. static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
  40. while (1)
  41. {
  42. rt_memset(&msg, 0, sizeof(msg));
  43. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
  44. if (result == RT_EOK)
  45. {
  46. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  47. rx_buffer[rx_length] = '\0';
  48. rt_device_write(serial, 0, rx_buffer, rx_length);
  49. }
  50. }
  51. }
  52. static int uart_dma_sample(int argc, char *argv[])
  53. {
  54. rt_err_t ret = RT_EOK;
  55. char uart_name[RT_NAME_MAX];
  56. static char msg_pool[256];
  57. char str[] = "hello RT-Thread!\r\n";
  58. if (argc == 2)
  59. {
  60. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  61. }
  62. else
  63. {
  64. rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
  65. }
  66. serial = rt_device_find(uart_name);
  67. if (!serial)
  68. {
  69. rt_kprintf("find %s failed!\n", uart_name);
  70. return RT_ERROR;
  71. }
  72. rt_mq_init(&rx_mq, "rx_mq",
  73. msg_pool,
  74. sizeof(struct rx_msg),
  75. sizeof(msg_pool),
  76. RT_IPC_FLAG_FIFO);
  77. ret = rt_device_open(serial, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
  78. if (ret != RT_EOK)
  79. {
  80. rt_kprintf("serial device open fail!.\n");
  81. return -RT_ERROR;
  82. }
  83. ret = rt_device_set_rx_indicate(serial, uart_input);
  84. if (ret != RT_EOK)
  85. {
  86. rt_kprintf("set rx indicate fail!.\n");
  87. return -RT_ERROR;
  88. }
  89. rt_device_write(serial, 0, str, (sizeof(str) - 1));
  90. rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
  91. if (thread != RT_NULL)
  92. {
  93. rt_thread_startup(thread);
  94. }
  95. else
  96. {
  97. ret = RT_ERROR;
  98. }
  99. return ret;
  100. }
  101. MSH_CMD_EXPORT(uart_dma_sample, uart device dma sample);
  102. #endif /* BSP_USING_DMA */