@@ -358,33 +358,43 @@ static inline uint16_t idx2ptr(uint16_t idx, uint16_t depth)
358
358
return idx ;
359
359
}
360
360
361
- // Works on local copies of w and r - return only the difference and as such can be used to determine an overflow
362
- static inline uint16_t _tu_fifo_count (tu_fifo_t * f , uint16_t wr_idx , uint16_t rd_idx )
361
+ // return only the index difference and as such can be used to determine an overflow i.e overflowable count
362
+ TU_ATTR_ALWAYS_INLINE static inline
363
+ uint16_t _ff_count (uint16_t depth , uint16_t wr_idx , uint16_t rd_idx )
363
364
{
364
365
// In case we have non-power of two depth we need a further modification
365
366
if (wr_idx >= rd_idx )
366
367
{
367
368
return (uint16_t ) (wr_idx - rd_idx );
368
369
} else
369
370
{
370
- return (uint16_t ) (2 * f -> depth - (rd_idx - wr_idx ));
371
+ return (uint16_t ) (2 * depth - (rd_idx - wr_idx ));
371
372
}
372
373
}
373
374
374
- // Works on local copies of w and r
375
375
// BE AWARE - THIS FUNCTION MIGHT NOT GIVE A CORRECT ANSWERE IN CASE WRITE POINTER "OVERFLOWS"
376
376
// Only one overflow is allowed for this function to work e.g. if depth = 100, you must not
377
377
// write more than 2*depth-1 items in one rush without updating write pointer. Otherwise
378
378
// write pointer wraps and you pointer states are messed up. This can only happen if you
379
379
// use DMAs, write functions do not allow such an error.
380
- static inline bool _tu_fifo_overflowed (tu_fifo_t * f , uint16_t wr_idx , uint16_t rd_idx )
380
+ TU_ATTR_ALWAYS_INLINE static inline
381
+ bool _ff_overflowed (uint16_t depth , uint16_t wr_idx , uint16_t rd_idx )
382
+ {
383
+ return _ff_count (depth , wr_idx , rd_idx ) > depth ;
384
+ }
385
+
386
+ // return remaining slot in fifo
387
+ TU_ATTR_ALWAYS_INLINE static inline
388
+ uint16_t _ff_remaining (uint16_t depth , uint16_t wr_idx , uint16_t rd_idx )
381
389
{
382
- return _tu_fifo_count (f , wr_idx , rd_idx ) > f -> depth ;
390
+ uint16_t const count = _ff_count (depth , wr_idx , rd_idx );
391
+ return (depth > count ) ? (depth - count ) : 0 ;
383
392
}
384
393
385
394
// Works on local copies of w
386
395
// For more details see _tu_fifo_overflow()!
387
- static inline void _tu_fifo_correct_read_pointer (tu_fifo_t * f , uint16_t wAbs )
396
+ TU_ATTR_ALWAYS_INLINE static inline
397
+ void _tu_fifo_correct_read_pointer (tu_fifo_t * f , uint16_t wAbs )
388
398
{
389
399
f -> rd_idx = backward_pointer (f , wAbs , f -> depth );
390
400
}
@@ -393,7 +403,7 @@ static inline void _tu_fifo_correct_read_pointer(tu_fifo_t* f, uint16_t wAbs)
393
403
// Must be protected by mutexes since in case of an overflow read pointer gets modified
394
404
static bool _tu_fifo_peek (tu_fifo_t * f , void * p_buffer , uint16_t wr_idx , uint16_t rd_idx )
395
405
{
396
- uint16_t cnt = _tu_fifo_count ( f , wr_idx , rd_idx );
406
+ uint16_t cnt = _ff_count ( f -> depth , wr_idx , rd_idx );
397
407
398
408
// Check overflow and correct if required
399
409
if (cnt > f -> depth )
@@ -417,7 +427,7 @@ static bool _tu_fifo_peek(tu_fifo_t* f, void * p_buffer, uint16_t wr_idx, uint16
417
427
// Must be protected by mutexes since in case of an overflow read pointer gets modified
418
428
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 )
419
429
{
420
- uint16_t cnt = _tu_fifo_count ( f , wr_idx , rd_idx );
430
+ uint16_t cnt = _ff_count ( f -> depth , wr_idx , rd_idx );
421
431
422
432
// Check overflow and correct if required
423
433
if (cnt > f -> depth )
@@ -441,13 +451,6 @@ static uint16_t _tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint1
441
451
return n ;
442
452
}
443
453
444
- // Works on local copies of w and r
445
- static inline uint16_t _tu_fifo_remaining (tu_fifo_t * f , uint16_t wr_idx , uint16_t rd_idx )
446
- {
447
- uint16_t const count = _tu_fifo_count (f , wr_idx , rd_idx );
448
- return (f -> depth > count ) ? (f -> depth - count ) : 0 ;
449
- }
450
-
451
454
static uint16_t _tu_fifo_write_n (tu_fifo_t * f , const void * data , uint16_t n , tu_fifo_copy_mode_t copy_mode )
452
455
{
453
456
if ( n == 0 ) return 0 ;
@@ -460,12 +463,12 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
460
463
uint8_t const * buf8 = (uint8_t const * ) data ;
461
464
462
465
TU_LOG (TU_FIFO_DBG , "rd = %3u, wr = %3u, count = %3u, remain = %3u, n = %3u: " ,
463
- rd_idx , wr_idx , _tu_fifo_count ( f , wr_idx , rd_idx ), _tu_fifo_remaining ( f , wr_idx , rd_idx ), n );
466
+ rd_idx , wr_idx , _ff_count ( f -> depth , wr_idx , rd_idx ), _ff_remaining ( f -> depth , wr_idx , rd_idx ), n );
464
467
465
468
if ( !f -> overwritable )
466
469
{
467
470
// limit up to full
468
- uint16_t const remain = _tu_fifo_remaining ( f , wr_idx , rd_idx );
471
+ uint16_t const remain = _ff_remaining ( f -> depth , wr_idx , rd_idx );
469
472
n = tu_min16 (n , remain );
470
473
}
471
474
else
@@ -493,7 +496,7 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
493
496
}
494
497
else
495
498
{
496
- uint16_t const overflowable_count = _tu_fifo_count ( f , wr_idx , rd_idx );
499
+ uint16_t const overflowable_count = _ff_count ( f -> depth , wr_idx , rd_idx );
497
500
if (overflowable_count + n >= 2 * f -> depth )
498
501
{
499
502
// Double overflowed
@@ -550,6 +553,10 @@ static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo
550
553
return n ;
551
554
}
552
555
556
+ //--------------------------------------------------------------------+
557
+ // Application API
558
+ //--------------------------------------------------------------------+
559
+
553
560
/******************************************************************************/
554
561
/*!
555
562
@brief Get number of items in FIFO.
@@ -567,7 +574,7 @@ static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo
567
574
/******************************************************************************/
568
575
uint16_t tu_fifo_count (tu_fifo_t * f )
569
576
{
570
- return tu_min16 (_tu_fifo_count ( f , f -> wr_idx , f -> rd_idx ), f -> depth );
577
+ return tu_min16 (_ff_count ( f -> depth , f -> wr_idx , f -> rd_idx ), f -> depth );
571
578
}
572
579
573
580
/******************************************************************************/
@@ -603,7 +610,7 @@ bool tu_fifo_empty(tu_fifo_t* f)
603
610
/******************************************************************************/
604
611
bool tu_fifo_full (tu_fifo_t * f )
605
612
{
606
- return _tu_fifo_count ( f , f -> wr_idx , f -> rd_idx ) >= f -> depth ;
613
+ return _ff_count ( f -> depth , f -> wr_idx , f -> rd_idx ) >= f -> depth ;
607
614
}
608
615
609
616
/******************************************************************************/
@@ -621,7 +628,7 @@ bool tu_fifo_full(tu_fifo_t* f)
621
628
/******************************************************************************/
622
629
uint16_t tu_fifo_remaining (tu_fifo_t * f )
623
630
{
624
- return _tu_fifo_remaining ( f , f -> wr_idx , f -> rd_idx );
631
+ return _ff_remaining ( f -> depth , f -> wr_idx , f -> rd_idx );
625
632
}
626
633
627
634
/******************************************************************************/
@@ -647,7 +654,7 @@ uint16_t tu_fifo_remaining(tu_fifo_t* f)
647
654
/******************************************************************************/
648
655
bool tu_fifo_overflowed (tu_fifo_t * f )
649
656
{
650
- return _tu_fifo_overflowed ( f , f -> wr_idx , f -> rd_idx );
657
+ return _ff_overflowed ( f -> depth , f -> wr_idx , f -> rd_idx );
651
658
}
652
659
653
660
// Only use in case tu_fifo_overflow() returned true!
@@ -949,7 +956,7 @@ void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
949
956
uint16_t wr_idx = f -> wr_idx ;
950
957
uint16_t rd_idx = f -> rd_idx ;
951
958
952
- uint16_t cnt = _tu_fifo_count ( f , wr_idx , rd_idx );
959
+ uint16_t cnt = _ff_count ( f -> depth , wr_idx , rd_idx );
953
960
954
961
// Check overflow and correct if required - may happen in case a DMA wrote too fast
955
962
if (cnt > f -> depth )
@@ -1015,7 +1022,7 @@ void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
1015
1022
{
1016
1023
uint16_t wr_idx = f -> wr_idx ;
1017
1024
uint16_t rd_idx = f -> rd_idx ;
1018
- uint16_t remain = _tu_fifo_remaining ( f , wr_idx , rd_idx );
1025
+ uint16_t remain = _ff_remaining ( f -> depth , wr_idx , rd_idx );
1019
1026
1020
1027
if (remain == 0 )
1021
1028
{
0 commit comments