Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 1 | #include "ringbuffer.h" |
2 | #include "trace.h" | ||||
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 3 | #include "utils.h" |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 4 | |
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 5 | /* WARNINGI: Since console output is internally using this ringbuffer to implement |
6 | * buffered writes, we cannot use any TRACE_*() or printf() style functions here, | ||||
7 | * as it would create infinite recursion! */ | ||||
8 | |||||
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 9 | void rbuf_reset(volatile ringbuf * rb) |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 10 | { |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 11 | unsigned long state; |
12 | |||||
13 | local_irq_save(state); | ||||
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 14 | rb->ird = 0; |
15 | rb->iwr = 0; | ||||
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 16 | local_irq_restore(state); |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 17 | } |
18 | |||||
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 19 | uint8_t rbuf_read(volatile ringbuf * rb) |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 20 | { |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 21 | unsigned long state; |
22 | uint8_t val; | ||||
23 | |||||
24 | local_irq_save(state); | ||||
25 | val = rb->buf[rb->ird]; | ||||
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 26 | rb->ird = (rb->ird + 1) % RING_BUFLEN; |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 27 | local_irq_restore(state); |
28 | |||||
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 29 | return val; |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 30 | } |
31 | |||||
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 32 | uint8_t rbuf_peek(volatile ringbuf * rb) |
Christina Quast | 7a7f98c | 2015-05-02 14:10:13 +0200 | [diff] [blame] | 33 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 34 | return rb->buf[rb->ird]; |
Christina Quast | 7a7f98c | 2015-05-02 14:10:13 +0200 | [diff] [blame] | 35 | } |
36 | |||||
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 37 | bool rbuf_is_empty(volatile ringbuf * rb) |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 38 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 39 | return rb->ird == rb->iwr; |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 40 | } |
41 | |||||
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 42 | static bool __rbuf_is_full(volatile ringbuf * rb) |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 43 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 44 | return rb->ird == (rb->iwr + 1) % RING_BUFLEN; |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 45 | } |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 46 | |
47 | bool rbuf_is_full(volatile ringbuf * rb) | ||||
48 | { | ||||
49 | unsigned long state; | ||||
50 | bool rc; | ||||
51 | |||||
52 | local_irq_save(state); | ||||
53 | rc = rb->ird == (rb->iwr + 1) % RING_BUFLEN; | ||||
54 | local_irq_restore(state); | ||||
55 | |||||
56 | return rc; | ||||
57 | } | ||||
58 | |||||
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 59 | int rbuf_write(volatile ringbuf * rb, uint8_t item) |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 60 | { |
61 | unsigned long state; | ||||
62 | |||||
63 | local_irq_save(state); | ||||
64 | if (!__rbuf_is_full(rb)) { | ||||
65 | rb->buf[rb->iwr] = item; | ||||
66 | rb->iwr = (rb->iwr + 1) % RING_BUFLEN; | ||||
67 | local_irq_restore(state); | ||||
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 68 | return 0; |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 69 | } else { |
70 | local_irq_restore(state); | ||||
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 71 | return -1; |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 72 | } |
73 | } | ||||
74 | |||||
75 |