-
Notifications
You must be signed in to change notification settings - Fork 0
/
clocked_read.c
97 lines (82 loc) · 2.25 KB
/
clocked_read.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
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
#include "hardware/dma.h"
#include "clocked_read.h"
#include "clocked_read.pio.h"
#include "base.h"
#include "pin_config.h"
static uint8_t buffer[CLOCKED_READ_BUFFER_COUNT][MAX_DATA_BUFFER_SIZE];
static unsigned buffer_size[CLOCKED_READ_BUFFER_COUNT];
static enum buffer_status buffer_status[CLOCKED_READ_BUFFER_COUNT];
static char buffer_filename[CLOCKED_READ_BUFFER_COUNT][CLOCKED_READ_BUFFER_FILENAME_MAX_LENGTH];
static void check_buffer_index(unsigned buffer_index)
{
if (buffer_index >= CLOCKED_READ_BUFFER_COUNT) PANIC(PANIC_BOUNDS_CHECK_FAILED);
}
static int find_buffer_index(enum buffer_status with_buffer_status)
{
for (unsigned i = 0; i < CLOCKED_READ_BUFFER_COUNT; i++) {
if (buffer_status[i] == with_buffer_status) {
return i;
}
}
return -1;
}
static int get_next_free_buffer_index(void)
{
return find_buffer_index(FREE);
}
int get_written_buffer_index(void)
{
return find_buffer_index(WRITTEN);
}
unsigned can_allocate_buffer(void)
{
return get_next_free_buffer_index() >= 0;
}
unsigned allocate_buffer(unsigned size)
{
const int i = get_next_free_buffer_index();
if (i < 0) PANIC(PANIC_ALLOCATION_ERROR);
check_buffer_index(i);
buffer_status[i] = BUSY;
if (size > MAX_DATA_BUFFER_SIZE) size = MAX_DATA_BUFFER_SIZE;
buffer_size[i] = size;
return i;
}
uint8_t* get_buffer_data(unsigned buffer_index)
{
check_buffer_index(buffer_index);
return buffer[buffer_index];
}
char* get_buffer_filename(unsigned buffer_index)
{
check_buffer_index(buffer_index);
return buffer_filename[buffer_index];
}
enum buffer_status get_buffer_status(unsigned buffer_index)
{
check_buffer_index(buffer_index);
return buffer_status[buffer_index];
}
void release_buffer(unsigned buffer_index)
{
check_buffer_index(buffer_index);
if (buffer_status[buffer_index] != WRITTEN) PANIC(PANIC_UNEXPECTED_STATE);
buffer_status[buffer_index] = FREE;
}
void wrote_buffer(unsigned buffer_index)
{
check_buffer_index(buffer_index);
if (buffer_status[buffer_index] != BUSY) PANIC(PANIC_UNEXPECTED_STATE);
buffer_status[buffer_index] = WRITTEN;
}
unsigned get_buffer_size(unsigned buffer_index)
{
check_buffer_index(buffer_index);
return buffer_size[buffer_index];
}
void reset_buffers(void)
{
for (int i = 0; i < CLOCKED_READ_BUFFER_COUNT; i++) {
buffer_status[i] = FREE;
}
}