-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfifo.h
More file actions
107 lines (67 loc) · 1.81 KB
/
fifo.h
File metadata and controls
107 lines (67 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef __FIFO_H
#define __FIFO_H
#include <stdint.h>
#include <stdbool.h>
#define IRQ_DISABLE()
#define IRQ_ENABLE()
typedef struct {
uint32_t first; // erstes Item
uint32_t count; // aktuelle Anzahl in FIFO
uint32_t len; // max. Anzahl in FIFO
char *buf; // Buffer
} FIFO;
void fifo_init(FIFO *fifo, uint32_t len, char *buf, uint32_t item_size);
// fifo_push
// returns: true when item pushed into fifo
// false when not pushed because fifo is full
bool fifo_push(FIFO *fifo, const char item);
// fifo_pop
// returns: true when an item was popped out of the fifo
// false when no item was popped out of the fifo because it was empty
bool fifo_pop(FIFO *fifo, char *item);
// fifo_is_empty:
// returns: true when fifo is empty, otherwise false
bool fifo_is_empty(const FIFO *fifo);
// fifo_is_full:
// returns: true when fifo is full
bool fifo_is_full(const FIFO *fifo);
bool fifo_push(FIFO *fifo, const char item) {
IRQ_DISABLE();
bool res = false;
if(fifo->count < fifo->len) {
res = true;
uint32_t idx = (fifo->first + fifo->count) % fifo->len;
fifo->buf[idx] = item;
++fifo->count;
}
IRQ_ENABLE();
return res;
}
// Erste Nachricht aus FIFO entnehmen
bool fifo_pop(FIFO *fifo, char *item) {
IRQ_DISABLE();
bool res = false;
if(fifo->count > 0) {
res = true;
*item = fifo->buf[fifo->first];
--fifo->count;
fifo->first = (fifo->first + 1) % fifo->len;
}
IRQ_ENABLE();
return res;
}
bool fifo_is_empty(const FIFO *fifo) {
bool empty;
IRQ_DISABLE();
empty = fifo->count == 0;
IRQ_ENABLE();
return empty;
}
bool fifo_is_full(const FIFO *fifo) {
bool full;
IRQ_DISABLE();
full = fifo->count == fifo->len;
IRQ_ENABLE();
return full;
}
#endif