Skip to content

Commit 8245751

Browse files
committed
minor clean up
1 parent 2a1b81e commit 8245751

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/common/tusb_fifo.c

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@
2828
#include "osal/osal.h"
2929
#include "tusb_fifo.h"
3030

31+
#define TU_FIFO_DBG 0
32+
3133
// Suppress IAR warning
3234
// Warning[Pa082]: undefined behavior: the order of volatile accesses is undefined in this statement
3335
#if defined(__ICCARM__)
3436
#pragma diag_suppress = Pa082
3537
#endif
3638

37-
#define TU_FIFO_DBG 0
38-
39-
// implement mutex lock and unlock
40-
#if CFG_FIFO_MUTEX
39+
#if OSAL_MUTEX_REQUIRED
4140

4241
static inline void _ff_lock(osal_mutex_t mutex)
4342
{
@@ -80,8 +79,8 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
8079
f->depth = depth;
8180
f->item_size = (uint16_t) (item_size & 0x7FFF);
8281
f->overwritable = overwritable;
83-
84-
f->rd_idx = f->wr_idx = 0;
82+
f->rd_idx = 0;
83+
f->wr_idx = 0;
8584

8685
_ff_unlock(f->mutex_wr);
8786
_ff_unlock(f->mutex_rd);
@@ -142,13 +141,13 @@ static void _ff_pull_const_addr(void * app_buf, const uint8_t * ff_buf, uint16_t
142141
}
143142
}
144143

145-
// send one item to FIFO WITHOUT updating write pointer
144+
// send one item to fifo WITHOUT updating write pointer
146145
static inline void _ff_push(tu_fifo_t* f, void const * app_buf, uint16_t rel)
147146
{
148147
memcpy(f->buffer + (rel * f->item_size), app_buf, f->item_size);
149148
}
150149

151-
// send n items to FIFO WITHOUT updating write pointer
150+
// send n items to fifo WITHOUT updating write pointer
152151
static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t rel, tu_fifo_copy_mode_t copy_mode)
153152
{
154153
uint16_t const nLin = f->depth - rel;
@@ -227,13 +226,13 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t
227226
}
228227
}
229228

230-
// get one item from FIFO WITHOUT updating read pointer
229+
// get one item from fifo WITHOUT updating read pointer
231230
static inline void _ff_pull(tu_fifo_t* f, void * app_buf, uint16_t rel)
232231
{
233232
memcpy(app_buf, f->buffer + (rel * f->item_size), f->item_size);
234233
}
235234

236-
// get n items from FIFO WITHOUT updating read pointer
235+
// get n items from fifo WITHOUT updating read pointer
237236
static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu_fifo_copy_mode_t copy_mode)
238237
{
239238
uint16_t const nLin = f->depth - rel;
@@ -475,14 +474,14 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
475474
uint16_t rd_idx = f->rd_idx;
476475

477476
uint8_t const* buf8 = (uint8_t const*) data;
478-
uint16_t overflowable_count = _tu_fifo_count(f, wr_idx, rd_idx);
479-
uint16_t const remain = _tu_fifo_remaining(f, wr_idx, rd_idx);
480477

481-
TU_LOG(TU_FIFO_DBG, "rd = %3u, wr = %3u, count = %3u, remain = %3u, n = %3u: ", rd_idx, wr_idx, _tu_fifo_count(f, wr_idx, rd_idx), remain, n);
478+
TU_LOG(TU_FIFO_DBG, "rd = %3u, wr = %3u, count = %3u, remain = %3u, n = %3u: ",
479+
rd_idx, wr_idx, _tu_fifo_count(f, wr_idx, rd_idx), _tu_fifo_remaining(f, wr_idx, rd_idx), n);
482480

483481
if ( !f->overwritable )
484482
{
485483
// limit up to full
484+
uint16_t const remain = _tu_fifo_remaining(f, wr_idx, rd_idx);
486485
n = tu_min16(n, remain);
487486
}
488487
else
@@ -508,23 +507,27 @@ static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu
508507
// We start writing at the read pointer's position since we fill the whole buffer
509508
wr_idx = rd_idx;
510509
}
511-
else if (overflowable_count + n >= 2*f->depth)
512-
{
513-
// Double overflowed
514-
// Index is bigger than the allowed range [0,2*depth)
515-
// re-position write index to have a full fifo after pushed
516-
wr_idx = advance_pointer(f, rd_idx, f->depth - n);
517-
518-
// TODO we should also shift out n bytes from read index since we avoid changing rd index !!
519-
// However memmove() is expensive due to actual copying + wrapping consideration.
520-
// Also race condition could happen anyway if read() is invoke while moving result in corrupted memory
521-
// currently deliberately not implemented --> result in incorrect data read back
522-
}else
510+
else
523511
{
524-
// normal + single overflowed:
525-
// Index is in the range of [0,2*depth) and thus detect and recoverable. Recovering is handled in read()
526-
// Therefore we just increase write index
527-
// we will correct (re-position) read index later on in fifo_read() function
512+
uint16_t const overflowable_count = _tu_fifo_count(f, wr_idx, rd_idx);
513+
if (overflowable_count + n >= 2*f->depth)
514+
{
515+
// Double overflowed
516+
// Index is bigger than the allowed range [0,2*depth)
517+
// re-position write index to have a full fifo after pushed
518+
wr_idx = advance_pointer(f, rd_idx, f->depth - n);
519+
520+
// TODO we should also shift out n bytes from read index since we avoid changing rd index !!
521+
// However memmove() is expensive due to actual copying + wrapping consideration.
522+
// Also race condition could happen anyway if read() is invoke while moving result in corrupted memory
523+
// currently deliberately not implemented --> result in incorrect data read back
524+
}else
525+
{
526+
// normal + single overflowed:
527+
// Index is in the range of [0,2*depth) and thus detect and recoverable. Recovering is handled in read()
528+
// Therefore we just increase write index
529+
// we will correct (re-position) read index later on in fifo_read() function
530+
}
528531
}
529532
}
530533

@@ -868,7 +871,8 @@ bool tu_fifo_clear(tu_fifo_t *f)
868871
_ff_lock(f->mutex_wr);
869872
_ff_lock(f->mutex_rd);
870873

871-
f->rd_idx = f->wr_idx = 0;
874+
f->rd_idx = 0;
875+
f->wr_idx = 0;
872876

873877
_ff_unlock(f->mutex_wr);
874878
_ff_unlock(f->mutex_rd);
@@ -1031,9 +1035,9 @@ void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
10311035

10321036
if (remain == 0)
10331037
{
1034-
info->len_lin = 0;
1038+
info->len_lin = 0;
10351039
info->len_wrap = 0;
1036-
info->ptr_lin = NULL;
1040+
info->ptr_lin = NULL;
10371041
info->ptr_wrap = NULL;
10381042
return;
10391043
}

0 commit comments

Comments
 (0)