diff --git a/sys/tsrb/tsrb.c b/sys/tsrb/tsrb.c index 90bd72e2bc..a8e594b768 100644 --- a/sys/tsrb/tsrb.c +++ b/sys/tsrb/tsrb.c @@ -19,100 +19,60 @@ #include "irq.h" #include "tsrb.h" - -static void _push(tsrb_t *rb, uint8_t c) -{ - rb->buf[rb->writes++ & (rb->size - 1)] = c; -} - -static uint8_t _pop(tsrb_t *rb) -{ - return rb->buf[rb->reads++ & (rb->size - 1)]; -} - -static uint8_t _peek(tsrb_t *rb, unsigned int idx) -{ - return rb->buf[(rb->reads + idx) & (rb->size - 1)]; -} +#include "turb.h" int tsrb_get_one(tsrb_t *rb) { - int retval = -1; unsigned irq_state = irq_disable(); - if (!tsrb_empty(rb)) { - retval = _pop(rb); - } + int retval = turb_get_one(rb); irq_restore(irq_state); return retval; } int tsrb_peek_one(tsrb_t *rb) { - int retval = -1; unsigned irq_state = irq_disable(); - if (!tsrb_empty(rb)) { - retval = _peek(rb, 0); - } + int retval = turb_peek_one(rb); irq_restore(irq_state); return retval; } int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n) { - size_t tmp = n; unsigned irq_state = irq_disable(); - while (tmp && !tsrb_empty(rb)) { - *dst++ = _pop(rb); - tmp--; - } + int cnt = turb_get(rb, dst, n); irq_restore(irq_state); - return (n - tmp); + return cnt; } int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n) { - size_t idx = 0; unsigned irq_state = irq_disable(); - unsigned int avail = tsrb_avail(rb); - while (idx < n && idx != avail) { - *dst++ = _peek(rb, idx++); - } + int idx = turb_peek(rb, dst, n); irq_restore(irq_state); return idx; } int tsrb_drop(tsrb_t *rb, size_t n) { - size_t tmp = n; unsigned irq_state = irq_disable(); - while (tmp && !tsrb_empty(rb)) { - _pop(rb); - tmp--; - } + int cnt = turb_drop(rb, n); irq_restore(irq_state); - return (n - tmp); + return cnt; } int tsrb_add_one(tsrb_t *rb, uint8_t c) { - int retval = -1; unsigned irq_state = irq_disable(); - if (!tsrb_full(rb)) { - _push(rb, c); - retval = 0; - } + int retval = turb_add_one(rb, c); irq_restore(irq_state); return retval; } int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n) { - size_t tmp = n; unsigned irq_state = irq_disable(); - while (tmp && !tsrb_full(rb)) { - _push(rb, *src++); - tmp--; - } + int cnt = turb_add(rb, src, n); irq_restore(irq_state); - return (n - tmp); + return cnt; }