ChibiOS/HAL 9.0.0
hal_buffered_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_buffered_serial.h
19 * @brief Buffered Serial Driver macros and structures.
20 *
21 * @addtogroup HAL_BUFFERED_SERIAL
22 * @{
23 */
24
25#ifndef HAL_BUFFERED_SERIAL_H
26#define HAL_BUFFERED_SERIAL_H
27
28/*===========================================================================*/
29/* Driver constants. */
30/*===========================================================================*/
31
32/*===========================================================================*/
33/* Driver pre-compile time settings. */
34/*===========================================================================*/
35
36/*===========================================================================*/
37/* Derived constants and error checks. */
38/*===========================================================================*/
39
40/*===========================================================================*/
41/* Driver data structures and types. */
42/*===========================================================================*/
43
44/**
45 * @brief @p BufferedSerial state machine states.
46 */
47typedef enum {
48 BS_UNINIT = 0, /**< Not initialized. */
49 BS_STOP = 1, /**< Stopped. */
50 BS_READY = 2 /**< Ready. */
51} bsstate_t;
52
53/**
54 * @brief Structure representing a buffered serial class.
55 */
57
58/**
59 * @brief @p BufferedSerial specific methods.
60 */
61#define __buffered_serial_methods \
62 _base_asynchronous_channel_methods
63
64/**
65 * @extends BaseAsynchronousChannelVMT
66 *
67 * @brief @p BufferedSerial virtual methods table.
68 */
72
73/**
74 * @brief @p BufferedSerial specific data.
75 */
76#define __buffered_serial_data \
77 _base_asynchronous_channel_data \
78 /* Driver state.*/ \
79 bsstate_t state; \
80 /* Input queue.*/ \
81 input_queue_t iqueue; \
82 /* Output queue.*/ \
83 output_queue_t oqueue;
84
85/**
86 * @extends BaseAsynchronousChannel
87 *
88 * @brief Buffered serial channel class.
89 * @details This class extends @p BaseAsynchronousChannel by adding physical
90 * I/O queues.
91 */
93 /** @brief Virtual Methods Table.*/
94 const struct BufferedSerialVMT *vmt;
96};
97
98/*===========================================================================*/
99/* Driver macros. */
100/*===========================================================================*/
101
102/*===========================================================================*/
103/* External declarations. */
104/*===========================================================================*/
105
106#ifdef __cplusplus
107extern "C" {
108#endif
109 void bsIncomingDataI(BufferedSerial *bsp, uint8_t b);
111#ifdef __cplusplus
112}
113#endif
114
115/*===========================================================================*/
116/* Inline functions. */
117/*===========================================================================*/
118
119/**
120 * @name Methods implementations
121 * @{
122 */
123/**
124 * @brief Object initialization implementation.
125 *
126 * @param[in] ip Pointer to a @p BufferedSerial structure to be
127 * initialized.
128 * @param[in] vmt VMT pointer for the new object.
129 * @param[in] ib pointer to the input buffer
130 * @param[in] ibsize size of the input buffer
131 * @param[in] inotify pointer to a callback function that is invoked when
132 * some data is read from the input queue. The value
133 * can be @p NULL.
134 * @param[in] iarg parameter for the input notification callback
135 * @param[in] ob pointer to the output buffer
136 * @param[in] obsize size of the output buffer
137 * @param[in] onotify pointer to a callback function that is invoked when
138 * some data is written in the output queue. The value
139 * can be @p NULL.
140 * @param[in] oarg parameter for the output notification callback
141 *
142 * @init
143 */
145static inline void __buffered_serial_objinit_impl(void *ip, const void *vmt,
146 uint8_t *ib, size_t ibsize,
147 qnotify_t inotify, void *iarg,
148 uint8_t *ob, size_t obsize,
149 qnotify_t onotify, void *oarg) {
150 BufferedSerial *bsp = (BufferedSerial *)ip;
151
152 bsp->vmt = (const struct BufferedSerialVMT *)vmt; /* TODO use new obj model.*/
153 osalEventObjectInit(&bsp->event); /* TODO super class should do this.*/
154 bsp->state = BS_STOP;
155 iqObjectInit(&bsp->iqueue, ib, ibsize, inotify, iarg);
156 oqObjectInit(&bsp->oqueue, ob, obsize, onotify, oarg);
157}
158
160static inline size_t __buffered_serial_write_impl(void *ip,
161 const uint8_t *bp,
162 size_t n) {
163
164 return oqWriteTimeout(&((BufferedSerial *)ip)->oqueue, bp,
165 n, TIME_INFINITE);
166}
167
169static inline size_t __buffered_serial_read_impl(void *ip,
170 uint8_t *bp,
171 size_t n) {
172
173 return iqReadTimeout(&((BufferedSerial *)ip)->iqueue, bp,
174 n, TIME_INFINITE);
175}
176
178static inline msg_t __buffered_serial_put_impl(void *ip,
179 uint8_t b) {
180
181 return oqPutTimeout(&((BufferedSerial *)ip)->oqueue, b, TIME_INFINITE);
182}
183
185static inline msg_t __buffered_serial_get_impl(void *ip) {
186
187 return iqGetTimeout(&((BufferedSerial *)ip)->iqueue, TIME_INFINITE);
188}
189
192 uint8_t b,
193 sysinterval_t timeout) {
194
195 return oqPutTimeout(&((BufferedSerial *)ip)->oqueue, b, timeout);
196}
197
200 sysinterval_t timeout) {
201
202 return iqGetTimeout(&((BufferedSerial *)ip)->iqueue, timeout);
203}
204
206static inline size_t __buffered_serial_write_timeout_impl(void *ip,
207 const uint8_t *bp,
208 size_t n,
209 sysinterval_t timeout) {
210
211 return oqWriteTimeout(&((BufferedSerial *)ip)->oqueue, bp, n, timeout);
212}
213
215static inline size_t __buffered_serial_read_timeout_impl(void *ip,
216 uint8_t *bp,
217 size_t n,
218 sysinterval_t timeout) {
219
220 return iqReadTimeout(&((BufferedSerial *)ip)->iqueue, bp, n, timeout);
221}
222
224static inline msg_t __buffered_serial_ctl_impl(void *ip,
225 unsigned int operation,
226 void *arg) {
227 BufferedSerial *bsp = (BufferedSerial *)ip;
228
229 osalDbgCheck(bsp != NULL);
230
231 switch (operation) {
232 case CHN_CTL_NOP:
233 osalDbgCheck(arg == NULL);
234 break;
235 case CHN_CTL_INVALID:
236 return HAL_RET_UNKNOWN_CTL;
237 default:
238 return HAL_RET_UNKNOWN_CTL;
239 }
240
241 return HAL_RET_SUCCESS;
242}
243/** @} */
244
245#endif /* HAL_BUFFERED_SERIAL_H */
246
247/** @} */
#define CC_FORCE_INLINE
Enforces a function inline.
Definition ccportab.h:108
bsstate_t
BufferedSerial state machine states.
static CC_FORCE_INLINE size_t __buffered_serial_read_timeout_impl(void *ip, uint8_t *bp, size_t n, sysinterval_t timeout)
static CC_FORCE_INLINE msg_t __buffered_serial_put_impl(void *ip, uint8_t b)
static CC_FORCE_INLINE size_t __buffered_serial_write_impl(void *ip, const uint8_t *bp, size_t n)
static CC_FORCE_INLINE msg_t __buffered_serial_get_timeout_impl(void *ip, sysinterval_t timeout)
#define __buffered_serial_data
BufferedSerial specific data.
static CC_FORCE_INLINE msg_t __buffered_serial_get_impl(void *ip)
static CC_FORCE_INLINE size_t __buffered_serial_read_impl(void *ip, uint8_t *bp, size_t n)
struct hal_buffered_serial BufferedSerial
Structure representing a buffered serial class.
static CC_FORCE_INLINE msg_t __buffered_serial_ctl_impl(void *ip, unsigned int operation, void *arg)
static CC_FORCE_INLINE size_t __buffered_serial_write_timeout_impl(void *ip, const uint8_t *bp, size_t n, sysinterval_t timeout)
msg_t bsRequestDataI(BufferedSerial *bsp)
Handles outgoing data.
static CC_FORCE_INLINE msg_t __buffered_serial_put_timeout_impl(void *ip, uint8_t b, sysinterval_t timeout)
void bsIncomingDataI(BufferedSerial *bsp, uint8_t b)
Handles incoming data.
static CC_FORCE_INLINE void __buffered_serial_objinit_impl(void *ip, const void *vmt, uint8_t *ib, size_t ibsize, qnotify_t inotify, void *iarg, uint8_t *ob, size_t obsize, qnotify_t onotify, void *oarg)
Object initialization implementation.
#define __buffered_serial_methods
BufferedSerial specific methods.
@ BS_UNINIT
static const struct EFlashDriverVMT vmt
Definition hal_efl.c:71
size_t oqWriteTimeout(output_queue_t *oqp, const uint8_t *bp, size_t n, sysinterval_t timeout)
Output queue write with timeout.
Definition hal_queues.c:651
void iqObjectInit(input_queue_t *iqp, uint8_t *bp, size_t size, qnotify_t infy, void *link)
Initializes an input queue.
Definition hal_queues.c:177
msg_t iqGetTimeout(input_queue_t *iqp, sysinterval_t timeout)
Input queue read with timeout.
Definition hal_queues.c:302
void(* qnotify_t)(io_queue_t *qp)
Queue notification callback type.
Definition hal_queues.h:65
size_t iqReadTimeout(input_queue_t *iqp, uint8_t *bp, size_t n, sysinterval_t timeout)
Input queue read with timeout.
Definition hal_queues.c:386
msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, sysinterval_t timeout)
Output queue write with timeout.
Definition hal_queues.c:535
void oqObjectInit(output_queue_t *oqp, uint8_t *bp, size_t size, qnotify_t onfy, void *link)
Initializes an output queue.
Definition hal_queues.c:443
#define HAL_RET_SUCCESS
Definition hal.h:93
#define HAL_RET_UNKNOWN_CTL
Unknown control code.
Definition hal.h:119
#define CHN_CTL_INVALID
Invalid operation code.
#define CHN_CTL_NOP
Does nothing.
int32_t msg_t
Type of a message.
Definition osal.h:159
uint32_t sysinterval_t
Type of system time interval.
Definition osal.h:169
static void osalEventObjectInit(event_source_t *esp)
Initializes an event source object.
Definition osal.h:737
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
#define TIME_INFINITE
Definition osal.h:66
BufferedSerial virtual methods table.
Buffered serial channel class.
const struct BufferedSerialVMT * vmt
Virtual Methods Table.