#include "osal/osal.h"
#include "tusb_fifo.h"
#if defined(__ICCARM__)
#pragma diag_suppress = Pa082
#endif
#if CFG_FIFO_MUTEX
static inline void _ff_lock(tu_fifo_mutex_t mutex)
{
if (mutex) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
}
static inline void _ff_unlock(tu_fifo_mutex_t mutex)
{
if (mutex) osal_mutex_unlock(mutex);
}
#else
#define _ff_lock(_mutex)
#define _ff_unlock(_mutex)
#endif
typedef enum
{
TU_FIFO_COPY_INC, TU_FIFO_COPY_CST_FULL_WORDS, } tu_fifo_copy_mode_t;
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
{
if (depth > 0x8000) return false;
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
f->buffer = (uint8_t*) buffer;
f->depth = depth;
f->item_size = item_size;
f->overwritable = overwritable;
f->max_pointer_idx = 2*depth - 1;
f->non_used_index_space = UINT16_MAX - f->max_pointer_idx;
f->rd_idx = f->wr_idx = 0;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
return true;
}
static inline uint16_t _ff_mod(uint16_t idx, uint16_t depth)
{
while ( idx >= depth) idx -= depth;
return idx;
}
static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t len)
{
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
uint16_t full_words = len >> 2;
while(full_words--)
{
tu_unaligned_write32(ff_buf, *rx_fifo);
ff_buf += 4;
}
uint8_t const bytes_rem = len & 0x03;
if ( bytes_rem )
{
uint32_t tmp32 = *rx_fifo;
memcpy(ff_buf, &tmp32, bytes_rem);
}
}
static void _ff_pull_const_addr(void * app_buf, const uint8_t * ff_buf, uint16_t len)
{
volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
uint16_t full_words = len >> 2;
while(full_words--)
{
*tx_fifo = tu_unaligned_read32(ff_buf);
ff_buf += 4;
}
uint8_t const bytes_rem = len & 0x03;
if ( bytes_rem )
{
uint32_t tmp32 = 0;
memcpy(&tmp32, ff_buf, bytes_rem);
*tx_fifo = tmp32;
}
}
static inline void _ff_push(tu_fifo_t* f, void const * app_buf, uint16_t rel)
{
memcpy(f->buffer + (rel * f->item_size), app_buf, f->item_size);
}
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)
{
uint16_t const nLin = f->depth - rel;
uint16_t const nWrap = n - nLin;
uint16_t nLin_bytes = nLin * f->item_size;
uint16_t nWrap_bytes = nWrap * f->item_size;
uint8_t* ff_buf = f->buffer + (rel * f->item_size);
switch (copy_mode)
{
case TU_FIFO_COPY_INC:
if(n <= nLin)
{
memcpy(ff_buf, app_buf, n*f->item_size);
}
else
{
memcpy(ff_buf, app_buf, nLin_bytes);
memcpy(f->buffer, ((uint8_t const*) app_buf) + nLin_bytes, nWrap_bytes);
}
break;
case TU_FIFO_COPY_CST_FULL_WORDS:
if(n <= nLin)
{
_ff_push_const_addr(ff_buf, app_buf, n*f->item_size);
}
else
{
uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
_ff_push_const_addr(ff_buf, app_buf, nLin_4n_bytes);
ff_buf += nLin_4n_bytes;
volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
uint8_t rem = nLin_bytes & 0x03;
if (rem > 0)
{
uint8_t remrem = tu_min16(nWrap_bytes, 4-rem);
nWrap_bytes -= remrem;
uint32_t tmp32 = *rx_fifo;
uint8_t * src_u8 = ((uint8_t *) &tmp32);
while(rem--) *ff_buf++ = *src_u8++;
ff_buf = f->buffer;
while(remrem--) *ff_buf++ = *src_u8++;
}
else
{
ff_buf = f->buffer; }
if (nWrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, nWrap_bytes);
}
break;
}
}
static inline void _ff_pull(tu_fifo_t* f, void * app_buf, uint16_t rel)
{
memcpy(app_buf, f->buffer + (rel * f->item_size), f->item_size);
}
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)
{
uint16_t const nLin = f->depth - rel;
uint16_t const nWrap = n - nLin;
uint16_t nLin_bytes = nLin * f->item_size;
uint16_t nWrap_bytes = nWrap * f->item_size;
uint8_t* ff_buf = f->buffer + (rel * f->item_size);
switch (copy_mode)
{
case TU_FIFO_COPY_INC:
if ( n <= nLin )
{
memcpy(app_buf, ff_buf, n*f->item_size);
}
else
{
memcpy(app_buf, ff_buf, nLin_bytes);
memcpy((uint8_t*) app_buf + nLin_bytes, f->buffer, nWrap_bytes);
}
break;
case TU_FIFO_COPY_CST_FULL_WORDS:
if ( n <= nLin )
{
_ff_pull_const_addr(app_buf, ff_buf, n*f->item_size);
}
else
{
uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
_ff_pull_const_addr(app_buf, ff_buf, nLin_4n_bytes);
ff_buf += nLin_4n_bytes;
volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
uint8_t rem = nLin_bytes & 0x03;
if (rem > 0)
{
uint8_t remrem = tu_min16(nWrap_bytes, 4-rem);
nWrap_bytes -= remrem;
uint32_t tmp32=0;
uint8_t * dst_u8 = (uint8_t *)&tmp32;
while(rem--) *dst_u8++ = *ff_buf++;
ff_buf = f->buffer;
while(remrem--) *dst_u8++ = *ff_buf++;
*tx_fifo = tmp32;
}
else
{
ff_buf = f->buffer; }
if (nWrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, nWrap_bytes);
}
break;
default: break;
}
}
static uint16_t advance_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
{
if ((p > (uint16_t)(p + offset)) || ((uint16_t)(p + offset) > f->max_pointer_idx))
{
p = (p + offset) + f->non_used_index_space;
}
else
{
p += offset;
}
return p;
}
static uint16_t backward_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
{
if ((p < (uint16_t)(p - offset)) || ((uint16_t)(p - offset) > f->max_pointer_idx))
{
p = (p - offset) - f->non_used_index_space;
}
else
{
p -= offset;
}
return p;
}
static uint16_t get_relative_pointer(tu_fifo_t* f, uint16_t p)
{
return _ff_mod(p, f->depth);
}
static inline uint16_t _tu_fifo_count(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
uint16_t cnt = wAbs-rAbs;
if (rAbs > wAbs) cnt -= f->non_used_index_space;
return cnt;
}
static inline bool _tu_fifo_empty(uint16_t wAbs, uint16_t rAbs)
{
return wAbs == rAbs;
}
static inline bool _tu_fifo_full(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
return (_tu_fifo_count(f, wAbs, rAbs) == f->depth);
}
static inline bool _tu_fifo_overflowed(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
return (_tu_fifo_count(f, wAbs, rAbs) > f->depth);
}
static inline void _tu_fifo_correct_read_pointer(tu_fifo_t* f, uint16_t wAbs)
{
f->rd_idx = backward_pointer(f, wAbs, f->depth);
}
static bool _tu_fifo_peek(tu_fifo_t* f, void * p_buffer, uint16_t wAbs, uint16_t rAbs)
{
uint16_t cnt = _tu_fifo_count(f, wAbs, rAbs);
if (cnt > f->depth)
{
_tu_fifo_correct_read_pointer(f, wAbs);
cnt = f->depth;
}
if (cnt == 0) return false;
uint16_t rRel = get_relative_pointer(f, rAbs);
_ff_pull(f, p_buffer, rRel);
return true;
}
static uint16_t _tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint16_t wAbs, uint16_t rAbs, tu_fifo_copy_mode_t copy_mode)
{
uint16_t cnt = _tu_fifo_count(f, wAbs, rAbs);
if (cnt > f->depth)
{
_tu_fifo_correct_read_pointer(f, wAbs);
rAbs = f->rd_idx;
cnt = f->depth;
}
if (cnt == 0) return 0;
if (cnt < n) n = cnt;
uint16_t rRel = get_relative_pointer(f, rAbs);
_ff_pull_n(f, p_buffer, n, rRel, copy_mode);
return n;
}
static inline uint16_t _tu_fifo_remaining(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
{
return f->depth - _tu_fifo_count(f, wAbs, rAbs);
}
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;
_ff_lock(f->mutex_wr);
uint16_t w = f->wr_idx, r = f->rd_idx;
uint8_t const* buf8 = (uint8_t const*) data;
if (!f->overwritable)
{
n = tu_min16(n, _tu_fifo_remaining(f, w, r));
}
else if (n >= f->depth)
{
buf8 = buf8 + (n - f->depth) * f->item_size;
n = f->depth;
w = r;
}
uint16_t wRel = get_relative_pointer(f, w);
_ff_push_n(f, buf8, n, wRel, copy_mode);
f->wr_idx = advance_pointer(f, w, n);
_ff_unlock(f->mutex_wr);
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)
{
_ff_lock(f->mutex_rd);
n = _tu_fifo_peek_n(f, buffer, n, f->wr_idx, f->rd_idx, copy_mode);
f->rd_idx = advance_pointer(f, f->rd_idx, n);
_ff_unlock(f->mutex_rd);
return n;
}
uint16_t tu_fifo_count(tu_fifo_t* f)
{
return tu_min16(_tu_fifo_count(f, f->wr_idx, f->rd_idx), f->depth);
}
bool tu_fifo_empty(tu_fifo_t* f)
{
return _tu_fifo_empty(f->wr_idx, f->rd_idx);
}
bool tu_fifo_full(tu_fifo_t* f)
{
return _tu_fifo_full(f, f->wr_idx, f->rd_idx);
}
uint16_t tu_fifo_remaining(tu_fifo_t* f)
{
return _tu_fifo_remaining(f, f->wr_idx, f->rd_idx);
}
bool tu_fifo_overflowed(tu_fifo_t* f)
{
return _tu_fifo_overflowed(f, f->wr_idx, f->rd_idx);
}
void tu_fifo_correct_read_pointer(tu_fifo_t* f)
{
_ff_lock(f->mutex_rd);
_tu_fifo_correct_read_pointer(f, f->wr_idx);
_ff_unlock(f->mutex_rd);
}
bool tu_fifo_read(tu_fifo_t* f, void * buffer)
{
_ff_lock(f->mutex_rd);
bool ret = _tu_fifo_peek(f, buffer, f->wr_idx, f->rd_idx);
f->rd_idx = advance_pointer(f, f->rd_idx, ret);
_ff_unlock(f->mutex_rd);
return ret;
}
uint16_t tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n)
{
return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_INC);
}
uint16_t tu_fifo_read_n_const_addr_full_words(tu_fifo_t* f, void * buffer, uint16_t n)
{
return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_CST_FULL_WORDS);
}
bool tu_fifo_peek(tu_fifo_t* f, void * p_buffer)
{
_ff_lock(f->mutex_rd);
bool ret = _tu_fifo_peek(f, p_buffer, f->wr_idx, f->rd_idx);
_ff_unlock(f->mutex_rd);
return ret;
}
uint16_t tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n)
{
_ff_lock(f->mutex_rd);
uint16_t ret = _tu_fifo_peek_n(f, p_buffer, n, f->wr_idx, f->rd_idx, TU_FIFO_COPY_INC);
_ff_unlock(f->mutex_rd);
return ret;
}
bool tu_fifo_write(tu_fifo_t* f, const void * data)
{
_ff_lock(f->mutex_wr);
bool ret;
uint16_t const w = f->wr_idx;
if ( _tu_fifo_full(f, w, f->rd_idx) && !f->overwritable )
{
ret = false;
}else
{
uint16_t wRel = get_relative_pointer(f, w);
_ff_push(f, data, wRel);
f->wr_idx = advance_pointer(f, w, 1);
ret = true;
}
_ff_unlock(f->mutex_wr);
return ret;
}
uint16_t tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n)
{
return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_INC);
}
uint16_t tu_fifo_write_n_const_addr_full_words(tu_fifo_t* f, const void * data, uint16_t n)
{
return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_CST_FULL_WORDS);
}
bool tu_fifo_clear(tu_fifo_t *f)
{
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
f->rd_idx = f->wr_idx = 0;
f->max_pointer_idx = 2*f->depth-1;
f->non_used_index_space = UINT16_MAX - f->max_pointer_idx;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
return true;
}
bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable)
{
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
f->overwritable = overwritable;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
return true;
}
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
{
f->wr_idx = advance_pointer(f, f->wr_idx, n);
}
void tu_fifo_advance_read_pointer(tu_fifo_t *f, uint16_t n)
{
f->rd_idx = advance_pointer(f, f->rd_idx, n);
}
void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
{
uint16_t w = f->wr_idx, r = f->rd_idx;
uint16_t cnt = _tu_fifo_count(f, w, r);
if (cnt > f->depth)
{
_ff_lock(f->mutex_rd);
_tu_fifo_correct_read_pointer(f, w);
_ff_unlock(f->mutex_rd);
r = f->rd_idx;
cnt = f->depth;
}
if (cnt == 0)
{
info->len_lin = 0;
info->len_wrap = 0;
info->ptr_lin = NULL;
info->ptr_wrap = NULL;
return;
}
w = get_relative_pointer(f, w);
r = get_relative_pointer(f, r);
info->ptr_lin = &f->buffer[r];
if (w > r) {
info->len_lin = cnt;
info->len_wrap = 0;
info->ptr_wrap = NULL;
}
else
{
info->len_lin = f->depth - r; info->len_wrap = cnt - info->len_lin;
info->ptr_wrap = f->buffer;
}
}
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 free = _tu_fifo_remaining(f, w, r);
if (free == 0)
{
info->len_lin = 0;
info->len_wrap = 0;
info->ptr_lin = NULL;
info->ptr_wrap = NULL;
return;
}
w = get_relative_pointer(f, w);
r = get_relative_pointer(f, r);
info->ptr_lin = &f->buffer[w];
if (w < r)
{
info->len_lin = r-w;
info->len_wrap = 0;
info->ptr_wrap = NULL;
}
else
{
info->len_lin = f->depth - w;
info->len_wrap = free - info->len_lin; info->ptr_wrap = f->buffer; }
}