diff options
author | Harald Welte <laforge@gnumonks.org> | 2017-02-27 13:53:17 +0100 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2017-02-27 14:24:11 +0100 |
commit | 3f5e3ddffc72484baad813628f3d18621325277a (patch) | |
tree | 136c17c1b4552a6346617ede1290e6f156ac85b9 /firmware/libcommon/source/ringbuffer.c | |
parent | 7ed6f3bc375f4eddecb7ccf10e837f3068cb4e42 (diff) |
Change directory structure to align with Atmel softpack
This way we can easily check with 'diff' for differences in our code and
Atmel softpack. Also, this layout is more suitable for building various
different firmware images (e.g. factory-test, dfu-loader, main
application) for a variety of different boards (simtrace, owhw, qmod).
Diffstat (limited to 'firmware/libcommon/source/ringbuffer.c')
-rw-r--r-- | firmware/libcommon/source/ringbuffer.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/firmware/libcommon/source/ringbuffer.c b/firmware/libcommon/source/ringbuffer.c new file mode 100644 index 0000000..71fe4ed --- /dev/null +++ b/firmware/libcommon/source/ringbuffer.c @@ -0,0 +1,70 @@ +#include "ringbuffer.h" +#include "trace.h" +#include "utils.h" + +void rbuf_reset(volatile ringbuf * rb) +{ + unsigned long state; + + local_irq_save(state); + rb->ird = 0; + rb->iwr = 0; + local_irq_restore(state); +} + +uint8_t rbuf_read(volatile ringbuf * rb) +{ + unsigned long state; + uint8_t val; + + local_irq_save(state); + val = rb->buf[rb->ird]; + rb->ird = (rb->ird + 1) % RING_BUFLEN; + local_irq_restore(state); + + return val; +} + +uint8_t rbuf_peek(volatile ringbuf * rb) +{ + return rb->buf[rb->ird]; +} + +bool rbuf_is_empty(volatile ringbuf * rb) +{ + return rb->ird == rb->iwr; +} + +static bool __rbuf_is_full(volatile ringbuf * rb) +{ + return rb->ird == (rb->iwr + 1) % RING_BUFLEN; +} + +bool rbuf_is_full(volatile ringbuf * rb) +{ + unsigned long state; + bool rc; + + local_irq_save(state); + rc = rb->ird == (rb->iwr + 1) % RING_BUFLEN; + local_irq_restore(state); + + return rc; +} + +void rbuf_write(volatile volatile ringbuf * rb, uint8_t item) +{ + unsigned long state; + + local_irq_save(state); + if (!__rbuf_is_full(rb)) { + rb->buf[rb->iwr] = item; + rb->iwr = (rb->iwr + 1) % RING_BUFLEN; + local_irq_restore(state); + } else { + local_irq_restore(state); + TRACE_ERROR("Ringbuffer full, losing bytes!"); + } +} + + |