source: trunk/fw_g473rct/SES/src/ring.c@ 70

Last change on this file since 70 was 69, checked in by f.jahn, 4 weeks ago

Started implementing Modbus on USB CDC channel.

File size: 1.6 KB
Line 
1#include "ring.h"
2
3//------------------------------------------------------------------------------
4
5void rb_init(ring_buffer_t* rb)
6{
7 rb->head = 0;
8 rb->tail = 0;
9}
10
11//------------------------------------------------------------------------------
12
13bool rb_push(ring_buffer_t* rb, uint8_t data)
14{
15 uint16_t next = (rb->head + 1) & (RB_SIZE - 1);
16
17 if (next == rb->tail)
18 return false; // buffer full
19
20 rb->buffer[rb->head] = data;
21 rb->head = next;
22 return true;
23}
24
25//------------------------------------------------------------------------------
26
27bool rb_pop(ring_buffer_t* rb, uint8_t* data)
28{
29 if (rb->head == rb->tail)
30 return false; // empty
31
32 *data = rb->buffer[rb->tail];
33 rb->tail = (rb->tail + 1) & (RB_SIZE - 1);
34 return true;
35}
36
37//------------------------------------------------------------------------------
38
39uint16_t rb_available(const ring_buffer_t* rb)
40{
41 return (rb->head - rb->tail) & (RB_SIZE - 1);
42}
43
44//------------------------------------------------------------------------------
45
46bool rb_peek(const ring_buffer_t* const rb, uint8_t* const data)
47{
48 if (rb->head == rb->tail)
49 return false; // empty
50
51 *data = rb->buffer[rb->tail];
52 return true;
53}
54
55//------------------------------------------------------------------------------
56
57bool rb_peek_at(const ring_buffer_t* const rb, uint16_t index, uint8_t* data)
58{
59 if (index >= rb_available(rb))
60 return false; // not enough data
61
62 uint16_t pos = (rb->tail + index) & (RB_SIZE - 1);
63 *data = rb->buffer[pos];
64
65 return true;
66}
Note: See TracBrowser for help on using the repository browser.