Open FFBoard
Open source force feedback firmware
tusb_fifo.c File Reference

Go to the source code of this file.

Enumerations

enum  tu_fifo_copy_mode_t { TU_FIFO_COPY_INC , TU_FIFO_COPY_CST_FULL_WORDS }
 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. More...
 

Functions

static TU_ATTR_ALWAYS_INLINE void _ff_lock (osal_mutex_t mutex)
 
static TU_ATTR_ALWAYS_INLINE void _ff_unlock (osal_mutex_t mutex)
 
bool tu_fifo_config (tu_fifo_t *f, void *buffer, uint16_t depth, uint16_t item_size, bool overwritable)
 
static void _ff_push_const_addr (uint8_t *ff_buf, const void *app_buf, uint16_t len)
 
static void _ff_pull_const_addr (void *app_buf, const uint8_t *ff_buf, uint16_t len)
 
static void _ff_push (tu_fifo_t *f, void const *app_buf, uint16_t rel)
 
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)
 
static void _ff_pull (tu_fifo_t *f, void *app_buf, uint16_t rel)
 
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)
 
static TU_ATTR_ALWAYS_INLINE uint16_t _ff_count (uint16_t depth, uint16_t wr_idx, uint16_t rd_idx)
 
static TU_ATTR_ALWAYS_INLINE uint16_t _ff_remaining (uint16_t depth, uint16_t wr_idx, uint16_t rd_idx)
 
static uint16_t advance_index (uint16_t depth, uint16_t idx, uint16_t offset)
 
static uint16_t backward_index (uint16_t depth, uint16_t idx, uint16_t offset)
 
static TU_ATTR_ALWAYS_INLINE uint16_t idx2ptr (uint16_t depth, uint16_t idx)
 
static TU_ATTR_ALWAYS_INLINE uint16_t _ff_correct_read_index (tu_fifo_t *f, uint16_t wr_idx)
 
static bool _tu_fifo_peek (tu_fifo_t *f, void *p_buffer, uint16_t wr_idx, uint16_t rd_idx)
 
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)
 
static uint16_t _tu_fifo_write_n (tu_fifo_t *f, const void *data, uint16_t n, tu_fifo_copy_mode_t copy_mode)
 
static uint16_t _tu_fifo_read_n (tu_fifo_t *f, void *buffer, uint16_t n, tu_fifo_copy_mode_t copy_mode)
 
uint16_t tu_fifo_count (tu_fifo_t *f)
 Get number of items in FIFO. More...
 
bool tu_fifo_empty (tu_fifo_t *f)
 Check if FIFO is empty. More...
 
bool tu_fifo_full (tu_fifo_t *f)
 Check if FIFO is full. More...
 
uint16_t tu_fifo_remaining (tu_fifo_t *f)
 Get remaining space in FIFO. More...
 
bool tu_fifo_overflowed (tu_fifo_t *f)
 Check if overflow happened. More...
 
void tu_fifo_correct_read_pointer (tu_fifo_t *f)
 
bool tu_fifo_read (tu_fifo_t *f, void *buffer)
 Read one element out of the buffer. More...
 
uint16_t tu_fifo_read_n (tu_fifo_t *f, void *buffer, uint16_t n)
 This function will read n elements from the array index specified by the read pointer and increment the read index. This function checks for an overflow and corrects read pointer if required. More...
 
uint16_t tu_fifo_read_n_const_addr_full_words (tu_fifo_t *f, void *buffer, uint16_t n)
 This function will read n elements from the array index specified by the read pointer and increment the read index. This function checks for an overflow and corrects read pointer if required. The dest address will not be incremented which is useful for writing to registers. More...
 
bool tu_fifo_peek (tu_fifo_t *f, void *p_buffer)
 Read one item without removing it from the FIFO. This function checks for an overflow and corrects read pointer if required. More...
 
uint16_t tu_fifo_peek_n (tu_fifo_t *f, void *p_buffer, uint16_t n)
 Read n items without removing it from the FIFO This function checks for an overflow and corrects read pointer if required. More...
 
bool tu_fifo_write (tu_fifo_t *f, const void *data)
 Write one element into the buffer. More...
 
