Remove n from tu_fifo_get_write_info() and fix bug in vendor class

This commit is contained in:
Reinhard Panhuber 2021-04-30 17:37:14 +02:00
parent 6acfa14fec
commit 5add664874
4 changed files with 16 additions and 33 deletions

View File

@ -654,15 +654,17 @@ static bool audiod_decode_type_I_pcm(uint8_t rhport, audiod_function_t* audio, u
for (cnt_ff = 0; cnt_ff < n_ff_used; cnt_ff++) for (cnt_ff = 0; cnt_ff < n_ff_used; cnt_ff++)
{ {
tu_fifo_get_write_info(&audio->rx_supp_ff[cnt_ff], &info, nBytesPerFFToRead); tu_fifo_get_write_info(&audio->rx_supp_ff[cnt_ff], &info);
if (info.len_lin != 0) if (info.len_lin != 0)
{ {
info.len_lin = tu_min16(nBytesPerFFToRead, info.len_lin);
src = &audio->lin_buf_out[cnt_ff*audio->n_channels_per_ff_rx * audio->n_bytes_per_sampe_rx]; src = &audio->lin_buf_out[cnt_ff*audio->n_channels_per_ff_rx * audio->n_bytes_per_sampe_rx];
dst_end = info.ptr_lin + info.len_lin; dst_end = info.ptr_lin + info.len_lin;
src = audiod_interleaved_copy_bytes_fast_decode(nBytesToCopy, info.ptr_lin, dst_end, src, n_ff_used); src = audiod_interleaved_copy_bytes_fast_decode(nBytesToCopy, info.ptr_lin, dst_end, src, n_ff_used);
// Handle wrapped part of FIFO // Handle wrapped part of FIFO
info.len_wrap = tu_min16(nBytesPerFFToRead - info.len_lin, info.len_wrap);
if (info.len_wrap != 0) if (info.len_wrap != 0)
{ {
dst_end = info.ptr_wrap + info.len_wrap; dst_end = info.ptr_wrap + info.len_wrap;
@ -987,11 +989,9 @@ static uint16_t audiod_encode_type_I_pcm(uint8_t rhport, audiod_function_t* audi
tu_fifo_get_read_info(&audio->tx_supp_ff[cnt_ff], &info); tu_fifo_get_read_info(&audio->tx_supp_ff[cnt_ff], &info);
// Limit up to desired length
info.len_lin = tu_min16(nBytesPerFFToSend, info.len_lin);
if (info.len_lin != 0) if (info.len_lin != 0)
{ {
info.len_lin = tu_min16(nBytesPerFFToSend, info.len_lin); // Limit up to desired length
src_end = info.ptr_lin + info.len_lin; src_end = info.ptr_lin + info.len_lin;
dst = audiod_interleaved_copy_bytes_fast_encode(nBytesToCopy, info.ptr_lin, src_end, dst, n_ff_used); dst = audiod_interleaved_copy_bytes_fast_encode(nBytesToCopy, info.ptr_lin, src_end, dst, n_ff_used);

View File

@ -59,7 +59,7 @@ uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str);
static inline bool tud_vendor_mounted (void); static inline bool tud_vendor_mounted (void);
static inline uint32_t tud_vendor_available (void); static inline uint32_t tud_vendor_available (void);
static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize); static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize);
static inline bool tud_vendor_peek (int pos, uint8_t* u8); static inline bool tud_vendor_peek (uint8_t* u8);
static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize); static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize);
static inline uint32_t tud_vendor_write_str (char const* str); static inline uint32_t tud_vendor_write_str (char const* str);
static inline uint32_t tud_vendor_write_available (void); static inline uint32_t tud_vendor_write_available (void);
@ -95,9 +95,9 @@ static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize)
return tud_vendor_n_read(0, buffer, bufsize); return tud_vendor_n_read(0, buffer, bufsize);
} }
static inline bool tud_vendor_peek (int pos, uint8_t* u8) static inline bool tud_vendor_peek (uint8_t* u8)
{ {
return tud_vendor_n_peek(0, pos, u8); return tud_vendor_n_peek(0, u8);
} }
static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize) static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize)

