test_uart_v1.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-30 CDT first version
  9. */
  10. /*
  11. * 程序清单:这是一个 串口 设备使用例程
  12. * 例程导出了 uart_sample_v1 命令到控制终端
  13. * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备(uart1)
  14. * 程序功能:通过串口输出字符串:
  15. * drv_usart: drv_usart_v1
  16. * commnucation:using DMA/interrupt,
  17. * uart_ch: uartx (x对应测试通道)
  18. * 输出输入的字符
  19. *
  20. * 命令调用格式:
  21. * uart1 中断,命令调用格式:uart_sample_v1 uart1 int
  22. * uart1 DMA,命令调用格式:uart_sample_v1 uart1 dma
  23. *
  24. * 修改rtconfig.h
  25. * #define RT_SERIAL_USING_DMA
  26. * #define BSP_USING_UART
  27. * #define BSP_USING_UART1
  28. * #define BSP_UART1_RX_USING_DMA
  29. * #define BSP_UART1_TX_USING_DMA
  30. * #define BSP_USING_UART2
  31. * #define BSP_UART2_RX_USING_DMA
  32. * #define BSP_UART2_TX_USING_DMA
  33. * #define BSP_USING_UART5
  34. *
  35. */
  36. #include <rtthread.h>
  37. #include <rtdevice.h>
  38. #if defined(HC32F460) && defined(BSP_USING_UART2)
  39. #define SAMPLE_DEFAULT_UART_NAME "uart2"
  40. #elif defined(HC32F4A0) && defined (BSP_USING_UART6)
  41. #define SAMPLE_DEFAULT_UART_NAME "uart6"
  42. #elif defined(HC32F448) && defined (BSP_USING_UART1)
  43. #define SAMPLE_DEFAULT_UART_NAME "uart1"
  44. #elif defined(HC32F472) && defined (BSP_USING_UART1)
  45. #define SAMPLE_DEFAULT_UART_NAME "uart1"
  46. #elif defined(HC32F4A8) && defined (BSP_USING_UART6)
  47. #define SAMPLE_DEFAULT_UART_NAME "uart6"
  48. #endif
  49. #if defined(SAMPLE_DEFAULT_UART_NAME)
  50. /* 串口接收消息结构*/
  51. struct rx_msg
  52. {
  53. rt_device_t dev;
  54. rt_size_t size;
  55. };
  56. /* 消息队列控制块 */
  57. static struct rt_messagequeue rx_mq;
  58. /* 用于接收消息的信号量 */
  59. static struct rt_semaphore rx_sem;
  60. static rt_device_t serial;
  61. static struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  62. /* DMA接收数据回调函数 */
  63. static rt_err_t uart_input_dma(rt_device_t dev, rt_size_t size)
  64. {
  65. struct rx_msg msg;
  66. rt_err_t result;
  67. msg.dev = dev;
  68. msg.size = size;
  69. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  70. if (result == -RT_EFULL)
  71. {
  72. /* 消息队列满 */
  73. rt_kprintf("message queue full!\n");
  74. }
  75. return result;
  76. }
  77. /* INT接收数据回调函数 */
  78. static rt_err_t uart_input_int(rt_device_t dev, rt_size_t size)
  79. {
  80. /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
  81. rt_sem_release(&rx_sem);
  82. return RT_EOK;
  83. }
  84. /* 发送完成回调函数 */
  85. static rt_err_t uart_ouput(rt_device_t dev, void *buffer)
  86. {
  87. return RT_EOK;
  88. }
  89. static void serial_thread_entry_dma(void *parameter)
  90. {
  91. struct rx_msg msg;
  92. rt_err_t result;
  93. rt_uint32_t rx_length;
  94. static char rx_buffer[256];
  95. static rt_uint32_t buf_size = sizeof(rx_buffer);
  96. static rt_uint32_t put_index = 0;
  97. while (1)
  98. {
  99. rt_memset(&msg, 0, sizeof(msg));
  100. /* 从消息队列中读取消息*/
  101. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
  102. if (result > 0U)
  103. {
  104. /* 从串口读取数据*/
  105. while (msg.size)
  106. {
  107. if (msg.size > (buf_size - put_index))
  108. {
  109. rx_length = rt_device_read(msg.dev, 0, rx_buffer + put_index, buf_size - put_index);
  110. msg.size -= rx_length;
  111. }
  112. else
  113. {
  114. rx_length = rt_device_read(msg.dev, 0, rx_buffer + put_index, msg.size);
  115. msg.size = 0UL;
  116. }
  117. rt_device_write(serial, 0, rx_buffer + put_index, rx_length);
  118. put_index += rx_length;
  119. put_index %= sizeof(rx_buffer);
  120. }
  121. }
  122. }
  123. }
  124. static void serial_thread_entry_int(void *parameter)
  125. {
  126. char ch;
  127. while (1)
  128. {
  129. /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
  130. while (rt_device_read(serial, -1, &ch, 1) != 1)
  131. {
  132. /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
  133. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  134. }
  135. /* 读取到的数据通过串口错位输出 */
  136. rt_device_write(serial, 0, &ch, 1);
  137. }
  138. }
  139. int uart_sample_v1(int argc, char *argv[])
  140. {
  141. rt_thread_t thread;
  142. rt_err_t ret = RT_EOK;
  143. rt_size_t n;
  144. rt_err_t open_flag = 0UL;
  145. static char uart_name[RT_NAME_MAX];
  146. static char comm_mode[RT_NAME_MAX];
  147. const static char comm_mode_int[] = "int";
  148. const static char comm_mode_dma[] = "dma";
  149. const static char comm_info_dma[] = "\r\n drv_version: drv_usart_v1 \r\n communication: using DMA \r\n uart_ch: ";
  150. const static char comm_info_int[] = "\r\n drv_version: drv_usart_v1 \r\n communication: using interrupt \r\n uart_ch: ";
  151. static char comm_info[150];
  152. rt_memset(uart_name, 0, sizeof(uart_name));
  153. rt_memset(comm_mode, 0, sizeof(comm_mode));
  154. if (argc == 1)
  155. {
  156. rt_strncpy(uart_name, SAMPLE_DEFAULT_UART_NAME, RT_NAME_MAX);
  157. rt_strncpy(comm_mode, comm_mode_int, sizeof(comm_mode_int));
  158. }
  159. else if (argc == 2)
  160. {
  161. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  162. rt_strncpy(comm_mode, comm_mode_int, sizeof(comm_mode_int));
  163. }
  164. else if (argc == 3)
  165. {
  166. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  167. rt_strncpy(comm_mode, argv[2], RT_NAME_MAX);
  168. }
  169. else
  170. {
  171. rt_kprintf("argc error!\n");
  172. return -RT_ERROR;
  173. }
  174. /* 查找系统中的串口设备 */
  175. serial = rt_device_find(uart_name);
  176. if (!serial)
  177. {
  178. rt_kprintf("find %s failed!\n", uart_name);
  179. return -RT_ERROR;
  180. }
  181. /* modify configure */
  182. config.baud_rate = BAUD_RATE_115200; //baudrate 115200
  183. config.data_bits = DATA_BITS_8; //data bit 8
  184. config.stop_bits = STOP_BITS_1; //stop bit 1
  185. config.parity = PARITY_NONE;
  186. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  187. if (0 == rt_strncmp(comm_mode, comm_mode_dma, 3))
  188. {
  189. static char msg_pool[256U];
  190. /* 初始化消息队列 */
  191. rt_mq_init(&rx_mq, "rx_mq",
  192. msg_pool, /* 存放消息的缓冲区 */
  193. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  194. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  195. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  196. /* 以DMA接收和发送模式打开串口设备 */
  197. open_flag |= RT_DEVICE_FLAG_DMA_RX | RT_DEVICE_FLAG_DMA_TX;
  198. rt_device_open(serial, open_flag);
  199. /* 设置回调函数 */
  200. rt_device_set_rx_indicate(serial, uart_input_dma);
  201. rt_device_set_tx_complete(serial, uart_ouput);
  202. /* 发送字符串 */
  203. n = rt_strlen(comm_info_dma);
  204. rt_strncpy(comm_info, comm_info_dma, n);
  205. rt_strncpy(comm_info + n, uart_name, rt_strlen(uart_name));
  206. rt_device_write(serial, 0, comm_info, rt_strlen(comm_info));
  207. /* 创建 serial 线程 */
  208. thread = rt_thread_create("serial", serial_thread_entry_dma, RT_NULL, 1024, 25, 10);
  209. }
  210. else if (0 == rt_strncmp(comm_mode, comm_mode_int, 3))
  211. {
  212. /* 以中断模式打开串口设备 */
  213. open_flag = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX;
  214. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  215. rt_device_open(serial, open_flag);
  216. /* 设置回调函数 */
  217. rt_device_set_rx_indicate(serial, uart_input_int);
  218. rt_device_set_tx_complete(serial, uart_ouput);
  219. /* 发送字符串 */
  220. n = rt_strlen(comm_info_int);
  221. rt_strncpy(comm_info, comm_info_int, n);
  222. rt_strncpy(comm_info + n, uart_name, rt_strlen(uart_name));
  223. rt_device_write(serial, 0, comm_info, rt_strlen(comm_info));
  224. /* 创建 serial 线程 */
  225. thread = rt_thread_create("serial", serial_thread_entry_int, RT_NULL, 1024, 25, 10);
  226. }
  227. else
  228. {
  229. rt_kprintf("communication mode error, please input cmd: uart_sample_v1 %s int or uart_sample_v1 uartx dma!\n", uart_name);
  230. return -RT_ERROR;
  231. }
  232. if (thread != RT_NULL)
  233. {
  234. rt_thread_startup(thread);
  235. }
  236. else
  237. {
  238. ret = -RT_ERROR;
  239. }
  240. return ret;
  241. }
  242. /* 导出到 msh 命令列表中 */
  243. MSH_CMD_EXPORT(uart_sample_v1, uart device sample);
  244. #endif