Skip to content

Commit c84de8f

Browse files
committed
minor clean up
1 parent 9e551c9 commit c84de8f

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

src/common/tusb_fifo.c

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
9797
// TODO generalize with configurable 1 byte or 4 byte each read
9898
static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t len)
9999
{
100-
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
100+
volatile const uint32_t * reg_rx = (volatile const uint32_t *) app_buf;
101101

102102
// Reading full available 32 bit words from const app address
103103
uint16_t full_words = len >> 2;
104104
while(full_words--)
105105
{
106-
tu_unaligned_write32(ff_buf, *rx_fifo);
106+
tu_unaligned_write32(ff_buf, *reg_rx);
107107
ff_buf += 4;
108108
}
109109

110110
// Read the remaining 1-3 bytes from const app address
111111
uint8_t const bytes_rem = len & 0x03;
112112
if ( bytes_rem )
113113
{
114-
uint32_t tmp32 = *rx_fifo;
114+
uint32_t tmp32 = *reg_rx;
115115
memcpy(ff_buf, &tmp32, bytes_rem);
116116
}
117117
}
@@ -120,24 +120,24 @@ static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t
120120
// where all data is written to a constant address in full word copies
121121
static void _ff_pull_const_addr(void * app_buf, const uint8_t * ff_buf, uint16_t len)
122122
{
123-
volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
123+
volatile uint32_t * reg_tx = (volatile uint32_t *) app_buf;
124124

125-
// Pushing full available 32 bit words to const app address
125+
// Write full available 32 bit words to const address
126126
uint16_t full_words = len >> 2;
127127
while(full_words--)
128128
{
129-
*tx_fifo = tu_unaligned_read32(ff_buf);
129+
*reg_tx = tu_unaligned_read32(ff_buf);
130130
ff_buf += 4;
131131
}
132132

133-
// Write the remaining 1-3 bytes into const app address
133+
// Write the remaining 1-3 bytes into const address
134134
uint8_t const bytes_rem = len & 0x03;
135135
if ( bytes_rem )
136136
{
137137
uint32_t tmp32 = 0;
138138
memcpy(&tmp32, ff_buf, bytes_rem);
139139

140-
*tx_fifo = tmp32;
140+
*reg_tx = tmp32;
141141
}
142142
}
143143

@@ -148,21 +148,21 @@ static inline void _ff_push(tu_fifo_t* f, void const * app_buf, uint16_t rel)
148148
}
149149

150150
// send n items to fifo WITHOUT updating write pointer
151-
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)
151+
static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t wr_ptr, tu_fifo_copy_mode_t copy_mode)
152152
{
153-
uint16_t const nLin = f->depth - rel;
154-
uint16_t const nWrap = n - nLin;
153+
uint16_t const lin_count = f->depth - wr_ptr;
154+
uint16_t const wrap_count = n - lin_count;
155155

156-
uint16_t nLin_bytes = nLin * f->item_size;
157-
uint16_t nWrap_bytes = nWrap * f->item_size;
156+
uint16_t lin_bytes = lin_count * f->item_size;
157+
uint16_t wrap_bytes = wrap_count * f->item_size;
158158

159159
// current buffer of fifo
160-
uint8_t* ff_buf = f->buffer + (rel * f->item_size);
160+
uint8_t* ff_buf = f->buffer + (wr_ptr * f->item_size);
161161

162162
switch (copy_mode)
163163
{
164164
case TU_FIFO_COPY_INC:
165-
if(n <= nLin)
165+
if(n <= lin_count)
166166
{
167167
// Linear only
168168
memcpy(ff_buf, app_buf, n*f->item_size);
@@ -172,17 +172,17 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t
172172
// Wrap around
173173

174174
// Write data to linear part of buffer
175-
memcpy(ff_buf, app_buf, nLin_bytes);
175+
memcpy(ff_buf, app_buf, lin_bytes);
176176

177177
// Write data wrapped around
178178
// TU_ASSERT(nWrap_bytes <= f->depth, );
179-
memcpy(f->buffer, ((uint8_t const*) app_buf) + nLin_bytes, nWrap_bytes);
179+
memcpy(f->buffer, ((uint8_t const*) app_buf) + lin_bytes, wrap_bytes);
180180
}
181181
break;
182182

183183
case TU_FIFO_COPY_CST_FULL_WORDS:
184184
// Intended for hardware buffers from which it can be read word by word only
185-
if(n <= nLin)
185+
if(n <= lin_count)
186186
{
187187
// Linear only
188188
_ff_push_const_addr(ff_buf, app_buf, n*f->item_size);
@@ -192,17 +192,18 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t
192192
// Wrap around case
193193

194194
// Write full words to linear part of buffer
195-
uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
195+
uint16_t nLin_4n_bytes = lin_bytes & 0xFFFC;
196196
_ff_push_const_addr(ff_buf, app_buf, nLin_4n_bytes);
197197
ff_buf += nLin_4n_bytes;
198198

199199
// There could be odd 1-3 bytes before the wrap-around boundary
200-
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
201-
uint8_t rem = nLin_bytes & 0x03;
200+
uint8_t rem = lin_bytes & 0x03;
202201
if (rem > 0)
203202
{
204-
uint8_t remrem = (uint8_t) tu_min16(nWrap_bytes, 4-rem);
205-
nWrap_bytes -= remrem;
203+
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
204+
205+
uint8_t remrem = (uint8_t) tu_min16(wrap_bytes, 4-rem);
206+
wrap_bytes -= remrem;
206207

207208
uint32_t tmp32 = *rx_fifo;
208209
uint8_t * src_u8 = ((uint8_t *) &tmp32);
@@ -220,7 +221,7 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t
220221
}
221222

222223
// Write data wrapped part
223-
if (nWrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, nWrap_bytes);
224+
if (wrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, wrap_bytes);
224225
}
225226
break;
226227
}
@@ -233,21 +234,21 @@ static inline void _ff_pull(tu_fifo_t* f, void * app_buf, uint16_t rel)
233234
}
234235

