1 /*!
2  * \file      fifo.h
3  *
4  * \brief     FIFO buffer implementation
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 Semtech
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  */
23 #include "fifo.h"
24 
FifoNext(Fifo_t * fifo,uint16_t index)25 static uint16_t FifoNext( Fifo_t *fifo, uint16_t index )
26 {
27     return ( index + 1 ) % fifo->Size;
28 }
29 
FifoInit(Fifo_t * fifo,uint8_t * buffer,uint16_t size)30 void FifoInit( Fifo_t *fifo, uint8_t *buffer, uint16_t size )
31 {
32     fifo->Begin = 0;
33     fifo->End = 0;
34     fifo->Data = buffer;
35     fifo->Size = size;
36 }
37 
FifoPush(Fifo_t * fifo,uint8_t data)38 void FifoPush( Fifo_t *fifo, uint8_t data )
39 {
40     fifo->End = FifoNext( fifo, fifo->End );
41     fifo->Data[fifo->End] = data;
42 }
43 
FifoPop(Fifo_t * fifo)44 uint8_t FifoPop( Fifo_t *fifo )
45 {
46     uint8_t data = fifo->Data[FifoNext( fifo, fifo->Begin )];
47 
48     fifo->Begin = FifoNext( fifo, fifo->Begin );
49     return data;
50 }
51 
FifoFlush(Fifo_t * fifo)52 void FifoFlush( Fifo_t *fifo )
53 {
54     fifo->Begin = 0;
55     fifo->End = 0;
56 }
57 
IsFifoEmpty(Fifo_t * fifo)58 bool IsFifoEmpty( Fifo_t *fifo )
59 {
60     return ( fifo->Begin == fifo->End );
61 }
62 
IsFifoFull(Fifo_t * fifo)63 bool IsFifoFull( Fifo_t *fifo )
64 {
65     return ( FifoNext( fifo, fifo->End ) == fifo->Begin );
66 }
67