X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fstruct%2Fkfile_fifo.c;fp=bertos%2Fstruct%2Fkfile_fifo.c;h=d09ae1e7e7662420190511420223efad298a8369;hb=034d37aa87fba63475b9c5cd6f00d800ab10ed46;hp=0000000000000000000000000000000000000000;hpb=dfbfef1a940347e6a0340e5729ac867e1711c159;p=bertos.git diff --git a/bertos/struct/kfile_fifo.c b/bertos/struct/kfile_fifo.c new file mode 100644 index 00000000..d09ae1e7 --- /dev/null +++ b/bertos/struct/kfile_fifo.c @@ -0,0 +1,76 @@ +/** + * \file + * + * + * \brief KFile interface over a FIFO buffer. + * + * \version $Id: cfg_adc.h 2348 2009-02-16 13:43:44Z duplo $ + * \author Francesco Sacchi + */ + +#include "kfile_fifo.h" +#include "fifobuf.h" + +#include + +#include + +static size_t kfilefifo_read(struct KFile *_fd, void *_buf, size_t size) +{ + KFileFifo *fd = KFILEFIFO_CAST(_fd); + uint8_t *buf = (uint8_t *)_buf; + + while (size-- && !fifo_isempty_locked(fd->fifo)) + *buf++ = fifo_pop_locked(fd->fifo); + + return (void *)buf - _buf; +} + +static size_t kfilefifo_write(struct KFile *_fd, const void *_buf, size_t size) +{ + KFileFifo *fd = KFILEFIFO_CAST(_fd); + const uint8_t *buf = (const uint8_t *)_buf; + + while (size-- && !fifo_isfull_locked(fd->fifo)) + fifo_push_locked(fd->fifo, *buf++); + + return (void *)buf - _buf; +} + +void kfilefifo_init(KFileFifo *kf, FIFOBuffer *fifo) +{ + memset(kf, 0, sizeof(*kf)); + + kf->fifo = fifo; + kf->fd.read = kfilefifo_read; + kf->fd.write = kfilefifo_write; + DB(kf->fd._type = KFT_KFILEFIFO); +}