FIFO Library
Software FIFO
 All Data Structures Functions Variables Typedefs Groups Pages
fifo.h
1 /*****************************************************************************
2 
3  Software FIFO using circular buffer
4  structs and generic functions to increase usability
5 
6  Copyright (C) 2015 Falk Brunner
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22  You can contact the author at Falk.Brunner@gmx.de
23 
24 *****************************************************************************/
25 
56 #ifndef _FIFO_H
57 #define _FIFO_H
58 
64 
65 typedef uint8_t fifo_data_t;
66 typedef uint16_t fifo_size_t;
69 
74 typedef struct {
75  fifo_data_t volatile * volatile write_p;
76  fifo_data_t volatile * volatile read_p;
77  fifo_data_t volatile * volatile base;
78  fifo_data_t volatile * volatile top;
79  fifo_size_t volatile size;
80 } fifo_t;
81 
82 //-FUNCTIONS---------------------------------------------------------------------------------------------------------
87 
96 void fifo_init(fifo_t *fifo, fifo_data_t *data, fifo_size_t size);
97 
104 void fifo_write(fifo_t *fifo, fifo_data_t data);
105 
112 void fifo_write_busy(fifo_t *fifo, fifo_data_t data);
113 
120 
127 
134 
141 
148 void fifo_write_bursted(fifo_t *fifo, fifo_size_t count);
149 
156 void fifo_read_bursted(fifo_t *fifo, fifo_size_t count);
157 
164 
171 
174 //-ISR FUNCTIONS---------------------------------------------------------------------------------------------------------
179 
186 inline void fifo_write_ISR(fifo_t *fifo, fifo_data_t data);
187 
193 inline fifo_data_t fifo_read_ISR(fifo_t *fifo);
194 
201 
207 inline fifo_size_t fifo_get_free_ISR(fifo_t *fifo);
208 
211 //- INLINE FUNCTIONS ------------------------------------------------------------------------------------------------------------------
212 
214  fifo_data_t data;
215  fifo_data_t *tmp;
216 
217  tmp = (fifo_data_t*)fifo->read_p;
218  data = *tmp++;
219  if (tmp > fifo->top) {
220  fifo->read_p = fifo->base;
221  } else {
222  fifo->read_p = tmp;
223  }
224  return data;
225 }
226 
227 inline void fifo_write_ISR(fifo_t *fifo, fifo_data_t data) {
228  fifo_data_t *tmp;
229 
230  tmp = (fifo_data_t*)fifo->write_p;
231  *tmp++ = data;
232  if (tmp > fifo->top) {
233  fifo->write_p = fifo->base;
234  } else {
235  fifo->write_p = tmp;
236  }
237 }
238 
240  fifo_size_t tmp;
241  fifo_data_t *tmp_w, *tmp_r;
242 
243  tmp_w = (fifo_data_t*)fifo->write_p;
244  tmp_r = (fifo_data_t*)fifo->read_p;
245 
246  tmp = tmp_w - tmp_r;
247  if (tmp_w < tmp_r) {
248  tmp += fifo->size;
249  }
250  return tmp;
251 }
252 
254  fifo_size_t tmp;
255  fifo_data_t *tmp_w, *tmp_r;
256 
257  tmp_w = (fifo_data_t*)fifo->write_p;
258  tmp_r = (fifo_data_t*)fifo->read_p;
259  tmp = tmp_w - tmp_r;
260  if (tmp_w < tmp_r) {
261  tmp += fifo->size;
262  }
263  return (fifo->size - tmp - 1);
264 }
265 
266 #endif
void fifo_init(fifo_t *fifo, fifo_data_t *data, fifo_size_t size)
FIFO initialization.
Definition: fifo.c:162
fifo_size_t fifo_get_level_ISR(fifo_t *fifo)
Get fill level of FIFO.
Definition: fifo.h:239
fifo_data_t volatile *volatile base
Definition: fifo.h:77
void fifo_write_bursted(fifo_t *fifo, fifo_size_t count)
FIFO write pointer correction after burst write access.
Definition: fifo.c:110
fifo_data_t volatile *volatile read_p
Definition: fifo.h:76
fifo_data_t fifo_read_busy(fifo_t *fifo)
FIFO read access with busy waiting for available data (blocking function)
Definition: fifo.c:54
void fifo_write(fifo_t *fifo, fifo_data_t data)
FIFO write access without check for free space.
Definition: fifo.c:40
fifo_data_t fifo_read_ISR(fifo_t *fifo)
FIFO read access without check for available data.
Definition: fifo.h:213
fifo_size_t fifo_get_free_ISR(fifo_t *fifo)
Get free space of fifo.
Definition: fifo.h:253
fifo_data_t fifo_read(fifo_t *fifo)
FIFO read access without check for available data.
Definition: fifo.c:60
fifo_size_t fifo_get_free(fifo_t *fifo)
Get free space of fifo.
Definition: fifo.c:91
uint8_t fifo_data_t
Definition: fifo.h:65
void fifo_write_ISR(fifo_t *fifo, fifo_data_t data)
FIFO write access without check for free space.
Definition: fifo.h:227
fifo_size_t fifo_get_level(fifo_t *fifo)
Get fill level of FIFO.
Definition: fifo.c:76
fifo_size_t volatile size
Definition: fifo.h:79
FIFO control data struct.
Definition: fifo.h:74
uint16_t fifo_size_t
Definition: fifo.h:66
fifo_data_t volatile *volatile write_p
Definition: fifo.h:75
fifo_size_t fifo_get_write_wrap(fifo_t *fifo)
Get number of elements for write access until pointer wrap around.
Definition: fifo.c:142
fifo_data_t volatile *volatile top
Definition: fifo.h:78
void fifo_write_busy(fifo_t *fifo, fifo_data_t data)
FIFO write access with busy waiting for free space (blocking function)
Definition: fifo.c:34
void fifo_read_bursted(fifo_t *fifo, fifo_size_t count)
FIFO read pointer correction after burst read access.
Definition: fifo.c:126
fifo_size_t fifo_get_read_wrap(fifo_t *fifo)
Get number of elements for read access until pointer wrap around.
Definition: fifo.c:153