RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
dev_serial_v2.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-06-01 KyleChan first version
9 */
10
11#ifndef __DEV_SERIAL_V2_H__
12#define __DEV_SERIAL_V2_H__
13
14#include <rtthread.h>
15
16
125
126
131
132#define BAUD_RATE_2400 2400
133#define BAUD_RATE_4800 4800
134#define BAUD_RATE_9600 9600
135#define BAUD_RATE_19200 19200
136#define BAUD_RATE_38400 38400
137#define BAUD_RATE_57600 57600
138#define BAUD_RATE_115200 115200
139#define BAUD_RATE_230400 230400
140#define BAUD_RATE_460800 460800
141#define BAUD_RATE_500000 500000
142#define BAUD_RATE_921600 921600
143#define BAUD_RATE_2000000 2000000
144#define BAUD_RATE_2500000 2500000
145#define BAUD_RATE_3000000 3000000
146
147#define DATA_BITS_5 5
148#define DATA_BITS_6 6
149#define DATA_BITS_7 7
150#define DATA_BITS_8 8
151#define DATA_BITS_9 9
152
153#define STOP_BITS_1 0
154#define STOP_BITS_2 1
155#define STOP_BITS_3 2
156#define STOP_BITS_4 3
157
158#ifdef _WIN32
159#include <windows.h>
160#else
161#define PARITY_NONE 0
162#define PARITY_ODD 1
163#define PARITY_EVEN 2
164#endif
165
166#define BIT_ORDER_LSB 0
167#define BIT_ORDER_MSB 1
168
169#define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
170#define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
171
172#define RT_DEVICE_FLAG_RX_BLOCKING 0x1000
173#define RT_DEVICE_FLAG_RX_NON_BLOCKING 0x2000
174
175#define RT_DEVICE_FLAG_TX_BLOCKING 0x4000
176#define RT_DEVICE_FLAG_TX_NON_BLOCKING 0x8000
177
178#define RT_SERIAL_RX_BLOCKING RT_DEVICE_FLAG_RX_BLOCKING
179#define RT_SERIAL_RX_NON_BLOCKING RT_DEVICE_FLAG_RX_NON_BLOCKING
180#define RT_SERIAL_TX_BLOCKING RT_DEVICE_FLAG_TX_BLOCKING
181#define RT_SERIAL_TX_NON_BLOCKING RT_DEVICE_FLAG_TX_NON_BLOCKING
182
183#define RT_DEVICE_CHECK_OPTMODE 0x20
184
185#define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
186#define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
187#define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
188#define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
189#define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
190
191#define RT_SERIAL_ERR_OVERRUN 0x01
192#define RT_SERIAL_ERR_FRAMING 0x02
193#define RT_SERIAL_ERR_PARITY 0x03
194
195#define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
196#define RT_SERIAL_TX_DATAQUEUE_LWM 30
197
198#define RT_SERIAL_RX_MINBUFSZ 64
199#define RT_SERIAL_TX_MINBUFSZ 64
200
201#define RT_SERIAL_TX_BLOCKING_BUFFER 1
202#define RT_SERIAL_TX_BLOCKING_NO_BUFFER 0
203
204#define RT_SERIAL_FLOWCONTROL_CTSRTS 1
205#define RT_SERIAL_FLOWCONTROL_NONE 0
206
207/* Default config for serial_configure structure */
208#define RT_SERIAL_CONFIG_DEFAULT \
209{ \
210 BAUD_RATE_115200, /* 115200 bits/s */ \
211 DATA_BITS_8, /* 8 databits */ \
212 STOP_BITS_1, /* 1 stopbit */ \
213 PARITY_NONE, /* No parity */ \
214 BIT_ORDER_LSB, /* LSB first sent */ \
215 NRZ_NORMAL, /* Normal mode */ \
216 RT_SERIAL_RX_MINBUFSZ, /* rxBuf size */ \
217 RT_SERIAL_TX_MINBUFSZ, /* txBuf size */ \
218 RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
219 0 \
220}
221
228
229struct serial_configure
230{
232
242};
243
248{
249 struct rt_ringbuffer rb;
250
251 struct rt_completion rx_cpt;
252
254
255 /* software fifo */
257};
258
264{
265 struct rt_ringbuffer rb;
266
268
270
271 struct rt_completion tx_cpt;
272
273 /* software fifo */
275};
276
281struct rt_serial_device
282{
283 struct rt_device parent;
284
285 const struct rt_uart_ops *ops;
287
288 void *serial_rx;
289 void *serial_tx;
290
292};
293
298struct rt_uart_ops
299{
300 rt_err_t (*configure)(struct rt_serial_device *serial,
301 struct serial_configure *cfg);
302
303 rt_err_t (*control)(struct rt_serial_device *serial,
304 int cmd,
305 void *arg);
306
307 int (*putc)(struct rt_serial_device *serial, char c);
308 int (*getc)(struct rt_serial_device *serial);
309
311 rt_uint8_t *buf,
312 rt_size_t size,
313 rt_uint32_t tx_flag);
314};
315
322void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
323
324
338 const char *name,
339 rt_uint32_t flag,
340 void *data);
341
351
352
354
355#endif
struct rt_device * rt_device_t
#define RT_OBJECT_HOOKLIST_DECLARE(handler_type, name)
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial, const char *name, rt_uint32_t flag, void *data)
Register a serial device to device list
rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
register a serial device to system device list and add a device object to system object list
void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
Serial interrupt service routine
void(* rt_hw_serial_rxind_hookproto_t)(rt_device_t dev, rt_size_t size)
Sets a hook function when RX indicate is called
rt_base_t rt_ssize_t
int rt_bool_t
rt_base_t rt_err_t
unsigned char rt_uint8_t
unsigned short rt_uint16_t
rt_ubase_t rt_size_t
unsigned int rt_uint32_t
serial device structure
struct rt_device parent
const struct rt_uart_ops * ops
struct rt_device_notify rx_notify
struct serial_configure config
Serial Receive FIFO mode
struct rt_completion rx_cpt
struct rt_ringbuffer rb
Serial Transmit FIFO mode
struct rt_ringbuffer rb
struct rt_completion tx_cpt
Configure the serial device
rt_err_t(* configure)(struct rt_serial_device *serial, struct serial_configure *cfg)
rt_err_t(* control)(struct rt_serial_device *serial, int cmd, void *arg)
int(* getc)(struct rt_serial_device *serial)
int(* putc)(struct rt_serial_device *serial, char c)
rt_ssize_t(* transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, rt_uint32_t tx_flag)
rt_uint32_t flowcontrol