Skip to content

Commit 660343d

Browse files
committed
update fifo per PanRe review
1 parent 04a5c03 commit 660343d

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/common/tusb_fifo.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,10 @@ static uint16_t backward_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
352352
return new_p;
353353
}
354354

355-
// index to pointer, simply an modulo with minus
355+
// index to pointer, simply an modulo with minus.
356356
static inline uint16_t idx2ptr(uint16_t idx, uint16_t depth)
357357
{
358+
// Only run at most 3 times since index is limit in the range of [0..2*depth)
358359
while ( idx >= depth ) idx -= depth;
359360
return idx;
360361
}
@@ -509,6 +510,7 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
509510
else if (overflowable_count + n >= 2*f->depth)
510511
{
511512
// Double overflowed
513+
// Index is bigger than the allowed range [0,2*depth)
512514
// re-position write index to have a full fifo after pushed
513515
wr_idx = advance_pointer(f, rd_idx, f->depth - n);
514516

@@ -518,7 +520,9 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
518520
// currently deliberately not implemented --> result in incorrect data read back
519521
}else
520522
{
521-
// normal + single overflowed: just increase write index
523+
// normal + single overflowed:
524+
// Index is in the range of [0,2*depth) and thus detect and recoverable. Recovering is handled in read()
525+
// Therefore we just increase write index
522526
// we will correct (re-position) read index later on in fifo_read() function
523527
}
524528
}

src/common/tusb_fifo.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern "C" {
5454

5555
/* Write/Read index is always in the range of:
5656
* 0 .. 2*depth-1
57-
* The extra window allow us to determine the fifo state of empty or full with only 2 indexes
57+
* The extra window allow us to determine the fifo state of empty or full with only 2 indices
5858
* Following are examples with depth = 3
5959
*
6060
* - empty: W = R
@@ -86,15 +86,16 @@ extern "C" {
8686
* -------------------------
8787
* | R | 1 | 2 | 3 | W | 5 |
8888
*
89-
* - Double Overflowed: write(3), write(1), write(2)
89+
* - Double Overflowed i.e index is out of allowed range [0,2*depth) e.g:
90+
* write(3), write(1), write(2)
9091
* Continue to write after overflowed to 2nd overflowed.
9192
* We must prevent 2nd overflowed since it will cause incorrect computed of count, in above example
9293
* if not handled the fifo will be empty instead of continue-to-be full. Since we must not modify
9394
* read index in write() function, which cause race condition. We will re-position write index so that
9495
* after data is written it is a full fifo i.e W = depth - R
9596
*
9697
* re-position W = 1 before write(2)
97-
* Note: we should also move data from mem[4] to read index as well, but deliberately skipped here
98+
* Note: we should also move data from mem[3] to read index as well, but deliberately skipped here
9899
* since it is an expensive operation !!!
99100
* |
100101
* -------------------------

0 commit comments

Comments
 (0)