uint16_t tu_fifo_write_n (tu_fifo_t *f, const void *data, uint16_t n)
 This function will write n elements into the array index specified by the write pointer and increment the write index. More...
 
uint16_t tu_fifo_write_n_const_addr_full_words (tu_fifo_t *f, const void *data, uint16_t n)
 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. More...
 
bool tu_fifo_clear (tu_fifo_t *f)
 Clear the fifo read and write pointers. More...
 
bool tu_fifo_set_overwritable (tu_fifo_t *f, bool overwritable)
 Change the fifo mode to overwritable or not overwritable. More...
 
void tu_fifo_advance_write_pointer (tu_fifo_t *f, uint16_t n)
 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. More...
 
void tu_fifo_advance_read_pointer (tu_fifo_t *f, uint16_t n)
 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. More...
 
void tu_fifo_get_read_info (tu_fifo_t *f, tu_fifo_buffer_info_t *info)
 Get read info. More...
 
void tu_fifo_get_write_info (tu_fifo_t *f, tu_fifo_buffer_info_t *info)
 Get linear write info. More...
 

Enumeration Type Documentation

◆ tu_fifo_copy_mode_t

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.

Enumerator
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.

Definition at line 62 of file tusb_fifo.c.

Function Documentation

◆ _ff_correct_read_index()

static TU_ATTR_ALWAYS_INLINE uint16_t _ff_correct_read_index ( tu_fifo_t f,
uint16_t  wr_idx 
)
inlinestatic

Definition at line 402 of file tusb_fifo.c.

◆ _ff_count()

static TU_ATTR_ALWAYS_INLINE uint16_t _ff_count ( uint16_t  depth,
uint16_t  wr_idx,
uint16_t  rd_idx 
)
inlinestatic

Definition at line 330 of file tusb_fifo.c.

◆ _ff_lock()

static TU_ATTR_ALWAYS_INLINE void _ff_lock ( osal_mutex_t  mutex)
inlinestatic

Definition at line 41 of file tusb_fifo.c.

◆ _ff_pull()

static void _ff_pull ( tu_fifo_t f,
void *  app_buf,
uint16_t  rel 
)
inlinestatic

Definition at line 237 of file tusb_fifo.c.

◆ _ff_pull_const_addr()

static void _ff_pull_const_addr ( void *  app_buf,
const uint8_t *  ff_buf,
uint16_t  len 
)
static

Definition at line 124 of file tusb_fifo.c.

◆ _ff_pull_n()

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 
)
static

Definition at line 243 of file tusb_fifo.c.

◆ _ff_push()

static void _ff_push ( tu_fifo_t f,
void const *  app_buf,
uint16_t  rel 
)
inlinestatic

Definition at line 149 of file tusb_fifo.c.

◆ _ff_push_const_addr()

static void _ff_push_const_addr ( uint8_t *  ff_buf,
const void *  app_buf,
uint16_t  len 
)
static

Definition at line 101 of file tusb_fifo.c.

◆ _ff_push_n()

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 
)
static

Definition at line 155 of file tusb_fifo.c.

◆ _ff_remaining()

static TU_ATTR_ALWAYS_INLINE uint16_t _ff_remaining ( uint16_t  depth,
uint16_t  wr_idx,
uint16_t  rd_idx 
)
inlinestatic

Definition at line 344 of file tusb_fifo.c.

◆ _ff_unlock()

static TU_ATTR_ALWAYS_INLINE void _ff_unlock ( osal_mutex_t  mutex)
inlinestatic

Definition at line 46 of file tusb_fifo.c.

◆ _tu_fifo_peek()

static bool _tu_fifo_peek ( tu_fifo_t f,
void *  p_buffer,
uint16_t  wr_idx,
uint16_t  rd_idx 
)
static

Definition at line 420 of file tusb_fifo.c.

◆ _tu_fifo_peek_n()

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 
)
static

Definition at line 444 of file tusb_fifo.c.

◆ _tu_fifo_read_n()

static uint16_t _tu_fifo_read_n ( tu_fifo_t f,
void *  buffer,
uint16_t  n,
tu_fifo_copy_mode_t  copy_mode 
)
static

Definition at line 556 of file tusb_fifo.c.

