@@ -68,22 +68,19 @@ typedef enum
68
68
69
69
bool tu_fifo_config (tu_fifo_t * f , void * buffer , uint16_t depth , uint16_t item_size , bool overwritable )
70
70
{
71
- if (depth > 0x8000 ) return false; // Maximum depth is 2^15 items
71
+ // Limit index space to 2*depth - this allows for a fast "modulo" calculation
72
+ // but limits the maximum depth to 2^16/2 = 2^15 and buffer overflows are detectable
73
+ // only if overflow happens once (important for unsupervised DMA applications)
74
+ if (depth > 0x8000 ) return false;
72
75
73
76
_ff_lock (f -> mutex_wr );
74
77
_ff_lock (f -> mutex_rd );
75
78
76
- f -> buffer = (uint8_t * ) buffer ;
77
- f -> depth = depth ;
78
- f -> item_size = item_size ;
79
+ f -> buffer = (uint8_t * ) buffer ;
80
+ f -> depth = depth ;
81
+ f -> item_size = ( uint16_t ) ( item_size & 0x7FFF ) ;
79
82
f -> overwritable = overwritable ;
80
83
81
- // Limit index space to 2*depth - this allows for a fast "modulo" calculation
82
- // but limits the maximum depth to 2^16/2 = 2^15 and buffer overflows are detectable
83
- // only if overflow happens once (important for unsupervised DMA applications)
84
- //f->max_pointer_idx = (uint16_t) (2*depth - 1);
85
- f -> non_used_index_space = (uint16_t ) (UINT16_MAX - (2 * f -> depth - 1 ));
86
-
87
84
f -> rd_idx = f -> wr_idx = 0 ;
88
85
89
86
_ff_unlock (f -> mutex_wr );
@@ -331,7 +328,8 @@ static uint16_t advance_pointer(tu_fifo_t* f, uint16_t idx, uint16_t offset)
331
328
uint16_t next_p = (uint16_t ) (idx + offset );
332
329
if ( (idx > next_p ) || (next_p >= 2 * f -> depth ) )
333
330
{
334
- next_p = (uint16_t ) (next_p + f -> non_used_index_space );
331
+ uint16_t const non_used_index_space = (uint16_t ) (UINT16_MAX - (2 * f -> depth - 1 ));
332
+ next_p = (uint16_t ) (next_p + non_used_index_space );
335
333
}
336
334
337
335
return next_p ;
@@ -346,7 +344,8 @@ static uint16_t backward_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
346
344
uint16_t new_p = (uint16_t ) (p - offset );
347
345
if ( (p < new_p ) || (new_p >= 2 * f -> depth ) )
348
346
{
349
- new_p = (uint16_t ) (new_p - f -> non_used_index_space );
347
+ uint16_t const non_used_index_space = (uint16_t ) (UINT16_MAX - (2 * f -> depth - 1 ));
348
+ new_p = (uint16_t ) (new_p - non_used_index_space );
350
349
}
351
350
352
351
return new_p ;
@@ -363,13 +362,15 @@ static inline uint16_t idx2ptr(uint16_t idx, uint16_t depth)
363
362
// Works on local copies of w and r - return only the difference and as such can be used to determine an overflow
364
363
static inline uint16_t _tu_fifo_count (tu_fifo_t * f , uint16_t wr_idx , uint16_t rd_idx )
365
364
{
366
- uint16_t cnt = ( uint16_t ) ( wr_idx - rd_idx ) ;
365
+ uint16_t cnt ;
367
366
368
367
// In case we have non-power of two depth we need a further modification
369
- if (rd_idx > wr_idx )
368
+ if (wr_idx >= rd_idx )
369
+ {
370
+ cnt = (uint16_t ) (wr_idx - rd_idx );
371
+ } else
370
372
{
371
- // 2*f->depth - (rd_idx - wr_idx);
372
- cnt = (uint16_t ) (cnt - f -> non_used_index_space );
373
+ cnt = (uint16_t ) (2 * f -> depth - (rd_idx - wr_idx ));
373
374
}
374
375
375
376
return cnt ;
@@ -395,7 +396,7 @@ static inline bool _tu_fifo_full(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
395
396
// use DMAs, write functions do not allow such an error.
396
397
static inline bool _tu_fifo_overflowed (tu_fifo_t * f , uint16_t wr_idx , uint16_t rd_idx )
397
398
{
398
- return ( _tu_fifo_count (f , wr_idx , rd_idx ) > f -> depth ) ;
399
+ return _tu_fifo_count (f , wr_idx , rd_idx ) > f -> depth ;
399
400
}
400
401
401
402
// Works on local copies of w
@@ -868,8 +869,6 @@ bool tu_fifo_clear(tu_fifo_t *f)
868
869
_ff_lock (f -> mutex_rd );
869
870
870
871
f -> rd_idx = f -> wr_idx = 0 ;
871
- //f->max_pointer_idx = (uint16_t) (2*f->depth-1);
872
- f -> non_used_index_space = (uint16_t ) (UINT16_MAX - (2 * f -> depth - 1 ));
873
872
874
873
_ff_unlock (f -> mutex_wr );
875
874
_ff_unlock (f -> mutex_rd );
0 commit comments