1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-16 18:13:49 +01:00

sys/tsrb: use turb.h for c-file implemetation

This commit is contained in:
Karl Fessel 2025-10-12 21:45:19 +02:00
parent 40cca9246d
commit 0099b623d9

View File

@ -19,100 +19,60 @@
#include "irq.h" #include "irq.h"
#include "tsrb.h" #include "tsrb.h"
#include "turb.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)];
}
int tsrb_get_one(tsrb_t *rb) int tsrb_get_one(tsrb_t *rb)
{ {
int retval = -1;
unsigned irq_state = irq_disable(); unsigned irq_state = irq_disable();
if (!tsrb_empty(rb)) { int retval = turb_get_one(rb);
retval = _pop(rb);
}
irq_restore(irq_state); irq_restore(irq_state);
return retval; return retval;
} }
int tsrb_peek_one(tsrb_t *rb) int tsrb_peek_one(tsrb_t *rb)
{ {
int retval = -1;
unsigned irq_state = irq_disable(); unsigned irq_state = irq_disable();
if (!tsrb_empty(rb)) { int retval = turb_peek_one(rb);
retval = _peek(rb, 0);
}
irq_restore(irq_state); irq_restore(irq_state);
return retval; return retval;
} }
int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n) int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n)
{ {
size_t tmp = n;
unsigned irq_state = irq_disable(); unsigned irq_state = irq_disable();
while (tmp && !tsrb_empty(rb)) { int cnt = turb_get(rb, dst, n);
*dst++ = _pop(rb);
tmp--;
}
irq_restore(irq_state); irq_restore(irq_state);
return (n - tmp); return cnt;
} }
int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n) int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n)
{ {
size_t idx = 0;
unsigned irq_state = irq_disable(); unsigned irq_state = irq_disable();
unsigned int avail = tsrb_avail(rb); int idx = turb_peek(rb, dst, n);
while (idx < n && idx != avail) {
*dst++ = _peek(rb, idx++);
}
irq_restore(irq_state); irq_restore(irq_state);
return idx; return idx;
} }
int tsrb_drop(tsrb_t *rb, size_t n) int tsrb_drop(tsrb_t *rb, size_t n)
{ {
size_t tmp = n;
unsigned irq_state = irq_disable(); unsigned irq_state = irq_disable();
while (tmp && !tsrb_empty(rb)) { int cnt = turb_drop(rb, n);
_pop(rb);
tmp--;
}
irq_restore(irq_state); irq_restore(irq_state);
return (n - tmp); return cnt;
} }
int tsrb_add_one(tsrb_t *rb, uint8_t c) int tsrb_add_one(tsrb_t *rb, uint8_t c)
{ {
int retval = -1;
unsigned irq_state = irq_disable(); unsigned irq_state = irq_disable();
if (!tsrb_full(rb)) { int retval = turb_add_one(rb, c);
_push(rb, c);
retval = 0;
}
irq_restore(irq_state); irq_restore(irq_state);
return retval; return retval;
} }
int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n) int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n)
{ {
size_t tmp = n;
unsigned irq_state = irq_disable(); unsigned irq_state = irq_disable();
while (tmp && !tsrb_full(rb)) { int cnt = turb_add(rb, src, n);
_push(rb, *src++);
tmp--;
}
irq_restore(irq_state); irq_restore(irq_state);
return (n - tmp); return cnt;
} }