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