ChibiOS 21.11.4
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 (legacy)
36 * @{
37 */
38#define SD_PARITY_ERROR CHN_PARITY_ERROR
39#define SD_FRAMING_ERROR CHN_FRAMING_ERROR
40#define SD_OVERRUN_ERROR CHN_OVERRUN_ERROR
41#define SD_NOISE_ERROR CHN_NOISE_ERROR
42#define SD_BREAK_DETECTED CHN_BREAK_DETECTED
43#define SD_QUEUE_FULL_ERROR CHN_BUFFER_FULL_ERROR
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 */
88typedef 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 */
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 */
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 */
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 * @param[in] sdp pointer to a @p SerialDriver object
144 * @param[in] b the byte value to be written in the output queue
145 * @return The operation status.
146 * @retval MSG_OK if the operation succeeded.
147 * @retval MSG_TIMEOUT if the queue is full.
148 *
149 * @iclass
150 */
151#define sdPutI(sdp, b) oqPutI(&(sdp)->oqueue, b)
152
153/**
154 * @brief Direct write to a @p SerialDriver.
155 * @note This function bypasses the indirect access to the channel and
156 * writes directly on the output queue. This is faster but cannot
157 * be used to write to different channels implementations.
158 *
159 * @param[in] sdp pointer to a @p SerialDriver object
160 * @param[in] b the byte value to be written in the output queue
161 * @return The operation status.
162 * @retval MSG_OK if the operation succeeded.
163 * @retval MSG_RESET if the @p SerialDriver has been stopped.
164 *
165 * @api
166 */
167#define sdPut(sdp, b) oqPut(&(sdp)->oqueue, b)
168
169/**
170 * @brief Direct write to a @p SerialDriver with timeout specification.
171 * @note This function bypasses the indirect access to the channel and
172 * writes directly on the output queue. This is faster but cannot
173 * be used to write to different channels implementations.
174 *
175 * @param[in] sdp pointer to a @p SerialDriver object
176 * @param[in] b the byte value to be written in the output queue
177 * @param[in] t the number of ticks before the operation timeouts,
178 * the following special values are allowed:
179 * - @a TIME_IMMEDIATE immediate timeout.
180 * - @a TIME_INFINITE no timeout.
181 * @return The operation status.
182 * @retval MSG_OK if the operation succeeded.
183 * @retval MSG_TIMEOUT if the specified time expired.
184 * @retval MSG_RESET if the @p SerialDriver has been stopped.
185 *
186 * @api
187 */
188#define sdPutTimeout(sdp, b, t) oqPutTimeout(&(sdp)->oqueue, b, t)
189
190/**
191 * @brief Direct read from a @p SerialDriver.
192 * @note This function bypasses the indirect access to the channel and
193 * reads directly from the input queue. This is faster but cannot
194 * be used to read from different channels implementations.
195 *
196 * @param[in] sdp pointer to a @p SerialDriver object
197 * @return A byte value from the input queue.
198 * @retval MSG_TIMEOUT if the queue is empty.
199 *
200 * @iclass
201 */
202#define sdGetI(sdp) iqGetI(&(sdp)->iqueue)
203
204/**
205 * @brief Direct read from a @p SerialDriver.
206 * @note This function bypasses the indirect access to the channel and
207 * reads directly from the input queue. This is faster but cannot
208 * be used to read from different channels implementations.
209 *
210 * @param[in] sdp pointer to a @p SerialDriver object
211 * @return A byte value from the input queue.
212 * @retval MSG_RESET if the @p SerialDriver has been stopped.
213 *
214 * @api
215 */
216#define sdGet(sdp) iqGet(&(sdp)->iqueue)
217
218/**
219 * @brief Direct read from a @p SerialDriver with timeout specification.
220 * @note This function bypasses the indirect access to the channel and
221 * reads directly from the input queue. This is faster but cannot
222 * be used to read from different channels implementations.
223 *
224 * @param[in] sdp pointer to a @p SerialDriver object
225 * @param[in] t the number of ticks before the operation timeouts,
226 * the following special values are allowed:
227 * - @a TIME_IMMEDIATE immediate timeout.
228 * - @a TIME_INFINITE no timeout.
229 * @return A byte value from the input queue.
230 * @retval MSG_TIMEOUT if the specified time expired.
231 * @retval MSG_RESET if the @p SerialDriver has been stopped.
232 *
233 * @api
234 */
235#define sdGetTimeout(sdp, t) iqGetTimeout(&(sdp)->iqueue, t)
236
237/**
238 * @brief Direct non-blocking write to a @p SerialDriver.
239 * @note This function bypasses the indirect access to the channel and
240 * writes directly to the output queue. This is faster but cannot
241 * be used to write from different channels implementations.
242 *
243 * @param[in] sdp pointer to a @p SerialDriver object
244 * @param[in] b pointer to the data buffer
245 * @param[in] n the maximum amount of data to be transferred, the
246 * value 0 is reserved
247 * @return The number of bytes effectively transferred.
248 *
249 * @iclass
250 */
251#define sdWriteI(sdp, b, n) oqWriteI(&(sdp)->oqueue, b, n)
252
253/**
254 * @brief Direct blocking write to a @p SerialDriver.
255 * @note This function bypasses the indirect access to the channel and
256 * writes directly to the output queue. This is faster but cannot
257 * be used to write from different channels implementations.
258 *
259 * @param[in] sdp pointer to a @p SerialDriver object
260 * @param[in] b pointer to the data buffer
261 * @param[in] n the maximum amount of data to be transferred, the
262 * value 0 is reserved
263 *
264 * @api
265 */
266#define sdWrite(sdp, b, n) oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE)
267
268/**
269 * @brief Direct blocking write to a @p SerialDriver with timeout
270 * specification.
271 * @note This function bypasses the indirect access to the channel and
272 * writes directly to the output queue. This is faster but cannot
273 * be used to write to different channels implementations.
274 *
275 * @param[in] sdp pointer to a @p SerialDriver object
276 * @param[in] b pointer to the data buffer
277 * @param[in] n the maximum amount of data to be transferred, the
278 * value 0 is reserved
279 * @param[in] t the number of ticks before the operation timeouts,
280 * the following special values are allowed:
281 * - @a TIME_IMMEDIATE immediate timeout.
282 * - @a TIME_INFINITE no timeout.
283 * @return The number of bytes effectively transferred.
284 *
285 * @api
286 */
287#define sdWriteTimeout(sdp, b, n, t) \
288 oqWriteTimeout(&(sdp)->oqueue, b, n, t)
289
290/**
291 * @brief Direct non-blocking write to a @p SerialDriver.
292 * @note This function bypasses the indirect access to the channel and
293 * writes directly to the output queue. This is faster but cannot
294 * be used to write to different channels implementations.
295 *
296 * @param[in] sdp pointer to a @p SerialDriver object
297 * @param[in] b pointer to the data buffer
298 * @param[in] n the maximum amount of data to be transferred, the
299 * value 0 is reserved
300 *
301 * @api
302 */
303#define sdAsynchronousWrite(sdp, b, n) \
304 oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE)
305
306/**
307 * @brief Direct non-blocking read from a @p SerialDriver.
308 * @note This function bypasses the indirect access to the channel and
309 * reads directly from the input queue. This is faster but cannot
310 * be used to read from different channels implementations.
311 *
312 * @param[in] sdp pointer to a @p SerialDriver object
313 * @param[in] b pointer to the data buffer
314 * @param[in] n the maximum amount of data to be transferred, the
315 * value 0 is reserved
316 * @return The number of bytes effectively transferred.
317 *
318 * @iclass
319 */
320#define sdReadI(sdp, b, n) iqReadI(&(sdp)->iqueue, b, n)
321
322/**
323 * @brief Direct blocking read from a @p SerialDriver.
324 * @note This function bypasses the indirect access to the channel and
325 * reads directly from the input queue. This is faster but cannot
326 * be used to read from different channels implementations.
327 *
328 * @param[in] sdp pointer to a @p SerialDriver object
329 * @param[in] b pointer to the data buffer
330 * @param[in] n the maximum amount of data to be transferred, the
331 * value 0 is reserved
332 *
333 * @api
334 */
335#define sdRead(sdp, b, n) iqReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE)
336
337/**
338 * @brief Direct blocking read from a @p SerialDriver with timeout
339 * specification.
340 * @note This function bypasses the indirect access to the channel and
341 * reads directly from the input queue. This is faster but cannot
342 * be used to read from different channels implementations.
343 *
344 * @param[in] sdp pointer to a @p SerialDriver object
345 * @param[in] b pointer to the data buffer
346 * @param[in] n the maximum amount of data to be transferred, the
347 * value 0 is reserved
348 * @param[in] t the number of ticks before the operation timeouts,
349 * the following special values are allowed:
350 * - @a TIME_IMMEDIATE immediate timeout.
351 * - @a TIME_INFINITE no timeout.
352 * @return The number of bytes effectively transferred.
353 *
354 * @api
355 */
356#define sdReadTimeout(sdp, b, n, t) iqReadTimeout(&(sdp)->iqueue, b, n, t)
357
358/**
359 * @brief Direct non-blocking read from a @p SerialDriver.
360 * @note This function bypasses the indirect access to the channel and
361 * reads directly from the input queue. This is faster but cannot
362 * be used to read from different channels implementations.
363 *
364 * @param[in] sdp pointer to a @p SerialDriver object
365 * @param[in] b pointer to the data buffer
366 * @param[in] n the maximum amount of data to be transferred, the
367 * value 0 is reserved
368 * @return The number of bytes effectively transferred.
369 *
370 * @api
371 */
372#define sdAsynchronousRead(sdp, b, n) \
373 iqReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE)
374/** @} */
375
376/*===========================================================================*/
377/* External declarations. */
378/*===========================================================================*/
379
380#ifdef __cplusplus
381extern "C" {
382#endif
383 void sdInit(void);
384#if !defined(SERIAL_ADVANCED_BUFFERING_SUPPORT) || \
385 (SERIAL_ADVANCED_BUFFERING_SUPPORT == FALSE)
386 void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify);
387#else
388 void sdObjectInit(SerialDriver *sdp);
389#endif
390 msg_t sdStart(SerialDriver *sdp, const SerialConfig *config);
391 void sdStop(SerialDriver *sdp);
392 void sdIncomingDataI(SerialDriver *sdp, uint8_t b);
394 bool sdPutWouldBlock(SerialDriver *sdp);
395 bool sdGetWouldBlock(SerialDriver *sdp);
396 msg_t sdControl(SerialDriver *sdp, unsigned int operation, void *arg);
397#ifdef __cplusplus
398}
399#endif
400
401#endif /* HAL_USE_SERIAL == TRUE */
402
403#endif /* HAL_SERIAL_H */
404
405/** @} */
void(* qnotify_t)(io_queue_t *qp)
Queue notification callback type.
Definition hal_queues.h:65
msg_t sdStart(SerialDriver *sdp, const SerialConfig *config)
Configures and starts the driver.
Definition hal_serial.c:185
struct hal_serial_driver SerialDriver
Structure representing a serial driver.
Definition hal_serial.h:97
#define _serial_driver_data
SerialDriver specific data.
#define _serial_driver_methods
SerialDriver specific methods.
Definition hal_serial.h:104
msg_t sdControl(SerialDriver *sdp, unsigned int operation, void *arg)
Control operation on a serial port.
Definition hal_serial.c:358
bool sdGetWouldBlock(SerialDriver *sdp)
Direct input check on a SerialDriver.
Definition hal_serial.c:334
void sdIncomingDataI(SerialDriver *sdp, uint8_t b)
Handles incoming data.
Definition hal_serial.c:256
void sdStop(SerialDriver *sdp)
Stops the driver.
Definition hal_serial.c:222
void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify)
Initializes a generic serial driver object.
Definition hal_serial.c:157
struct hal_serial_config SerialConfig
PLATFORM Serial Driver configuration structure.
void sdInit(void)
Serial Driver initialization.
Definition hal_serial.c:134
bool sdPutWouldBlock(SerialDriver *sdp)
Direct output check on a SerialDriver.
Definition hal_serial.c:309
sdstate_t
Driver state machine possible states.
Definition hal_serial.h:88
msg_t sdRequestDataI(SerialDriver *sdp)
Handles outgoing data.
Definition hal_serial.c:282
@ SD_STOP
Definition hal_serial.h:90
@ SD_READY
Definition hal_serial.h:91
@ SD_UNINIT
Definition hal_serial.h:89
int32_t msg_t
Definition chearly.h:88
PLATFORM serial subsystem low level driver header.
SerialDriver virtual methods table.
Definition hal_serial.h:112
Full duplex serial driver class.
Definition hal_serial.h:123
const struct SerialDriverVMT * vmt
Virtual Methods Table.
Definition hal_serial.h:125