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

Go to the source code of this file.

Classes

struct  tu_fifo_t
 
struct  tu_fifo_t::TU_ATTR_PACKED
 
struct  tu_fifo_buffer_info_t
 

Functions

bool tu_fifo_set_overwritable (tu_fifo_t *f, bool overwritable)
 Change the fifo mode to overwritable or not overwritable. More...
 
bool tu_fifo_clear (tu_fifo_t *f)
 Clear the fifo read and write pointers. More...
 
bool tu_fifo_config (tu_fifo_t *f, void *buffer, uint16_t depth, uint16_t item_size, bool overwritable)
 
static TU_ATTR_ALWAYS_INLINE void tu_fifo_config_mutex (tu_fifo_t *f, osal_mutex_t wr_mutex, osal_mutex_t rd_mutex)
 
bool tu_fifo_write (tu_fifo_t *f, void const *data)
 Write one element into the buffer. More...
 
uint16_t tu_fifo_write_n (tu_fifo_t *f, void const *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_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...
 
uint16_t tu_fifo_count (tu_fifo_t *f)
 Get number of items in FIFO. More...
 
uint16_t tu_fifo_remaining (tu_fifo_t *f)
 Get remaining space 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...
 
bool tu_fifo_overflowed (tu_fifo_t *f)
 Check if overflow happened. More...
 
void tu_fifo_correct_read_pointer (tu_fifo_t *f)
 
static TU_ATTR_ALWAYS_INLINE uint16_t tu_fifo_depth (tu_fifo_t *f)
 
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...
 

Function Documentation

◆ 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_config_mutex()

static TU_ATTR_ALWAYS_INLINE void tu_fifo_config_mutex ( tu_fifo_t f,
osal_mutex_t  wr_mutex,
osal_mutex_t  rd_mutex 
)
inlinestatic

Definition at line 149 of file tusb_fifo.h.

◆ 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_depth()

static TU_ATTR_ALWAYS_INLINE uint16_t tu_fifo_depth ( tu_fifo_t f)
inlinestatic

Definition at line 180 of file tusb_fifo.h.

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