aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/ring_buffer.h
blob: 43e0b286663926cc86dfe6bd96aaf744034d3d83 (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
/**
 *  @brief simple circular buffer
 */

#ifndef _RING_BUFFER_H_
#define _RING_BUFFER_H_

#ifdef __cplusplus
extern "C"{
#endif

/* The buffer is empty when head == tail.
 * The buffer is full when the head is one byte in front of the tail
 * The total buffer size must be a power of two
 * One byte is left free to distinguish empty from full */
typedef struct ring_buffer {
    /** Buffer items are stored into */
    volatile uint8 *buf;
    /** Index of the next item to remove */
    uint16 head;
    /** Index where the next item will get inserted */
    uint16 tail;
    /** Buffer capacity minus one */
    uint16 size;
} ring_buffer;

/** Initialise a ring buffer.
 *
 *  @param rb    instance to initialise
 *  @param size  number of items in the buffer
 *  @param buf   buffer to store items into
*/
static inline void rb_init(ring_buffer *rb, uint16 size, uint8 *buf) {
    rb->head = 0;
    rb->tail = 0;
    rb->size = size - 1;
    rb->buf = buf;
}

/** Append an item onto the end of the ring buffer */
static inline void rb_insert(ring_buffer *rb, uint8 element) {
    rb->buf[rb->tail] = element;
    rb->tail = (rb->tail == rb->size) ? 0 : rb->tail + 1;
}

/** Remove and return the first item from the ring buffer */
static inline uint8 rb_remove(ring_buffer *rb) {
    uint8 ch = rb->buf[rb->head];
    rb->head = (rb->head == rb->size) ? 0 : rb->head + 1;
    return ch;
}

static inline uint32 rb_full_count(ring_buffer *rb) {
    /* PENDING: Broken */
    volatile ring_buffer *arb = rb;
    return arb->tail - arb->head;
}

/** Discard all items from the buffer */
static inline void  rb_reset(ring_buffer *rb) {
    rb->tail = rb->head;
}

#ifdef __cplusplus
} // extern "C"
#endif



#endif