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
52
56
57
58
62
63
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
112
113
114
115
120
121
122
123
124
125
126
127
128
129
130
135
136
137
138
145
146
147
148
149
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
226
227
228
229
230
231
232
233
234
235
236
237
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
342
343
344
345
350
351
352
353
354
355
356
357
358
359
360
361
362
363
368
369
370
371
372
373
374
375
376
377
378
379
380
385
386
387
388
389
390
391
392
398
399
400
401
402
403
404
405
406
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
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
490
491
492
493
494
495
496
497
498
499
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
597
598
599
600
601
602
603
604
605
606
607
608
615
616
617
618
619
620
621
622
623
624
625
626
633
634
635
636
637
638
639
640
641
642
643
644
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
782
783
784
785
786
787
788
789
790
791
792
793
794
795
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
887
888
889
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1093
/* ... */
#include "osal/osal.h"
#include "tusb_fifo.h"
#define TU_FIFO_DBG 0
#if defined(__ICCARM__)
#pragma diag_suppress = Pa082
#endif
#if OSAL_MUTEX_REQUIRED
TU_ATTR_ALWAYS_INLINE static inline void _ff_lock(osal_mutex_t mutex)
{
if (mutex) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
}{ ... }
TU_ATTR_ALWAYS_INLINE static inline void _ff_unlock(osal_mutex_t mutex)
{
if (mutex) osal_mutex_unlock(mutex);
}{ ... }
/* ... */#else
#define _ff_lock(_mutex)
#define _ff_unlock(_mutex)
/* ... */
#endif
/* ... */
typedef enum
{
TU_FIFO_COPY_INC,
#ifdef TUP_MEM_CONST_ADDR
TU_FIFO_COPY_CST_FULL_WORDS,
#endif
...} tu_fifo_copy_mode_t;
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
{
if (depth > 0x8000) return false;
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
f->buffer = (uint8_t*) buffer;
f->depth = depth;
f->item_size = (uint16_t) (item_size & 0x7FFF);
f->overwritable = overwritable;
f->rd_idx = 0;
f->wr_idx = 0;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
return true;
}{ ... }
#ifdef TUP_MEM_CONST_ADDR
static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t len)
{
volatile const uint32_t * reg_rx = (volatile const uint32_t *) app_buf;
uint16_t full_words = len >> 2;
while(full_words--)
{
tu_unaligned_write32(ff_buf, *reg_rx);
ff_buf += 4;
}while (full_words--) { ... }
uint8_t const bytes_rem = len & 0x03;
if ( bytes_rem )
{
uint32_t tmp32 = *reg_rx;
memcpy(ff_buf, &tmp32, bytes_rem);
}if (bytes_rem) { ... }
}_ff_push_const_addr (uint8_t * ff_buf, const void * app_buf, uint16_t len) { ... }
static void _ff_pull_const_addr(void * app_buf, const uint8_t * ff_buf, uint16_t len)
{
volatile uint32_t * reg_tx = (volatile uint32_t *) app_buf;
uint16_t full_words = len >> 2;
while(full_words--)
{
*reg_tx = tu_unaligned_read32(ff_buf);
ff_buf += 4;
}while (full_words--) { ... }
uint8_t const bytes_rem = len & 0x03;
if ( bytes_rem )
{
uint32_t tmp32 = 0;
memcpy(&tmp32, ff_buf, bytes_rem);
*reg_tx = tmp32;
}if (bytes_rem) { ... }
}_ff_pull_const_addr (void * app_buf, const uint8_t * ff_buf, uint16_t len) { ... }
/* ... */#endif
static inline void _ff_push(tu_fifo_t* f, void const * app_buf, uint16_t rel)
{
memcpy(f->buffer + (rel * f->item_size), app_buf, f->item_size);
}{ ... }
static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t wr_ptr, tu_fifo_copy_mode_t copy_mode)
{
uint16_t const lin_count = f->depth - wr_ptr;
uint16_t const wrap_count = n - lin_count;
uint16_t lin_bytes = lin_count * f->item_size;
uint16_t wrap_bytes = wrap_count * f->item_size;
uint8_t* ff_buf = f->buffer + (wr_ptr * f->item_size);
switch (copy_mode)
{
case TU_FIFO_COPY_INC:
if(n <= lin_count)
{
memcpy(ff_buf, app_buf, n*f->item_size);
}if (n <= lin_count) { ... }
else
{
memcpy(ff_buf, app_buf, lin_bytes);
memcpy(f->buffer, ((uint8_t const*) app_buf) + lin_bytes, wrap_bytes);
}else { ... }
break;
#ifdef TUP_MEM_CONST_ADDRcase TU_FIFO_COPY_INC:
case TU_FIFO_COPY_CST_FULL_WORDS:
if(n <= lin_count)
{
_ff_push_const_addr(ff_buf, app_buf, n*f->item_size);
}if (n <= lin_count) { ... }
else
{
uint16_t nLin_4n_bytes = lin_bytes & 0xFFFC;
_ff_push_const_addr(ff_buf, app_buf, nLin_4n_bytes);
ff_buf += nLin_4n_bytes;
uint8_t rem = lin_bytes & 0x03;
if (rem > 0)
{
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
uint8_t remrem = (uint8_t) tu_min16(wrap_bytes, 4-rem);
wrap_bytes -= remrem;
uint32_t tmp32 = *rx_fifo;
uint8_t * src_u8 = ((uint8_t *) &tmp32);
while(rem--) *ff_buf++ = *src_u8++;
ff_buf = f->buffer;
while(remrem--) *ff_buf++ = *src_u8++;
}if (rem > 0) { ... }
else
{
ff_buf = f->buffer;
}else { ... }
if (wrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, wrap_bytes);
}else { ... }
break;/* ... */
#endifcase TU_FIFO_COPY_CST_FULL_WORDS:
default: break;
}switch (copy_mode) { ... }
}{ ... }
static inline void _ff_pull(tu_fifo_t* f, void * app_buf, uint16_t rel)
{
memcpy(app_buf, f->buffer + (rel * f->item_size), f->item_size);
}{ ... }
static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rd_ptr, tu_fifo_copy_mode_t copy_mode)
{
uint16_t const lin_count = f->depth - rd_ptr;
uint16_t const wrap_count = n - lin_count;
uint16_t lin_bytes = lin_count * f->item_size;
uint16_t wrap_bytes = wrap_count * f->item_size;
uint8_t* ff_buf = f->buffer + (rd_ptr * f->item_size);
switch (copy_mode)
{
case TU_FIFO_COPY_INC:
if ( n <= lin_count )
{
memcpy(app_buf, ff_buf, n*f->item_size);
}if (n <= lin_count) { ... }
else
{
memcpy(app_buf, ff_buf, lin_bytes);
memcpy((uint8_t*) app_buf + lin_bytes, f->buffer, wrap_bytes);
}else { ... }
break;
#ifdef TUP_MEM_CONST_ADDRcase TU_FIFO_COPY_INC:
case TU_FIFO_COPY_CST_FULL_WORDS:
if ( n <= lin_count )
{
_ff_pull_const_addr(app_buf, ff_buf, n*f->item_size);
}if (n <= lin_count) { ... }
else
{
uint16_t lin_4n_bytes = lin_bytes & 0xFFFC;
_ff_pull_const_addr(app_buf, ff_buf, lin_4n_bytes);
ff_buf += lin_4n_bytes;
uint8_t rem = lin_bytes & 0x03;
if (rem > 0)
{
volatile uint32_t * reg_tx = (volatile uint32_t *) app_buf;
uint8_t remrem = (uint8_t) tu_min16(wrap_bytes, 4-rem);
wrap_bytes -= remrem;
uint32_t tmp32=0;
uint8_t * dst_u8 = (uint8_t *)&tmp32;
while(rem--) *dst_u8++ = *ff_buf++;
ff_buf = f->buffer;
while(remrem--) *dst_u8++ = *ff_buf++;
*reg_tx = tmp32;
}if (rem > 0) { ... }
else
{
ff_buf = f->buffer;
}else { ... }
if (wrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, wrap_bytes);
}else { ... }
/* ... */#endif
break;
case TU_FIFO_COPY_CST_FULL_WORDS:
default: break;
}switch (copy_mode) { ... }
}{ ... }
TU_ATTR_ALWAYS_INLINE static inline
uint16_t _ff_count(uint16_t depth, uint16_t wr_idx, uint16_t rd_idx)
{
if (wr_idx >= rd_idx)
{
return (uint16_t) (wr_idx - rd_idx);
}if (wr_idx >= rd_idx) { ... } else
{
return (uint16_t) (2*depth - (rd_idx - wr_idx));
}else { ... }
}{ ... }
TU_ATTR_ALWAYS_INLINE static inline
uint16_t _ff_remaining(uint16_t depth, uint16_t wr_idx, uint16_t rd_idx)
{
uint16_t const count = _ff_count(depth, wr_idx, rd_idx);
return (depth > count) ? (depth - count) : 0;
}{ ... }
static uint16_t advance_index(uint16_t depth, uint16_t idx, uint16_t offset)
{
uint16_t new_idx = (uint16_t) (idx + offset);
if ( (idx > new_idx) || (new_idx >= 2*depth) )
{
uint16_t const non_used_index_space = (uint16_t) (UINT16_MAX - (2*depth-1));
new_idx = (uint16_t) (new_idx + non_used_index_space);
}if ((idx > new_idx) || (new_idx >= 2*depth)) { ... }
return new_idx;
}{ ... }
#if 0
static uint16_t backward_index(uint16_t depth, uint16_t idx, uint16_t offset)
{
uint16_t new_idx = (uint16_t) (idx - offset);
if ( (idx < new_idx) || (new_idx >= 2*depth) )
{
uint16_t const non_used_index_space = (uint16_t) (UINT16_MAX - (2*depth-1));
new_idx = (uint16_t) (new_idx - non_used_index_space);
}if ((idx < new_idx) || (new_idx >= 2*depth)) { ... }
return new_idx;
}backward_index (uint16_t depth, uint16_t idx, uint16_t offset) { ... }
/* ... */#endif
TU_ATTR_ALWAYS_INLINE static inline
uint16_t idx2ptr(uint16_t depth, uint16_t idx)
{
while ( idx >= depth ) idx -= depth;
return idx;
}{ ... }
TU_ATTR_ALWAYS_INLINE static inline
uint16_t _ff_correct_read_index(tu_fifo_t* f, uint16_t wr_idx)
{
uint16_t rd_idx;
if ( wr_idx >= f->depth )
{
rd_idx = wr_idx - f->depth;
}if (wr_idx >= f->depth) { ... }else
{
rd_idx = wr_idx + f->depth;
}
f->rd_idx = rd_idx;
return rd_idx;
}{ ... }
static bool _tu_fifo_peek(tu_fifo_t* f, void * p_buffer, uint16_t wr_idx, uint16_t rd_idx)
{
uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
if ( cnt == 0 ) return false;
if ( cnt > f->depth )
{
rd_idx = _ff_correct_read_index(f, wr_idx);
cnt = f->depth;
}if (cnt > f->depth) { ... }
uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
_ff_pull(f, p_buffer, rd_ptr);
return true;
}{ ... }
static uint16_t _tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint16_t wr_idx, uint16_t rd_idx, tu_fifo_copy_mode_t copy_mode)
{
uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
if ( cnt == 0 ) return 0;
if ( cnt > f->depth )
{
rd_idx = _ff_correct_read_index(f, wr_idx);
cnt = f->depth;
}if (cnt > f->depth) { ... }
if ( cnt < n ) n = cnt;
uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
_ff_pull_n(f, p_buffer, n, rd_ptr, copy_mode);
return n;
}{ ... }
static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu_fifo_copy_mode_t copy_mode)
{
if ( n == 0 ) return 0;
_ff_lock(f->mutex_wr);
uint16_t wr_idx = f->wr_idx;
uint16_t rd_idx = f->rd_idx;
uint8_t const* buf8 = (uint8_t const*) data;
TU_LOG(TU_FIFO_DBG, "rd = %3u, wr = %3u, count = %3u, remain = %3u, n = %3u: ",
rd_idx, wr_idx, _ff_count(f->depth, wr_idx, rd_idx), _ff_remaining(f->depth, wr_idx, rd_idx), n);
if ( !f->overwritable )
{
uint16_t const remain = _ff_remaining(f->depth, wr_idx, rd_idx);
n = tu_min16(n, remain);
}if (!f->overwritable) { ... }
else
{
if ( n >= f->depth )
{
if ( copy_mode == TU_FIFO_COPY_INC )
{
buf8 += (n - f->depth) * f->item_size;
}if (copy_mode == TU_FIFO_COPY_INC) { ... }else
{
}
n = f->depth;
wr_idx = rd_idx;
}if (n >= f->depth) { ... }
else
{
uint16_t const overflowable_count = _ff_count(f->depth, wr_idx, rd_idx);
if (overflowable_count + n >= 2*f->depth)
{
wr_idx = advance_index(f->depth, rd_idx, f->depth - n);
}if (overflowable_count + n >= 2*f->depth) { ... }else
{
}
}else { ... }
}else { ... }
if (n)
{
uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
TU_LOG(TU_FIFO_DBG, "actual_n = %u, wr_ptr = %u", n, wr_ptr);
_ff_push_n(f, buf8, n, wr_ptr, copy_mode);
f->wr_idx = advance_index(f->depth, wr_idx, n);
TU_LOG(TU_FIFO_DBG, "\tnew_wr = %u\r\n", f->wr_idx);
}if (n) { ... }
_ff_unlock(f->mutex_wr);
return n;
}{ ... }
static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo_copy_mode_t copy_mode)
{
_ff_lock(f->mutex_rd);
n = _tu_fifo_peek_n(f, buffer, n, f->wr_idx, f->rd_idx, copy_mode);
f->rd_idx = advance_index(f->depth, f->rd_idx, n);
_ff_unlock(f->mutex_rd);
return n;
}{ ... }
/* ... */
...
uint16_t tu_fifo_count(tu_fifo_t* f)
{
return tu_min16(_ff_count(f->depth, f->wr_idx, f->rd_idx), f->depth);
}{ ... }
...
/* ... */
...
bool tu_fifo_empty(tu_fifo_t* f)
{
return f->wr_idx == f->rd_idx;
}{ ... }
...
/* ... */
...
bool tu_fifo_full(tu_fifo_t* f)
{
return _ff_count(f->depth, f->wr_idx, f->rd_idx) >= f->depth;
}{ ... }
...
/* ... */
...
uint16_t tu_fifo_remaining(tu_fifo_t* f)
{
return _ff_remaining(f->depth, f->wr_idx, f->rd_idx);
}{ ... }
...
/* ... */
...
bool tu_fifo_overflowed(tu_fifo_t* f)
{
return _ff_count(f->depth, f->wr_idx, f->rd_idx) > f->depth;
}{ ... }
void tu_fifo_correct_read_pointer(tu_fifo_t* f)
{
_ff_lock(f->mutex_rd);
_ff_correct_read_index(f, f->wr_idx);
_ff_unlock(f->mutex_rd);
}{ ... }
...
/* ... */
...
bool tu_fifo_read(tu_fifo_t* f, void * buffer)
{
_ff_lock(f->mutex_rd);
bool ret = _tu_fifo_peek(f, buffer, f->wr_idx, f->rd_idx);
f->rd_idx = advance_index(f->depth, f->rd_idx, ret);
_ff_unlock(f->mutex_rd);
return ret;
}{ ... }
...
/* ... */
...
uint16_t tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n)
{
return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_INC);
}{ ... }
#ifdef TUP_MEM_CONST_ADDR...
/* ... */
...
uint16_t tu_fifo_read_n_const_addr_full_words(tu_fifo_t* f, void * buffer, uint16_t n)
{
return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_CST_FULL_WORDS);
}tu_fifo_read_n_const_addr_full_words (tu_fifo_t* f, void * buffer, uint16_t n) { ... }
/* ... */#endif
/* ... */
...
bool tu_fifo_peek(tu_fifo_t* f, void * p_buffer)
{
_ff_lock(f->mutex_rd);
bool ret = _tu_fifo_peek(f, p_buffer, f->wr_idx, f->rd_idx);
_ff_unlock(f->mutex_rd);
return ret;
}{ ... }
...
/* ... */
...
uint16_t tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n)
{
_ff_lock(f->mutex_rd);
uint16_t ret = _tu_fifo_peek_n(f, p_buffer, n, f->wr_idx, f->rd_idx, TU_FIFO_COPY_INC);
_ff_unlock(f->mutex_rd);
return ret;
}{ ... }
...
/* ... */
...
bool tu_fifo_write(tu_fifo_t* f, const void * data)
{
_ff_lock(f->mutex_wr);
bool ret;
uint16_t const wr_idx = f->wr_idx;
if ( tu_fifo_full(f) && !f->overwritable )
{
ret = false;
}if (tu_fifo_full(f) && !f->overwritable) { ... }else
{
uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
_ff_push(f, data, wr_ptr);
f->wr_idx = advance_index(f->depth, wr_idx, 1);
ret = true;
}
_ff_unlock(f->mutex_wr);
return ret;
}{ ... }
...
/* ... */
...
uint16_t tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n)
{
return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_INC);
}{ ... }
#ifdef TUP_MEM_CONST_ADDR...
/* ... */
...
uint16_t tu_fifo_write_n_const_addr_full_words(tu_fifo_t* f, const void * data, uint16_t n)
{
return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_CST_FULL_WORDS);
}tu_fifo_write_n_const_addr_full_words (tu_fifo_t* f, const void * data, uint16_t n) { ... }
/* ... */#endif
/* ... */
...
bool tu_fifo_clear(tu_fifo_t *f)
{
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
f->rd_idx = 0;
f->wr_idx = 0;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
return true;
}{ ... }
...
/* ... */
...
bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable)
{
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
f->overwritable = overwritable;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
return true;
}{ ... }
...
/* ... */
...
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
{
f->wr_idx = advance_index(f->depth, f->wr_idx, n);
}{ ... }
...
/* ... */
...
void tu_fifo_advance_read_pointer(tu_fifo_t *f, uint16_t n)
{
f->rd_idx = advance_index(f->depth, f->rd_idx, n);
}{ ... }
...
/* ... */
...
void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
{
uint16_t wr_idx = f->wr_idx;
uint16_t rd_idx = f->rd_idx;
uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
if (cnt > f->depth)
{
_ff_lock(f->mutex_rd);
rd_idx = _ff_correct_read_index(f, wr_idx);
_ff_unlock(f->mutex_rd);
cnt = f->depth;
}if (cnt > f->depth) { ... }
if (cnt == 0)
{
info->len_lin = 0;
info->len_wrap = 0;
info->ptr_lin = NULL;
info->ptr_wrap = NULL;
return;
}if (cnt == 0) { ... }
uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
info->ptr_lin = &f->buffer[rd_ptr];
if (wr_ptr > rd_ptr)
{
info->len_lin = cnt;
info->len_wrap = 0;
info->ptr_wrap = NULL;
}if (wr_ptr > rd_ptr) { ... }
else
{
info->len_lin = f->depth - rd_ptr;
info->len_wrap = cnt - info->len_lin;
info->ptr_wrap = f->buffer;
}else { ... }
}{ ... }
...
/* ... */
...
void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
{
uint16_t wr_idx = f->wr_idx;
uint16_t rd_idx = f->rd_idx;
uint16_t remain = _ff_remaining(f->depth, wr_idx, rd_idx);
if (remain == 0)
{
info->len_lin = 0;
info->len_wrap = 0;
info->ptr_lin = NULL;
info->ptr_wrap = NULL;
return;
}if (remain == 0) { ... }
uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
info->ptr_lin = &f->buffer[wr_ptr];
if (wr_ptr < rd_ptr)
{
info->len_lin = rd_ptr-wr_ptr;
info->len_wrap = 0;
info->ptr_wrap = NULL;
}if (wr_ptr < rd_ptr) { ... }
else
{
info->len_lin = f->depth - wr_ptr;
info->len_wrap = remain - info->len_lin;
info->ptr_wrap = f->buffer;
}else { ... }
}{ ... }