MCU-Common
Modules useful for embedded (MCU) programming
fifo.h
Go to the documentation of this file.
1 /*
2  * This file is part of MCU-Common.
3  *
4  * Copyright (C) 2017 Adam Heinrich <adam@adamh.cz>
5  *
6  * MCU-Common is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * MCU-Common is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MCU-Common. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  */
31 
32 #ifndef MCU_COMMON_FIFO_H
33 #define MCU_COMMON_FIFO_H
34 
35 #include <stddef.h>
36 #include <stdbool.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
52 #define FIFO_INIT(fifo, elem_size, fifo_capacity) \
53  do { \
54  static char buffer[(elem_size)*((fifo_capacity)+1)]; \
55  (fifo)->buffer = buffer; \
56  (fifo)->element_size = (elem_size); \
57  (fifo)->buffer_capacity = (fifo_capacity)+1; \
58  fifo_init(fifo); \
59  } while (0)
60 
61 
63 struct fifo {
66  void *buffer;
68  size_t element_size;
74  volatile size_t tail;
76  volatile size_t head;
77 };
78 
79 bool fifo_init(struct fifo *fifo);
80 
81 size_t fifo_capacity(const struct fifo *fifo);
82 size_t fifo_readable(const struct fifo *fifo);
83 size_t fifo_writable(const struct fifo *fifo);
84 
85 size_t fifo_read(struct fifo *fifo, void *dst, size_t count);
86 size_t fifo_write(struct fifo *fifo, const void *src, size_t count);
87 
88 size_t fifo_gets(struct fifo *fifo, char *str);
89 size_t fifo_puts(struct fifo *fifo, const char *str);
90 
93 #ifdef __cplusplus
94 }
95 #endif
96 
97 #endif /* MCU_COMMON_FIFO_H */
size_t fifo_read(struct fifo *fifo, void *dst, size_t count)
Reads data from FIFO.
Definition: fifo.c:145
size_t fifo_readable(const struct fifo *fifo)
Returns number of elements which can be read from the FIFO.
Definition: fifo.c:101
size_t fifo_write(struct fifo *fifo, const void *src, size_t count)
Writes data to FIFO.
Definition: fifo.c:183
size_t fifo_gets(struct fifo *fifo, char *str)
Reads null-terminated string from FIFO.
Definition: fifo.c:223
size_t fifo_writable(const struct fifo *fifo)
Returns number of elements which can be written to the FIFO.
Definition: fifo.c:123
bool fifo_init(struct fifo *fifo)
Initializes FIFO.
Definition: fifo.c:67
size_t fifo_capacity(const struct fifo *fifo)
Returns number of elements the FIFO can hold.
Definition: fifo.c:87
size_t fifo_puts(struct fifo *fifo, const char *str)
Writes null-terminated string to FIFO.
Definition: fifo.c:259
FIFO instance.
Definition: fifo.h:63
size_t element_size
Size of a single element.
Definition: fifo.h:68
void * buffer
Pointer to the buffer holding FIFO elements.
Definition: fifo.h:66
volatile size_t tail
Read index (handled internally)
Definition: fifo.h:74
volatile size_t head
Write index (handled internally)
Definition: fifo.h:76
size_t buffer_capacity
Number of elements the buffer can hold (The actual capacity of the FIFO is buffer_capacity-1 as one e...
Definition: fifo.h:72