forked from payne92/bare-metal-arm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring.c
executable file
·79 lines (64 loc) · 1.52 KB
/
ring.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
//
// ring.c -- Ring buffers
//
// Copyright (c) 2012-2013 Andrew Payne <[email protected]>
//
#include <freedom.h>
#include "common.h"
inline void buf_reset(RingBuffer *buf, int size)
{
buf->head = buf->tail = 0;
buf->size = size;
}
inline int buf_len(RingBuffer *buf)
{
int len = buf->tail - buf->head;
if (len < 0)
len += buf->size;
return len;
}
inline int buf_isfull(RingBuffer *buf)
{
return buf_len(buf) == (buf->size-1);
}
inline int buf_isempty(RingBuffer *buf)
{
return buf->head == buf->tail;
}
inline uint8_t buf_get_byte(RingBuffer *buf)
{
uint8_t item;
item = buf->data[buf->head++];
if (buf->head == buf->size) // Wrap
buf->head = 0;
return item;
}
inline void buf_put_byte(RingBuffer *buf, uint8_t val)
{
buf->data[buf->tail++] = val;
if (buf->tail == buf->size)
buf->tail = 0;
}
#ifdef OMIT
inline int buf_get(RingBuffer *buf, uint8_t *out, int maxlen)
{
int len = min(buf_len(buf), maxlen);
int chunk, rlen;
rlen = len;
while (rlen) {
chunk = buf->tail - buf->head;
if(chunk < 0)
chunk = buf->size - buf->head;
memcpy(out, buf->data + buf->head, chunk);
out += chunk;
rlen -= chunk;
buf->head += chunk;
if(buf->head == buf->size)
buf->head = 0;
}
return len;
}
inline int buf_put(RingBuffer *buf, uint8_t *in, int len)
{
}
#endif