1
6
7
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
177
178
179
180
181
182
183
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
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
278
279
280
281
282
283
284
285
286
290
294
295
298
299
305
306
307
308
309
310
311
312
316
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
340
341
342
343
344
345
346
347
348
349
350
351
352
355
356
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
387
388
389
390
400
401
404
405
406
409
410
411
412
413
414
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
466
467
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
500
501
504
505
506
507
508
509
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
549
550
557
558
559
560
561
562
563
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
631
632
633
650
651
652
660
661
662
663
664
665
666
667
668
669
670
673
674
675
676
677
678
679
682
692
693
694
695
696
697
698
705
706
707
708
709
710
711
712
715
727
728
729
730
736
737
738
739
740
741
742
743
746
747
751
752
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
805
806
814
815
816
817
818
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
852
853
863
864
865
866
875
876
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
927
928
929
930
931
932
933
934
935
936
937
938
939
940
943
944
945
948
949
950
951
952
953
954
955
956
957
958
959
963
964
965
966
967
968
969
970
971
972
973
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1025
1026
1027
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1046
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1068
1071
1072
1073
1093
1094
1095
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1178
1179
1180
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1198
1199
1200
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1255
1256
1257
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1338
1341
1344
1347
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1393
1394
1395
1396
1397
1400
1401
1402
1403
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
/* ... */
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/list.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/ringbuf.h"
#include "esp_heap_caps.h"9 includes
#define rbALIGN_MASK (0x03)
#define rbALIGN_SIZE( xSize ) ( ( xSize + rbALIGN_MASK ) & ~rbALIGN_MASK )
#define rbCHECK_ALIGNED( pvPtr ) ( ( ( UBaseType_t ) ( pvPtr ) & rbALIGN_MASK ) == 0 )
#define rbALLOW_SPLIT_FLAG ( ( UBaseType_t ) 1 )
#define rbBYTE_BUFFER_FLAG ( ( UBaseType_t ) 2 )
#define rbBUFFER_FULL_FLAG ( ( UBaseType_t ) 4 )
#define rbBUFFER_STATIC_FLAG ( ( UBaseType_t ) 8 )
#define rbUSING_QUEUE_SET ( ( UBaseType_t ) 16 )
#define rbITEM_FREE_FLAG ( ( UBaseType_t ) 1 )
#define rbITEM_DUMMY_DATA_FLAG ( ( UBaseType_t ) 2 )
#define rbITEM_SPLIT_FLAG ( ( UBaseType_t ) 4 )
#define rbITEM_WRITTEN_FLAG ( ( UBaseType_t ) 8 ) 12 defines
typedef struct {
size_t xItemLen;
UBaseType_t uxItemFlags;
}{ ... } ItemHeader_t;
#define rbHEADER_SIZE sizeof(ItemHeader_t)
typedef struct RingbufferDefinition Ringbuffer_t;
typedef BaseType_t (*CheckItemFitsFunction_t)(Ringbuffer_t *pxRingbuffer, size_t xItemSize);
typedef void (*CopyItemFunction_t)(Ringbuffer_t *pxRingbuffer, const uint8_t *pcItem, size_t xItemSize);
typedef BaseType_t (*CheckItemAvailFunction_t)(Ringbuffer_t *pxRingbuffer);
typedef void *(*GetItemFunction_t)(Ringbuffer_t *pxRingbuffer, BaseType_t *pxIsSplit, size_t xMaxSize, size_t *pxItemSize);
typedef void (*ReturnItemFunction_t)(Ringbuffer_t *pxRingbuffer, uint8_t *pvItem);
typedef size_t (*GetCurMaxSizeFunction_t)(Ringbuffer_t *pxRingbuffer);
typedef struct RingbufferDefinition {
size_t xSize;
size_t xMaxItemSize;
UBaseType_t uxRingbufferFlags;
CheckItemFitsFunction_t xCheckItemFits;
CopyItemFunction_t vCopyItem;
GetItemFunction_t pvGetItem;
ReturnItemFunction_t vReturnItem;
GetCurMaxSizeFunction_t xGetCurMaxSize;
uint8_t *pucAcquire;
uint8_t *pucWrite;
uint8_t *pucRead;
uint8_t *pucFree;
uint8_t *pucHead;
uint8_t *pucTail;
BaseType_t xItemsWaiting;
List_t xTasksWaitingToSend;
List_t xTasksWaitingToReceive;
QueueSetHandle_t xQueueSet;
portMUX_TYPE mux;
}{ ... } Ringbuffer_t;
_Static_assert(sizeof(StaticRingbuffer_t) == sizeof(Ringbuffer_t), "StaticRingbuffer_t != Ringbuffer_t");
Macros and Types
/* ... */
static void prvInitializeNewRingbuffer(size_t xBufferSize,
RingbufferType_t xBufferType,
Ringbuffer_t *pxNewRingbuffer,
uint8_t *pucRingbufferStorage);
static size_t prvGetFreeSize(Ringbuffer_t *pxRingbuffer);
static BaseType_t prvCheckItemAvail(Ringbuffer_t *pxRingbuffer);
static BaseType_t prvCheckItemFitsDefault(Ringbuffer_t *pxRingbuffer, size_t xItemSize);
static BaseType_t prvCheckItemFitsByteBuffer(Ringbuffer_t *pxRingbuffer, size_t xItemSize);
/* ... */
static void prvCopyItemNoSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize);
/* ... */
static void prvCopyItemAllowSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize);
static void prvCopyItemByteBuf(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize);
/* ... */
static void *prvGetItemDefault(Ringbuffer_t *pxRingbuffer,
BaseType_t *pxIsSplit,
size_t xUnusedParam,
size_t *pxItemSize);
static void *prvGetItemByteBuf(Ringbuffer_t *pxRingbuffer,
BaseType_t *pxUnusedParam,
size_t xMaxSize,
size_t *pxItemSize);
/* ... */
static void prvReturnItemDefault(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem);
static void prvReturnItemByteBuf(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem);
static size_t prvGetCurMaxSizeNoSplit(Ringbuffer_t *pxRingbuffer);
static size_t prvGetCurMaxSizeAllowSplit(Ringbuffer_t *pxRingbuffer);
static size_t prvGetCurMaxSizeByteBuf(Ringbuffer_t *pxRingbuffer);
/* ... */
static BaseType_t prvSendAcquireGeneric(Ringbuffer_t *pxRingbuffer,
const void *pvItem,
void **ppvItem,
size_t xItemSize,
TickType_t xTicksToWait);
/* ... */
static BaseType_t prvReceiveGeneric(Ringbuffer_t *pxRingbuffer,
void **pvItem1,
void **pvItem2,
size_t *xItemSize1,
size_t *xItemSize2,
size_t xMaxSize,
TickType_t xTicksToWait);
static BaseType_t prvReceiveGenericFromISR(Ringbuffer_t *pxRingbuffer,
void **pvItem1,
void **pvItem2,
size_t *xItemSize1,
size_t *xItemSize2,
size_t xMaxSize);
Forward Declares
static void prvInitializeNewRingbuffer(size_t xBufferSize,
RingbufferType_t xBufferType,
Ringbuffer_t *pxNewRingbuffer,
uint8_t *pucRingbufferStorage)
{
pxNewRingbuffer->xSize = xBufferSize;
pxNewRingbuffer->pucHead = pucRingbufferStorage;
pxNewRingbuffer->pucTail = pucRingbufferStorage + xBufferSize;
pxNewRingbuffer->pucFree = pucRingbufferStorage;
pxNewRingbuffer->pucRead = pucRingbufferStorage;
pxNewRingbuffer->pucWrite = pucRingbufferStorage;
pxNewRingbuffer->pucAcquire = pucRingbufferStorage;
pxNewRingbuffer->xItemsWaiting = 0;
pxNewRingbuffer->uxRingbufferFlags = 0;
if (xBufferType == RINGBUF_TYPE_NOSPLIT) {
pxNewRingbuffer->xCheckItemFits = prvCheckItemFitsDefault;
pxNewRingbuffer->vCopyItem = prvCopyItemNoSplit;
pxNewRingbuffer->pvGetItem = prvGetItemDefault;
pxNewRingbuffer->vReturnItem = prvReturnItemDefault;
/* ... */
pxNewRingbuffer->xMaxItemSize = rbALIGN_SIZE(pxNewRingbuffer->xSize / 2) - rbHEADER_SIZE;
pxNewRingbuffer->xGetCurMaxSize = prvGetCurMaxSizeNoSplit;
}{...} else if (xBufferType == RINGBUF_TYPE_ALLOWSPLIT) {
pxNewRingbuffer->uxRingbufferFlags |= rbALLOW_SPLIT_FLAG;
pxNewRingbuffer->xCheckItemFits = prvCheckItemFitsDefault;
pxNewRingbuffer->vCopyItem = prvCopyItemAllowSplit;
pxNewRingbuffer->pvGetItem = prvGetItemDefault;
pxNewRingbuffer->vReturnItem = prvReturnItemDefault;
pxNewRingbuffer->xMaxItemSize = pxNewRingbuffer->xSize - (sizeof(ItemHeader_t) * 2);
pxNewRingbuffer->xGetCurMaxSize = prvGetCurMaxSizeAllowSplit;
}{...} else {
pxNewRingbuffer->uxRingbufferFlags |= rbBYTE_BUFFER_FLAG;
pxNewRingbuffer->xCheckItemFits = prvCheckItemFitsByteBuffer;
pxNewRingbuffer->vCopyItem = prvCopyItemByteBuf;
pxNewRingbuffer->pvGetItem = prvGetItemByteBuf;
pxNewRingbuffer->vReturnItem = prvReturnItemByteBuf;
pxNewRingbuffer->xMaxItemSize = pxNewRingbuffer->xSize;
pxNewRingbuffer->xGetCurMaxSize = prvGetCurMaxSizeByteBuf;
}{...}
vListInitialise(&pxNewRingbuffer->xTasksWaitingToSend);
vListInitialise(&pxNewRingbuffer->xTasksWaitingToReceive);
pxNewRingbuffer->xQueueSet = NULL;
portMUX_INITIALIZE(&pxNewRingbuffer->mux);
}{ ... }
static size_t prvGetFreeSize(Ringbuffer_t *pxRingbuffer)
{
size_t xReturn;
if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
xReturn = 0;
}{...} else {
BaseType_t xFreeSize = pxRingbuffer->pucFree - pxRingbuffer->pucAcquire;
if (xFreeSize <= 0) {
xFreeSize += pxRingbuffer->xSize;
}{...}
xReturn = xFreeSize;
}{...}
configASSERT(xReturn <= pxRingbuffer->xSize);
return xReturn;
}{ ... }
static BaseType_t prvCheckItemFitsDefault(Ringbuffer_t *pxRingbuffer, size_t xItemSize)
{
configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucAcquire));
configASSERT(pxRingbuffer->pucAcquire >= pxRingbuffer->pucHead && pxRingbuffer->pucAcquire < pxRingbuffer->pucTail);
size_t xTotalItemSize = rbALIGN_SIZE(xItemSize) + rbHEADER_SIZE;
if (pxRingbuffer->pucAcquire == pxRingbuffer->pucFree) {
return (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) ? pdFALSE : pdTRUE;
}{...}
if (pxRingbuffer->pucFree > pxRingbuffer->pucAcquire) {
return (xTotalItemSize <= pxRingbuffer->pucFree - pxRingbuffer->pucAcquire) ? pdTRUE : pdFALSE;
}{...}
if (xTotalItemSize <= pxRingbuffer->pucTail - pxRingbuffer->pucAcquire) {
return pdTRUE;
}{...}
if (pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG) {
return (xTotalItemSize + rbHEADER_SIZE <= pxRingbuffer->xSize - (pxRingbuffer->pucAcquire - pxRingbuffer->pucFree)) ? pdTRUE : pdFALSE;
}{...} else {
return (xTotalItemSize <= pxRingbuffer->pucFree - pxRingbuffer->pucHead) ? pdTRUE : pdFALSE;
}{...}
}{ ... }
static BaseType_t prvCheckItemFitsByteBuffer(Ringbuffer_t *pxRingbuffer, size_t xItemSize)
{
configASSERT(pxRingbuffer->pucAcquire >= pxRingbuffer->pucHead && pxRingbuffer->pucAcquire < pxRingbuffer->pucTail);
if (pxRingbuffer->pucAcquire == pxRingbuffer->pucFree) {
return (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) ? pdFALSE : pdTRUE;
}{...}
if (pxRingbuffer->pucFree > pxRingbuffer->pucAcquire) {
return (xItemSize <= pxRingbuffer->pucFree - pxRingbuffer->pucAcquire) ? pdTRUE : pdFALSE;
}{...}
return (xItemSize <= pxRingbuffer->xSize - (pxRingbuffer->pucAcquire - pxRingbuffer->pucFree)) ? pdTRUE : pdFALSE;
}{ ... }
static uint8_t* prvAcquireItemNoSplit(Ringbuffer_t *pxRingbuffer, size_t xItemSize)
{
size_t xAlignedItemSize = rbALIGN_SIZE(xItemSize);
size_t xRemLen = pxRingbuffer->pucTail - pxRingbuffer->pucAcquire;
configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucAcquire));
configASSERT(pxRingbuffer->pucAcquire >= pxRingbuffer->pucHead && pxRingbuffer->pucAcquire < pxRingbuffer->pucTail);
configASSERT(xRemLen >= rbHEADER_SIZE);
if (xRemLen < xAlignedItemSize + rbHEADER_SIZE) {
ItemHeader_t *pxDummy = (ItemHeader_t *)pxRingbuffer->pucAcquire;
pxDummy->uxItemFlags = rbITEM_DUMMY_DATA_FLAG;
pxDummy->xItemLen = 0;
pxRingbuffer->pucAcquire = pxRingbuffer->pucHead;
}{...}
ItemHeader_t *pxHeader = (ItemHeader_t *)pxRingbuffer->pucAcquire;
pxHeader->xItemLen = xItemSize;
pxHeader->uxItemFlags = 0;
uint8_t* item_address = pxRingbuffer->pucAcquire + rbHEADER_SIZE;
pxRingbuffer->pucAcquire += rbHEADER_SIZE + xAlignedItemSize;
if (pxRingbuffer->pucTail - pxRingbuffer->pucAcquire < rbHEADER_SIZE) {
pxRingbuffer->pucAcquire = pxRingbuffer->pucHead;
}{...}
if (pxRingbuffer->pucAcquire == pxRingbuffer->pucFree) {
pxRingbuffer->uxRingbufferFlags |= rbBUFFER_FULL_FLAG;
}{...}
return item_address;
}{ ... }
static void prvSendItemDoneNoSplit(Ringbuffer_t *pxRingbuffer, uint8_t* pucItem)
{
configASSERT(rbCHECK_ALIGNED(pucItem));
configASSERT(pucItem >= pxRingbuffer->pucHead);
configASSERT(pucItem <= pxRingbuffer->pucTail);
ItemHeader_t *pxCurHeader = (ItemHeader_t *)(pucItem - rbHEADER_SIZE);
configASSERT(pxCurHeader->xItemLen <= pxRingbuffer->xMaxItemSize);
configASSERT((pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) == 0);
configASSERT((pxCurHeader->uxItemFlags & rbITEM_WRITTEN_FLAG) == 0);
pxCurHeader->uxItemFlags &= ~rbITEM_SPLIT_FLAG;
pxCurHeader->uxItemFlags |= rbITEM_WRITTEN_FLAG;
pxRingbuffer->xItemsWaiting++;
/* ... */
pxCurHeader = (ItemHeader_t *)pxRingbuffer->pucWrite;
while (((pxCurHeader->uxItemFlags & rbITEM_WRITTEN_FLAG) || (pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG))) {
if (pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) {
pxCurHeader->uxItemFlags |= rbITEM_WRITTEN_FLAG;
pxRingbuffer->pucWrite = pxRingbuffer->pucHead;
}{...} else {
size_t xAlignedItemSize = rbALIGN_SIZE(pxCurHeader->xItemLen);
pxRingbuffer->pucWrite += xAlignedItemSize + rbHEADER_SIZE;
configASSERT(pxRingbuffer->pucWrite <= pxRingbuffer->pucHead + pxRingbuffer->xSize);
}{...}
if ((pxRingbuffer->pucTail - pxRingbuffer->pucWrite) < rbHEADER_SIZE) {
pxRingbuffer->pucWrite = pxRingbuffer->pucHead;
}{...}
if (pxRingbuffer->pucWrite == pxRingbuffer->pucAcquire) {
break;
}{...}
pxCurHeader = (ItemHeader_t *)pxRingbuffer->pucWrite;
}{...}
}{ ... }
static void prvCopyItemNoSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize)
{
uint8_t* item_addr = prvAcquireItemNoSplit(pxRingbuffer, xItemSize);
memcpy(item_addr, pucItem, xItemSize);
prvSendItemDoneNoSplit(pxRingbuffer, item_addr);
}{ ... }
static void prvCopyItemAllowSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize)
{
size_t xAlignedItemSize = rbALIGN_SIZE(xItemSize);
size_t xRemLen = pxRingbuffer->pucTail - pxRingbuffer->pucAcquire;
configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucAcquire));
configASSERT(pxRingbuffer->pucAcquire >= pxRingbuffer->pucHead && pxRingbuffer->pucAcquire < pxRingbuffer->pucTail);
configASSERT(xRemLen >= rbHEADER_SIZE);
if (xRemLen < xAlignedItemSize + rbHEADER_SIZE) {
ItemHeader_t *pxFirstHeader = (ItemHeader_t *)pxRingbuffer->pucAcquire;
pxFirstHeader->uxItemFlags = 0;
pxFirstHeader->xItemLen = xRemLen - rbHEADER_SIZE;
pxRingbuffer->pucAcquire += rbHEADER_SIZE;
xRemLen -= rbHEADER_SIZE;
if (xRemLen > 0) {
memcpy(pxRingbuffer->pucAcquire, pucItem, xRemLen);
pxRingbuffer->xItemsWaiting++;
pucItem += xRemLen;
xItemSize -= xRemLen;
xAlignedItemSize -= xRemLen;
pxFirstHeader->uxItemFlags |= rbITEM_SPLIT_FLAG;
}{...} else {
pxFirstHeader->uxItemFlags |= rbITEM_DUMMY_DATA_FLAG;
}{...}
pxRingbuffer->pucAcquire = pxRingbuffer->pucHead;
}{...}
ItemHeader_t *pxSecondHeader = (ItemHeader_t *)pxRingbuffer->pucAcquire;
pxSecondHeader->xItemLen = xItemSize;
pxSecondHeader->uxItemFlags = 0;
pxRingbuffer->pucAcquire += rbHEADER_SIZE;
memcpy(pxRingbuffer->pucAcquire, pucItem, xItemSize);
pxRingbuffer->xItemsWaiting++;
pxRingbuffer->pucAcquire += xAlignedItemSize;
if (pxRingbuffer->pucTail - pxRingbuffer->pucAcquire < rbHEADER_SIZE) {
pxRingbuffer->pucAcquire = pxRingbuffer->pucHead;
}{...}
if (pxRingbuffer->pucAcquire == pxRingbuffer->pucFree) {
pxRingbuffer->uxRingbufferFlags |= rbBUFFER_FULL_FLAG;
}{...}
pxRingbuffer->pucWrite = pxRingbuffer->pucAcquire;
}{ ... }
static void prvCopyItemByteBuf(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize)
{
configASSERT(pxRingbuffer->pucAcquire >= pxRingbuffer->pucHead && pxRingbuffer->pucAcquire < pxRingbuffer->pucTail);
size_t xRemLen = pxRingbuffer->pucTail - pxRingbuffer->pucAcquire;
if (xRemLen < xItemSize) {
memcpy(pxRingbuffer->pucAcquire, pucItem, xRemLen);
pxRingbuffer->xItemsWaiting += xRemLen;
pucItem += xRemLen;
xItemSize -= xRemLen;
pxRingbuffer->pucAcquire = pxRingbuffer->pucHead;
}{...}
memcpy(pxRingbuffer->pucAcquire, pucItem, xItemSize);
pxRingbuffer->xItemsWaiting += xItemSize;
pxRingbuffer->pucAcquire += xItemSize;
if (pxRingbuffer->pucAcquire == pxRingbuffer->pucTail) {
pxRingbuffer->pucAcquire = pxRingbuffer->pucHead;
}{...}
if (pxRingbuffer->pucAcquire == pxRingbuffer->pucFree) {
pxRingbuffer->uxRingbufferFlags |= rbBUFFER_FULL_FLAG;
}{...}
pxRingbuffer->pucWrite = pxRingbuffer->pucAcquire;
}{ ... }
static BaseType_t prvCheckItemAvail(Ringbuffer_t *pxRingbuffer)
{
if ((pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) && pxRingbuffer->pucRead != pxRingbuffer->pucFree) {
return pdFALSE;
}{...}
if ((pxRingbuffer->xItemsWaiting > 0) && ((pxRingbuffer->pucRead != pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG))) {
if ((pxRingbuffer->uxRingbufferFlags & (rbBYTE_BUFFER_FLAG | rbALLOW_SPLIT_FLAG)) == 0) {
ItemHeader_t *pxHeader = (ItemHeader_t *)pxRingbuffer->pucRead;
if ((pxHeader->uxItemFlags & rbITEM_WRITTEN_FLAG) == 0) {
return pdFALSE;
}{...}
}{...}
return pdTRUE;
}{...} else {
return pdFALSE;
}{...}
}{ ... }
static void *prvGetItemDefault(Ringbuffer_t *pxRingbuffer,
BaseType_t *pxIsSplit,
size_t xUnusedParam,
size_t *pxItemSize)
{
ItemHeader_t *pxHeader = (ItemHeader_t *)pxRingbuffer->pucRead;
configASSERT(pxIsSplit != NULL);
configASSERT((pxRingbuffer->xItemsWaiting > 0) && ((pxRingbuffer->pucRead != pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG)));
configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucRead));
configASSERT(pxRingbuffer->pucRead >= pxRingbuffer->pucHead && pxRingbuffer->pucRead < pxRingbuffer->pucTail);
configASSERT((pxHeader->xItemLen <= pxRingbuffer->xMaxItemSize) || (pxHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG));
uint8_t *pcReturn;
if (pxHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) {
pxRingbuffer->pucRead = pxRingbuffer->pucHead;
pxHeader = (ItemHeader_t *)pxRingbuffer->pucRead;
configASSERT(pxHeader->xItemLen <= pxRingbuffer->xMaxItemSize);
}{...}
pcReturn = pxRingbuffer->pucRead + rbHEADER_SIZE;
if (pxHeader->xItemLen == 0) {
configASSERT(pcReturn >= pxRingbuffer->pucHead && pcReturn <= pxRingbuffer->pucTail);
}{...} else {
configASSERT(pcReturn >= pxRingbuffer->pucHead && pcReturn < pxRingbuffer->pucTail);
}{...}
*pxItemSize = pxHeader->xItemLen;
pxRingbuffer->xItemsWaiting --;
*pxIsSplit = (pxHeader->uxItemFlags & rbITEM_SPLIT_FLAG) ? pdTRUE : pdFALSE;
pxRingbuffer->pucRead += rbHEADER_SIZE + rbALIGN_SIZE(pxHeader->xItemLen);
if ((pxRingbuffer->pucTail - pxRingbuffer->pucRead) < rbHEADER_SIZE) {
pxRingbuffer->pucRead = pxRingbuffer->pucHead;
}{...}
return (void *)pcReturn;
}{ ... }
static void *prvGetItemByteBuf(Ringbuffer_t *pxRingbuffer,
BaseType_t *pxUnusedParam,
size_t xMaxSize,
size_t *pxItemSize)
{
configASSERT((pxRingbuffer->xItemsWaiting > 0) && ((pxRingbuffer->pucRead != pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG)));
configASSERT(pxRingbuffer->pucRead >= pxRingbuffer->pucHead && pxRingbuffer->pucRead < pxRingbuffer->pucTail);
configASSERT(pxRingbuffer->pucRead == pxRingbuffer->pucFree);
uint8_t *ret = pxRingbuffer->pucRead;
if ((pxRingbuffer->pucRead > pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG)) {
if (xMaxSize == 0 || pxRingbuffer->pucTail - pxRingbuffer->pucRead <= xMaxSize) {
*pxItemSize = pxRingbuffer->pucTail - pxRingbuffer->pucRead;
pxRingbuffer->xItemsWaiting -= pxRingbuffer->pucTail - pxRingbuffer->pucRead;
pxRingbuffer->pucRead = pxRingbuffer->pucHead;
}{...} else {
*pxItemSize = xMaxSize;
pxRingbuffer->xItemsWaiting -= xMaxSize;
pxRingbuffer->pucRead += xMaxSize;
}{...}
}{...} else {
if (xMaxSize == 0 || pxRingbuffer->pucWrite - pxRingbuffer->pucRead <= xMaxSize) {
*pxItemSize = pxRingbuffer->pucWrite - pxRingbuffer->pucRead;
pxRingbuffer->xItemsWaiting -= pxRingbuffer->pucWrite - pxRingbuffer->pucRead;
pxRingbuffer->pucRead = pxRingbuffer->pucWrite;
}{...} else {
*pxItemSize = xMaxSize;
pxRingbuffer->xItemsWaiting -= xMaxSize;
pxRingbuffer->pucRead += xMaxSize;
}{...}
}{...}
return (void *)ret;
}{ ... }
static void prvReturnItemDefault(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem)
{
configASSERT(rbCHECK_ALIGNED(pucItem));
configASSERT(pucItem >= pxRingbuffer->pucHead);
configASSERT(pucItem <= pxRingbuffer->pucTail);
ItemHeader_t *pxCurHeader = (ItemHeader_t *)(pucItem - rbHEADER_SIZE);
configASSERT(pxCurHeader->xItemLen <= pxRingbuffer->xMaxItemSize);
configASSERT((pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) == 0);
configASSERT((pxCurHeader->uxItemFlags & rbITEM_FREE_FLAG) == 0);
pxCurHeader->uxItemFlags &= ~rbITEM_SPLIT_FLAG;
pxCurHeader->uxItemFlags |= rbITEM_FREE_FLAG;
/* ... */
pxCurHeader = (ItemHeader_t *)pxRingbuffer->pucFree;
while (((pxCurHeader->uxItemFlags & rbITEM_FREE_FLAG) || (pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG)) && pxRingbuffer->pucFree != pxRingbuffer->pucRead) {
if (pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) {
pxCurHeader->uxItemFlags |= rbITEM_FREE_FLAG;
pxRingbuffer->pucFree = pxRingbuffer->pucHead;
}{...} else {
size_t xAlignedItemSize = rbALIGN_SIZE(pxCurHeader->xItemLen);
pxRingbuffer->pucFree += xAlignedItemSize + rbHEADER_SIZE;
configASSERT(pxRingbuffer->pucFree <= pxRingbuffer->pucHead + pxRingbuffer->xSize);
}{...}
if ((pxRingbuffer->pucTail - pxRingbuffer->pucFree) < rbHEADER_SIZE) {
pxRingbuffer->pucFree = pxRingbuffer->pucHead;
}{...}
pxCurHeader = (ItemHeader_t *)pxRingbuffer->pucFree;
}{...}
if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
if (pxRingbuffer->pucFree != pxRingbuffer->pucAcquire) {
pxRingbuffer->uxRingbufferFlags &= ~rbBUFFER_FULL_FLAG;
}{...} else if (pxRingbuffer->pucFree == pxRingbuffer->pucAcquire && pxRingbuffer->pucFree == pxRingbuffer->pucRead) {
pxRingbuffer->uxRingbufferFlags &= ~rbBUFFER_FULL_FLAG;
}{...}
}{...}
}{ ... }
static void prvReturnItemByteBuf(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem)
{
configASSERT((uint8_t *)pucItem >= pxRingbuffer->pucHead);
configASSERT((uint8_t *)pucItem < pxRingbuffer->pucTail);
pxRingbuffer->pucFree = pxRingbuffer->pucRead;
if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
pxRingbuffer->uxRingbufferFlags &= ~rbBUFFER_FULL_FLAG;
}{...}
}{ ... }
static size_t prvGetCurMaxSizeNoSplit(Ringbuffer_t *pxRingbuffer)
{
BaseType_t xFreeSize;
if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
return 0;
}{...}
if (pxRingbuffer->pucAcquire < pxRingbuffer->pucFree) {
xFreeSize = pxRingbuffer->pucFree - pxRingbuffer->pucAcquire;
}{...} else {
size_t xSize1 = pxRingbuffer->pucTail - pxRingbuffer->pucAcquire;
size_t xSize2 = pxRingbuffer->pucFree - pxRingbuffer->pucHead;
xFreeSize = (xSize1 > xSize2) ? xSize1 : xSize2;
}{...}
xFreeSize -= rbHEADER_SIZE;
if (xFreeSize < 0) {
xFreeSize = 0;
}{...} else if (xFreeSize > pxRingbuffer->xMaxItemSize) {
xFreeSize = pxRingbuffer->xMaxItemSize;
}{...}
return xFreeSize;
}{ ... }
static size_t prvGetCurMaxSizeAllowSplit(Ringbuffer_t *pxRingbuffer)
{
BaseType_t xFreeSize;
if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
return 0;
}{...}
if (pxRingbuffer->pucAcquire == pxRingbuffer->pucHead && pxRingbuffer->pucFree == pxRingbuffer->pucHead) {
xFreeSize = pxRingbuffer->xSize - rbHEADER_SIZE;
}{...} else if (pxRingbuffer->pucAcquire < pxRingbuffer->pucFree) {
xFreeSize = (pxRingbuffer->pucFree - pxRingbuffer->pucAcquire) - rbHEADER_SIZE;
}{...} else {
xFreeSize = (pxRingbuffer->pucFree - pxRingbuffer->pucHead) +
(pxRingbuffer->pucTail - pxRingbuffer->pucAcquire) -
(rbHEADER_SIZE * 2);
}{...}
if (xFreeSize < 0) {
xFreeSize = 0;
}{...} else if (xFreeSize > pxRingbuffer->xMaxItemSize) {
xFreeSize = pxRingbuffer->xMaxItemSize;
}{...}
return xFreeSize;
}{ ... }
static size_t prvGetCurMaxSizeByteBuf(Ringbuffer_t *pxRingbuffer)
{
BaseType_t xFreeSize;
if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
return 0;
}{...}
/* ... */
xFreeSize = pxRingbuffer->pucFree - pxRingbuffer->pucAcquire;
if (xFreeSize <= 0) {
xFreeSize += pxRingbuffer->xSize;
}{...}
return xFreeSize;
}{ ... }
static BaseType_t prvSendAcquireGeneric(Ringbuffer_t *pxRingbuffer,
const void *pvItem,
void **ppvItem,
size_t xItemSize,
TickType_t xTicksToWait)
{
BaseType_t xReturn = pdFALSE;
BaseType_t xExitLoop = pdFALSE;
BaseType_t xEntryTimeSet = pdFALSE;
BaseType_t xNotifyQueueSet = pdFALSE;
TimeOut_t xTimeOut;
while (xExitLoop == pdFALSE) {
portENTER_CRITICAL(&pxRingbuffer->mux);
if (pxRingbuffer->xCheckItemFits(pxRingbuffer, xItemSize) == pdTRUE) {
if (ppvItem) {
*ppvItem = prvAcquireItemNoSplit(pxRingbuffer, xItemSize);
}{...} else {
pxRingbuffer->vCopyItem(pxRingbuffer, pvItem, xItemSize);
if (pxRingbuffer->xQueueSet) {
xNotifyQueueSet = pdTRUE;
}{...} else {
if (listLIST_IS_EMPTY(&pxRingbuffer->xTasksWaitingToReceive) == pdFALSE) {
if (xTaskRemoveFromEventList(&pxRingbuffer->xTasksWaitingToReceive) == pdTRUE) {
portYIELD_WITHIN_API();
}{...}
}{...}
}{...}
}{...}
xReturn = pdTRUE;
xExitLoop = pdTRUE;
goto loop_end;
}{...} else if (xTicksToWait == (TickType_t) 0) {
xExitLoop = pdTRUE;
goto loop_end;
}{...} else if (xEntryTimeSet == pdFALSE) {
vTaskInternalSetTimeOutState(&xTimeOut);
xEntryTimeSet = pdTRUE;
}{...}
if (xTaskCheckForTimeOut(&xTimeOut, &xTicksToWait) == pdFALSE) {
vTaskPlaceOnEventList(&pxRingbuffer->xTasksWaitingToSend, xTicksToWait);
portYIELD_WITHIN_API();
}{...} else {
xExitLoop = pdTRUE;
}{...}
loop_end:
portEXIT_CRITICAL(&pxRingbuffer->mux);
}{...}
if (xNotifyQueueSet == pdTRUE) {
xQueueSend((QueueHandle_t)pxRingbuffer->xQueueSet, (QueueSetMemberHandle_t *)&pxRingbuffer, 0);
}{...}
return xReturn;
}{ ... }
static BaseType_t prvReceiveGeneric(Ringbuffer_t *pxRingbuffer,
void **pvItem1,
void **pvItem2,
size_t *xItemSize1,
size_t *xItemSize2,
size_t xMaxSize,
TickType_t xTicksToWait)
{
BaseType_t xReturn = pdFALSE;
BaseType_t xExitLoop = pdFALSE;
BaseType_t xEntryTimeSet = pdFALSE;
TimeOut_t xTimeOut;
ESP_STATIC_ANALYZER_CHECK(!pvItem1 || !xItemSize1, pdFALSE);
while (xExitLoop == pdFALSE) {
portENTER_CRITICAL(&pxRingbuffer->mux);
if (prvCheckItemAvail(pxRingbuffer) == pdTRUE) {
BaseType_t xIsSplit = pdFALSE;
if (pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) {
*pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, NULL, xMaxSize, xItemSize1);
}{...} else {
*pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize1);
}{...}
if (pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG) {
ESP_STATIC_ANALYZER_CHECK(!pvItem2 || !xItemSize2, pdFALSE);
if (xIsSplit == pdTRUE) {
*pvItem2 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize2);
configASSERT(*pvItem2 < *pvItem1);
configASSERT(xIsSplit == pdFALSE);
}{...} else {
*pvItem2 = NULL;
}{...}
}{...}
xReturn = pdTRUE;
xExitLoop = pdTRUE;
goto loop_end;
}{...} else if (xTicksToWait == (TickType_t) 0) {
xExitLoop = pdTRUE;
goto loop_end;
}{...} else if (xEntryTimeSet == pdFALSE) {
vTaskInternalSetTimeOutState(&xTimeOut);
xEntryTimeSet = pdTRUE;
}{...}
if (xTaskCheckForTimeOut(&xTimeOut, &xTicksToWait) == pdFALSE) {
vTaskPlaceOnEventList(&pxRingbuffer->xTasksWaitingToReceive, xTicksToWait);
portYIELD_WITHIN_API();
}{...} else {
xExitLoop = pdTRUE;
}{...}
loop_end:
portEXIT_CRITICAL(&pxRingbuffer->mux);
}{...}
return xReturn;
}{ ... }
static BaseType_t prvReceiveGenericFromISR(Ringbuffer_t *pxRingbuffer,
void **pvItem1,
void **pvItem2,
size_t *xItemSize1,
size_t *xItemSize2,
size_t xMaxSize)
{
BaseType_t xReturn = pdFALSE;
ESP_STATIC_ANALYZER_CHECK(!pvItem1 || !xItemSize1, pdFALSE);
portENTER_CRITICAL_ISR(&pxRingbuffer->mux);
if (prvCheckItemAvail(pxRingbuffer) == pdTRUE) {
BaseType_t xIsSplit = pdFALSE;
if (pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) {
*pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, NULL, xMaxSize, xItemSize1);
}{...} else {
*pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize1);
}{...}
if (pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG) {
ESP_STATIC_ANALYZER_CHECK(!pvItem2 || !xItemSize2, pdFALSE);
if (xIsSplit == pdTRUE) {
*pvItem2 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize2);
configASSERT(*pvItem2 < *pvItem1);
configASSERT(xIsSplit == pdFALSE);
}{...} else {
*pvItem2 = NULL;
}{...}
}{...}
xReturn = pdTRUE;
}{...} else {
xReturn = pdFALSE;
}{...}
portEXIT_CRITICAL_ISR(&pxRingbuffer->mux);
return xReturn;
}{ ... }
Static Functions
RingbufHandle_t xRingbufferCreate(size_t xBufferSize, RingbufferType_t xBufferType)
{
configASSERT(xBufferSize > 0);
configASSERT(xBufferType < RINGBUF_TYPE_MAX);
if (xBufferType != RINGBUF_TYPE_BYTEBUF) {
xBufferSize = rbALIGN_SIZE(xBufferSize);
}{...}
Ringbuffer_t *pxNewRingbuffer = calloc(1, sizeof(Ringbuffer_t));
uint8_t *pucRingbufferStorage = malloc(xBufferSize);
if (pxNewRingbuffer == NULL || pucRingbufferStorage == NULL) {
goto err;
}{...}
prvInitializeNewRingbuffer(xBufferSize, xBufferType, pxNewRingbuffer, pucRingbufferStorage);
return (RingbufHandle_t)pxNewRingbuffer;
err:
free(pxNewRingbuffer);
free(pucRingbufferStorage);
return NULL;
}{ ... }
RingbufHandle_t xRingbufferCreateNoSplit(size_t xItemSize, size_t xItemNum)
{
return xRingbufferCreate((rbALIGN_SIZE(xItemSize) + rbHEADER_SIZE) * xItemNum, RINGBUF_TYPE_NOSPLIT);
}{ ... }
RingbufHandle_t xRingbufferCreateStatic(size_t xBufferSize,
RingbufferType_t xBufferType,
uint8_t *pucRingbufferStorage,
StaticRingbuffer_t *pxStaticRingbuffer)
{
configASSERT(xBufferSize > 0);
configASSERT(xBufferType < RINGBUF_TYPE_MAX);
configASSERT(pucRingbufferStorage != NULL && pxStaticRingbuffer != NULL);
if (xBufferType != RINGBUF_TYPE_BYTEBUF) {
configASSERT(rbCHECK_ALIGNED(xBufferSize));
}{...}
Ringbuffer_t *pxNewRingbuffer = (Ringbuffer_t *)pxStaticRingbuffer;
prvInitializeNewRingbuffer(xBufferSize, xBufferType, pxNewRingbuffer, pucRingbufferStorage);
pxNewRingbuffer->uxRingbufferFlags |= rbBUFFER_STATIC_FLAG;
return (RingbufHandle_t)pxNewRingbuffer;
}{ ... }
BaseType_t xRingbufferSendAcquire(RingbufHandle_t xRingbuffer, void **ppvItem, size_t xItemSize, TickType_t xTicksToWait)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
configASSERT(ppvItem != NULL);
configASSERT((pxRingbuffer->uxRingbufferFlags & (rbBYTE_BUFFER_FLAG | rbALLOW_SPLIT_FLAG)) == 0);
*ppvItem = NULL;
if (xItemSize > pxRingbuffer->xMaxItemSize) {
return pdFALSE;
}{...}
return prvSendAcquireGeneric(pxRingbuffer, NULL, ppvItem, xItemSize, xTicksToWait);
}{ ... }
BaseType_t xRingbufferSendComplete(RingbufHandle_t xRingbuffer, void *pvItem)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
BaseType_t xNotifyQueueSet = pdFALSE;
configASSERT(pxRingbuffer);
configASSERT(pvItem != NULL);
configASSERT((pxRingbuffer->uxRingbufferFlags & (rbBYTE_BUFFER_FLAG | rbALLOW_SPLIT_FLAG)) == 0);
portENTER_CRITICAL(&pxRingbuffer->mux);
prvSendItemDoneNoSplit(pxRingbuffer, pvItem);
if (pxRingbuffer->xQueueSet) {
xNotifyQueueSet = pdTRUE;
}{...} else {
if (listLIST_IS_EMPTY(&pxRingbuffer->xTasksWaitingToReceive) == pdFALSE) {
if (xTaskRemoveFromEventList(&pxRingbuffer->xTasksWaitingToReceive) == pdTRUE) {
portYIELD_WITHIN_API();
}{...}
}{...}
}{...}
portEXIT_CRITICAL(&pxRingbuffer->mux);
if (xNotifyQueueSet == pdTRUE) {
xQueueSend((QueueHandle_t)pxRingbuffer->xQueueSet, (QueueSetMemberHandle_t *)&pxRingbuffer, 0);
}{...}
return pdTRUE;
}{ ... }
BaseType_t xRingbufferSend(RingbufHandle_t xRingbuffer,
const void *pvItem,
size_t xItemSize,
TickType_t xTicksToWait)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
configASSERT(pvItem != NULL || xItemSize == 0);
if (xItemSize > pxRingbuffer->xMaxItemSize) {
return pdFALSE;
}{...}
if ((pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) && xItemSize == 0) {
return pdTRUE;
}{...}
return prvSendAcquireGeneric(pxRingbuffer, pvItem, NULL, xItemSize, xTicksToWait);
}{ ... }
BaseType_t xRingbufferSendFromISR(RingbufHandle_t xRingbuffer,
const void *pvItem,
size_t xItemSize,
BaseType_t *pxHigherPriorityTaskWoken)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
BaseType_t xNotifyQueueSet = pdFALSE;
BaseType_t xReturn;
configASSERT(pxRingbuffer);
configASSERT(pvItem != NULL || xItemSize == 0);
if (xItemSize > pxRingbuffer->xMaxItemSize) {
return pdFALSE;
}{...}
if ((pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) && xItemSize == 0) {
return pdTRUE;
}{...}
portENTER_CRITICAL_ISR(&pxRingbuffer->mux);
if (pxRingbuffer->xCheckItemFits(xRingbuffer, xItemSize) == pdTRUE) {
pxRingbuffer->vCopyItem(xRingbuffer, pvItem, xItemSize);
if (pxRingbuffer->xQueueSet) {
xNotifyQueueSet = pdTRUE;
}{...} else {
if (listLIST_IS_EMPTY(&pxRingbuffer->xTasksWaitingToReceive) == pdFALSE) {
if (xTaskRemoveFromEventList(&pxRingbuffer->xTasksWaitingToReceive) == pdTRUE) {
if (pxHigherPriorityTaskWoken != NULL) {
*pxHigherPriorityTaskWoken = pdTRUE;
}{...}
}{...}
}{...}
}{...}
xReturn = pdTRUE;
}{...} else {
xReturn = pdFALSE;
}{...}
portEXIT_CRITICAL_ISR(&pxRingbuffer->mux);
if (xNotifyQueueSet == pdTRUE) {
xQueueSendFromISR((QueueHandle_t)pxRingbuffer->xQueueSet, (QueueSetMemberHandle_t *)&pxRingbuffer, pxHigherPriorityTaskWoken);
}{...}
return xReturn;
}{ ... }
void *xRingbufferReceive(RingbufHandle_t xRingbuffer, size_t *pxItemSize, TickType_t xTicksToWait)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer && pxItemSize);
void *pvTempItem;
if (prvReceiveGeneric(pxRingbuffer, &pvTempItem, NULL, pxItemSize, NULL, 0, xTicksToWait) == pdTRUE) {
return pvTempItem;
}{...} else {
return NULL;
}{...}
}{ ... }
void *xRingbufferReceiveFromISR(RingbufHandle_t xRingbuffer, size_t *pxItemSize)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer && pxItemSize);
void *pvTempItem;
if (prvReceiveGenericFromISR(pxRingbuffer, &pvTempItem, NULL, pxItemSize, NULL, 0) == pdTRUE) {
return pvTempItem;
}{...} else {
return NULL;
}{...}
}{ ... }
BaseType_t xRingbufferReceiveSplit(RingbufHandle_t xRingbuffer,
void **ppvHeadItem,
void **ppvTailItem,
size_t *pxHeadItemSize,
size_t *pxTailItemSize,
TickType_t xTicksToWait)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer && ppvHeadItem && ppvTailItem && pxHeadItemSize && pxTailItemSize);
configASSERT(pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG);
return prvReceiveGeneric(pxRingbuffer, ppvHeadItem, ppvTailItem, pxHeadItemSize, pxTailItemSize, 0, xTicksToWait);
}{ ... }
BaseType_t xRingbufferReceiveSplitFromISR(RingbufHandle_t xRingbuffer,
void **ppvHeadItem,
void **ppvTailItem,
size_t *pxHeadItemSize,
size_t *pxTailItemSize)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer && ppvHeadItem && ppvTailItem && pxHeadItemSize && pxTailItemSize);
configASSERT(pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG);
return prvReceiveGenericFromISR(pxRingbuffer, ppvHeadItem, ppvTailItem, pxHeadItemSize, pxTailItemSize, 0);
}{ ... }
void *xRingbufferReceiveUpTo(RingbufHandle_t xRingbuffer,
size_t *pxItemSize,
TickType_t xTicksToWait,
size_t xMaxSize)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer && pxItemSize);
configASSERT(pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG);
if (xMaxSize == 0) {
return NULL;
}{...}
void *pvTempItem;
if (prvReceiveGeneric(pxRingbuffer, &pvTempItem, NULL, pxItemSize, NULL, xMaxSize, xTicksToWait) == pdTRUE) {
return pvTempItem;
}{...} else {
return NULL;
}{...}
}{ ... }
void *xRingbufferReceiveUpToFromISR(RingbufHandle_t xRingbuffer, size_t *pxItemSize, size_t xMaxSize)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer && pxItemSize);
configASSERT(pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG);
if (xMaxSize == 0) {
return NULL;
}{...}
void *pvTempItem;
if (prvReceiveGenericFromISR(pxRingbuffer, &pvTempItem, NULL, pxItemSize, NULL, xMaxSize) == pdTRUE) {
return pvTempItem;
}{...} else {
return NULL;
}{...}
}{ ... }
void vRingbufferReturnItem(RingbufHandle_t xRingbuffer, void *pvItem)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
configASSERT(pvItem != NULL);
portENTER_CRITICAL(&pxRingbuffer->mux);
pxRingbuffer->vReturnItem(pxRingbuffer, (uint8_t *)pvItem);
if (listLIST_IS_EMPTY(&pxRingbuffer->xTasksWaitingToSend) == pdFALSE) {
if (xTaskRemoveFromEventList(&pxRingbuffer->xTasksWaitingToSend) == pdTRUE) {
portYIELD_WITHIN_API();
}{...}
}{...}
portEXIT_CRITICAL(&pxRingbuffer->mux);
}{ ... }
void vRingbufferReturnItemFromISR(RingbufHandle_t xRingbuffer, void *pvItem, BaseType_t *pxHigherPriorityTaskWoken)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
configASSERT(pvItem != NULL);
portENTER_CRITICAL_ISR(&pxRingbuffer->mux);
pxRingbuffer->vReturnItem(pxRingbuffer, (uint8_t *)pvItem);
if (listLIST_IS_EMPTY(&pxRingbuffer->xTasksWaitingToSend) == pdFALSE) {
if (xTaskRemoveFromEventList(&pxRingbuffer->xTasksWaitingToSend) == pdTRUE) {
if (pxHigherPriorityTaskWoken != NULL) {
*pxHigherPriorityTaskWoken = pdTRUE;
}{...}
}{...}
}{...}
portEXIT_CRITICAL_ISR(&pxRingbuffer->mux);
}{ ... }
void vRingbufferDelete(RingbufHandle_t xRingbuffer)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
if (!(pxRingbuffer->uxRingbufferFlags & rbBUFFER_STATIC_FLAG)) {
free(pxRingbuffer->pucHead);
free(pxRingbuffer);
}{...}
}{ ... }
size_t xRingbufferGetMaxItemSize(RingbufHandle_t xRingbuffer)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
return pxRingbuffer->xMaxItemSize;
}{ ... }
size_t xRingbufferGetCurFreeSize(RingbufHandle_t xRingbuffer)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
size_t xFreeSize;
portENTER_CRITICAL(&pxRingbuffer->mux);
xFreeSize = pxRingbuffer->xGetCurMaxSize(pxRingbuffer);
portEXIT_CRITICAL(&pxRingbuffer->mux);
return xFreeSize;
}{ ... }
BaseType_t xRingbufferAddToQueueSetRead(RingbufHandle_t xRingbuffer, QueueSetHandle_t xQueueSet)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
BaseType_t xReturn;
configASSERT(pxRingbuffer && xQueueSet);
portENTER_CRITICAL(&pxRingbuffer->mux);
if (pxRingbuffer->xQueueSet != NULL || prvCheckItemAvail(pxRingbuffer) == pdTRUE) {
/* ... */
xReturn = pdFALSE;
}{...} else {
pxRingbuffer->xQueueSet = xQueueSet;
xReturn = pdTRUE;
}{...}
portEXIT_CRITICAL(&pxRingbuffer->mux);
return xReturn;
}{ ... }
BaseType_t xRingbufferRemoveFromQueueSetRead(RingbufHandle_t xRingbuffer, QueueSetHandle_t xQueueSet)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
BaseType_t xReturn;
configASSERT(pxRingbuffer && xQueueSet);
portENTER_CRITICAL(&pxRingbuffer->mux);
if (pxRingbuffer->xQueueSet != xQueueSet || prvCheckItemAvail(pxRingbuffer) == pdTRUE) {
/* ... */
xReturn = pdFALSE;
}{...} else {
pxRingbuffer->xQueueSet = NULL;
xReturn = pdTRUE;
}{...}
portEXIT_CRITICAL(&pxRingbuffer->mux);
return xReturn;
}{ ... }
void vRingbufferGetInfo(RingbufHandle_t xRingbuffer,
UBaseType_t *uxFree,
UBaseType_t *uxRead,
UBaseType_t *uxWrite,
UBaseType_t *uxAcquire,
UBaseType_t *uxItemsWaiting)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
portENTER_CRITICAL(&pxRingbuffer->mux);
if (uxFree != NULL) {
*uxFree = (UBaseType_t)(pxRingbuffer->pucFree - pxRingbuffer->pucHead);
}{...}
if (uxRead != NULL) {
*uxRead = (UBaseType_t)(pxRingbuffer->pucRead - pxRingbuffer->pucHead);
}{...}
if (uxWrite != NULL) {
*uxWrite = (UBaseType_t)(pxRingbuffer->pucWrite - pxRingbuffer->pucHead);
}{...}
if (uxAcquire != NULL) {
*uxAcquire = (UBaseType_t)(pxRingbuffer->pucAcquire - pxRingbuffer->pucHead);
}{...}
if (uxItemsWaiting != NULL) {
*uxItemsWaiting = (UBaseType_t)(pxRingbuffer->xItemsWaiting);
}{...}
portEXIT_CRITICAL(&pxRingbuffer->mux);
}{ ... }
void xRingbufferPrintInfo(RingbufHandle_t xRingbuffer)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
configASSERT(pxRingbuffer);
printf("Rb size:%" PRId32 "\tfree: %" PRId32 "\trptr: %" PRId32 "\tfreeptr: %" PRId32 "\twptr: %" PRId32 ", aptr: %" PRId32 "\n",
(int32_t)pxRingbuffer->xSize, (int32_t)prvGetFreeSize(pxRingbuffer),
(int32_t)(pxRingbuffer->pucRead - pxRingbuffer->pucHead),
(int32_t)(pxRingbuffer->pucFree - pxRingbuffer->pucHead),
(int32_t)(pxRingbuffer->pucWrite - pxRingbuffer->pucHead),
(int32_t)(pxRingbuffer->pucAcquire - pxRingbuffer->pucHead));
}{ ... }
BaseType_t xRingbufferGetStaticBuffer(RingbufHandle_t xRingbuffer, uint8_t **ppucRingbufferStorage, StaticRingbuffer_t **ppxStaticRingbuffer)
{
Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
BaseType_t xReturn;
configASSERT(pxRingbuffer && ppucRingbufferStorage && ppxStaticRingbuffer);
if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_STATIC_FLAG) {
*ppucRingbufferStorage = pxRingbuffer->pucHead;
*ppxStaticRingbuffer = (StaticRingbuffer_t *)pxRingbuffer;
xReturn = pdTRUE;
}{...} else {
xReturn = pdFALSE;
}{...}
return xReturn;
}{ ... }
RingbufHandle_t xRingbufferCreateWithCaps(size_t xBufferSize, RingbufferType_t xBufferType, UBaseType_t uxMemoryCaps)
{
RingbufHandle_t xRingbuffer;
StaticRingbuffer_t *pxStaticRingbuffer;
uint8_t *pucRingbufferStorage;
if (xBufferType != RINGBUF_TYPE_BYTEBUF) {
xBufferSize = rbALIGN_SIZE(xBufferSize);
}{...}
pxStaticRingbuffer = heap_caps_malloc(sizeof(StaticRingbuffer_t), (uint32_t)uxMemoryCaps);
pucRingbufferStorage = heap_caps_malloc(xBufferSize, (uint32_t)uxMemoryCaps);
if (pxStaticRingbuffer == NULL || pucRingbufferStorage == NULL) {
goto err;
}{...}
xRingbuffer = xRingbufferCreateStatic(xBufferSize, xBufferType, pucRingbufferStorage, pxStaticRingbuffer);
if (xRingbuffer == NULL) {
goto err;
}{...}
return xRingbuffer;
err:
heap_caps_free(pxStaticRingbuffer);
heap_caps_free(pucRingbufferStorage);
return NULL;
}{ ... }
void vRingbufferDeleteWithCaps(RingbufHandle_t xRingbuffer)
{
BaseType_t __attribute__((unused)) xResult;
StaticRingbuffer_t *pxStaticRingbuffer = NULL;
uint8_t *pucRingbufferStorage = NULL;
xResult = xRingbufferGetStaticBuffer(xRingbuffer, &pucRingbufferStorage, &pxStaticRingbuffer);
configASSERT(xResult == pdTRUE);
vRingbufferDelete(xRingbuffer);
heap_caps_free(pxStaticRingbuffer);
heap_caps_free(pucRingbufferStorage);
}{ ... }