GlobLib
HAL and API libraries for MCUs and hardware.
fifo8.c
Go to the documentation of this file.
1 
17 #include "fifo8.h"
18 
19 
20 fifo8_error_t FIFO8_put(FIFO8 *target, uint8_t byte){
21 
22  //The fifo is full when the head is one position behind the tale
23  if((target->head == ((target->tail-1) & target->mask))){
24 
25  return E_FIFO8_FULL;
26  }
27 
28  //Add a byte than increase the position
29  target->buffer[target->head] = byte;
30  target->head++;
31 
32  //Wrap around if at end
33  target->head = (target->head & target->mask);
34 
35  //Start dumping characters if need
36  if((target->idle == 1) && (target->mode == FIFO8_AUTO)){
37 
38  FIFO8_get(target);
39  }
40 
41  return E_FIFO8_NOERROR;
42 
43 }
44 
46 
47  //The fifo is empty when the head and tail are at the same position.
48  if(target->head == target->tail){
49 
50  target->idle = 1;
51  return E_FIFO8_EMPTY;
52  }
53 
54  target->idle = 0;
55 
56  //Get a byte and wrap around if need
57  target->out(target->buffer[target->tail]);
58  target->tail++;
59  target->tail = (target->tail & target->mask);
60 
61  return E_FIFO8_NOERROR;
62 }
63 
64 fifo8_error_t FIFO8_init(FIFO8 *target, fifo8_mode_t mode, uint8_t *buffer, uint16_t size, void (*output)(uint8_t byte)){
65 
66  //First check if the size is a power of 2
67  if(!(size && !(size & (size - 1)))){
68 
69  return E_FIFO8_NONBINARY;
70  }
71 
72  //Check if the mode exists
73  if((mode != FIFO8_AUTO) && (mode != FIFO8_TRIGGER) && (mode != FIFO8_DUMP)){
74 
75  return E_FIFO8_NOMODE;
76  }
77 
78  //All good, set up inital values.
79  target->buffer = buffer;
80  target->head = 0;
81  target->tail = 0;
82  target->mask = size - 1;
83  target->idle = 1;
84  target->mode = mode;
85  target->out = output;
86 
87  return E_FIFO8_NOERROR;
88 }
89 
90 uint16_t FIFO8_size(FIFO8 *target){
91 
92  return (target->head - target->tail) & target->mask;
93 }
94 
96 
97  uint8_t test = 0;
98 
99  //The idle bit is used to determine if the fifo is empty
100  while(!test){
101 
102  FIFO8_get(target);
103  test = target->idle;
104  }
105 
106  return E_FIFO8_NOERROR;
107 }
108 uint8_t FIFO8_pop(FIFO8 *target){
109 
110  uint8_t byte;
111  //The fifo is empty when the head and tail are at the same position.
112  if(target->head == target->tail){
113 
114  target->idle = 1;
115  return E_FIFO8_EMPTY;
116  }
117 
118  target->idle = 0;
119 
120  //Get a byte and wrap around if need
121  byte = (target->buffer[target->tail]);
122  target->tail++;
123  target->tail = (target->tail & target->mask);
124 
125  return byte;
126 
127 }
128 
uint8_t idle
Used to determine if the FIFO8 is already outputing bytes.
Definition: fifo8.h:81
Fifo contents flushed to output function ASAP.
Definition: fifo8.h:47
fifo8_error_t FIFO8_get(FIFO8 *target)
Takes a character from the buffer.
Definition: fifo8.c:45
fifo8_error_t
Error codes returned by function calls.
Definition: fifo8.h:56
uint16_t mask
Bit mask used for FIFO8 buffer memory wrapping.
Definition: fifo8.h:80
Header file for 8-bit circular FIFO software buffer.
The FIFO mode doesn't exist.
Definition: fifo8.h:59
The FIFO buffer is empty.
Definition: fifo8.h:62
The FIFO buffer is full.
Definition: fifo8.h:61
uint16_t FIFO8_size(FIFO8 *target)
Return the size in bytes of the FIFO8 buffer.
Definition: fifo8.c:90
fifo8_error_t FIFO8_put(FIFO8 *target, uint8_t byte)
Adds a character to the buffer.
Definition: fifo8.c:20
uint8_t FIFO8_pop(FIFO8 *target)
Reset the fifo buffer and set its size to 0.
Definition: fifo8.c:108
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.
Definition: fifo8.c:64
Fifo contents flushed to output function when buffer is full.
Definition: fifo8.h:49
void(* out)(uint8_t byte)
Pointer to the output function used by read function FIFO_get()
Definition: fifo8.h:83
fifo8_error_t FIFO8_flush(FIFO8 *target)
Flush all contents of the FIFO8 to the output function.
Definition: fifo8.c:95
fifo8_mode_t mode
The FIFO8 mode of operation.
Definition: fifo8.h:82
No error.
Definition: fifo8.h:58
fifo8_mode_t
Operating modes for the buffer.
Definition: fifo8.h:45
uint8_t * buffer
Pointer to the FIFO8 memory buffer.
Definition: fifo8.h:77
Fifo content flushed to output function on call FIFO_flush()
Definition: fifo8.h:48
uint16_t tail
The current read position in the FIFO8 memory buffer.
Definition: fifo8.h:79
Data structure for FIFO8 perihperal interface.
Definition: fifo8.h:75
uint16_t head
The current write position in the FIFO8 memory buffer.
Definition: fifo8.h:78
The size of the FIFO buffer is not a power of 2.
Definition: fifo8.h:60