Skip to content

Commit 2a1b81e

Browse files
committed
minimize tu_fifo size to 16
- remove non_used_index_space - packed overwritable with item_size
1 parent 9c73c1a commit 2a1b81e

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

src/common/tusb_fifo.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,19 @@ typedef enum
6868

6969
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
7070
{
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;
7275

7376
_ff_lock(f->mutex_wr);
7477
_ff_lock(f->mutex_rd);
7578

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);
7982
f->overwritable = overwritable;
8083

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-
8784
f->rd_idx = f->wr_idx = 0;
8885

8986
_ff_unlock(f->mutex_wr);
@@ -331,7 +328,8 @@ static uint16_t advance_pointer(tu_fifo_t* f, uint16_t idx, uint16_t offset)
331328
uint16_t next_p = (uint16_t) (idx + offset);
332329
if ( (idx > next_p) || (next_p >= 2*f->depth) )
333330
{
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);
335333
}
336334

337335
return next_p;
@@ -346,7 +344,8 @@ static uint16_t backward_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
346344
uint16_t new_p = (uint16_t) (p - offset);
347345
if ( (p < new_p) || (new_p >= 2*f->depth) )
348346
{
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);
350349
}
351350

352351
return new_p;
@@ -363,13 +362,15 @@ static inline uint16_t idx2ptr(uint16_t idx, uint16_t depth)
363362
// Works on local copies of w and r - return only the difference and as such can be used to determine an overflow
364363
static inline uint16_t _tu_fifo_count(tu_fifo_t* f, uint16_t wr_idx, uint16_t rd_idx)
365364
{
366-
uint16_t cnt = (uint16_t) (wr_idx-rd_idx);
365+
uint16_t cnt;
367366

368367
// 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
370372
{
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));
373374
}
374375

375376
return cnt;
@@ -395,7 +396,7 @@ static inline bool _tu_fifo_full(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
395396
// use DMAs, write functions do not allow such an error.
396397
static inline bool _tu_fifo_overflowed(tu_fifo_t* f, uint16_t wr_idx, uint16_t rd_idx)
397398
{
398-
return (_tu_fifo_count(f, wr_idx, rd_idx) > f->depth);
399+
return _tu_fifo_count(f, wr_idx, rd_idx) > f->depth;
399400
}
400401

401402
// Works on local copies of w
@@ -868,8 +869,6 @@ bool tu_fifo_clear(tu_fifo_t *f)
868869
_ff_lock(f->mutex_rd);
869870

870871
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));
873872

874873
_ff_unlock(f->mutex_wr);
875874
_ff_unlock(f->mutex_rd);

src/common/tusb_fifo.h

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ extern "C" {
6969
* | 0 | 1 | W | 3 | 4 | R |
7070
*
7171
* - Number of items in the fifo can be determined in either cases:
72-
* - case W > R: Count = W - R
73-
* - case W < R: Count = 2*depth - (R - W)
72+
* - case W >= R: Count = W - R
73+
* - case W < R: Count = 2*depth - (R - W)
7474
*
7575
* In non-overwritable mode, computed Count (in above 2 cases) is at most equal to depth.
7676
* However, in over-writable mode, write index can be repeatedly increased and count can be
@@ -82,10 +82,10 @@ extern "C" {
8282
* -------------------------
8383
* | R | 1 | 2 | 3 | W | 5 |
8484
*
85-
* - Double Overflowed i.e index is out of allowed range [0,2*depth) e.g:
85+
* - Double Overflowed i.e index is out of allowed range [0,2*depth)
86+
* This occurs when we continue to write after 1st overflowed to 2nd overflowed. e.g:
8687
* write(3), write(1), write(2)
87-
* Continue to write after overflowed to 2nd overflowed.
88-
* We must prevent 2nd overflowed since it will cause incorrect computed of count, in above example
88+
* This must be prevented since it will cause unrecoverable state, in above example
8989
* if not handled the fifo will be empty instead of continue-to-be full. Since we must not modify
9090
* read index in write() function, which cause race condition. We will re-position write index so that
9191
* after data is written it is a full fifo i.e W = depth - R
@@ -106,17 +106,16 @@ extern "C" {
106106
*/
107107
typedef struct
108108
{
109-
uint8_t* buffer ; ///< buffer pointer
110-
uint16_t depth ; ///< max items
111-
uint16_t item_size ; ///< size of each item
109+
uint8_t* buffer ; // buffer pointer
110+
uint16_t depth ; // max items
112111

113-
bool overwritable ;
112+
struct TU_ATTR_PACKED {
113+
uint16_t item_size : 15; // size of each item
114+
bool overwritable : 1 ; // ovwerwritable when full
115+
};
114116

115-
uint16_t non_used_index_space ; ///< required for non-power-of-two buffer length
116-
//uint16_t max_pointer_idx ; ///< maximum absolute pointer index
117-
118-
volatile uint16_t wr_idx ; ///< write index
119-
volatile uint16_t rd_idx ; ///< read index
117+
volatile uint16_t wr_idx ; // write index
118+
volatile uint16_t rd_idx ; // read index
120119

121120
#if OSAL_MUTEX_REQUIRED
122121
osal_mutex_t mutex_wr;
@@ -133,13 +132,12 @@ typedef struct
133132
void * ptr_wrap ; ///< wrapped part start pointer
134133
} tu_fifo_buffer_info_t;
135134

136-
#define TU_FIFO_INIT(_buffer, _depth, _type, _overwritable) \
137-
{ \
138-
.buffer = _buffer, \
139-
.depth = _depth, \
140-
.item_size = sizeof(_type), \
141-
.overwritable = _overwritable, \
142-
.non_used_index_space = (uint16_t)(UINT16_MAX - (2*(_depth)-1)), \
135+
#define TU_FIFO_INIT(_buffer, _depth, _type, _overwritable) \
136+
{ \
137+
.buffer = _buffer, \
138+
.depth = _depth, \
139+
.item_size = sizeof(_type), \
140+
.overwritable = _overwritable, \
143141
}
144142

145143
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \

src/device/usbd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ bool tud_init (uint8_t rhport)
388388

389389
TU_LOG(USBD_DBG, "USBD init on controller %u\r\n", rhport);
390390
TU_LOG_INT(USBD_DBG, sizeof(usbd_device_t));
391+
TU_LOG_INT(USBD_DBG, sizeof(tu_fifo_t));
392+
TU_LOG_INT(USBD_DBG, sizeof(tu_edpt_stream_t));
391393

392394
tu_varclr(&_usbd_dev);
393395

0 commit comments

Comments
 (0)