GlobLib
HAL and API libraries for MCUs and hardware.
FIFO8

8-Bit circular FIFO software buffer. More...

Files

file  fifo8.h
 Header file for 8-bit circular FIFO software buffer.
 

Data Structures

struct  FIFO8
 Data structure for FIFO8 perihperal interface. More...
 

Enumerations

Functions

fifo8_error_t FIFO8_init (FIFO8 *target, fifo8_mode_t mode, uint8_t *buffer, uint16_t size, void(*output)(uint8_t byte))
 Initializes the FIFO8 buffer. More...
 
fifo8_error_t FIFO8_put (FIFO8 *target, uint8_t byte)
 Adds a character to the buffer. More...
 
fifo8_error_t FIFO8_get (FIFO8 *target)
 Takes a character from the buffer. More...
 
uint16_t FIFO8_size (FIFO8 *target)
 Return the size in bytes of the FIFO8 buffer. More...
 
fifo8_error_t FIFO8_flush (FIFO8 *target)
 Flush all contents of the FIFO8 to the output function. More...
 
uint8_t FIFO8_pop (FIFO8 *target)
 Reset the fifo buffer and set its size to 0.
 

Detailed Description

8-Bit circular FIFO software buffer.

Author
Stuart Ianna
Version
0.11
Date
December 2018
Warning
None
Bug:
None
Todo:
Verified Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Example - Auto sending via USART transmit
//Change to include appropriate MCU target
#include <stm32f103cb_core.h>
#include "fifo8.h"
//Create an instance for the FIFO object
FIFO8 tx;
//Function to reload the FIFO
void loadtx(uint8_t byte);
//FIFO memory buffer must be a log2 based number
#define TX_FIFO_SIZE 32
uint8_t tx_buffer[TX_FIFO_SIZE];
//Overriden USART ISR functions - used to trigger FIFOs
void dataAvailable(uint8_t byte); //Called when a byte is ready to be read, used for echo
void dataSent(void); //Called when a byte is finished being sent, used to triger FIFO
int main(void)
{
//USART Hardware setup
USART_setup(USART_1);
USART_setRxISR(USART_1,&dataAvailable);
USART_setTxISR(USART_1,&dataSent);
//Setup the fifo buffer
FIFO8_init(&tx,FIFO8_AUTO,tx_buffer,TX_FIFO_SIZE,USART_add_put(USART_1));
while(1){
//Bytes are now sent using FIF08_put
//Bytes are automatically sent when the port is free
FIFO8_put('s');
//Wait
for(int i = 0;i < 0xFFF;i ++);
}
return 0;
}
void dataAvailable(uint8_t byte){
//Echo bytes
FIFO8_put(byte);
}
void dataSent(void){
//Get another byte from the FIFO
FIFO8_get(&tx);
}
Example - Triggerd reading via USART receive
//Change to include appropriate MCU target
#include <stm32f103cb_core.h>
#include "fifo8.h"
//Create an instance for the FIFO object
FIFO8 rx;
//Function to reload the FIFO
void loadtx(uint8_t byte);
//FIFO memory buffer
#define RX_FIFO_SIZE 8
uint8_t rx_buffer[RX_FIFO_SIZE];
//Overriden USART ISR functions - used to trigger FIFOs
void dataAvailable(uint8_t byte); //Called when a byte is ready to be read, used to load buffer
void getrx(uint8_t byte); //This is called when bytes are read (dumped)
int main(void)
{
//USART Hardware setup
USART_setup(USART_1);
USART_setRxISR(&serial,&dataAvailable);
//Setup the fifo buffer
FIFO8_init(&rx,FIFO8_TRIGGER,rx_buffer,RX_FIFO_SIZE,&getrx);
while(1){
//Empty buffer to terminal if it has greater than four bytes
if(FIFO8_size(&rx) >= 4){
}
}
return 0;
}
void dataAvailable(uint8_t byte){
//Add the recieved to the FIFO
FIFO8_put(&rx,byte);
}
void getrx(uint8_t byte){
//Echo back to terminal
USART_put(USART1,byte);
}

Data Structure Documentation

struct FIFO8

