4 * Copyright (C) 2003,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright (C) 2001 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief FIFO buffer handling routines
18 * Revision 1.2 2004/06/03 11:27:09 bernie
19 * Add dual-license information.
21 * Revision 1.1 2004/05/23 15:43:16 bernie
22 * Import mware modules.
28 #include <drv/kdebug.h>
30 void fifo_init(volatile FIFOBuffer *fb, unsigned char *buf, size_t size)
32 fb->head = fb->tail = fb->begin = buf;
33 fb->end = buf + size - 1;
38 * Pop a character from the fifo buffer.
40 * \note Calling \c fifo_push() on a full buffer is undefined.
41 * The caller must make sure the buffer has at least
42 * one free slot before calling this function.
44 * \note It is safe to call fifo_pop() and fifo_push() from
45 * concurrent contexts.
47 void fifo_push(volatile FIFOBuffer *fb, unsigned char c)
49 #pragma interrupt called
50 /* Write at tail position */
53 if (fb->tail == fb->end)
54 /* wrap tail around */
57 /* Move tail forward */
62 * Pop a character from the fifo buffer.
64 * \note Calling \c fifo_pop() on an empty buffer is undefined.
65 * The caller must make sure the buffer contains at least
66 * one character before calling this function.
68 * \note It is safe to call fifo_pop() and fifo_push() from
69 * concurrent contexts.
71 unsigned char fifo_pop(volatile FIFOBuffer *fb)
73 #pragma interrupt called
74 if (fb->head == fb->end)
76 /* wrap head around */
81 /* move head forward */
89 void fifo_pushblock(volatile FIFOBuffer *fb, unsigned char *block, size_t len)
93 /* Se c'e' spazio da tail alla fine del buffer */
94 if (fb->tail >= fb->head)
96 freelen = fb->end - fb->tail + 1;
98 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
101 /* Scrivi quello che entra fino alla fine del buffer */
102 memcpy(fb->tail, block, freelen);
105 fb->tail = fb->begin;
109 /* Scrivi tutto il blocco */
110 memcpy(fb->tail, block, len);
118 while (!(freelen = fb->head - fb->tail - 1))
119 Delay(FIFO_POLLDELAY);
121 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
124 /* Scrivi quello che entra fino alla fine del buffer */
125 memcpy(fb->tail, block, freelen);
132 /* Scrivi tutto il blocco */
133 memcpy(fb->tail, block, len);