235236
// get n items from fifo WITHOUT updating read pointer
236-
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)
237+
static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rd_ptr, tu_fifo_copy_mode_t copy_mode)
237238
{
238-
uint16_t const nLin = f->depth - rel;
239-
uint16_t const nWrap = n - nLin; // only used if wrapped
239+
uint16_t const lin_count = f->depth - rd_ptr;
240+
uint16_t const wrap_count = n - lin_count; // only used if wrapped
240241

241-
uint16_t nLin_bytes = nLin * f->item_size;
242-
uint16_t nWrap_bytes = nWrap * f->item_size;
242+
uint16_t lin_bytes = lin_count * f->item_size;
243+
uint16_t wrap_bytes = wrap_count * f->item_size;
243244

244245
// current buffer of fifo
245-
uint8_t* ff_buf = f->buffer + (rel * f->item_size);
246+
uint8_t* ff_buf = f->buffer + (rd_ptr * f->item_size);
246247

247248
switch (copy_mode)
248249
{
249250
case TU_FIFO_COPY_INC:
250-
if ( n <= nLin )
251+
if ( n <= lin_count )
251252
{
252253
// Linear only
253254
memcpy(app_buf, ff_buf, n*f->item_size);
@@ -257,15 +258,15 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
257258
// Wrap around
258259

259260
// Read data from linear part of buffer
260-
memcpy(app_buf, ff_buf, nLin_bytes);
261+
memcpy(app_buf, ff_buf, lin_bytes);
261262

262263
// Read data wrapped part
263-
memcpy((uint8_t*) app_buf + nLin_bytes, f->buffer, nWrap_bytes);
264+
memcpy((uint8_t*) app_buf + lin_bytes, f->buffer, wrap_bytes);
264265
}
265266
break;
266267

267268
case TU_FIFO_COPY_CST_FULL_WORDS:
268-
if ( n <= nLin )
269+
if ( n <= lin_count )
269270
{
270271
// Linear only
271272
_ff_pull_const_addr(app_buf, ff_buf, n*f->item_size);
@@ -275,17 +276,18 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
275276
// Wrap around case
276277

277278
// Read full words from linear part of buffer
278-
uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
279-
_ff_pull_const_addr(app_buf, ff_buf, nLin_4n_bytes);
280-
ff_buf += nLin_4n_bytes;
279+
uint16_t lin_4n_bytes = lin_bytes & 0xFFFC;
280+
_ff_pull_const_addr(app_buf, ff_buf, lin_4n_bytes);
281+
ff_buf += lin_4n_bytes;
281282

282283
// There could be odd 1-3 bytes before the wrap-around boundary
283-
volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
284-
uint8_t rem = nLin_bytes & 0x03;
284+
uint8_t rem = lin_bytes & 0x03;
285285
if (rem > 0)
286286
{
287-
uint8_t remrem = (uint8_t) tu_min16(nWrap_bytes, 4-rem);
288-
nWrap_bytes -= remrem;
287+
volatile uint32_t * reg_tx = (volatile uint32_t *) app_buf;
288+
289+
uint8_t remrem = (uint8_t) tu_min16(wrap_bytes, 4-rem);
290+
wrap_bytes -= remrem;
289291

290292
uint32_t tmp32=0;
291293
uint8_t * dst_u8 = (uint8_t *)&tmp32;
@@ -297,15 +299,15 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
297299
ff_buf = f->buffer;
298300
while(remrem--) *dst_u8++ = *ff_buf++;
299301

300-
*tx_fifo = tmp32;
302+
*reg_tx = tmp32;
301303
}
302304
else
303305
{
304306
ff_buf = f->buffer; // wrap around to beginning
305307
}
306308

307309
// Read data wrapped part
308-
if (nWrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, nWrap_bytes);
310+
if (wrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, wrap_bytes);
309311
}
310312
break;
311313

@@ -413,16 +415,16 @@ static bool _tu_fifo_peek(tu_fifo_t* f, void * p_buffer, uint16_t wr_idx, uint16
413415
{
414416
uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
415417

418+
// nothing to peek
419+
if ( cnt == 0 ) return false;
420+
416421
// Check overflow and correct if required
417422
if ( cnt > f->depth )
418423
{
419424
rd_idx = _ff_correct_read_index(f, wr_idx);
420425
cnt = f->depth;
421426
}
422427

423-
// Skip beginning of buffer
424-
if ( cnt == 0 ) return false;
425-
426428
uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
427429

428430
// Peek data
@@ -437,16 +439,16 @@ static uint16_t _tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint1
437439
{
438440
uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
439441

442+
// nothing to peek
443+
if ( cnt == 0 ) return 0;
444+
440445
// Check overflow and correct if required
441446
if ( cnt > f->depth )
442447
{
443448
rd_idx = _ff_correct_read_index(f, wr_idx);
444449
cnt = f->depth;
445450
}
446451

447-
// Skip beginning of buffer
448-
if ( cnt == 0 ) return 0;
449-
450452
// Check if we can read something at and after offset - if too less is available we read what remains
451453
if ( cnt < n ) n = cnt;
452454

0 commit comments

Comments
 (0)