test_uart_v1.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #elif defined(HC32F334) && defined (BSP_USING_UART1)
  49. #define SAMPLE_DEFAULT_UART_NAME "uart1"
  50. #endif
  51. #if defined(SAMPLE_DEFAULT_UART_NAME)
  52. /* 串口接收消息结构*/
  53. struct rx_msg
  54. {
  55. rt_device_t dev;
  56. rt_size_t size;
  57. };
  58. /* 消息队列控制块 */
  59. static struct rt_messagequeue rx_mq;
  60. /* 用于接收消息的信号量 */
  61. static struct rt_semaphore rx_sem;
  62. static rt_device_t serial;
  63. static struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  64. /* DMA接收数据回调函数 */
  65. static rt_err_t uart_input_dma(rt_device_t dev, rt_size_t size)
  66. {
  67. struct rx_msg msg;
  68. rt_err_t result;
  69. msg.dev = dev;
  70. msg.size = size;
  71. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  72. if (result == -RT_EFULL)
  73. {
  74. /* 消息队列满 */
  75. rt_kprintf("message queue full!\n");
  76. }
  77. return result;
  78. }
  79. /* INT接收数据回调函数 */
  80. static rt_err_t uart_input_int(rt_device_t dev, rt_size_t size)
  81. {
  82. /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
  83. rt_sem_release(&rx_sem);
  84. return RT_EOK;
  85. }
  86. /* 发送完成回调函数 */
  87. static rt_err_t uart_ouput(rt_device_t dev, void *buffer)
  88. {
  89. return RT_EOK;
  90. }
  91. static void serial_thread_entry_dma(void *parameter)
  92. {
  93. struct rx_msg msg;
  94. rt_err_t result;
  95. rt_uint32_t rx_length;
  96. static char rx_buffer[256];
  97. static rt_uint32_t buf_size = sizeof(rx_buffer);
  98. static rt_uint32_t put_index = 0;
  99. while (1)
  100. {
  101. rt_memset(&msg, 0, sizeof(msg));
  102. /* 从消息队列中读取消息*/
  103. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
  104. if (result > 0U)
  105. {
  106. /* 从串口读取数据*/
  107. while (msg.size)
  108. {
  109. if (msg.size > (buf_size - put_index))
  110. {
  111. rx_length = rt_device_read(msg.dev, 0, rx_buffer + put_index, buf_size - put_index);
  112. msg.size -= rx_length;
  113. }
  114. else
  115. {
  116. rx_length = rt_device_read(msg.dev, 0, rx_buffer + put_index, msg.size);
  117. msg.size = 0UL;
  118. }
  119. rt_device_write(serial, 0, rx_buffer + put_index, rx_length);
  120. put_index += rx_length;
  121. put_index %= sizeof(rx_buffer);
  122. }
  123. }
  124. }
  125. }
  126. static void serial_thread_entry_int(void *parameter)
  127. {
  128. char ch;
  129. while (1)
  130. {
  131. /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
  132. while (rt_device_read(serial, -1, &ch, 1) != 1)
  133. {
  134. /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
  135. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  136. }
  137. /* 读取到的数据通过串口错位输出 */
  138. rt_device_write(serial, 0, &ch, 1);
  139. }
  140. }
  141. int uart_sample_v1(int argc, char *argv[])
  142. {
  143. rt_thread_t thread;
  144. rt_err_t ret = RT_EOK;
  145. rt_size_t n;
  146. rt_err_t open_flag = 0UL;
  147. static char uart_name[RT_NAME_MAX];
  148. static char comm_mode[RT_NAME_MAX];
  149. const static char comm_mode_int[] = "int";
  150. const static char comm_mode_dma[] = "dma";
  151. const static char comm_info_dma[] = "\r\n drv_version: drv_usart_v1 \r\n communication: using DMA \r\n uart_ch: ";
  152. const static char comm_info_int[] = "\r\n drv_version: drv_usart_v1 \r\n communication: using interrupt \r\n uart_ch: ";
  153. static char comm_info[150];
  154. rt_memset(uart_name, 0, sizeof(uart_name));
  155. rt_memset(comm_mode, 0, sizeof(comm_mode));
  156. if (argc == 1)
  157. {
  158. rt_strncpy(uart_name, SAMPLE_DEFAULT_UART_NAME, RT_NAME_MAX);
  159. rt_strncpy(comm_mode, comm_mode_int, sizeof(comm_mode_int));
  160. }
  161. else if (argc == 2)
  162. {
  163. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  164. rt_strncpy(comm_mode, comm_mode_int, sizeof(comm_mode_int));
  165. }
  166. else if (argc == 3)
  167. {
  168. rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
  169. rt_strncpy(comm_mode, argv[2], RT_NAME_MAX);
  170. }
  171. else
  172. {
  173. rt_kprintf("argc error!\n");
  174. return -RT_ERROR;
  175. }
  176. /* 查找系统中的串口设备 */
  177. serial = rt_device_find(uart_name);
  178. if (!serial)
  179. {
  180. rt_kprintf("find %s failed!\n", uart_name);
  181. return -RT_ERROR;
  182. }
  183. /* modify configure */
  184. config.baud_rate = BAUD_RATE_115200; //baudrate 115200
  185. config.data_bits = DATA_BITS_8; //data bit 8
  186. config.stop_bits = STOP_BITS_1; //stop bit 1
  187. config.parity = PARITY_NONE;
  188. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  189. if (0 == rt_strncmp(comm_mode, comm_mode_dma, 3))
  190. {
  191. static char msg_pool[256U];
  192. /* 初始化消息队列 */
  193. rt_mq_init(&rx_mq, "rx_mq",
  194. msg_pool, /* 存放消息的缓冲区 */
  195. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  196. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  197. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  198. /* 以DMA接收和发送模式打开串口设备 */
  199. open_flag |= RT_DEVICE_FLAG_DMA_RX | RT_DEVICE_FLAG_DMA_TX;
  200. rt_device_open(serial, open_flag);
  201. /* 设置回调函数 */
  202. rt_device_set_rx_indicate(serial, uart_input_dma);
  203. rt_device_set_tx_complete(serial, uart_ouput);
  204. /* 发送字符串 */
  205. n = rt_strlen(comm_info_dma);
  206. rt_strncpy(comm_info, comm_info_dma, n);
  207. rt_strncpy(comm_info + n, uart_name, rt_strlen(uart_name));
  208. rt_device_write(serial, 0, comm_info, rt_strlen(comm_info));
  209. /* 创建 serial 线程 */
  210. thread = rt_thread_create("serial", serial_thread_entry_dma, RT_NULL, 1024, 25, 10);
  211. }
  212. else if (0 == rt_strncmp(comm_mode, comm_mode_int, 3))
  213. {
  214. /* 以中断模式打开串口设备 */
  215. open_flag = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX;
  216. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  217. rt_device_open(serial, open_flag);
  218. /* 设置回调函数 */
  219. rt_device_set_rx_indicate(serial, uart_input_int);
  220. rt_device_set_tx_complete(serial, uart_ouput);
  221. /* 发送字符串 */
  222. n = rt_strlen(comm_info_int);
  223. rt_strncpy(comm_info, comm_info_int, n);
  224. rt_strncpy(comm_info + n, uart_name, rt_strlen(uart_name));
  225. rt_device_write(serial, 0, comm_info, rt_strlen(comm_info));
  226. /* 创建 serial 线程 */
  227. thread = rt_thread_create("serial", serial_thread_entry_int, RT_NULL, 1024, 25, 10);
  228. }
  229. else
  230. {
  231. rt_kprintf("communication mode error, please input cmd: uart_sample_v1 %s int or uart_sample_v1 uartx dma!\n", uart_name);
  232. return -RT_ERROR;
  233. }
  234. if (thread != RT_NULL)
  235. {
  236. rt_thread_startup(thread);
  237. }
  238. else
  239. {
  240. ret = -RT_ERROR;
  241. }
  242. return ret;
  243. }
  244. /* 导出到 msh 命令列表中 */
  245. MSH_CMD_EXPORT(uart_sample_v1, uart device sample);
  246. #endif