◆ _tu_fifo_write_n()

static uint16_t _tu_fifo_write_n ( tu_fifo_t f,
const void *  data,
uint16_t  n,
tu_fifo_copy_mode_t  copy_mode 
)
static

Definition at line 469 of file tusb_fifo.c.

◆ advance_index()

static uint16_t advance_index ( uint16_t  depth,
uint16_t  idx,
uint16_t  offset 
)
static

Definition at line 356 of file tusb_fifo.c.

◆ backward_index()

static uint16_t backward_index ( uint16_t  depth,
uint16_t  idx,
uint16_t  offset 
)
static

Definition at line 373 of file tusb_fifo.c.

◆ idx2ptr()

static TU_ATTR_ALWAYS_INLINE uint16_t idx2ptr ( uint16_t  depth,
uint16_t  idx 
)
inlinestatic

Definition at line 391 of file tusb_fifo.c.

◆ tu_fifo_advance_read_pointer()

void tu_fifo_advance_read_pointer ( tu_fifo_t f,
uint16_t  n 
)

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.

USE WITH CARE - WE DO NOT CONDUCT SAFETY CHECKS HERE!

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]nNumber of items the read pointer moves forward

Definition at line 969 of file tusb_fifo.c.

◆ tu_fifo_advance_write_pointer()

void tu_fifo_advance_write_pointer ( tu_fifo_t f,
uint16_t  n 
)

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.

USE WITH CARE - WE DO NOT CONDUCT SAFETY CHECKS HERE!

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]nNumber of items the write pointer moves forward

Definition at line 948 of file tusb_fifo.c.

◆ tu_fifo_clear()

bool tu_fifo_clear ( tu_fifo_t f)

Clear the fifo read and write pointers.

Parameters
[in]fPointer to the FIFO buffer to manipulate

Definition at line 896 of file tusb_fifo.c.

◆ tu_fifo_config()

bool tu_fifo_config ( tu_fifo_t f,
void *  buffer,
uint16_t  depth,
uint16_t  item_size,
bool  overwritable 
)

Definition at line 70 of file tusb_fifo.c.

◆ tu_fifo_correct_read_pointer()

void tu_fifo_correct_read_pointer ( tu_fifo_t f)

Definition at line 676 of file tusb_fifo.c.

◆ tu_fifo_count()

uint16_t tu_fifo_count ( tu_fifo_t f)

Get number of items 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. In case an overflow occurred, this function return f.depth at maximum. Overflows are checked and corrected for in the read functions!

Parameters
[in]fPointer to the FIFO buffer to manipulate
Returns
Number of items in FIFO

Definition at line 590 of file tusb_fifo.c.

◆ tu_fifo_empty()

bool tu_fifo_empty ( tu_fifo_t f)

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.

Parameters
[in]fPointer to the FIFO buffer to manipulate
Returns
Number of items in FIFO

Definition at line 608 of file tusb_fifo.c.

◆ tu_fifo_full()

bool tu_fifo_full ( tu_fifo_t f)

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.

Parameters
[in]fPointer to the FIFO buffer to manipulate
Returns
Number of items in FIFO

Definition at line 626 of file tusb_fifo.c.

◆ tu_fifo_get_read_info()

void tu_fifo_get_read_info ( tu_fifo_t f,
tu_fifo_buffer_info_t info 
)

Get read info.

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 corresponding pointer is invalid. The read pointer does NOT get advanced, use tu_fifo_advance_read_pointer() to do so!

Parameters
[in]fPointer to FIFO
[out]*infoPointer to struct which holds the desired infos

Definition at line 989 of file tusb_fifo.c.

◆ tu_fifo_get_write_info()

void tu_fifo_get_write_info ( tu_fifo_t f,
tu_fifo_buffer_info_t info 
)

Get linear write info.

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 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!

Parameters
[in]fPointer to FIFO
[out]*infoPointer to struct which holds the desired infos

Definition at line 1057 of file tusb_fifo.c.

◆ tu_fifo_overflowed()

bool tu_fifo_overflowed ( tu_fifo_t f)

Check if overflow happened.

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 your pointer states are messed up. This can only happen if you use DMAs, write functions do not allow such an error. Avoid such nasty things!

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().