Data structure for FIFO8 perihperal interface.

This structure containts variables needed by each instance of of the FIFO8 buffer.

Values contained in this structure do not need to be modified manually, they are managed by function calls.

A new instance of this structure must be initialized by FIFO8_setup().

Definition at line 75 of file fifo8.h.

Data Fields

uint8_t * buffer
 Pointer to the FIFO8 memory buffer.
 
uint16_t head
 The current write position in the FIFO8 memory buffer.
 
uint16_t tail
 The current read position in the FIFO8 memory buffer.
 
uint16_t mask
 Bit mask used for FIFO8 buffer memory wrapping.
 
uint8_t idle
 Used to determine if the FIFO8 is already outputing bytes.
 
fifo8_mode_t mode
 The FIFO8 mode of operation.
 
void(* out )(uint8_t byte)
 Pointer to the output function used by read function FIFO_get()
 

Enumeration Type Documentation

Error codes returned by function calls.

Enumerator
E_FIFO8_NOERROR 

No error.

E_FIFO8_NOMODE 

The FIFO mode doesn't exist.

E_FIFO8_NONBINARY 

The size of the FIFO buffer is not a power of 2.

E_FIFO8_FULL 

The FIFO buffer is full.

E_FIFO8_EMPTY 

The FIFO buffer is empty.

Definition at line 56 of file fifo8.h.

Operating modes for the buffer.

These values are used with function

Enumerator
FIFO8_AUTO 

Fifo contents flushed to output function ASAP.

FIFO8_TRIGGER 

Fifo content flushed to output function on call FIFO_flush()

FIFO8_DUMP 

Fifo contents flushed to output function when buffer is full.

Definition at line 45 of file fifo8.h.

Function Documentation

fifo8_error_t FIFO8_flush ( FIFO8 target)

Flush all contents of the FIFO8 to the output function.

This function recursibly calles FIFO8_get() until the memory buffer is empty.

Parameters
targetPointer to the FIFO8 object to get the size of.
Returns
E_FIFO_EMPTY if called when the buffer is already empty, E_FIFO_NOERROR if not.

Definition at line 95 of file fifo8.c.

fifo8_error_t FIFO8_get ( FIFO8 target)

Takes a character from the buffer.

The character taken from the buffer is passed to the output function set in FIFO8_init()

Parameters
targetPointer to the FIFO8 object to take a byte from.
Returns
E_FIFO8_EMPTY if the buffer is empty, E_FIFO8_NOERROR if it is not.

Definition at line 45 of file fifo8.c.

fifo8_error_t FIFO8_init ( FIFO8 target,
fifo8_mode_t  mode,
uint8_t *  buffer,
uint16_t  size,
void(*)(uint8_t byte)  output 
)

Initializes the FIFO8 buffer.

This must be called before any other function of the FIFO8 buffer will work.

Parameters
targetPointer to the FIFO8 object to initialize.
modeThe mode of the buffer.
bufferPointer to the first byte of memory to be used for the buffer.
sizeThe size of the buffer (Must be a power or 2).
outputPointer to the output function called when FIFO8_get() is used.
Returns
E_FIFO8_NOERROR if no error, E_FIFO_8_NOMODE if the passed mode doesn't exist or E_FIFO8_NONBINARY if the memory buffer isn't a power of 2..

Definition at line 64 of file fifo8.c.

fifo8_error_t FIFO8_put ( FIFO8 target,
uint8_t  byte 
)

Adds a character to the buffer.

If the FIFO8 mode is FIFO8_AUTO, and this is the first byte in the fifo, this will start the output stream as well.

Parameters
targetPointer to the FIFO8 object to add a byte to.
byteThe byte to add to the buffer.
Returns
E_FIFO8_FULL if the buffer is full, E_FIFO8_NOERROR if it is not.

Definition at line 20 of file fifo8.c.

uint16_t FIFO8_size ( FIFO8 target)

Return the size in bytes of the FIFO8 buffer.

Parameters
targetPointer to the FIFO8 object to get the size of.
Returns
The size of the FIFO8 buffer in bytes.

Definition at line 90 of file fifo8.c.