1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
|
/*
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @file hal_queues.c
* @brief I/O Queues code.
*
* @addtogroup HAL_QUEUES
* @details Queues are mostly used in serial-like device drivers.
* Serial device drivers are usually designed to have a lower side
* (lower driver, it is usually an interrupt service routine) and an
* upper side (upper driver, accessed by the application threads).<br>
* There are several kind of queues:<br>
* - <b>Input queue</b>, unidirectional queue where the writer is the
* lower side and the reader is the upper side.
* - <b>Output queue</b>, unidirectional queue where the writer is the
* upper side and the reader is the lower side.
* - <b>Full duplex queue</b>, bidirectional queue. Full duplex queues
* are implemented by pairing an input queue and an output queue
* together.
* .
* @{
*/
#include <string.h>
#include "hal.h"
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/**
* @brief Non-blocking input queue read.
* @details The function reads data from an input queue into a buffer. The
* operation completes when the specified amount of data has been
* transferred or when the input queue has been emptied.
*
* @param[in] iqp pointer to an @p input_queue_t structure
* @param[out] bp pointer to the data buffer
* @param[in] n the maximum amount of data to be transferred, the
* value 0 is reserved
* @return The number of bytes effectively transferred.
*
* @notapi
*/
static size_t iq_read(input_queue_t *iqp, uint8_t *bp, size_t n) {
size_t s1, s2;
osalDbgCheck(n > 0U);
/* Number of bytes that can be read in a single atomic operation.*/
if (n > iqGetFullI(iqp)) {
n = iqGetFullI(iqp);
}
/* Number of bytes before buffer limit.*/
/*lint -save -e9033 [10.8] Checked to be safe.*/
s1 = (size_t)(iqp->q_top - iqp->q_rdptr);
/*lint -restore*/
if (n < s1) {
memcpy((void *)bp, (void *)iqp->q_rdptr, n);
iqp->q_rdptr += n;
}
else if (n > s1) {
memcpy((void *)bp, (void *)iqp->q_rdptr, s1);
bp += s1;
s2 = n - s1;
memcpy((void *)bp, (void *)iqp->q_buffer, s2);
iqp->q_rdptr = iqp->q_buffer + s2;
}
else { /* n == s1 */
memcpy((void *)bp, (void *)iqp->q_rdptr, n);
iqp->q_rdptr = iqp->q_buffer;
}
iqp->q_counter -= n;
return n;
}
/**
* @brief Non-blocking output queue write.
* @details The function writes data from a buffer to an output queue. The
* operation completes when the specified amount of data has been
* transferred or when the output queue has been filled.
*
* @param[in] oqp pointer to an @p output_queue_t structure
* @param[in] bp pointer to the data buffer
* @param[in] n the maximum amount of data to be transferred, the
* value 0 is reserved
* @return The number of bytes effectively transferred.
*
* @notapi
*/
static size_t oq_write(output_queue_t *oqp, const uint8_t *bp, size_t n) {
size_t s1, s2;
osalDbgCheck(n > 0U);
/* Number of bytes that can be written in a single atomic operation.*/
if (n > oqGetEmptyI(oqp)) {
n = oqGetEmptyI(oqp);
}
/* Number of bytes before buffer limit.*/
/*lint -save -e9033 [10.8] Checked to be safe.*/
s1 = (size_t)(oqp->q_top - oqp->q_wrptr);
/*lint -restore*/
if (n < s1) {
memcpy((void *)oqp->q_wrptr, (const void *)bp, n);
oqp->q_wrptr += n;
}
else if (n > s1) {
memcpy((void *)oqp->q_wrptr, (const void *)bp, s1);
bp += s1;
s2 = n - s1;
memcpy((void *)oqp->q_buffer, (const void *)bp, s2);
oqp->q_wrptr = oqp->q_buffer + s2;
}
else { /* n == s1 */
memcpy((void *)oqp->q_wrptr, (const void *)bp, n);
oqp->q_wrptr = oqp->q_buffer;
}
oqp->q_counter -= n;
return n;
}
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver interrupt handlers. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Initializes an input queue.
* @details A Semaphore is internally initialized and works as a counter of
* the bytes contained in the queue.
* @note The callback is invoked from within the S-Locked system state.
*
* @param[out] iqp pointer to an @p input_queue_t structure
* @param[in] bp pointer to a memory area allocated as queue buffer
* @param[in] size size of the queue buffer
* @param[in] infy pointer to a callback function that is invoked when
* data is read from the queue. The value can be @p NULL.
* @param[in] link application defined pointer
*
* @init
*/
void iqObjectInit(input_queue_t *iqp, uint8_t *bp, size_t size,
qnotify_t infy, void *link) {
osalThreadQueueObjectInit(&iqp->q_waiting);
iqp->q_counter = 0;
iqp->q_buffer = bp;
iqp->q_rdptr = bp;
iqp->q_wrptr = bp;
iqp->q_top = bp + size;
iqp->q_notify = infy;
iqp->q_link = link;
}
/**
* @brief Resets an input queue.
* @details All the data in the input queue is erased and lost, any waiting
* thread is resumed with status @p MSG_RESET.
* @note A reset operation can be used by a low level driver in order to
* obtain immediate attention from the high level layers.
*
* @param[in] iqp pointer to an @p input_queue_t structure
*
* @iclass
*/
void iqResetI(input_queue_t *iqp) {
osalDbgCheckClassI();
iqp->q_rdptr = iqp->q_buffer;
iqp->q_wrptr = iqp->q_buffer;
iqp->q_counter = 0;
osalThreadDequeueAllI(&iqp->q_waiting, MSG_RESET);
}
/**
* @brief Input queue write.
* @details A byte value is written into the low end of an input queue. The
* operation completes immediately.
*
* @param[in] iqp pointer to an @p input_queue_t structure
* @param[in] b the byte value to be written in the queue
* @return The operation status.
* @retval MSG_OK if the operation has been completed with success.
* @retval MSG_TIMEOUT if the queue is full.
*
* @iclass
*/
msg_t iqPutI(input_queue_t *iqp, uint8_t b) {
osalDbgCheckClassI();
/* Queue space check.*/
if (!iqIsFullI(iqp)) {
iqp->q_counter++;
*iqp->q_wrptr++ = b;
if (iqp->q_wrptr >= iqp->q_top) {
iqp->q_wrptr = iqp->q_buffer;
}
osalThreadDequeueNextI(&iqp->q_waiting, MSG_OK);
return MSG_OK;
}
return MSG_TIMEOUT;
}
/**
* @brief Input queue non-blocking read.
* @details This function reads a byte value from an input queue. The
* operation completes immediately.
* @note The callback is invoked after removing a character from the
* queue.
*
* @param[in] iqp pointer to an @p input_queue_t structure
* @return A byte value from the queue.
* @retval MSG_TIMEOUT if the queue is empty.
* @retval MSG_RESET if the queue has been reset.
*
* @iclass
*/
msg_t iqGetI(input_queue_t *iqp) {
osalDbgCheckClassI();
/* Queue data check.*/
if (!iqIsEmptyI(iqp)) {
uint8_t b;
/* Getting the character from the queue.*/
iqp->q_counter--;
b = *iqp->q_rdptr++;
if (iqp->q_rdptr >= iqp->q_top) {
iqp->q_rdptr = iqp->q_buffer;
}
/* Inform the low side that the queue has at least one slot available.*/
if (iqp->q_notify != NULL) {
iqp->q_notify(iqp);
}
return (msg_t)b;
}
return MSG_TIMEOUT;
}
/**
* @brief Input queue read with timeout.
* @details This function reads a byte value from an input queue. If the queue
* is empty then the calling thread is suspended until a byte arrives
* in the queue or a timeout occurs.
* @note The callback is invoked after removing a character from the
* queue.
*
* @param[in] iqp pointer to an @p input_queue_t structure
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return A byte value from the queue.
* @retval MSG_TIMEOUT if the specified time expired.
* @retval MSG_RESET if the queue has been reset.
*
* @api
*/
msg_t iqGetTimeout(input_queue_t *iqp, sysinterval_t timeout) {
uint8_t b;
osalSysLock();
/* Waiting until there is a character available or a timeout occurs.*/
while (iqIsEmptyI(iqp)) {
msg_t msg = osalThreadEnqueueTimeoutS(&iqp->q_waiting, timeout);
if (msg < MSG_OK) {
osalSysUnlock();
return msg;
}
}
/* Getting the character from the queue.*/
iqp->q_counter--;
b = *iqp->q_rdptr++;
if (iqp->q_rdptr >= iqp->q_top) {
iqp->q_rdptr = iqp->q_buffer;
}
/* Inform the low side that the queue has at least one slot available.*/
if (iqp->q_notify != NULL) {
iqp->q_notify(iqp);
}
osalSysUnlock();
return (msg_t)b;
}
/**
* @brief Input queue non-blocking read.
* @details The function reads data from an input queue into a buffer. The
* operation completes immediately.
*
* @param[in] iqp pointer to an @p input_queue_t structure
* @param[out] bp pointer to the data buffer
* @param[in] n the maximum amount of data to be transferred, the
* value 0 is reserved
* @return The number of bytes effectively transferred.
*
* @iclass
*/
size_t iqReadI(input_queue_t *iqp, uint8_t *bp, size_t n) {
qnotify_t nfy = iqp->q_notify;
size_t rd;
osalDbgCheckClassI();
rd = iq_read(iqp, bp, n);
/* Inform the low side that the queue has at least one character
available.*/
if ((rd > (size_t)0) && (nfy != NULL)) {
nfy(iqp);
}
return rd;
}
/**
* @brief Input queue read with timeout.
* @details The function reads data from an input queue into a buffer. The
* operation completes when the specified amount of data has been
* transferred or after the specified timeout or if the queue has
* been reset.
* @note The function is not atomic, if you need atomicity it is suggested
* to use a semaphore or a mutex for mutual exclusion.
* @note The callback is invoked after removing each character from the
* queue.
*
* @param[in] iqp pointer to an @p input_queue_t structure
* @param[out] bp pointer to the data buffer
* @param[in] n the maximum amount of data to be transferred, the
* value 0 is reserved
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return The number of bytes effectively transferred.
*
* @api
*/
size_t iqReadTimeout(input_queue_t *iqp, uint8_t *bp,
size_t n, sysinterval_t timeout) {
qnotify_t nfy = iqp->q_notify;
size_t max = n;
osalDbgCheck(n > 0U);
osalSysLock();
while (n > 0U) {
size_t done;
done = iq_read(iqp, bp, n);
if (done == (size_t)0) {
msg_t msg = osalThreadEnqueueTimeoutS(&iqp->q_waiting, timeout);
/* Anything except MSG_OK causes the operation to stop.*/
if (msg != MSG_OK) {
break;
}
}
else {
/* Inform the low side that the queue has at least one empty slot
available.*/
if (nfy != NULL) {
nfy(iqp);
}
/* Giving a preemption chance in a controlled point.*/
osalSysUnlock();
n -= done;
bp += done;
osalSysLock();
}
}
osalSysUnlock();
return max - n;
}
/**
* @brief Initializes an output queue.
* @details A Semaphore is internally initialized and works as a counter of
* the free bytes in the queue.
* @note The callback is invoked from within the S-Locked system state.
*
* @param[out] oqp pointer to an @p output_queue_t structure
* @param[in] bp pointer to a memory area allocated as queue buffer
* @param[in] size size of the queue buffer
* @param[in] onfy pointer to a callback function that is invoked when
* data is written to the queue. The value can be @p NULL.
* @param[in] link application defined pointer
*
* @init
*/
void oqObjectInit(output_queue_t *oqp, uint8_t *bp, size_t size,
qnotify_t onfy, void *link) {
osalThreadQueueObjectInit(&oqp->q_waiting);
oqp->q_counter = size;
oqp->q_buffer = bp;
oqp->q_rdptr = bp;
oqp->q_wrptr = bp;
oqp->q_top = bp + size;
oqp->q_notify = onfy;
oqp->q_link = link;
}
/**
* @brief Resets an output queue.
* @details All the data in the output queue is erased and lost, any waiting
* thread is resumed with status @p MSG_RESET.
* @note A reset operation can be used by a low level driver in order to
* obtain immediate attention from the high level layers.
*
* @param[in] oqp pointer to an @p output_queue_t structure
*
* @iclass
*/
void oqResetI(output_queue_t *oqp) {
osalDbgCheckClassI();
oqp->q_rdptr = oqp->q_buffer;
oqp->q_wrptr = oqp->q_buffer;
oqp->q_counter = qSizeX(oqp);
osalThreadDequeueAllI(&oqp->q_waiting, MSG_RESET);
}
/**
* @brief Output queue non-blocking write.
* @details This function writes a byte value to an output queue. The
* operation completes immediately.
*
* @param[in] oqp pointer to an @p output_queue_t structure
* @param[in] b the byte value to be written in the queue
* @return The operation status.
* @retval MSG_OK if the operation succeeded.
* @retval MSG_TIMEOUT if the queue is full.
* @retval MSG_RESET if the queue has been reset.
*
* @iclass
*/
msg_t oqPutI(output_queue_t *oqp, uint8_t b) {
osalDbgCheckClassI();
/* Queue space check.*/
while (!oqIsFullI(oqp)) {
/* Putting the character into the queue.*/
oqp->q_counter--;
*oqp->q_wrptr++ = b;
if (oqp->q_wrptr >= oqp->q_top) {
oqp->q_wrptr = oqp->q_buffer;
}
/* Inform the low side that the queue has at least one character available.*/
if (oqp->q_notify != NULL) {
oqp->q_notify(oqp);
}
return MSG_OK;
}
return MSG_TIMEOUT;
}
/**
* @brief Output queue write with timeout.
* @details This function writes a byte value to an output queue. If the queue
* is full then the calling thread is suspended until there is space
* in the queue or a timeout occurs.
* @note The callback is invoked after putting the character into the
* queue.
*
* @param[in] oqp pointer to an @p output_queue_t structure
* @param[in] b the byte value to be written in the queue
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return The operation status.
* @retval MSG_OK if the operation succeeded.
* @retval MSG_TIMEOUT if the specified time expired.
* @retval MSG_RESET if the queue has been reset.
*
* @api
*/
msg_t oqPutTimeout(output_queue_t *oqp, uint8_t b, sysinterval_t timeout) {
osalSysLock();
/* Waiting until there is a slot available or a timeout occurs.*/
while (oqIsFullI(oqp)) {
msg_t msg = osalThreadEnqueueTimeoutS(&oqp->q_waiting, timeout);
if (msg < MSG_OK) {
osalSysUnlock();
return msg;
}
}
/* Putting the character into the queue.*/
oqp->q_counter--;
*oqp->q_wrptr++ = b;
if (oqp->q_wrptr >= oqp->q_top) {
oqp->q_wrptr = oqp->q_buffer;
}
/* Inform the low side that the queue has at least one character available.*/
if (oqp->q_notify != NULL) {
oqp->q_notify(oqp);
}
osalSysUnlock();
return MSG_OK;
}
/**
* @brief Output queue read.
* @details A byte value is read from the low end of an output queue. The
* operation completes immediately.
*
* @param[in] oqp pointer to an @p output_queue_t structure
* @return The byte value from the queue.
* @retval MSG_TIMEOUT if the queue is empty.
*
* @iclass
*/
msg_t oqGetI(output_queue_t *oqp) {
osalDbgCheckClassI();
/* Queue data check.*/
if (!oqIsEmptyI(oqp)) {
uint8_t b;
oqp->q_counter++;
b = *oqp->q_rdptr++;
if (oqp->q_rdptr >= oqp->q_top) {
oqp->q_rdptr = oqp->q_buffer;
}
osalThreadDequeueNextI(&oqp->q_waiting, MSG_OK);
return (msg_t)b;
}
return MSG_TIMEOUT;
}
/**
* @brief Output queue non-blocking write.
* @details The function writes data from a buffer to an output queue. The
* operation completes immediately.
*
* @param[in] oqp pointer to an @p output_queue_t structure
* @param[in] bp pointer to the data buffer
* @param[in] n the maximum amount of data to be transferred, the
* value 0 is reserved
* @return The number of bytes effectively transferred.
*
* @iclass
*/
size_t oqWriteI(output_queue_t *oqp, const uint8_t *bp, size_t n) {
qnotify_t nfy = oqp->q_notify;
size_t wr;
osalDbgCheckClassI();
wr = oq_write(oqp, bp, n);
/* Inform the low side that the queue has at least one character
available.*/
if ((wr > (size_t)0) && (nfy != NULL)) {
nfy(oqp);
}
return wr;
}
/**
* @brief Output queue write with timeout.
* @details The function writes data from a buffer to an output queue. The
* operation completes when the specified amount of data has been
* transferred or after the specified timeout or if the queue has
* been reset.
* @note The function is not atomic, if you need atomicity it is suggested
* to use a semaphore or a mutex for mutual exclusion.
* @note The callback is invoked after putting each character into the
* queue.
*
* @param[in] oqp pointer to an @p output_queue_t structure
* @param[in] bp pointer to the data buffer
* @param[in] n the maximum amount of data to be transferred, the
* value 0 is reserved
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return The number of bytes effectively transferred.
*
* @api
*/
size_t oqWriteTimeout(output_queue_t *oqp, const uint8_t *bp,
size_t n, sysinterval_t timeout) {
qnotify_t nfy = oqp->q_notify;
size_t max = n;
osalDbgCheck(n > 0U);
osalSysLock();
while (n > 0U) {
size_t done;
done = oq_write(oqp, bp, n);
if (done == (size_t)0) {
msg_t msg = osalThreadEnqueueTimeoutS(&oqp->q_waiting, timeout);
/* Anything except MSG_OK causes the operation to stop.*/
if (msg != MSG_OK) {
break;
}
}
else {
/* Inform the low side that the queue has at least one character
available.*/
if (nfy != NULL) {
nfy(oqp);
}
/* Giving a preemption chance in a controlled point.*/
osalSysUnlock();
n -= done;
bp += done;
osalSysLock();
}
}
osalSysUnlock();
return max - n;
}
/** @} */
|