Parameters
[in]fPointer to the FIFO buffer to manipulate
Returns
True if overflow happened

Definition at line 670 of file tusb_fifo.c.

◆ tu_fifo_peek()

bool tu_fifo_peek ( tu_fifo_t f,
void *  p_buffer 
)

Read one item without removing it from the FIFO. This function checks for an overflow and corrects read pointer if required.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]p_bufferPointer to the place holder for data read from the buffer
Returns
TRUE if the queue is not empty

Definition at line 772 of file tusb_fifo.c.

◆ tu_fifo_peek_n()

uint16_t tu_fifo_peek_n ( tu_fifo_t f,
void *  p_buffer,
uint16_t  n 
)

Read n items without removing it from the FIFO This function checks for an overflow and corrects read pointer if required.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]p_bufferPointer to the place holder for data read from the buffer
[in]nNumber of items to peek
Returns
Number of bytes written to p_buffer

Definition at line 795 of file tusb_fifo.c.

◆ tu_fifo_read()

bool tu_fifo_read ( tu_fifo_t f,
void *  buffer 
)

Read one element out of the buffer.

This function will return the element located at the array index of the read pointer, and then increment the read pointer index. This function checks for an overflow and corrects read pointer if required.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]bufferPointer to the place holder for data read from the buffer
Returns
TRUE if the queue is not empty

Definition at line 699 of file tusb_fifo.c.

◆ tu_fifo_read_n()

uint16_t tu_fifo_read_n ( tu_fifo_t f,
void *  buffer,
uint16_t  n 
)

This function will read n elements from the array index specified by the read pointer and increment the read index. This function checks for an overflow and corrects read pointer if required.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]bufferThe pointer to data location
[in]nNumber of element that buffer can afford
Returns
number of items read from the FIFO

Definition at line 730 of file tusb_fifo.c.

◆ tu_fifo_read_n_const_addr_full_words()

uint16_t tu_fifo_read_n_const_addr_full_words ( tu_fifo_t f,
void *  buffer,
uint16_t  n 
)

This function will read n elements from the array index specified by the read pointer and increment the read index. This function checks for an overflow and corrects read pointer if required. The dest address will not be incremented which is useful for writing to registers.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]bufferThe pointer to data location
[in]nNumber of element that buffer can afford
Returns
number of items read from the FIFO

Definition at line 753 of file tusb_fifo.c.

◆ tu_fifo_remaining()

uint16_t tu_fifo_remaining ( tu_fifo_t f)

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.

Parameters
[in]fPointer to the FIFO buffer to manipulate
Returns
Number of items in FIFO

Definition at line 644 of file tusb_fifo.c.

◆ tu_fifo_set_overwritable()

bool tu_fifo_set_overwritable ( tu_fifo_t f,
bool  overwritable 
)

Change the fifo mode to overwritable or not overwritable.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]overwritableOverwritable mode the fifo is set to

Definition at line 919 of file tusb_fifo.c.

◆ tu_fifo_write()

bool tu_fifo_write ( tu_fifo_t f,
const void *  data 
)

Write one element into the buffer.

This function will write one element into the array index specified by the write pointer and increment the write index.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]dataThe byte to add to the FIFO
Returns
TRUE if the data was written to the FIFO (overwrittable FIFO will always return TRUE)

Definition at line 819 of file tusb_fifo.c.

◆ tu_fifo_write_n()

uint16_t tu_fifo_write_n ( tu_fifo_t f,
const void *  data,
uint16_t  n 
)

This function will write n elements into the array index specified by the write pointer and increment the write index.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]dataThe pointer to data to add to the FIFO
[in]countNumber of element
Returns
Number of written elements

Definition at line 861 of file tusb_fifo.c.

◆ tu_fifo_write_n_const_addr_full_words()

uint16_t tu_fifo_write_n_const_addr_full_words ( tu_fifo_t f,
const void *  data,
uint16_t  n 
)

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.

Parameters
[in]fPointer to the FIFO buffer to manipulate
[in]dataThe pointer to data to add to the FIFO
[in]countNumber of element
Returns
Number of written elements

Definition at line 882 of file tusb_fifo.c.