2020-01-14 23:30:39 -05:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
2020-09-19 11:46:43 +02:00
|
|
|
* Copyright (c) 2020 Reinhard Panhuber - rework to unmasked pointers
|
2020-01-14 23:30:39 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "osal/osal.h"
|
|
|
|
#include "tusb_fifo.h"
|
|
|
|
|
2022-12-04 13:58:47 +07:00
|
|
|
// Suppress IAR warning
|
2020-12-17 22:20:56 +01:00
|
|
|
// Warning[Pa082]: undefined behavior: the order of volatile accesses is undefined in this statement
|
|
|
|
#if defined(__ICCARM__)
|
|
|
|
#pragma diag_suppress = Pa082
|
|
|
|
#endif
|
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
#define TU_FIFO_DBG 0
|
|
|
|
|
2020-01-14 23:30:39 -05:00
|
|
|
// implement mutex lock and unlock
|
|
|
|
#if CFG_FIFO_MUTEX
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
static inline void _ff_lock(tu_fifo_mutex_t mutex)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
if (mutex) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
static inline void _ff_unlock(tu_fifo_mutex_t mutex)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
if (mutex) osal_mutex_unlock(mutex);
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
#define _ff_lock(_mutex)
|
|
|
|
#define _ff_unlock(_mutex)
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-03-02 21:41:51 +01:00
|
|
|
/** \enum tu_fifo_copy_mode_t
|
2021-05-02 15:01:28 +07:00
|
|
|
* \brief Write modes intended to allow special read and write functions to be able to
|
|
|
|
* copy data to and from USB hardware FIFOs as needed for e.g. STM32s and others
|
2021-03-02 21:41:51 +01:00
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
TU_FIFO_COPY_INC, ///< Copy from/to an increasing source/destination address - default mode
|
|
|
|
TU_FIFO_COPY_CST_FULL_WORDS, ///< Copy from/to a constant source/destination address - required for e.g. STM32 to write into USB hardware FIFO
|
2021-03-02 21:41:51 +01:00
|
|
|
} tu_fifo_copy_mode_t;
|
|
|
|
|
2020-01-14 23:30:39 -05:00
|
|
|
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
|
|
|
|
{
|
2020-09-23 20:48:03 +02:00
|
|
|
if (depth > 0x8000) return false; // Maximum depth is 2^15 items
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_wr);
|
|
|
|
_ff_lock(f->mutex_rd);
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
f->buffer = (uint8_t*) buffer;
|
|
|
|
f->depth = depth;
|
|
|
|
f->item_size = item_size;
|
|
|
|
f->overwritable = overwritable;
|
|
|
|
|
2021-05-02 15:01:28 +07:00
|
|
|
// Limit index space to 2*depth - this allows for a fast "modulo" calculation
|
|
|
|
// but limits the maximum depth to 2^16/2 = 2^15 and buffer overflows are detectable
|
|
|
|
// only if overflow happens once (important for unsupervised DMA applications)
|
2022-12-09 18:20:09 +07:00
|
|
|
//f->max_pointer_idx = (uint16_t) (2*depth - 1);
|
2022-12-12 11:54:33 +07:00
|
|
|
f->non_used_index_space = (uint16_t) (UINT16_MAX - (2*f->depth-1));
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
f->rd_idx = f->wr_idx = 0;
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_wr);
|
|
|
|
_ff_unlock(f->mutex_rd);
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Pull & Push
|
|
|
|
//--------------------------------------------------------------------+
|
2020-05-14 14:24:55 +07:00
|
|
|
|
2021-01-18 17:12:39 +01:00
|
|
|
// Intended to be used to read from hardware USB FIFO in e.g. STM32 where all data is read from a constant address
|
2022-12-04 13:58:47 +07:00
|
|
|
// Code adapted from dcd_synopsys.c
|
2021-03-13 00:22:04 +07:00
|
|
|
// TODO generalize with configurable 1 byte or 4 byte each read
|
2021-04-07 15:56:43 +07:00
|
|
|
static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t len)
|
2021-01-18 17:12:39 +01:00
|
|
|
{
|
2021-10-15 17:35:05 +07:00
|
|
|
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
|
2021-01-18 17:12:39 +01:00
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
// Reading full available 32 bit words from const app address
|
2021-01-18 17:12:39 +01:00
|
|
|
uint16_t full_words = len >> 2;
|
2021-03-25 14:28:59 +01:00
|
|
|
while(full_words--)
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
tu_unaligned_write32(ff_buf, *rx_fifo);
|
|
|
|
ff_buf += 4;
|
2021-01-18 17:12:39 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
// Read the remaining 1-3 bytes from const app address
|
|
|
|
uint8_t const bytes_rem = len & 0x03;
|
2021-04-07 12:34:00 +07:00
|
|
|
if ( bytes_rem )
|
|
|
|
{
|
|
|
|
uint32_t tmp32 = *rx_fifo;
|
2021-04-07 15:56:43 +07:00
|
|
|
memcpy(ff_buf, &tmp32, bytes_rem);
|
2021-01-18 17:12:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
// Intended to be used to write to hardware USB FIFO in e.g. STM32
|
|
|
|
// where all data is written to a constant address in full word copies
|
|
|
|
static void _ff_pull_const_addr(void * app_buf, const uint8_t * ff_buf, uint16_t len)
|
2021-01-18 17:12:39 +01:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
|
2021-03-25 14:28:59 +01:00
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
// Pushing full available 32 bit words to const app address
|
2021-03-25 14:28:59 +01:00
|
|
|
uint16_t full_words = len >> 2;
|
|
|
|
while(full_words--)
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
*tx_fifo = tu_unaligned_read32(ff_buf);
|
|
|
|
ff_buf += 4;
|
2021-01-18 17:12:39 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
// Write the remaining 1-3 bytes into const app address
|
|
|
|
uint8_t const bytes_rem = len & 0x03;
|
2021-04-07 12:34:00 +07:00
|
|
|
if ( bytes_rem )
|
2021-04-06 23:12:04 +07:00
|
|
|
{
|
|
|
|
uint32_t tmp32 = 0;
|
2021-04-07 15:56:43 +07:00
|
|
|
memcpy(&tmp32, ff_buf, bytes_rem);
|
2021-03-25 14:28:59 +01:00
|
|
|
|
2021-04-06 23:12:04 +07:00
|
|
|
*tx_fifo = tmp32;
|
2021-01-18 17:12:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
// send one item to FIFO WITHOUT updating write pointer
|
2021-04-07 15:56:43 +07:00
|
|
|
static inline void _ff_push(tu_fifo_t* f, void const * app_buf, uint16_t rel)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
memcpy(f->buffer + (rel * f->item_size), app_buf, f->item_size);
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 19:33:04 +01:00
|
|
|
// send n items to FIFO WITHOUT updating write pointer
|
2021-04-07 15:56:43 +07:00
|
|
|
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)
|
2021-01-18 17:12:39 +01:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
uint16_t const nLin = f->depth - rel;
|
2021-04-07 13:15:25 +07:00
|
|
|
uint16_t const nWrap = n - nLin;
|
|
|
|
|
|
|
|
uint16_t nLin_bytes = nLin * f->item_size;
|
|
|
|
uint16_t nWrap_bytes = nWrap * f->item_size;
|
|
|
|
|
|
|
|
// current buffer of fifo
|
2021-04-07 15:56:43 +07:00
|
|
|
uint8_t* ff_buf = f->buffer + (rel * f->item_size);
|
2021-04-07 12:52:57 +07:00
|
|
|
|
2021-03-02 21:41:51 +01:00
|
|
|
switch (copy_mode)
|
2021-01-18 17:12:39 +01:00
|
|
|
{
|
|
|
|
case TU_FIFO_COPY_INC:
|
2021-04-07 12:52:57 +07:00
|
|
|
if(n <= nLin)
|
2021-03-23 19:33:04 +01:00
|
|
|
{
|
2021-04-07 12:52:57 +07:00
|
|
|
// Linear only
|
2021-04-07 13:15:25 +07:00
|
|
|
memcpy(ff_buf, app_buf, n*f->item_size);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Wrap around
|
|
|
|
|
|
|
|
// Write data to linear part of buffer
|
2021-04-07 13:15:25 +07:00
|
|
|
memcpy(ff_buf, app_buf, nLin_bytes);
|
2021-03-23 19:33:04 +01:00
|
|
|
|
2022-12-10 00:18:11 +07:00
|
|
|
// Write data wrapped around
|
|
|
|
// TU_ASSERT(nWrap_bytes <= f->depth, );
|
2021-04-07 13:15:25 +07:00
|
|
|
memcpy(f->buffer, ((uint8_t const*) app_buf) + nLin_bytes, nWrap_bytes);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-01-18 17:12:39 +01:00
|
|
|
break;
|
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
case TU_FIFO_COPY_CST_FULL_WORDS:
|
|
|
|
// Intended for hardware buffers from which it can be read word by word only
|
|
|
|
if(n <= nLin)
|
2021-03-23 19:33:04 +01:00
|
|
|
{
|
2021-04-07 12:52:57 +07:00
|
|
|
// Linear only
|
2021-04-07 13:15:25 +07:00
|
|
|
_ff_push_const_addr(ff_buf, app_buf, n*f->item_size);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Wrap around case
|
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
// Write full words to linear part of buffer
|
|
|
|
uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
|
2021-04-07 13:15:25 +07:00
|
|
|
_ff_push_const_addr(ff_buf, app_buf, nLin_4n_bytes);
|
|
|
|
ff_buf += nLin_4n_bytes;
|
2021-03-23 19:33:04 +01:00
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
// There could be odd 1-3 bytes before the wrap-around boundary
|
2021-10-15 17:35:05 +07:00
|
|
|
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
|
2021-04-07 12:52:57 +07:00
|
|
|
uint8_t rem = nLin_bytes & 0x03;
|
2021-03-23 19:33:04 +01:00
|
|
|
if (rem > 0)
|
|
|
|
{
|
2022-06-24 19:45:49 +07:00
|
|
|
uint8_t remrem = (uint8_t) tu_min16(nWrap_bytes, 4-rem);
|
2021-04-07 12:52:57 +07:00
|
|
|
nWrap_bytes -= remrem;
|
|
|
|
|
|
|
|
uint32_t tmp32 = *rx_fifo;
|
|
|
|
uint8_t * src_u8 = ((uint8_t *) &tmp32);
|
|
|
|
|
|
|
|
// Write 1-3 bytes before wrapped boundary
|
2021-04-07 13:15:25 +07:00
|
|
|
while(rem--) *ff_buf++ = *src_u8++;
|
2021-04-07 12:52:57 +07:00
|
|
|
|
|
|
|
// Read more bytes to beginning to complete a word
|
2021-04-07 13:15:25 +07:00
|
|
|
ff_buf = f->buffer;
|
|
|
|
while(remrem--) *ff_buf++ = *src_u8++;
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-03-25 13:53:26 +01:00
|
|
|
else
|
2021-03-23 19:33:04 +01:00
|
|
|
{
|
2021-04-07 13:15:25 +07:00
|
|
|
ff_buf = f->buffer; // wrap around to beginning
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-03-25 13:53:26 +01:00
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
// Write data wrapped part
|
2021-04-07 13:15:25 +07:00
|
|
|
if (nWrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, nWrap_bytes);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-01-18 17:12:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 15:58:28 +02:00
|
|
|
// get one item from FIFO WITHOUT updating read pointer
|
2021-04-07 15:56:43 +07:00
|
|
|
static inline void _ff_pull(tu_fifo_t* f, void * app_buf, uint16_t rel)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
memcpy(app_buf, f->buffer + (rel * f->item_size), f->item_size);
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
2020-09-25 15:58:28 +02:00
|
|
|
// get n items from FIFO WITHOUT updating read pointer
|
2021-04-07 15:56:43 +07:00
|
|
|
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)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
uint16_t const nLin = f->depth - rel;
|
2021-04-06 23:39:39 +07:00
|
|
|
uint16_t const nWrap = n - nLin; // only used if wrapped
|
|
|
|
|
2021-04-07 13:15:25 +07:00
|
|
|
uint16_t nLin_bytes = nLin * f->item_size;
|
|
|
|
uint16_t nWrap_bytes = nWrap * f->item_size;
|
|
|
|
|
|
|
|
// current buffer of fifo
|
2021-04-07 15:56:43 +07:00
|
|
|
uint8_t* ff_buf = f->buffer + (rel * f->item_size);
|
2021-04-07 13:15:25 +07:00
|
|
|
|
2021-03-23 19:33:04 +01:00
|
|
|
switch (copy_mode)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2021-03-23 19:33:04 +01:00
|
|
|
case TU_FIFO_COPY_INC:
|
2021-04-06 23:39:39 +07:00
|
|
|
if ( n <= nLin )
|
2021-03-23 19:33:04 +01:00
|
|
|
{
|
2021-04-06 23:39:39 +07:00
|
|
|
// Linear only
|
2021-04-07 13:15:25 +07:00
|
|
|
memcpy(app_buf, ff_buf, n*f->item_size);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Wrap around
|
|
|
|
|
|
|
|
// Read data from linear part of buffer
|
2021-04-07 13:15:25 +07:00
|
|
|
memcpy(app_buf, ff_buf, nLin_bytes);
|
2021-03-23 19:33:04 +01:00
|
|
|
|
|
|
|
// Read data wrapped part
|
2021-04-07 13:15:25 +07:00
|
|
|
memcpy((uint8_t*) app_buf + nLin_bytes, f->buffer, nWrap_bytes);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-04-06 23:39:39 +07:00
|
|
|
break;
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-03-23 19:33:04 +01:00
|
|
|
case TU_FIFO_COPY_CST_FULL_WORDS:
|
2021-04-06 23:39:39 +07:00
|
|
|
if ( n <= nLin )
|
2021-03-23 19:33:04 +01:00
|
|
|
{
|
2021-04-07 12:52:57 +07:00
|
|
|
// Linear only
|
2021-04-07 13:15:25 +07:00
|
|
|
_ff_pull_const_addr(app_buf, ff_buf, n*f->item_size);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Wrap around case
|
2021-03-25 14:28:59 +01:00
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
// Read full words from linear part of buffer
|
2021-04-06 23:39:39 +07:00
|
|
|
uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
|
2021-04-07 13:15:25 +07:00
|
|
|
_ff_pull_const_addr(app_buf, ff_buf, nLin_4n_bytes);
|
|
|
|
ff_buf += nLin_4n_bytes;
|
2021-03-23 19:33:04 +01:00
|
|
|
|
2021-04-06 23:12:04 +07:00
|
|
|
// There could be odd 1-3 bytes before the wrap-around boundary
|
2021-04-07 13:15:25 +07:00
|
|
|
volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
|
2021-04-06 23:39:39 +07:00
|
|
|
uint8_t rem = nLin_bytes & 0x03;
|
2021-03-23 19:33:04 +01:00
|
|
|
if (rem > 0)
|
|
|
|
{
|
2022-06-24 19:45:49 +07:00
|
|
|
uint8_t remrem = (uint8_t) tu_min16(nWrap_bytes, 4-rem);
|
2021-04-06 23:39:39 +07:00
|
|
|
nWrap_bytes -= remrem;
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
uint32_t tmp32=0;
|
2021-04-07 12:52:57 +07:00
|
|
|
uint8_t * dst_u8 = (uint8_t *)&tmp32;
|
2021-04-06 23:39:39 +07:00
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
// Read 1-3 bytes before wrapped boundary
|
2021-04-07 13:15:25 +07:00
|
|
|
while(rem--) *dst_u8++ = *ff_buf++;
|
2021-04-06 23:39:39 +07:00
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
// Read more bytes from beginning to complete a word
|
2021-04-07 13:15:25 +07:00
|
|
|
ff_buf = f->buffer;
|
|
|
|
while(remrem--) *dst_u8++ = *ff_buf++;
|
2021-04-06 23:39:39 +07:00
|
|
|
|
2021-04-07 12:52:57 +07:00
|
|
|
*tx_fifo = tmp32;
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-03-25 13:53:26 +01:00
|
|
|
else
|
2021-03-23 19:33:04 +01:00
|
|
|
{
|
2021-04-07 13:15:25 +07:00
|
|
|
ff_buf = f->buffer; // wrap around to beginning
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-03-25 13:53:26 +01:00
|
|
|
|
2021-04-06 23:39:39 +07:00
|
|
|
// Read data wrapped part
|
2021-04-07 13:15:25 +07:00
|
|
|
if (nWrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, nWrap_bytes);
|
2021-03-23 19:33:04 +01:00
|
|
|
}
|
2021-04-06 23:39:39 +07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 15:56:43 +07:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Index (free-running and real buffer pointer)
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
2022-12-09 18:20:09 +07:00
|
|
|
// Advance an absolute index
|
2022-12-08 16:39:24 +07:00
|
|
|
// "absolute" index is only in the range of [0..2*depth)
|
2022-12-09 18:20:09 +07:00
|
|
|
static uint16_t advance_pointer(tu_fifo_t* f, uint16_t idx, uint16_t offset)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
|
|
|
// We limit the index space of p such that a correct wrap around happens
|
2021-05-02 15:01:28 +07:00
|
|
|
// Check for a wrap around or if we are in unused index space - This has to be checked first!!
|
|
|
|
// We are exploiting the wrap around to the correct index
|
2022-12-09 18:20:09 +07:00
|
|
|
uint16_t next_p = (uint16_t) (idx + offset);
|
|
|
|
if ( (idx > next_p) || (next_p >= 2*f->depth) )
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
next_p = (uint16_t) (next_p + f->non_used_index_space);
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
2022-12-08 16:39:24 +07:00
|
|
|
|
|
|
|
return next_p;
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2020-09-19 11:46:43 +02:00
|
|
|
// Backward an absolute pointer
|
2020-09-25 15:58:28 +02:00
|
|
|
static uint16_t backward_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
|
2020-09-19 11:46:43 +02:00
|
|
|
{
|
|
|
|
// We limit the index space of p such that a correct wrap around happens
|
2021-05-02 15:01:28 +07:00
|
|
|
// Check for a wrap around or if we are in unused index space - This has to be checked first!!
|
|
|
|
// We are exploiting the wrap around to the correct index
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t new_p = (uint16_t) (p - offset);
|
2022-12-09 18:20:09 +07:00
|
|
|
if ( (p < new_p) || (new_p >= 2*f->depth) )
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
new_p = (uint16_t) (new_p - f->non_used_index_space);
|
2020-09-19 11:46:43 +02:00
|
|
|
}
|
2022-12-08 16:39:24 +07:00
|
|
|
|
|
|
|
return new_p;
|
2020-09-19 11:46:43 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 16:55:25 +07:00
|
|
|
// index to pointer, simply an modulo with minus.
|
2022-12-08 16:39:24 +07:00
|
|
|
static inline uint16_t idx2ptr(uint16_t idx, uint16_t depth)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2022-12-16 16:55:25 +07:00
|
|
|
// Only run at most 3 times since index is limit in the range of [0..2*depth)
|
2022-12-08 16:39:24 +07:00
|
|
|
while ( idx >= depth ) idx -= depth;
|
|
|
|
return idx;
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-17 11:55:33 +01:00
|
|
|
// Works on local copies of w and r - return only the difference and as such can be used to determine an overflow
|
2022-12-08 16:39:24 +07:00
|
|
|
static inline uint16_t _tu_fifo_count(tu_fifo_t* f, uint16_t wr_idx, uint16_t rd_idx)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t cnt = (uint16_t) (wr_idx-rd_idx);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
// In case we have non-power of two depth we need a further modification
|
2022-12-08 16:39:24 +07:00
|
|
|
if (rd_idx > wr_idx)
|
|
|
|
{
|
|
|
|
// 2*f->depth - (rd_idx - wr_idx);
|
|
|
|
cnt = (uint16_t) (cnt - f->non_used_index_space);
|
|
|
|
}
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Works on local copies of w and r
|
2022-12-09 18:20:09 +07:00
|
|
|
static inline bool _tu_fifo_empty(uint16_t wr_idx, uint16_t rd_idx)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2022-12-09 18:20:09 +07:00
|
|
|
return wr_idx == rd_idx;
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Works on local copies of w and r
|
|
|
|
static inline bool _tu_fifo_full(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
|
|
|
|
{
|
2022-12-09 18:20:09 +07:00
|
|
|
return _tu_fifo_count(f, wAbs, rAbs) == f->depth;
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Works on local copies of w and r
|
2020-09-23 20:48:03 +02:00
|
|
|
// BE AWARE - THIS FUNCTION MIGHT NOT GIVE A CORRECT ANSWERE IN CASE WRITE POINTER "OVERFLOWS"
|
|
|
|
// Only one overflow is allowed for this function to work e.g. if depth = 100, you must not
|
|
|
|
// write more than 2*depth-1 items in one rush without updating write pointer. Otherwise
|
|
|
|
// write pointer wraps and you pointer states are messed up. This can only happen if you
|
|
|
|
// use DMAs, write functions do not allow such an error.
|
2022-12-09 18:20:09 +07:00
|
|
|
static inline bool _tu_fifo_overflowed(tu_fifo_t* f, uint16_t wr_idx, uint16_t rd_idx)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2022-12-09 18:20:09 +07:00
|
|
|
return (_tu_fifo_count(f, wr_idx, rd_idx) > f->depth);
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Works on local copies of w
|
|
|
|
// For more details see _tu_fifo_overflow()!
|
|
|
|
static inline void _tu_fifo_correct_read_pointer(tu_fifo_t* f, uint16_t wAbs)
|
|
|
|
{
|
2020-09-19 11:46:43 +02:00
|
|
|
f->rd_idx = backward_pointer(f, wAbs, f->depth);
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Works on local copies of w and r
|
2020-09-19 11:46:43 +02:00
|
|
|
// Must be protected by mutexes since in case of an overflow read pointer gets modified
|
2022-12-08 16:39:24 +07:00
|
|
|
static bool _tu_fifo_peek(tu_fifo_t* f, void * p_buffer, uint16_t wr_idx, uint16_t rd_idx)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t cnt = _tu_fifo_count(f, wr_idx, rd_idx);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2020-09-19 11:46:43 +02:00
|
|
|
// Check overflow and correct if required
|
|
|
|
if (cnt > f->depth)
|
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
_tu_fifo_correct_read_pointer(f, wr_idx);
|
2020-09-19 11:46:43 +02:00
|
|
|
cnt = f->depth;
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
// Skip beginning of buffer
|
2021-04-30 13:39:55 +02:00
|
|
|
if (cnt == 0) return false;
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t rd_ptr = idx2ptr(rd_idx, f->depth);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
// Peek data
|
2022-12-08 16:39:24 +07:00
|
|
|
_ff_pull(f, p_buffer, rd_ptr);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Works on local copies of w and r
|
2020-09-19 11:46:43 +02:00
|
|
|
// Must be protected by mutexes since in case of an overflow read pointer gets modified
|
2022-12-08 16:39:24 +07:00
|
|
|
static uint16_t _tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint16_t wr_idx, uint16_t rd_idx, tu_fifo_copy_mode_t copy_mode)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t cnt = _tu_fifo_count(f, wr_idx, rd_idx);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2020-09-19 11:46:43 +02:00
|
|
|
// Check overflow and correct if required
|
|
|
|
if (cnt > f->depth)
|
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
_tu_fifo_correct_read_pointer(f, wr_idx);
|
|
|
|
rd_idx = f->rd_idx;
|
2020-09-19 11:46:43 +02:00
|
|
|
cnt = f->depth;
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
2020-09-19 11:46:43 +02:00
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
// Skip beginning of buffer
|
2021-04-30 13:39:55 +02:00
|
|
|
if (cnt == 0) return 0;
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2020-09-25 15:58:28 +02:00
|
|
|
// Check if we can read something at and after offset - if too less is available we read what remains
|
2021-02-17 20:44:26 +01:00
|
|
|
if (cnt < n) n = cnt;
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t rd_ptr = idx2ptr(rd_idx, f->depth);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
// Peek data
|
2022-12-08 16:39:24 +07:00
|
|
|
_ff_pull_n(f, p_buffer, n, rd_ptr, copy_mode);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Works on local copies of w and r
|
2022-12-08 16:39:24 +07:00
|
|
|
static inline uint16_t _tu_fifo_remaining(tu_fifo_t* f, uint16_t wr_idx, uint16_t rd_idx)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t const count = _tu_fifo_count(f, wr_idx, rd_idx);
|
|
|
|
return (f->depth > count) ? (f->depth - count) : 0;
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:41:51 +01:00
|
|
|
static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu_fifo_copy_mode_t copy_mode)
|
|
|
|
{
|
|
|
|
if ( n == 0 ) return 0;
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_wr);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t wr_idx = f->wr_idx;
|
|
|
|
uint16_t rd_idx = f->rd_idx;
|
|
|
|
|
2021-03-02 21:41:51 +01:00
|
|
|
uint8_t const* buf8 = (uint8_t const*) data;
|
2022-12-09 18:20:09 +07:00
|
|
|
uint16_t overflowable_count = _tu_fifo_count(f, wr_idx, rd_idx);
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t const remain = _tu_fifo_remaining(f, wr_idx, rd_idx);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
2022-12-09 18:20:09 +07:00
|
|
|
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);
|
|
|
|
|
|
|
|
if ( !f->overwritable )
|
2021-03-02 21:41:51 +01:00
|
|
|
{
|
2022-12-09 18:20:09 +07:00
|
|
|
// limit up to full
|
|
|
|
n = tu_min16(n, remain);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In over-writable mode, fifo_write() is allowed even when fifo is full. In such case,
|
|
|
|
// oldest data in fifo i.e at read pointer data will be overwritten
|
|
|
|
// Note: we can modify read buffer contents but we must not modify the read index itself within a write function!
|
|
|
|
// Since it would end up in a race condition with read functions!
|
|
|
|
if ( n >= f->depth )
|
2022-12-08 16:39:24 +07:00
|
|
|
{
|
2022-12-09 18:20:09 +07:00
|
|
|
// Only copy last part
|
|
|
|
if ( copy_mode == TU_FIFO_COPY_INC )
|
2022-12-08 16:39:24 +07:00
|
|
|
{
|
2022-12-09 18:20:09 +07:00
|
|
|
buf8 += (n - f->depth) * f->item_size;
|
2022-12-08 16:39:24 +07:00
|
|
|
}else
|
|
|
|
{
|
2022-12-09 18:20:09 +07:00
|
|
|
// TODO should read from hw fifo to discard data, however reading an odd number could
|
|
|
|
// accidentally discard data.
|
2022-12-08 16:39:24 +07:00
|
|
|
}
|
2022-12-09 18:20:09 +07:00
|
|
|
|
|
|
|
n = f->depth;
|
|
|
|
|
|
|
|
// We start writing at the read pointer's position since we fill the whole buffer
|
|
|
|
wr_idx = rd_idx;
|
2022-12-10 00:18:11 +07:00
|
|
|
}
|
|
|
|
else if (overflowable_count + n >= 2*f->depth)
|
2022-12-09 18:20:09 +07:00
|
|
|
{
|
|
|
|
// Double overflowed
|
2022-12-16 16:55:25 +07:00
|
|
|
// Index is bigger than the allowed range [0,2*depth)
|
2022-12-09 18:20:09 +07:00
|
|
|
// re-position write index to have a full fifo after pushed
|
|
|
|
wr_idx = advance_pointer(f, rd_idx, f->depth - n);
|
|
|
|
|
|
|
|
// TODO we should also shift out n bytes from read index since we avoid changing rd index !!
|
|
|
|
// However memmove() is expensive due to actual copying + wrapping consideration.
|
|
|
|
// Also race condition could happen anyway if read() is invoke while moving result in corrupted memory
|
|
|
|
// currently deliberately not implemented --> result in incorrect data read back
|
|
|
|
}else
|
|
|
|
{
|
2022-12-16 16:55:25 +07:00
|
|
|
// normal + single overflowed:
|
|
|
|
// Index is in the range of [0,2*depth) and thus detect and recoverable. Recovering is handled in read()
|
|
|
|
// Therefore we just increase write index
|
2022-12-09 18:20:09 +07:00
|
|
|
// we will correct (re-position) read index later on in fifo_read() function
|
2022-12-08 16:39:24 +07:00
|
|
|
}
|
2021-03-02 21:41:51 +01:00
|
|
|
}
|
2022-12-08 16:39:24 +07:00
|
|
|
|
|
|
|
if (n)
|
2021-03-02 21:41:51 +01:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t wr_ptr = idx2ptr(wr_idx, f->depth);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
2022-12-09 18:20:09 +07:00
|
|
|
TU_LOG(TU_FIFO_DBG, "actual_n = %u, wr_ptr = %u", n, wr_ptr);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
// Write data
|
|
|
|
_ff_push_n(f, buf8, n, wr_ptr, copy_mode);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
2022-12-09 18:20:09 +07:00
|
|
|
// Advance index
|
2022-12-08 16:39:24 +07:00
|
|
|
f->wr_idx = advance_pointer(f, wr_idx, n);
|
|
|
|
|
|
|
|
TU_LOG(TU_FIFO_DBG, "\tnew_wr = %u\n", f->wr_idx);
|
|
|
|
}
|
2021-03-02 21:41:51 +01:00
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_wr);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo_copy_mode_t copy_mode)
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_rd);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
|
|
|
// Peek the data
|
2021-05-02 15:01:28 +07:00
|
|
|
// f->rd_idx might get modified in case of an overflow so we can not use a local variable
|
|
|
|
n = _tu_fifo_peek_n(f, buffer, n, f->wr_idx, f->rd_idx, copy_mode);
|
2021-03-02 21:41:51 +01:00
|
|
|
|
|
|
|
// Advance read pointer
|
|
|
|
f->rd_idx = advance_pointer(f, f->rd_idx, n);
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_rd);
|
2021-03-02 21:41:51 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
2020-09-14 18:24:08 +02:00
|
|
|
@brief Get number of items in FIFO.
|
|
|
|
|
|
|
|
As this function only reads the read and write pointers once, this function is
|
2021-01-17 11:55:33 +01:00
|
|
|
reentrant and thus thread and ISR save without any mutexes. In case an
|
|
|
|
overflow occurred, this function return f.depth at maximum. Overflows are
|
|
|
|
checked and corrected for in the read functions!
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
|
|
|
|
@returns Number of items in FIFO
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
uint16_t tu_fifo_count(tu_fifo_t* f)
|
|
|
|
{
|
2021-01-17 11:55:33 +01:00
|
|
|
return tu_min16(_tu_fifo_count(f, f->wr_idx, f->rd_idx), f->depth);
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Check if FIFO is empty.
|
|
|
|
|
|
|
|
As this function only reads the read and write pointers once, this function is
|
|
|
|
reentrant and thus thread and ISR save without any mutexes.
|
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
|
|
|
|
@returns Number of items in FIFO
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
bool tu_fifo_empty(tu_fifo_t* f)
|
|
|
|
{
|
|
|
|
return _tu_fifo_empty(f->wr_idx, f->rd_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Check if FIFO is full.
|
|
|
|
|
|
|
|
As this function only reads the read and write pointers once, this function is
|
|
|
|
reentrant and thus thread and ISR save without any mutexes.
|
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
|
|
|
|
@returns Number of items in FIFO
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
bool tu_fifo_full(tu_fifo_t* f)
|
|
|
|
{
|
|
|
|
return _tu_fifo_full(f, f->wr_idx, f->rd_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Get remaining space in FIFO.
|
|
|
|
|
|
|
|
As this function only reads the read and write pointers once, this function is
|
|
|
|
reentrant and thus thread and ISR save without any mutexes.
|
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
|
|
|
|
@returns Number of items in FIFO
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
uint16_t tu_fifo_remaining(tu_fifo_t* f)
|
|
|
|
{
|
|
|
|
return _tu_fifo_remaining(f, f->wr_idx, f->rd_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Check if overflow happened.
|
|
|
|
|
|
|
|
BE AWARE - THIS FUNCTION MIGHT NOT GIVE A CORRECT ANSWERE IN CASE WRITE POINTER "OVERFLOWS"
|
2020-09-25 15:58:28 +02:00
|
|
|
Only one overflow is allowed for this function to work e.g. if depth = 100, you must not
|
|
|
|
write more than 2*depth-1 items in one rush without updating write pointer. Otherwise
|
2021-01-17 11:55:33 +01:00
|
|
|
write pointer wraps and your pointer states are messed up. This can only happen if you
|
2020-09-25 15:58:28 +02:00
|
|
|
use DMAs, write functions do not allow such an error. Avoid such nasty things!
|
|
|
|
|
2020-09-19 11:46:43 +02:00
|
|
|
All reading functions (read, peek) check for overflows and correct read pointer on their own such
|
|
|
|
that latest items are read.
|
|
|
|
If required (e.g. for DMA use) you can also correct the read pointer by
|
|
|
|
tu_fifo_correct_read_pointer().
|
2020-09-14 18:24:08 +02:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
|
|
|
|
@returns True if overflow happened
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
2020-09-25 15:58:28 +02:00
|
|
|
bool tu_fifo_overflowed(tu_fifo_t* f)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2020-09-25 15:58:28 +02:00
|
|
|
return _tu_fifo_overflowed(f, f->wr_idx, f->rd_idx);
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only use in case tu_fifo_overflow() returned true!
|
|
|
|
void tu_fifo_correct_read_pointer(tu_fifo_t* f)
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_rd);
|
2020-09-14 18:24:08 +02:00
|
|
|
_tu_fifo_correct_read_pointer(f, f->wr_idx);
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_rd);
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
2020-09-14 18:24:08 +02:00
|
|
|
@brief Read one element out of the buffer.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2020-05-12 18:50:26 +02:00
|
|
|
This function will return the element located at the array index of the
|
2020-09-19 11:46:43 +02:00
|
|
|
read pointer, and then increment the read pointer index.
|
|
|
|
This function checks for an overflow and corrects read pointer if required.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] buffer
|
|
|
|
Pointer to the place holder for data read from the buffer
|
|
|
|
|
|
|
|
@returns TRUE if the queue is not empty
|
2020-09-14 18:24:08 +02:00
|
|
|
*/
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
bool tu_fifo_read(tu_fifo_t* f, void * buffer)
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_rd);
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
// Peek the data
|
2021-05-02 15:01:28 +07:00
|
|
|
// f->rd_idx might get modified in case of an overflow so we can not use a local variable
|
|
|
|
bool ret = _tu_fifo_peek(f, buffer, f->wr_idx, f->rd_idx);
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
// Advance pointer
|
2020-09-23 20:48:03 +02:00
|
|
|
f->rd_idx = advance_pointer(f, f->rd_idx, ret);
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_rd);
|
2020-09-14 18:24:08 +02:00
|
|
|
return ret;
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
2020-05-12 18:50:26 +02:00
|
|
|
@brief This function will read n elements from the array index specified by
|
2020-09-19 11:46:43 +02:00
|
|
|
the read pointer and increment the read index.
|
|
|
|
This function checks for an overflow and corrects read pointer if required.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] buffer
|
|
|
|
The pointer to data location
|
2021-01-17 11:55:33 +01:00
|
|
|
@param[in] n
|
2020-01-14 23:30:39 -05:00
|
|
|
Number of element that buffer can afford
|
|
|
|
|
|
|
|
@returns number of items read from the FIFO
|
2020-09-14 18:24:08 +02:00
|
|
|
*/
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
2021-01-17 11:55:33 +01:00
|
|
|
uint16_t tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-03-02 21:41:51 +01:00
|
|
|
return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_INC);
|
|
|
|
}
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-03-23 19:33:04 +01:00
|
|
|
uint16_t tu_fifo_read_n_const_addr_full_words(tu_fifo_t* f, void * buffer, uint16_t n)
|
2021-03-02 21:41:51 +01:00
|
|
|
{
|
2021-03-23 19:33:04 +01:00
|
|
|
return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_CST_FULL_WORDS);
|
2021-01-17 11:55:33 +01:00
|
|
|
}
|
|
|
|
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
2020-09-19 11:46:43 +02:00
|
|
|
@brief Read one item without removing it from the FIFO.
|
|
|
|
This function checks for an overflow and corrects read pointer if required.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] p_buffer
|
|
|
|
Pointer to the place holder for data read from the buffer
|
|
|
|
|
|
|
|
@returns TRUE if the queue is not empty
|
2020-09-14 18:24:08 +02:00
|
|
|
*/
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
2021-04-30 14:56:14 +02:00
|
|
|
bool tu_fifo_peek(tu_fifo_t* f, void * p_buffer)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_rd);
|
2021-04-30 14:56:14 +02:00
|
|
|
bool ret = _tu_fifo_peek(f, p_buffer, f->wr_idx, f->rd_idx);
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_rd);
|
2020-09-19 11:46:43 +02:00
|
|
|
return ret;
|
2020-09-14 18:24:08 +02:00
|
|
|
}
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Read n items without removing it from the FIFO
|
2020-09-19 11:46:43 +02:00
|
|
|
This function checks for an overflow and corrects read pointer if required.
|
2020-05-14 14:24:55 +07:00
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] p_buffer
|
|
|
|
Pointer to the place holder for data read from the buffer
|
|
|
|
@param[in] n
|
|
|
|
Number of items to peek
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
@returns Number of bytes written to p_buffer
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
2021-04-30 14:56:14 +02:00
|
|
|
uint16_t tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n)
|
2020-09-14 18:24:08 +02:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_rd);
|
2022-01-19 09:00:32 +01:00
|
|
|
uint16_t ret = _tu_fifo_peek_n(f, p_buffer, n, f->wr_idx, f->rd_idx, TU_FIFO_COPY_INC);
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_rd);
|
2020-09-19 11:46:43 +02:00
|
|
|
return ret;
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
2020-09-14 18:24:08 +02:00
|
|
|
@brief Write one element into the buffer.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
This function will write one element into the array index specified by
|
2020-09-19 11:46:43 +02:00
|
|
|
the write pointer and increment the write index.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] data
|
|
|
|
The byte to add to the FIFO
|
|
|
|
|
|
|
|
@returns TRUE if the data was written to the FIFO (overwrittable
|
|
|
|
FIFO will always return TRUE)
|
2020-09-14 18:24:08 +02:00
|
|
|
*/
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
2020-09-14 18:24:08 +02:00
|
|
|
bool tu_fifo_write(tu_fifo_t* f, const void * data)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_wr);
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2022-01-19 10:17:39 +07:00
|
|
|
bool ret;
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t const wr_idx = f->wr_idx;
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
if ( _tu_fifo_full(f, wr_idx, f->rd_idx) && !f->overwritable )
|
2022-01-19 10:17:39 +07:00
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
}else
|
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t wr_ptr = idx2ptr(wr_idx, f->depth);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2022-01-19 10:17:39 +07:00
|
|
|
// Write data
|
2022-12-08 16:39:24 +07:00
|
|
|
_ff_push(f, data, wr_ptr);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2022-01-19 10:17:39 +07:00
|
|
|
// Advance pointer
|
2022-12-08 16:39:24 +07:00
|
|
|
f->wr_idx = advance_pointer(f, wr_idx, 1);
|
2020-09-14 18:24:08 +02:00
|
|
|
|
2022-01-19 10:17:39 +07:00
|
|
|
ret = true;
|
|
|
|
}
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_wr);
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2022-01-19 10:17:39 +07:00
|
|
|
return ret;
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief This function will write n elements into the array index specified by
|
2020-09-19 11:46:43 +02:00
|
|
|
the write pointer and increment the write index.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] data
|
|
|
|
The pointer to data to add to the FIFO
|
|
|
|
@param[in] count
|
|
|
|
Number of element
|
|
|
|
@return Number of written elements
|
2020-09-14 18:24:08 +02:00
|
|
|
*/
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
2021-01-17 11:55:33 +01:00
|
|
|
uint16_t tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n)
|
2020-01-14 23:30:39 -05:00
|
|
|
{
|
2021-03-02 21:41:51 +01:00
|
|
|
return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_INC);
|
|
|
|
}
|
2020-05-14 14:24:55 +07:00
|
|
|
|
2021-03-02 21:41:51 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief This function will write n elements into the array index specified by
|
|
|
|
the write pointer and increment the write index. The source address will
|
|
|
|
not be incremented which is useful for reading from registers.
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-03-02 21:41:51 +01:00
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] data
|
|
|
|
The pointer to data to add to the FIFO
|
|
|
|
@param[in] count
|
|
|
|
Number of element
|
|
|
|
@return Number of written elements
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
2021-03-23 19:33:04 +01:00
|
|
|
uint16_t tu_fifo_write_n_const_addr_full_words(tu_fifo_t* f, const void * data, uint16_t n)
|
2021-03-02 21:41:51 +01:00
|
|
|
{
|
2021-03-23 19:33:04 +01:00
|
|
|
return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_CST_FULL_WORDS);
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
2020-09-19 11:46:43 +02:00
|
|
|
@brief Clear the fifo read and write pointers
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
2020-09-14 18:24:08 +02:00
|
|
|
*/
|
2020-01-14 23:30:39 -05:00
|
|
|
/******************************************************************************/
|
|
|
|
bool tu_fifo_clear(tu_fifo_t *f)
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_wr);
|
|
|
|
_ff_lock(f->mutex_rd);
|
|
|
|
|
2020-09-14 18:24:08 +02:00
|
|
|
f->rd_idx = f->wr_idx = 0;
|
2022-12-09 18:20:09 +07:00
|
|
|
//f->max_pointer_idx = (uint16_t) (2*f->depth-1);
|
2022-12-12 11:54:33 +07:00
|
|
|
f->non_used_index_space = (uint16_t) (UINT16_MAX - (2*f->depth-1));
|
2021-04-07 15:56:43 +07:00
|
|
|
|
|
|
|
_ff_unlock(f->mutex_wr);
|
|
|
|
_ff_unlock(f->mutex_rd);
|
2020-01-14 23:30:39 -05:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-03 17:21:32 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Change the fifo mode to overwritable or not overwritable
|
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
2020-11-18 09:42:50 +01:00
|
|
|
@param[in] overwritable
|
|
|
|
Overwritable mode the fifo is set to
|
2021-03-23 19:33:04 +01:00
|
|
|
*/
|
2020-09-03 17:21:32 +02:00
|
|
|
/******************************************************************************/
|
2020-11-23 23:40:13 +07:00
|
|
|
bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable)
|
2020-09-03 17:21:32 +02:00
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_wr);
|
|
|
|
_ff_lock(f->mutex_rd);
|
2020-09-03 17:21:32 +02:00
|
|
|
|
|
|
|
f->overwritable = overwritable;
|
|
|
|
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_wr);
|
|
|
|
_ff_unlock(f->mutex_rd);
|
2020-09-03 17:21:32 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-04 17:11:01 +01:00
|
|
|
|
2020-09-19 11:46:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Advance write pointer - intended to be used in combination with DMA.
|
|
|
|
It is possible to fill the FIFO by use of a DMA in circular mode. Within
|
|
|
|
DMA ISRs you may update the write pointer to be able to read from the FIFO.
|
|
|
|
As long as the DMA is the only process writing into the FIFO this is safe
|
|
|
|
to use.
|
|
|
|
|
2022-12-04 13:58:47 +07:00
|
|
|
USE WITH CARE - WE DO NOT CONDUCT SAFETY CHECKS HERE!
|
2020-09-19 11:46:43 +02:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] n
|
|
|
|
Number of items the write pointer moves forward
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
|
|
|
|
{
|
|
|
|
f->wr_idx = advance_pointer(f, f->wr_idx, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Advance read pointer - intended to be used in combination with DMA.
|
|
|
|
It is possible to read from the FIFO by use of a DMA in linear mode. Within
|
|
|
|
DMA ISRs you may update the read pointer to be able to again write into the
|
|
|
|
FIFO. As long as the DMA is the only process reading from the FIFO this is
|
|
|
|
safe to use.
|
|
|
|
|
2022-12-04 13:58:47 +07:00
|
|
|
USE WITH CARE - WE DO NOT CONDUCT SAFETY CHECKS HERE!
|
2020-09-19 11:46:43 +02:00
|
|
|
|
|
|
|
@param[in] f
|
|
|
|
Pointer to the FIFO buffer to manipulate
|
|
|
|
@param[in] n
|
|
|
|
Number of items the read pointer moves forward
|
|
|
|
*/
|
|
|
|
/******************************************************************************/
|
|
|
|
void tu_fifo_advance_read_pointer(tu_fifo_t *f, uint16_t n)
|
|
|
|
{
|
|
|
|
f->rd_idx = advance_pointer(f, f->rd_idx, n);
|
|
|
|
}
|
2021-02-17 20:44:26 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
2021-04-30 12:59:12 +02:00
|
|
|
@brief Get read info
|
2021-02-17 20:44:26 +01:00
|
|
|
|
|
|
|
Returns the length and pointer from which bytes can be read in a linear manner.
|
|
|
|
This is of major interest for DMA transmissions. If returned length is zero the
|
2021-04-30 15:14:27 +02:00
|
|
|
corresponding pointer is invalid.
|
|
|
|
The read pointer does NOT get advanced, use tu_fifo_advance_read_pointer() to
|
|
|
|
do so!
|
2021-02-17 20:44:26 +01:00
|
|
|
@param[in] f
|
|
|
|
Pointer to FIFO
|
2021-04-30 12:59:12 +02:00
|
|
|
@param[out] *info
|
|
|
|
Pointer to struct which holds the desired infos
|
2021-03-23 19:33:04 +01:00
|
|
|
*/
|
2021-02-17 20:44:26 +01:00
|
|
|
/******************************************************************************/
|
2021-04-30 15:08:14 +02:00
|
|
|
void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
|
2021-02-17 20:44:26 +01:00
|
|
|
{
|
|
|
|
// Operate on temporary values in case they change in between
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t wr_idx = f->wr_idx;
|
|
|
|
uint16_t rd_idx = f->rd_idx;
|
2021-02-17 20:44:26 +01:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t cnt = _tu_fifo_count(f, wr_idx, rd_idx);
|
2021-02-17 20:44:26 +01:00
|
|
|
|
2021-04-30 12:59:12 +02:00
|
|
|
// Check overflow and correct if required - may happen in case a DMA wrote too fast
|
2021-02-17 20:44:26 +01:00
|
|
|
if (cnt > f->depth)
|
|
|
|
{
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_lock(f->mutex_rd);
|
2022-12-08 16:39:24 +07:00
|
|
|
_tu_fifo_correct_read_pointer(f, wr_idx);
|
2021-04-07 15:56:43 +07:00
|
|
|
_ff_unlock(f->mutex_rd);
|
2022-12-08 16:39:24 +07:00
|
|
|
|
|
|
|
rd_idx = f->rd_idx;
|
2021-02-17 20:44:26 +01:00
|
|
|
cnt = f->depth;
|
|
|
|
}
|
|
|
|
|
2021-05-02 15:01:28 +07:00
|
|
|
// Check if fifo is empty
|
2021-04-30 12:59:12 +02:00
|
|
|
if (cnt == 0)
|
|
|
|
{
|
2021-05-02 15:01:28 +07:00
|
|
|
info->len_lin = 0;
|
2021-04-30 12:59:12 +02:00
|
|
|
info->len_wrap = 0;
|
2021-05-02 15:01:28 +07:00
|
|
|
info->ptr_lin = NULL;
|
2021-04-30 12:59:12 +02:00
|
|
|
info->ptr_wrap = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2021-02-17 20:44:26 +01:00
|
|
|
|
|
|
|
// Get relative pointers
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t wr_ptr = idx2ptr(wr_idx, f->depth);
|
|
|
|
uint16_t rd_ptr = idx2ptr(rd_idx, f->depth);
|
2021-02-17 20:44:26 +01:00
|
|
|
|
2021-04-23 10:27:48 +02:00
|
|
|
// Copy pointer to buffer to start reading from
|
2022-12-08 16:39:24 +07:00
|
|
|
info->ptr_lin = &f->buffer[rd_ptr];
|
2021-04-23 10:27:48 +02:00
|
|
|
|
2021-02-17 20:44:26 +01:00
|
|
|
// Check if there is a wrap around necessary
|
2022-12-08 16:39:24 +07:00
|
|
|
if (wr_ptr > rd_ptr) {
|
2021-04-23 10:27:48 +02:00
|
|
|
// Non wrapping case
|
2021-05-02 15:01:28 +07:00
|
|
|
info->len_lin = cnt;
|
2022-12-08 16:39:24 +07:00
|
|
|
|
2021-04-30 12:59:12 +02:00
|
|
|
info->len_wrap = 0;
|
|
|
|
info->ptr_wrap = NULL;
|
2021-02-17 20:44:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
info->len_lin = f->depth - rd_ptr; // Also the case if FIFO was full
|
|
|
|
|
2021-04-30 15:08:14 +02:00
|
|
|
info->len_wrap = cnt - info->len_lin;
|
2021-04-30 12:59:12 +02:00
|
|
|
info->ptr_wrap = f->buffer;
|
2021-02-17 20:44:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Get linear write info
|
|
|
|
|
2021-04-30 15:14:27 +02:00
|
|
|
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
|
2021-04-30 17:37:14 +02:00
|
|
|
corresponding pointer is invalid. The returned lengths summed up are the currently free space in the FIFO.
|
|
|
|
The write pointer does NOT get advanced, use tu_fifo_advance_write_pointer() to do so!
|
|
|
|
TAKE CARE TO NOT OVERFLOW THE BUFFER MORE THAN TWO TIMES THE FIFO DEPTH - IT CAN NOT RECOVERE OTHERWISE!
|
2021-02-17 20:44:26 +01:00
|
|
|
@param[in] f
|
|
|
|
Pointer to FIFO
|
2021-04-30 12:59:12 +02:00
|
|
|
@param[out] *info
|
|
|
|
Pointer to struct which holds the desired infos
|
2021-03-23 19:33:04 +01:00
|
|
|
*/
|
2021-02-17 20:44:26 +01:00
|
|
|
/******************************************************************************/
|
2021-04-30 17:37:14 +02:00
|
|
|
void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
|
2021-02-17 20:44:26 +01:00
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t wr_idx = f->wr_idx;
|
|
|
|
uint16_t rd_idx = f->rd_idx;
|
|
|
|
uint16_t remain = _tu_fifo_remaining(f, wr_idx, rd_idx);
|
2021-02-17 20:44:26 +01:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
if (remain == 0)
|
2021-04-30 13:39:55 +02:00
|
|
|
{
|
|
|
|
info->len_lin = 0;
|
|
|
|
info->len_wrap = 0;
|
|
|
|
info->ptr_lin = NULL;
|
|
|
|
info->ptr_wrap = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:44:26 +01:00
|
|
|
// Get relative pointers
|
2022-12-08 16:39:24 +07:00
|
|
|
uint16_t wr_ptr = idx2ptr(wr_idx, f->depth);
|
|
|
|
uint16_t rd_ptr = idx2ptr(rd_idx, f->depth);
|
2021-04-23 10:27:48 +02:00
|
|
|
|
|
|
|
// Copy pointer to buffer to start writing to
|
2022-12-08 16:39:24 +07:00
|
|
|
info->ptr_lin = &f->buffer[wr_ptr];
|
2021-02-17 20:44:26 +01:00
|
|
|
|
2022-12-08 16:39:24 +07:00
|
|
|
if (wr_ptr < rd_ptr)
|
2021-02-17 20:44:26 +01:00
|
|
|
{
|
2021-04-23 10:27:48 +02:00
|
|
|
// Non wrapping case
|
2022-12-08 16:39:24 +07:00
|
|
|
info->len_lin = rd_ptr-wr_ptr;
|
2021-04-30 12:59:12 +02:00
|
|
|
info->len_wrap = 0;
|
|
|
|
info->ptr_wrap = NULL;
|
2021-02-17 20:44:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-08 16:39:24 +07:00
|
|
|
info->len_lin = f->depth - wr_ptr;
|
|
|
|
info->len_wrap = remain - info->len_lin; // Remaining length - n already was limited to remain or FIFO depth
|
|
|
|
info->ptr_wrap = f->buffer; // Always start of buffer
|
2021-02-17 20:44:26 +01:00
|
|
|
}
|
|
|
|
}
|