ChibiOS/HAL  6.1.0
hal_serial.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_serial.h
19  * @brief Serial Driver macros and structures.
20  *
21  * @addtogroup SERIAL
22  * @{
23  */
24 
25 #ifndef HAL_SERIAL_H
26 #define HAL_SERIAL_H
27 
28 #if (HAL_USE_SERIAL == TRUE) || defined(__DOXYGEN__)
29 
30 /*===========================================================================*/
31 /* Driver constants. */
32 /*===========================================================================*/
33 
34 /**
35  * @name Serial status flags
36  * @{
37  */
38 #define SD_PARITY_ERROR (eventflags_t)32 /**< @brief Parity. */
39 #define SD_FRAMING_ERROR (eventflags_t)64 /**< @brief Framing. */
40 #define SD_OVERRUN_ERROR (eventflags_t)128 /**< @brief Overflow. */
41 #define SD_NOISE_ERROR (eventflags_t)256 /**< @brief Line noise. */
42 #define SD_BREAK_DETECTED (eventflags_t)512 /**< @brief LIN Break. */
43 #define SD_QUEUE_FULL_ERROR (eventflags_t)1024 /**< @brief Queue full. */
44 /** @} */
45 
46 /*===========================================================================*/
47 /* Driver pre-compile time settings. */
48 /*===========================================================================*/
49 
50 /**
51  * @name Serial configuration options
52  * @{
53  */
54 /**
55  * @brief Default bit rate.
56  * @details Configuration parameter, this is the baud rate selected for the
57  * default configuration.
58  */
59 #if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
60 #define SERIAL_DEFAULT_BITRATE 38400
61 #endif
62 
63 /**
64  * @brief Serial buffers size.
65  * @details Configuration parameter, you can change the depth of the queue
66  * buffers depending on the requirements of your application.
67  * @note The default is 16 bytes for both the transmission and receive
68  * buffers.
69  * @note This is a global setting and it can be overridden by low level
70  * driver specific settings.
71  */
72 #if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__)
73 #define SERIAL_BUFFERS_SIZE 16
74 #endif
75 /** @} */
76 
77 /*===========================================================================*/
78 /* Derived constants and error checks. */
79 /*===========================================================================*/
80 
81 /*===========================================================================*/
82 /* Driver data structures and types. */
83 /*===========================================================================*/
84 
85 /**
86  * @brief Driver state machine possible states.
87  */
88 typedef enum {
89  SD_UNINIT = 0, /**< Not initialized. */
90  SD_STOP = 1, /**< Stopped. */
91  SD_READY = 2 /**< Ready. */
92 } sdstate_t;
93 
94 /**
95  * @brief Structure representing a serial driver.
96  */
97 typedef struct SerialDriver SerialDriver;
98 
99 #include "hal_serial_lld.h"
100 
101 /**
102  * @brief @p SerialDriver specific methods.
103  */
104 #define _serial_driver_methods \
105  _base_asynchronous_channel_methods
106 
107 /**
108  * @extends BaseAsynchronousChannelVMT
109  *
110  * @brief @p SerialDriver virtual methods table.
111  */
114 };
115 
116 /**
117  * @extends BaseAsynchronousChannel
118  *
119  * @brief Full duplex serial driver class.
120  * @details This class extends @p BaseAsynchronousChannel by adding physical
121  * I/O queues.
122  */
123 struct SerialDriver {
124  /** @brief Virtual Methods Table.*/
125  const struct SerialDriverVMT *vmt;
127 };
128 
129 /*===========================================================================*/
130 /* Driver macros. */
131 /*===========================================================================*/
132 
133 /**
134  * @name Macro Functions
135  * @{
136  */
137 /**
138  * @brief Direct write to a @p SerialDriver.
139  * @note This function bypasses the indirect access to the channel and
140  * writes directly on the output queue. This is faster but cannot
141  * be used to write to different channels implementations.
142  *
143  * @iclass
144  */
145 #define sdPutI(sdp, b) oqPutI(&(sdp)->oqueue, b)
146 
147 /**
148  * @brief Direct write to a @p SerialDriver.
149  * @note This function bypasses the indirect access to the channel and
150  * writes directly on the output queue. This is faster but cannot
151  * be used to write to different channels implementations.
152  *
153  * @api
154  */
155 #define sdPut(sdp, b) oqPut(&(sdp)->oqueue, b)
156 
157 /**
158  * @brief Direct write to a @p SerialDriver with timeout specification.
159  * @note This function bypasses the indirect access to the channel and
160  * writes directly on the output queue. This is faster but cannot
161  * be used to write to different channels implementations.
162  *
163  * @api
164  */
165 #define sdPutTimeout(sdp, b, t) oqPutTimeout(&(sdp)->oqueue, b, t)
166 
167 /**
168  * @brief Direct read from a @p SerialDriver.
169  * @note This function bypasses the indirect access to the channel and
170  * reads directly from the input queue. This is faster but cannot
171  * be used to read from different channels implementations.
172  *
173  * @iclass
174  */
175 #define sdGetI(sdp) iqGetI(&(sdp)->iqueue)
176 
177 /**
178  * @brief Direct read from a @p SerialDriver.
179  * @note This function bypasses the indirect access to the channel and
180  * reads directly from the input queue. This is faster but cannot
181  * be used to read from different channels implementations.
182  *
183  * @api
184  */
185 #define sdGet(sdp) iqGet(&(sdp)->iqueue)
186 
187 /**
188  * @brief Direct read from a @p SerialDriver with timeout specification.
189  * @note This function bypasses the indirect access to the channel and
190  * reads directly from the input queue. This is faster but cannot
191  * be used to read from different channels implementations.
192  *
193  * @api
194  */
195 #define sdGetTimeout(sdp, t) iqGetTimeout(&(sdp)->iqueue, t)
196 
197 /**
198  * @brief Direct blocking write to a @p SerialDriver.
199  * @note This function bypasses the indirect access to the channel and
200  * writes directly to the output queue. This is faster but cannot
201  * be used to write from different channels implementations.
202  *
203  * @iclass
204  */
205 #define sdWriteI(sdp, b, n) oqWriteI(&(sdp)->oqueue, b, n)
206 
207 /**
208  * @brief Direct blocking write to a @p SerialDriver.
209  * @note This function bypasses the indirect access to the channel and
210  * writes directly to the output queue. This is faster but cannot
211  * be used to write from different channels implementations.
212  *
213  * @api
214  */
215 #define sdWrite(sdp, b, n) oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE)
216 
217 /**
218  * @brief Direct blocking write to a @p SerialDriver with timeout
219  * specification.
220  * @note This function bypasses the indirect access to the channel and
221  * writes directly to the output queue. This is faster but cannot
222  * be used to write to different channels implementations.
223  *
224  * @api
225  */
226 #define sdWriteTimeout(sdp, b, n, t) \
227  oqWriteTimeout(&(sdp)->oqueue, b, n, t)
228 
229 /**
230  * @brief Direct non-blocking write to a @p SerialDriver.
231  * @note This function bypasses the indirect access to the channel and
232  * writes directly to the output queue. This is faster but cannot
233  * be used to write to different channels implementations.
234  *
235  * @api
236  */
237 #define sdAsynchronousWrite(sdp, b, n) \
238  oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE)
239 
240 /**
241  * @brief Direct blocking read from a @p SerialDriver.
242  * @note This function bypasses the indirect access to the channel and
243  * reads directly from the input queue. This is faster but cannot
244  * be used to read from different channels implementations.
245  *
246  * @iclass
247  */
248 #define sdReadI(sdp, b, n) iqReadI(&(sdp)->iqueue, b, n, TIME_INFINITE)
249 
250 /**
251  * @brief Direct blocking read from a @p SerialDriver.
252  * @note This function bypasses the indirect access to the channel and
253  * reads directly from the input queue. This is faster but cannot
254  * be used to read from different channels implementations.
255  *
256  * @api
257  */
258 #define sdRead(sdp, b, n) iqReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE)
259 
260 /**
261  * @brief Direct blocking read from a @p SerialDriver with timeout
262  * specification.
263  * @note This function bypasses the indirect access to the channel and
264  * reads directly from the input queue. This is faster but cannot
265  * be used to read from different channels implementations.
266  *
267  * @api
268  */
269 #define sdReadTimeout(sdp, b, n, t) iqReadTimeout(&(sdp)->iqueue, b, n, t)
270 
271 /**
272  * @brief Direct non-blocking read from a @p SerialDriver.
273  * @note This function bypasses the indirect access to the channel and
274  * reads directly from the input queue. This is faster but cannot
275  * be used to read from different channels implementations.
276  *
277  * @api
278  */
279 #define sdAsynchronousRead(sdp, b, n) \
280  iqReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE)
281 /** @} */
282 
283 /*===========================================================================*/
284 /* External declarations. */
285 /*===========================================================================*/
286 
287 #ifdef __cplusplus
288 extern "C" {
289 #endif
290  void sdInit(void);
291 #if !defined(SERIAL_ADVANCED_BUFFERING_SUPPORT) || \
292  (SERIAL_ADVANCED_BUFFERING_SUPPORT == FALSE)
293  void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify);
294 #else
295  void sdObjectInit(SerialDriver *sdp);
296 #endif
297  void sdStart(SerialDriver *sdp, const SerialConfig *config);
298  void sdStop(SerialDriver *sdp);
299  void sdIncomingDataI(SerialDriver *sdp, uint8_t b);
301  bool sdPutWouldBlock(SerialDriver *sdp);
302  bool sdGetWouldBlock(SerialDriver *sdp);
303  msg_t sdControl(SerialDriver *sdp, unsigned int operation, void *arg);
304 #ifdef __cplusplus
305 }
306 #endif
307 
308 #endif /* HAL_USE_SERIAL == TRUE */
309 
310 #endif /* HAL_SERIAL_H */
311 
312 /** @} */
void sdStart(SerialDriver *sdp, const SerialConfig *config)
Configures and starts the driver.
Definition: hal_serial.c:185
PLATFORM serial subsystem low level driver header.
void sdInit(void)
Serial Driver initialization.
Definition: hal_serial.c:135
bool sdPutWouldBlock(SerialDriver *sdp)
Direct output check on a SerialDriver.
Definition: hal_serial.c:293
sdstate_t
Driver state machine possible states.
Definition: hal_serial.h:88
msg_t sdRequestDataI(SerialDriver *sdp)
Handles outgoing data.
Definition: hal_serial.c:266
#define _serial_driver_methods
SerialDriver specific methods.
Definition: hal_serial.h:104
int32_t msg_t
Type of a message.
Definition: osal.h:160
void(* qnotify_t)(io_queue_t *qp)
Queue notification callback type.
Definition: hal_queues.h:65
const struct SerialDriverVMT * vmt
Virtual Methods Table.
Definition: hal_serial.h:125
Full duplex serial driver class.
Definition: hal_serial.h:123
SerialDriver virtual methods table.
Definition: hal_serial.h:112
msg_t sdControl(SerialDriver *sdp, unsigned int operation, void *arg)
Control operation on a serial port.
Definition: hal_serial.c:342
void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify)
Initializes a generic full duplex driver object.
Definition: hal_serial.c:158
bool sdGetWouldBlock(SerialDriver *sdp)
Direct input check on a SerialDriver.
Definition: hal_serial.c:318
void sdIncomingDataI(SerialDriver *sdp, uint8_t b)
Handles incoming data.
Definition: hal_serial.c:240
#define _serial_driver_data
SerialDriver specific data.
PLATFORM Serial Driver configuration structure.
void sdStop(SerialDriver *sdp)
Stops the driver.
Definition: hal_serial.c:206