@@ -358,33 +358,43 @@ static inline uint16_t idx2ptr(uint16_t idx, uint16_t depth)
358358 return idx ;
359359}
360360
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 )
363364{
364365 // In case we have non-power of two depth we need a further modification
365366 if (wr_idx >= rd_idx )
366367 {
367368 return (uint16_t ) (wr_idx - rd_idx );
368369 } else
369370 {
370- return (uint16_t ) (2 * f -> depth - (rd_idx - wr_idx ));
371+ return (uint16_t ) (2 * depth - (rd_idx - wr_idx ));
371372 }
372373}
373374
374- // Works on local copies of w and r
375375// BE AWARE - THIS FUNCTION MIGHT NOT GIVE A CORRECT ANSWERE IN CASE WRITE POINTER "OVERFLOWS"
376376// Only one overflow is allowed for this function to work e.g. if depth = 100, you must not
377377// write more than 2*depth-1 items in one rush without updating write pointer. Otherwise
378378// write pointer wraps and you pointer states are messed up. This can only happen if you
379379// 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 )
381389{
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 ;
383392}
384393
385394// Works on local copies of w
386395// 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 )
388398{
389399 f -> rd_idx = backward_pointer (f , wAbs , f -> depth );
390400}
@@ -393,7 +403,7 @@ static inline void _tu_fifo_correct_read_pointer(tu_fifo_t* f, uint16_t wAbs)
393403// Must be protected by mutexes since in case of an overflow read pointer gets modified
394404static bool _tu_fifo_peek (tu_fifo_t * f , void * p_buffer , uint16_t wr_idx , uint16_t rd_idx )
395405{
396- uint16_t cnt = _tu_fifo_count ( f , wr_idx , rd_idx );
406+ uint16_t cnt = _ff_count ( f -> depth , wr_idx , rd_idx );
397407
398408 // Check overflow and correct if required
399409 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
417427// Must be protected by mutexes since in case of an overflow read pointer gets modified
418428static 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 )
419429{
420- uint16_t cnt = _tu_fifo_count ( f , wr_idx , rd_idx );
430+ uint16_t cnt = _ff_count ( f -> depth , wr_idx , rd_idx );
421431
422432 // Check overflow and correct if required
423433 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
441451 return n ;
442452}
443453
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-
451454static uint16_t _tu_fifo_write_n (tu_fifo_t * f , const void * data , uint16_t n , tu_fifo_copy_mode_t copy_mode )
452455{
453456 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
460463 uint8_t const * buf8 = (uint8_t const * ) data ;
461464
462465 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 );
464467
465468 if ( !f -> overwritable )
466469 {
467470 // 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 );
469472 n = tu_min16 (n , remain );
470473 }
471474 else
@@ -493,7 +496,7 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
493496 }
494497 else
495498 {
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 );
497500 if (overflowable_count + n >= 2 * f -> depth )
498501 {
499502 // Double overflowed
@@ -550,6 +553,10 @@ static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo
550553 return n ;
551554}
552555
556+ //--------------------------------------------------------------------+
557+ // Application API
558+ //--------------------------------------------------------------------+
559+
553560/******************************************************************************/
554561/*!
555562 @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
567574/******************************************************************************/
568575uint16_t tu_fifo_count (tu_fifo_t * f )
569576{
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 );
571578}
572579
573580/******************************************************************************/
@@ -603,7 +610,7 @@ bool tu_fifo_empty(tu_fifo_t* f)
603610/******************************************************************************/
604611bool tu_fifo_full (tu_fifo_t * f )
605612{
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 ;
607614}
608615
609616/******************************************************************************/
@@ -621,7 +628,7 @@ bool tu_fifo_full(tu_fifo_t* f)
621628/******************************************************************************/
622629uint16_t tu_fifo_remaining (tu_fifo_t * f )
623630{
624- return _tu_fifo_remaining ( f , f -> wr_idx , f -> rd_idx );
631+ return _ff_remaining ( f -> depth , f -> wr_idx , f -> rd_idx );
625632}
626633
627634/******************************************************************************/
@@ -647,7 +654,7 @@ uint16_t tu_fifo_remaining(tu_fifo_t* f)
647654/******************************************************************************/
648655bool tu_fifo_overflowed (tu_fifo_t * f )
649656{
650- return _tu_fifo_overflowed ( f , f -> wr_idx , f -> rd_idx );
657+ return _ff_overflowed ( f -> depth , f -> wr_idx , f -> rd_idx );
651658}
652659
653660// 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)
949956 uint16_t wr_idx = f -> wr_idx ;
950957 uint16_t rd_idx = f -> rd_idx ;
951958
952- uint16_t cnt = _tu_fifo_count ( f , wr_idx , rd_idx );
959+ uint16_t cnt = _ff_count ( f -> depth , wr_idx , rd_idx );
953960
954961 // Check overflow and correct if required - may happen in case a DMA wrote too fast
955962 if (cnt > f -> depth )
@@ -1015,7 +1022,7 @@ void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
10151022{
10161023 uint16_t wr_idx = f -> wr_idx ;
10171024 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 );
10191026
10201027 if (remain == 0 )
10211028 {
0 commit comments