1
1
/*
2
- * Copyright (c) 2006-2024, RT-Thread Development Team
2
+ * Copyright (c) 2006-2024 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
15
15
#define __DEV_SERIAL_H__
16
16
17
17
#include <rtthread.h>
18
+ /**
19
+ * @addtogroup Drivers RTTHREAD Driver
20
+ * @defgroup Serial Serial
21
+ *
22
+ * @brief Serial driver api
23
+ *
24
+ * <b>Example</b>
25
+ * @code {.c}
26
+ *
27
+ * #include <rtthread.h>
28
+ *
29
+ * #define SAMPLE_UART_NAME "uart2"
30
+ * static struct rt_semaphore rx_sem;
31
+ * static rt_device_t serial;
32
+ *
33
+ * static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
34
+ * {
35
+ *
36
+ * rt_sem_release(&rx_sem);
37
+ *
38
+ * return RT_EOK;
39
+ * }
40
+ *
41
+ * static void serial_thread_entry(void *parameter)
42
+ * {
43
+ * char ch;
44
+ *
45
+ * while (1)
46
+ * {
47
+ *
48
+ * while (rt_device_read(serial, -1, &ch, 1) != 1)
49
+ * {
50
+ *
51
+ * rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
52
+ * }
53
+ *
54
+ * ch = ch + 1;
55
+ * rt_device_write(serial, 0, &ch, 1);
56
+ * }
57
+ * }
58
+ *
59
+ * static int uart_sample(int argc, char *argv[])
60
+ * {
61
+ * rt_err_t ret = RT_EOK;
62
+ * char uart_name[RT_NAME_MAX];
63
+ * char str[] = "hello RT-Thread!\r\n";
64
+ *
65
+ * if (argc == 2)
66
+ * {
67
+ * rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
68
+ * }
69
+ * else
70
+ * {
71
+ * rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
72
+ * }
73
+ *
74
+ *
75
+ * serial = rt_device_find(uart_name);
76
+ * if (!serial)
77
+ * {
78
+ * rt_kprintf("find %s failed!\n", uart_name);
79
+ * return RT_ERROR;
80
+ * }
81
+ *
82
+ *
83
+ * rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
84
+ *
85
+ * rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
86
+ *
87
+ * rt_device_set_rx_indicate(serial, uart_input);
88
+ *
89
+ * rt_device_write(serial, 0, str, (sizeof(str) - 1));
90
+ *
91
+ *
92
+ * rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
93
+ *
94
+ * if (thread != RT_NULL)
95
+ * {
96
+ * rt_thread_startup(thread);
97
+ * }
98
+ * else
99
+ * {
100
+ * ret = RT_ERROR;
101
+ * }
102
+ *
103
+ * return ret;
104
+ * }
105
+ *
106
+ * MSH_CMD_EXPORT(uart_sample, uart device sample);
107
+ * @endcode
108
+ *
109
+ * @ingroup Drivers
110
+ */
18
111
112
+
113
+ /*!
114
+ * @addtogroup Serial
115
+ * @{
116
+ */
19
117
#define BAUD_RATE_2400 2400
20
118
#define BAUD_RATE_4800 4800
21
119
#define BAUD_RATE_9600 9600
105
203
/**
106
204
* @brief Sets a hook function when RX indicate is called
107
205
*
108
- * @param thread is the target thread that initializing
109
206
*/
110
207
typedef void (* rt_hw_serial_rxind_hookproto_t )(rt_device_t dev , rt_size_t size );
111
208
RT_OBJECT_HOOKLIST_DECLARE (rt_hw_serial_rxind_hookproto_t , rt_hw_serial_rxind );
@@ -173,7 +270,7 @@ struct rt_serial_device
173
270
typedef struct rt_serial_device rt_serial_t ;
174
271
175
272
/**
176
- * uart operators
273
+ * @brief Configure the serial device
177
274
*/
178
275
struct rt_uart_ops
179
276
{
@@ -186,13 +283,41 @@ struct rt_uart_ops
186
283
rt_ssize_t (* dma_transmit )(struct rt_serial_device * serial , rt_uint8_t * buf , rt_size_t size , int direction );
187
284
};
188
285
286
+ /**
287
+ * @brief Serial interrupt service routine
288
+ * @param serial serial device
289
+ * @param event event mask
290
+ * @ingroup Serial
291
+ */
189
292
void rt_hw_serial_isr (struct rt_serial_device * serial , int event );
190
293
294
+ /**
295
+ * @brief Register a serial device to device list
296
+ *
297
+ * @param serial serial device
298
+ * @param name device name
299
+ * @param flag device flag
300
+ * @param data device private data
301
+ * @return rt_err_t error code
302
+ * @note This function will register a serial device to system device list,
303
+ * and add a device object to system object list.
304
+ * @ingroup Serial
305
+ */
191
306
rt_err_t rt_hw_serial_register (struct rt_serial_device * serial ,
192
307
const char * name ,
193
308
rt_uint32_t flag ,
194
309
void * data );
195
310
311
+ /**
312
+ * @brief register a serial device to system device list and add a device object to system object list
313
+ *
314
+ * @param serial serial device
315
+ * @return rt_err_t error code
316
+ *
317
+ * @ingroup Serial
318
+ */
196
319
rt_err_t rt_hw_serial_register_tty (struct rt_serial_device * serial );
197
320
321
+ /*! @}*/
322
+
198
323
#endif
0 commit comments