Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* Ring buffer |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 16 | */ |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 17 | #include "ringbuffer.h" |
| 18 | #include "trace.h" |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 19 | #include "utils.h" |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 20 | |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 21 | /* WARNING: Since console output is internally using this ringbuffer to implement |
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 22 | * buffered writes, we cannot use any TRACE_*() or printf() style functions here, |
| 23 | * as it would create infinite recursion! */ |
| 24 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 25 | void rbuf_reset(volatile ringbuf * rb) |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 26 | { |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 27 | unsigned long state; |
| 28 | |
| 29 | local_irq_save(state); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 30 | rb->ird = 0; |
| 31 | rb->iwr = 0; |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 32 | local_irq_restore(state); |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 35 | uint8_t rbuf_read(volatile ringbuf * rb) |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 36 | { |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 37 | unsigned long state; |
| 38 | uint8_t val; |
| 39 | |
| 40 | local_irq_save(state); |
| 41 | val = rb->buf[rb->ird]; |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 42 | rb->ird = (rb->ird + 1) % RING_BUFLEN; |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 43 | local_irq_restore(state); |
| 44 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 45 | return val; |
Christina Quast | 542f918 | 2015-04-11 09:31:15 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 48 | uint8_t rbuf_peek(volatile ringbuf * rb) |
Christina Quast | 7a7f98c | 2015-05-02 14:10:13 +0200 | [diff] [blame] | 49 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 50 | return rb->buf[rb->ird]; |
Christina Quast | 7a7f98c | 2015-05-02 14:10:13 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 53 | bool rbuf_is_empty(volatile ringbuf * rb) |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 54 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 55 | return rb->ird == rb->iwr; |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 58 | static bool __rbuf_is_full(volatile ringbuf * rb) |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 59 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 60 | return rb->ird == (rb->iwr + 1) % RING_BUFLEN; |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 61 | } |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 62 | |
| 63 | bool rbuf_is_full(volatile ringbuf * rb) |
| 64 | { |
| 65 | unsigned long state; |
| 66 | bool rc; |
| 67 | |
| 68 | local_irq_save(state); |
| 69 | rc = rb->ird == (rb->iwr + 1) % RING_BUFLEN; |
| 70 | local_irq_restore(state); |
| 71 | |
| 72 | return rc; |
| 73 | } |
| 74 | |
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 75 | int rbuf_write(volatile ringbuf * rb, uint8_t item) |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 76 | { |
| 77 | unsigned long state; |
| 78 | |
| 79 | local_irq_save(state); |
| 80 | if (!__rbuf_is_full(rb)) { |
| 81 | rb->buf[rb->iwr] = item; |
| 82 | rb->iwr = (rb->iwr + 1) % RING_BUFLEN; |
| 83 | local_irq_restore(state); |
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 84 | return 0; |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 85 | } else { |
| 86 | local_irq_restore(state); |
Harald Welte | 05cc7f6 | 2018-07-04 04:39:40 +0200 | [diff] [blame] | 87 | return -1; |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | |