FIFO Library
Software FIFO
 All Data Structures Functions Variables Typedefs Groups Pages
/*****************************************************************************
Software FIFO using circular buffer
structs and generic functions to increase usability
Copyright (C) 2015 Falk Brunner
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
You can contact the author at Falk.Brunner@gmx.de
*****************************************************************************/
#ifndef _FIFO_H
#define _FIFO_H
typedef uint8_t fifo_data_t;
typedef uint16_t fifo_size_t;
typedef struct {
fifo_data_t volatile * volatile write_p;
fifo_data_t volatile * volatile read_p;
fifo_data_t volatile * volatile base;
fifo_data_t volatile * volatile top;
fifo_size_t volatile size;
//-FUNCTIONS---------------------------------------------------------------------------------------------------------
void fifo_init(fifo_t *fifo, fifo_data_t *data, fifo_size_t size);
void fifo_write(fifo_t *fifo, fifo_data_t data);
//-ISR FUNCTIONS---------------------------------------------------------------------------------------------------------
inline void fifo_write_ISR(fifo_t *fifo, fifo_data_t data);
//- INLINE FUNCTIONS ------------------------------------------------------------------------------------------------------------------
fifo_data_t data;
fifo_data_t *tmp;
tmp = (fifo_data_t*)fifo->read_p;
data = *tmp++;
if (tmp > fifo->top) {
fifo->read_p = fifo->base;
} else {
fifo->read_p = tmp;
}
return data;
}
inline void fifo_write_ISR(fifo_t *fifo, fifo_data_t data) {
fifo_data_t *tmp;
tmp = (fifo_data_t*)fifo->write_p;
*tmp++ = data;
if (tmp > fifo->top) {
fifo->write_p = fifo->base;
} else {
fifo->write_p = tmp;
}
}
fifo_data_t *tmp_w, *tmp_r;
tmp_w = (fifo_data_t*)fifo->write_p;
tmp_r = (fifo_data_t*)fifo->read_p;
tmp = tmp_w - tmp_r;
if (tmp_w < tmp_r) {
tmp += fifo->size;
}
return tmp;
}
fifo_data_t *tmp_w, *tmp_r;
tmp_w = (fifo_data_t*)fifo->write_p;
tmp_r = (fifo_data_t*)fifo->read_p;
tmp = tmp_w - tmp_r;
if (tmp_w < tmp_r) {
tmp += fifo->size;
}
return (fifo->size - tmp - 1);
}
#endif