blob: db9401a279229fd6d5148601ffe53e214ffc8f5d (
plain)
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
|
#include "project.h"
static inline size_t
ring_next (volatile ring_t * r, size_t p)
{
p++;
if (p >= r->size)
p -= r->size;
return p;
}
void
ring_init (volatile ring_t * r, uint8_t * buf, size_t len)
{
r->data = buf;
r->size = len;
r->write = 0;
r->read = 0;
}
int
ring_write_byte (volatile ring_t * r, uint8_t c)
{
size_t n = ring_next (r, r->write);
if (n == r->read)
return -EAGAIN;
r->data[r->write] = c;
r->write = n;
return 0;
}
int
ring_read_byte (volatile ring_t * r, uint8_t * c)
{
size_t n = ring_next (r, r->read);
if (r->read == r->write)
return -EAGAIN;
*c = r->data[r->read];
r->read = n;
return 0;
}
int
ring_write (volatile ring_t * r, uint8_t * buf, size_t len, int blocking)
{
while (len--)
{
if (blocking)
{
while (ring_write_byte (r, *buf));
buf++;
}
else
{
if (ring_write_byte (r, *(buf++)))
return -EAGAIN;
}
}
return 0;
}
int
ring_empty (volatile ring_t * r)
{
return (r->read == r->write) ? 1 : 0;
}
|