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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
|
/*
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_buffers.c
* @brief I/O Buffers code.
*
* @addtogroup HAL_BUFFERS
* @details Buffers Queues are used when there is the need to exchange
* fixed-length data buffers between ISRs and threads.
* On the ISR side data can be exchanged only using buffers,
* on the thread side data can be exchanged both using buffers and/or
* using an emulation of regular byte queues.
* There are several kind of buffers queues:<br>
* - <b>Input queue</b>, unidirectional queue where the writer is the
* ISR side and the reader is the thread side.
* - <b>Output queue</b>, unidirectional queue where the writer is the
* thread side and the reader is the ISR 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. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Initializes an input buffers queue object.
*
* @param[out] ibqp pointer to the @p input_buffers_queue_t object
* @param[in] suspended initial state of the queue
* @param[in] bp pointer to a memory area allocated for buffers
* @param[in] size buffers size
* @param[in] n number of buffers
* @param[in] infy callback called when a buffer is returned to the queue
* @param[in] link application defined pointer
*
* @init
*/
void ibqObjectInit(input_buffers_queue_t *ibqp, bool suspended, uint8_t *bp,
size_t size, size_t n, bqnotify_t infy, void *link) {
osalDbgCheck((ibqp != NULL) && (bp != NULL) && (size >= 2U));
osalThreadQueueObjectInit(&ibqp->waiting);
ibqp->suspended = suspended;
ibqp->bcounter = 0;
ibqp->brdptr = bp;
ibqp->bwrptr = bp;
ibqp->btop = bp + ((size + sizeof (size_t)) * n);
ibqp->bsize = size + sizeof (size_t);
ibqp->bn = n;
ibqp->buffers = bp;
ibqp->ptr = NULL;
ibqp->top = NULL;
ibqp->notify = infy;
ibqp->link = link;
}
/**
* @brief Resets an input buffers queue.
* @details All the data in the input buffers 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] ibqp pointer to the @p input_buffers_queue_t object
*
* @iclass
*/
void ibqResetI(input_buffers_queue_t *ibqp) {
osalDbgCheckClassI();
ibqp->bcounter = 0;
ibqp->brdptr = ibqp->buffers;
ibqp->bwrptr = ibqp->buffers;
ibqp->ptr = NULL;
ibqp->top = NULL;
osalThreadDequeueAllI(&ibqp->waiting, MSG_RESET);
}
/**
* @brief Gets the next empty buffer from the queue.
* @note The function always returns the same buffer if called repeatedly.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
* @return A pointer to the next buffer to be filled.
* @retval NULL if the queue is full.
*
* @iclass
*/
uint8_t *ibqGetEmptyBufferI(input_buffers_queue_t *ibqp) {
osalDbgCheckClassI();
if (ibqIsFullI(ibqp)) {
return NULL;
}
return ibqp->bwrptr + sizeof (size_t);
}
/**
* @brief Posts a new filled buffer to the queue.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
* @param[in] size used size of the buffer, cannot be zero
*
* @iclass
*/
void ibqPostFullBufferI(input_buffers_queue_t *ibqp, size_t size) {
osalDbgCheckClassI();
osalDbgCheck((size > 0U) && (size <= (ibqp->bsize - sizeof (size_t))));
osalDbgAssert(!ibqIsFullI(ibqp), "buffers queue full");
/* Writing size field in the buffer.*/
*((size_t *)ibqp->bwrptr) = size;
/* Posting the buffer in the queue.*/
ibqp->bcounter++;
ibqp->bwrptr += ibqp->bsize;
if (ibqp->bwrptr >= ibqp->btop) {
ibqp->bwrptr = ibqp->buffers;
}
/* Waking up one waiting thread, if any.*/
osalThreadDequeueNextI(&ibqp->waiting, MSG_OK);
}
/**
* @brief Gets the next filled buffer from the queue.
* @note The function always acquires the same buffer if called repeatedly.
* @post After calling the function the fields @p ptr and @p top are set
* at beginning and end of the buffer data or @p NULL if the queue
* is empty.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
* @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 a buffer has been acquired.
* @retval MSG_TIMEOUT if the specified time expired.
* @retval MSG_RESET if the queue has been reset or has been put in
* suspended state.
*
* @api
*/
msg_t ibqGetFullBufferTimeout(input_buffers_queue_t *ibqp,
sysinterval_t timeout) {
msg_t msg;
osalSysLock();
msg = ibqGetFullBufferTimeoutS(ibqp, timeout);
osalSysUnlock();
return msg;
}
/**
* @brief Gets the next filled buffer from the queue.
* @note The function always acquires the same buffer if called repeatedly.
* @post After calling the function the fields @p ptr and @p top are set
* at beginning and end of the buffer data or @p NULL if the queue
* is empty.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
* @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 a buffer has been acquired.
* @retval MSG_TIMEOUT if the specified time expired.
* @retval MSG_RESET if the queue has been reset or has been put in
* suspended state.
*
* @sclass
*/
msg_t ibqGetFullBufferTimeoutS(input_buffers_queue_t *ibqp,
sysinterval_t timeout) {
osalDbgCheckClassS();
while (ibqIsEmptyI(ibqp)) {
if (ibqp->suspended) {
return MSG_RESET;
}
msg_t msg = osalThreadEnqueueTimeoutS(&ibqp->waiting, timeout);
if (msg < MSG_OK) {
return msg;
}
}
osalDbgAssert(!ibqIsEmptyI(ibqp), "still empty");
/* Setting up the "current" buffer and its boundary.*/
ibqp->ptr = ibqp->brdptr + sizeof (size_t);
ibqp->top = ibqp->ptr + *((size_t *)ibqp->brdptr);
return MSG_OK;
}
/**
* @brief Releases the buffer back in the queue.
* @note The object callback is called after releasing the buffer.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
*
* @api
*/
void ibqReleaseEmptyBuffer(input_buffers_queue_t *ibqp) {
osalSysLock();
ibqReleaseEmptyBufferS(ibqp);
osalSysUnlock();
}
/**
* @brief Releases the buffer back in the queue.
* @note The object callback is called after releasing the buffer.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
*
* @sclass
*/
void ibqReleaseEmptyBufferS(input_buffers_queue_t *ibqp) {
osalDbgCheckClassS();
osalDbgAssert(!ibqIsEmptyI(ibqp), "buffers queue empty");
/* Freeing a buffer slot in the queue.*/
ibqp->bcounter--;
ibqp->brdptr += ibqp->bsize;
if (ibqp->brdptr >= ibqp->btop) {
ibqp->brdptr = ibqp->buffers;
}
/* No "current" buffer.*/
ibqp->ptr = NULL;
/* Notifying the buffer release.*/
if (ibqp->notify != NULL) {
ibqp->notify(ibqp);
}
}
/**
* @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
* new buffer arrives in the queue or a timeout occurs.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
* @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 or has been put in
* suspended state.
*
* @api
*/
msg_t ibqGetTimeout(input_buffers_queue_t *ibqp, sysinterval_t timeout) {
msg_t msg;
osalSysLock();
/* This condition indicates that a new buffer must be acquired.*/
if (ibqp->ptr == NULL) {
msg = ibqGetFullBufferTimeoutS(ibqp, timeout);
if (msg != MSG_OK) {
osalSysUnlock();
return msg;
}
}
/* Next byte from the buffer.*/
msg = (msg_t)*ibqp->ptr;
ibqp->ptr++;
/* If the current buffer has been fully read then it is returned as
empty in the queue.*/
if (ibqp->ptr >= ibqp->top) {
ibqReleaseEmptyBufferS(ibqp);
}
osalSysUnlock();
return msg;
}
/**
* @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.
*
* @param[in] ibqp pointer to the @p input_buffers_queue_t object
* @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.
* @retval 0 if a timeout occurred.
*
* @api
*/
size_t ibqReadTimeout(input_buffers_queue_t *ibqp, uint8_t *bp,
size_t n, sysinterval_t timeout) {
size_t r = 0;
osalDbgCheck(n > 0U);
osalSysLock();
while (true) {
size_t size;
/* This condition indicates that a new buffer must be acquired.*/
if (ibqp->ptr == NULL) {
msg_t msg;
/* Getting a data buffer using the specified timeout.*/
msg = ibqGetFullBufferTimeoutS(ibqp, timeout);
/* Anything except MSG_OK interrupts the operation.*/
if (msg != MSG_OK) {
osalSysUnlock();
return r;
}
}
/* Size of the data chunk present in the current buffer.*/
size = (size_t)ibqp->top - (size_t)ibqp->ptr;
if (size > (n - r)) {
size = n - r;
}
/* Smaller chunks in order to not make the critical zone too long,
this impacts throughput however.*/
if (size > (size_t)BUFFERS_CHUNKS_SIZE) {
/* Giving the compiler a chance to optimize for a fixed size move.*/
memcpy(bp, ibqp->ptr, BUFFERS_CHUNKS_SIZE);
bp += (size_t)BUFFERS_CHUNKS_SIZE;
ibqp->ptr += (size_t)BUFFERS_CHUNKS_SIZE;
r += (size_t)BUFFERS_CHUNKS_SIZE;
}
else {
memcpy(bp, ibqp->ptr, size);
bp += size;
ibqp->ptr += size;
r += size;
}
/* Has the current data buffer been finished? if so then release it.*/
if (ibqp->ptr >= ibqp->top) {
ibqReleaseEmptyBufferS(ibqp);
}
/* Giving a preemption chance.*/
osalSysUnlock();
if (r >= n) {
return r;
}
osalSysLock();
}
}
/**
* @brief Initializes an output buffers queue object.
*
* @param[out] obqp pointer to the @p output_buffers_queue_t object
* @param[in] suspended initial state of the queue
* @param[in] bp pointer to a memory area allocated for buffers
* @param[in] size buffers size
* @param[in] n number of buffers
* @param[in] onfy callback called when a buffer is posted in the queue
* @param[in] link application defined pointer
*
* @init
*/
void obqObjectInit(output_buffers_queue_t *obqp, bool suspended, uint8_t *bp,
size_t size, size_t n, bqnotify_t onfy, void *link) {
osalDbgCheck((obqp != NULL) && (bp != NULL) && (size >= 2U));
osalThreadQueueObjectInit(&obqp->waiting);
obqp->suspended = suspended;
obqp->bcounter = n;
obqp->brdptr = bp;
obqp->bwrptr = bp;
obqp->btop = bp + ((size + sizeof (size_t)) * n);
obqp->bsize = size + sizeof (size_t);
obqp->bn = n;
obqp->buffers = bp;
obqp->ptr = NULL;
obqp->top = NULL;
obqp->notify = onfy;
obqp->link = link;
}
/**
* @brief Resets an output buffers queue.
* @details All the data in the output buffers 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] obqp pointer to the @p output_buffers_queue_t object
*
* @iclass
*/
void obqResetI(output_buffers_queue_t *obqp) {
osalDbgCheckClassI();
obqp->bcounter = bqSizeX(obqp);
obqp->brdptr = obqp->buffers;
obqp->bwrptr = obqp->buffers;
obqp->ptr = NULL;
obqp->top = NULL;
osalThreadDequeueAllI(&obqp->waiting, MSG_RESET);
}
/**
* @brief Gets the next filled buffer from the queue.
* @note The function always returns the same buffer if called repeatedly.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @param[out] sizep pointer to the filled buffer size
* @return A pointer to the filled buffer.
* @retval NULL if the queue is empty.
*
* @iclass
*/
uint8_t *obqGetFullBufferI(output_buffers_queue_t *obqp,
size_t *sizep) {
osalDbgCheckClassI();
if (obqIsEmptyI(obqp)) {
return NULL;
}
/* Buffer size.*/
*sizep = *((size_t *)obqp->brdptr);
return obqp->brdptr + sizeof (size_t);
}
/**
* @brief Releases the next filled buffer back in the queue.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
*
* @iclass
*/
void obqReleaseEmptyBufferI(output_buffers_queue_t *obqp) {
osalDbgCheckClassI();
osalDbgAssert(!obqIsEmptyI(obqp), "buffers queue empty");
/* Freeing a buffer slot in the queue.*/
obqp->bcounter++;
obqp->brdptr += obqp->bsize;
if (obqp->brdptr >= obqp->btop) {
obqp->brdptr = obqp->buffers;
}
/* Waking up one waiting thread, if any.*/
osalThreadDequeueNextI(&obqp->waiting, MSG_OK);
}
/**
* @brief Gets the next empty buffer from the queue.
* @note The function always acquires the same buffer if called repeatedly.
* @post After calling the function the fields @p ptr and @p top are set
* at beginning and end of the buffer data or @p NULL if the queue
* is empty.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @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 a buffer has been acquired.
* @retval MSG_TIMEOUT if the specified time expired.
* @retval MSG_RESET if the queue has been reset or has been put in
* suspended state.
*
* @api
*/
msg_t obqGetEmptyBufferTimeout(output_buffers_queue_t *obqp,
sysinterval_t timeout) {
msg_t msg;
osalSysLock();
msg = obqGetEmptyBufferTimeoutS(obqp, timeout);
osalSysUnlock();
return msg;
}
/**
* @brief Gets the next empty buffer from the queue.
* @note The function always acquires the same buffer if called repeatedly.
* @post After calling the function the fields @p ptr and @p top are set
* at beginning and end of the buffer data or @p NULL if the queue
* is empty.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @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 a buffer has been acquired.
* @retval MSG_TIMEOUT if the specified time expired.
* @retval MSG_RESET if the queue has been reset or has been put in
* suspended state.
*
* @sclass
*/
msg_t obqGetEmptyBufferTimeoutS(output_buffers_queue_t *obqp,
sysinterval_t timeout) {
osalDbgCheckClassS();
while (obqIsFullI(obqp)) {
if (obqp->suspended) {
return MSG_RESET;
}
msg_t msg = osalThreadEnqueueTimeoutS(&obqp->waiting, timeout);
if (msg < MSG_OK) {
return msg;
}
}
osalDbgAssert(!obqIsFullI(obqp), "still full");
/* Setting up the "current" buffer and its boundary.*/
obqp->ptr = obqp->bwrptr + sizeof (size_t);
obqp->top = obqp->bwrptr + obqp->bsize;
return MSG_OK;
}
/**
* @brief Posts a new filled buffer to the queue.
* @note The object callback is called after releasing the buffer.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @param[in] size used size of the buffer, cannot be zero
*
* @api
*/
void obqPostFullBuffer(output_buffers_queue_t *obqp, size_t size) {
osalSysLock();
obqPostFullBufferS(obqp, size);
osalSysUnlock();
}
/**
* @brief Posts a new filled buffer to the queue.
* @note The object callback is called after releasing the buffer.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @param[in] size used size of the buffer, cannot be zero
*
* @sclass
*/
void obqPostFullBufferS(output_buffers_queue_t *obqp, size_t size) {
osalDbgCheckClassS();
osalDbgCheck((size > 0U) && (size <= (obqp->bsize - sizeof (size_t))));
osalDbgAssert(!obqIsFullI(obqp), "buffers queue full");
/* Writing size field in the buffer.*/
*((size_t *)obqp->bwrptr) = size;
/* Posting the buffer in the queue.*/
obqp->bcounter--;
obqp->bwrptr += obqp->bsize;
if (obqp->bwrptr >= obqp->btop) {
obqp->bwrptr = obqp->buffers;
}
/* No "current" buffer.*/
obqp->ptr = NULL;
/* Notifying the buffer release.*/
if (obqp->notify != NULL) {
obqp->notify(obqp);
}
}
/**
* @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 a
* new buffer is freed in the queue or a timeout occurs.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @param[in] b byte value to be transferred
* @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 or has been put in
* suspended state.
*
* @api
*/
msg_t obqPutTimeout(output_buffers_queue_t *obqp, uint8_t b,
sysinterval_t timeout) {
msg_t msg;
osalSysLock();
/* This condition indicates that a new buffer must be acquired.*/
if (obqp->ptr == NULL) {
msg = obqGetEmptyBufferTimeoutS(obqp, timeout);
if (msg != MSG_OK) {
osalSysUnlock();
return msg;
}
}
/* Writing the byte to the buffer.*/
*obqp->ptr = b;
obqp->ptr++;
/* If the current buffer has been fully written then it is posted as
full in the queue.*/
if (obqp->ptr >= obqp->top) {
obqPostFullBufferS(obqp, obqp->bsize - sizeof (size_t));
}
osalSysUnlock();
return MSG_OK;
}
/**
* @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.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @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.
* @retval 0 if a timeout occurred.
*
* @api
*/
size_t obqWriteTimeout(output_buffers_queue_t *obqp, const uint8_t *bp,
size_t n, sysinterval_t timeout) {
size_t w = 0;
osalDbgCheck(n > 0U);
osalSysLock();
while (true) {
size_t size;
/* This condition indicates that a new buffer must be acquired.*/
if (obqp->ptr == NULL) {
msg_t msg;
/* Getting an empty buffer using the specified timeout.*/
msg = obqGetEmptyBufferTimeoutS(obqp, timeout);
/* Anything except MSG_OK interrupts the operation.*/
if (msg != MSG_OK) {
osalSysUnlock();
return w;
}
}
/* Size of the space available in the current buffer.*/
size = (size_t)obqp->top - (size_t)obqp->ptr;
if (size > (n - w)) {
size = n - w;
}
/* Smaller chunks in order to not make the critical zone too long,
this impacts throughput however.*/
if (size > (size_t)BUFFERS_CHUNKS_SIZE) {
/* Giving the compiler a chance to optimize for a fixed size move.*/
memcpy(obqp->ptr, bp, (size_t)BUFFERS_CHUNKS_SIZE);
bp += (size_t)BUFFERS_CHUNKS_SIZE;
obqp->ptr += (size_t)BUFFERS_CHUNKS_SIZE;
w += (size_t)BUFFERS_CHUNKS_SIZE;
}
else {
memcpy(obqp->ptr, bp, size);
bp += size;
obqp->ptr += size;
w += size;
}
/* Has the current data buffer been finished? if so then release it.*/
if (obqp->ptr >= obqp->top) {
obqPostFullBufferS(obqp, obqp->bsize - sizeof (size_t));
}
/* Giving a preemption chance.*/
osalSysUnlock();
if (w >= n) {
return w;
}
osalSysLock();
}
}
/**
* @brief Flushes the current, partially filled, buffer to the queue.
* @note The notification callback is not invoked because the function
* is meant to be called from ISR context. An operation status is
* returned instead.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
* @return The operation status.
* @retval false if no new filled buffer has been posted to the queue.
* @retval true if a new filled buffer has been posted to the queue.
*
* @iclass
*/
bool obqTryFlushI(output_buffers_queue_t *obqp) {
osalDbgCheckClassI();
/* If queue is empty and there is a buffer partially filled and
it is not being written.*/
if (obqIsEmptyI(obqp) && (obqp->ptr != NULL)) {
size_t size = (size_t)obqp->ptr - ((size_t)obqp->bwrptr + sizeof (size_t));
if (size > 0U) {
/* Writing size field in the buffer.*/
*((size_t *)obqp->bwrptr) = size;
/* Posting the buffer in the queue.*/
obqp->bcounter--;
obqp->bwrptr += obqp->bsize;
if (obqp->bwrptr >= obqp->btop) {
obqp->bwrptr = obqp->buffers;
}
/* No "current" buffer.*/
obqp->ptr = NULL;
return true;
}
}
return false;
}
/**
* @brief Flushes the current, partially filled, buffer to the queue.
*
* @param[in] obqp pointer to the @p output_buffers_queue_t object
*
* @api
*/
void obqFlush(output_buffers_queue_t *obqp) {
osalSysLock();
/* If there is a buffer partially filled and not being written.*/
if (obqp->ptr != NULL) {
size_t size = ((size_t)obqp->ptr - (size_t)obqp->bwrptr) - sizeof (size_t);
if (size > 0U) {
obqPostFullBufferS(obqp, size);
}
}
osalSysUnlock();
}
/** @} */
|