View File

@ -950,11 +950,9 @@ void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
Returns the length and pointer to which bytes can be written into FIFO in a linear manner. Returns the length and pointer to which bytes can be written into FIFO in a linear manner.
This is of major interest for DMA transmissions not using circular mode. If a returned length is zero the This is of major interest for DMA transmissions not using circular mode. If a returned length is zero the
corresponding pointer is invalid. The returned length is limited to the number of BYTES n which the user corresponding pointer is invalid. The returned lengths summed up are the currently free space in the FIFO.
wants to write into the buffer. The write pointer does NOT get advanced, use tu_fifo_advance_write_pointer() to do so!
The write pointer does NOT get advanced, use tu_fifo_advance_write_pointer() to do so! If the length TAKE CARE TO NOT OVERFLOW THE BUFFER MORE THAN TWO TIMES THE FIFO DEPTH - IT CAN NOT RECOVERE OTHERWISE!
returned is less than n i.e. len<n, then a wrap occurs and you need to execute this function a second
time to get a pointer to the wrapped part!
@param[in] f @param[in] f
Pointer to FIFO Pointer to FIFO
@param[out] *info @param[out] *info
@ -963,12 +961,12 @@ void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
Number of ITEMS to write into buffer Number of ITEMS to write into buffer
*/ */
/******************************************************************************/ /******************************************************************************/
void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info, uint16_t n) void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
{ {
uint16_t w = f->wr_idx, r = f->rd_idx; uint16_t w = f->wr_idx, r = f->rd_idx;
uint16_t free = _tu_fifo_remaining(f, w, r); uint16_t free = _tu_fifo_remaining(f, w, r);
if (free == 0 || n > 2*f->depth) // If overwrite is allowed it must be less than or equal to 2 x buffer length, otherwise the overflow can not be resolved by the read functions if (free == 0)
{ {
info->len_lin = 0; info->len_lin = 0;
info->len_wrap = 0; info->len_wrap = 0;
@ -977,21 +975,6 @@ void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info, uint16_t
return; return;
} }
// We need n here because we must enforce the read and write pointers to be not more separated than 2*depth!
if (!f->overwritable)
{
// Not overwritable limit up to full
n = tu_min16(n, free);
}
else if (n >= f->depth)
{
n = f->depth;
// We start writing at the read pointer's position since we fill the complete
// buffer and we do not want to modify the read pointer within a write function!
// This would end up in a race condition with read functions!
w = r;
}
// Get relative pointers // Get relative pointers
w = get_relative_pointer(f, w); w = get_relative_pointer(f, w);
r = get_relative_pointer(f, r); r = get_relative_pointer(f, r);
@ -1002,14 +985,14 @@ void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info, uint16_t
if (w < r) if (w < r)
{ {
// Non wrapping case // Non wrapping case
info->len_lin = tu_min16(n, r-w); // Limit to required length info->len_lin = r-w; // Limit to required length
info->len_wrap = 0; info->len_wrap = 0;
info->ptr_wrap = NULL; info->ptr_wrap = NULL;
} }
else else
{ {
info->len_lin = tu_min16(n, f->depth - w); // Limit to required length info->len_lin = f->depth - w;
info->len_wrap = n-info->len_lin; // Remaining length - n already was limited to free or FIFO depth info->len_wrap = free - info->len_lin; // Remaining length - n already was limited to free or FIFO depth
info->ptr_wrap = f->buffer; // Always start of buffer info->ptr_wrap = f->buffer; // Always start of buffer
} }

View File

@ -142,7 +142,7 @@ void tu_fifo_advance_read_pointer (tu_fifo_t *f, uint16_t n);
// This functions deliver a pointer to start reading/writing from/to and a valid linear length along which no wrap occurs. // This functions deliver a pointer to start reading/writing from/to and a valid linear length along which no wrap occurs.
void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info); void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info);
void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info, uint16_t n); void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info);
static inline uint16_t tu_fifo_depth(tu_fifo_t* f) static inline uint16_t tu_fifo_depth(tu_fifo_t* f)
{ {