KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bytefifo.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2012-2015 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : bytefifo.h
11  * Created : 10 mrt. 2015
12  * Author : Vincent van Beveren
13  */
14 #ifndef COLL_BYTEFIFO_H_
15 #define COLL_BYTEFIFO_H_
16 
17 /**
18  * @file
19  *
20  * @ingroup collections
21  *
22  * Implements a simple byte-orientated Fifo with a maximum size of 255 bytes.
23  */
24 
25 #include <stdint.h>
26 #include <stdbool.h>
27 
28 typedef struct {
29  uint8_t * const buf;
30  const uint8_t cap;
31  uint8_t len;
32  uint8_t wp;
33  uint8_t rp;
34 } ByteFifo;
35 
36 /**
37  * Initializes the ByteFifo.
38  *
39  * @param NAME The name
40  * @param CAP The maximum capacity. May not exceed 255.
41  */
42 #define BF_INIT(NAME, CAP) \
43  static uint8_t NAME ## _buf[CAP]; \
44  static ByteFifo NAME = { \
45  .buf = NAME ## _buf, \
46  .cap = CAP, \
47  .len = 0, \
48  .wp = 0, \
49  .rp = CAP - 1 \
50  };
51 
52 /**
53  * Returns whether or not the byte-fifo is full.
54  *
55  * @param bf Pointer to a ByteFifo.
56  *
57  * @retval true Its full.
58  * @retval false Its not full.
59  */
60 static inline bool bfFull(ByteFifo * const bf) {
61  return bf->len == bf->cap;
62 }
63 
64 /**
65  * Returns whether or not the byte-fifo is empty.
66  *
67  * @param bf Pointer to a ByteFifo.
68  *
69  * @retval true Its empty.
70  * @retval false Its not empty.
71  */
72 static inline bool bfEmpty(ByteFifo * const bf) {
73  return bf->len == 0;
74 }
75 
76 
77 /**
78  * Writes a byte to the byte-fifo.
79  *
80  * @param bf Pointer to a ByteFifo.
81  * @param b Byte to write.
82  *
83  * @retval true Byte was added to the Fifo
84  * @retval false Byte could not be added, fifo is full.
85  */
86 bool bfWrite(ByteFifo * const bf, uint8_t b);
87 
88 
89 /**
90  * Reads a byte from the byte-fifo.
91  *
92  * @param bf Pointer to a ByteFifo.
93  * @param b Pointer to fill.
94  *
95  * @retval true Byte was read from the fifo.
96  * @retval false Byte could not be read, fifo empty.
97  */
98 bool bfRead(ByteFifo * const bf, uint8_t * const b);
99 
100 /**
101  * Clear the fifo.
102  *
103  * @param bf Pointer to a ByteFifo.
104  *
105  */
106 static inline void bfClear(ByteFifo * const bf) {
107  bf->len = 0, bf->wp = 0, bf->rp = bf->cap - 1; \
108 }
109 
110 
111 #endif /* COLL_BYTEFIFO_H_ */
static bool bfEmpty(ByteFifo *const bf)
Returns whether or not the byte-fifo is empty.
Definition: bytefifo.h:72
bool bfWrite(ByteFifo *const bf, uint8_t b)
Writes a byte to the byte-fifo.
Definition: bytefifo.c:17
static void bfClear(ByteFifo *const bf)
Clear the fifo.
Definition: bytefifo.h:106
static bool bfFull(ByteFifo *const bf)
Returns whether or not the byte-fifo is full.
Definition: bytefifo.h:60
bool bfRead(ByteFifo *const bf, uint8_t *const b)
Reads a byte from the byte-fifo.
Definition: bytefifo.c:28