-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathram_fifo.c
52 lines (40 loc) · 985 Bytes
/
ram_fifo.c
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
/**
* FIFO in RAM.
* (C) Juan Schiavoni 2021
*/
#include <stdlib.h>
#include "ram_fifo.h"
static uint32_t *capture_buf = NULL;
static uint32_t capture_set = 0;
static uint32_t capture_get = 0;
static uint32_t capture_count = 0;
static uint32_t capture_size = 0;
bool ram_fifo_init(size_t size) {
capture_buf = malloc(size * sizeof(uint32_t));
capture_size = size;
capture_set = 0;
capture_get = 0;
capture_count = 0;
return (capture_buf != NULL);
}
bool ram_fifo_is_empty(void){
return (capture_count == 0);
}
bool ram_fifo_set(uint32_t item) {
if (capture_set >= capture_size){
capture_set = 0;
}
if (capture_count < capture_size) {
capture_buf[capture_set++] = item;
capture_count++;
return true;
}
return false;
}
uint32_t ram_fifo_get(void) {
if (capture_get >= capture_size){
capture_get = 0;
}
capture_count--;
return capture_buf[capture_get++];
}