4 * Copyright (C) 2003,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright (C) 2001 Bernardo Innocenti <bernie@develer.com>
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief FIFO buffer handling routines
18 * Revision 1.1 2004/05/23 15:43:16 bernie
19 * Import mware modules.
25 #include <drv/kdebug.h>
27 void fifo_init(volatile FIFOBuffer *fb, unsigned char *buf, size_t size)
29 fb->head = fb->tail = fb->begin = buf;
30 fb->end = buf + size - 1;
35 * Pop a character from the fifo buffer.
37 * \note Calling \c fifo_push() on a full buffer is undefined.
38 * The caller must make sure the buffer has at least
39 * one free slot before calling this function.
41 * \note It is safe to call fifo_pop() and fifo_push() from
42 * concurrent contexts.
44 void fifo_push(volatile FIFOBuffer *fb, unsigned char c)
46 #pragma interrupt called
47 /* Write at tail position */
50 if (fb->tail == fb->end)
51 /* wrap tail around */
54 /* Move tail forward */
59 * Pop a character from the fifo buffer.
61 * \note Calling \c fifo_pop() on an empty buffer is undefined.
62 * The caller must make sure the buffer contains at least
63 * one character before calling this function.
65 * \note It is safe to call fifo_pop() and fifo_push() from
66 * concurrent contexts.
68 unsigned char fifo_pop(volatile FIFOBuffer *fb)
70 #pragma interrupt called
71 if (fb->head == fb->end)
73 /* wrap head around */
78 /* move head forward */
86 void fifo_pushblock(volatile FIFOBuffer *fb, unsigned char *block, size_t len)
90 /* Se c'e' spazio da tail alla fine del buffer */
91 if (fb->tail >= fb->head)
93 freelen = fb->end - fb->tail + 1;
95 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
98 /* Scrivi quello che entra fino alla fine del buffer */
99 memcpy(fb->tail, block, freelen);
102 fb->tail = fb->begin;
106 /* Scrivi tutto il blocco */
107 memcpy(fb->tail, block, len);
115 while (!(freelen = fb->head - fb->tail - 1))
116 Delay(FIFO_POLLDELAY);
118 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
121 /* Scrivi quello che entra fino alla fine del buffer */
122 memcpy(fb->tail, block, freelen);
129 /* Scrivi tutto il blocco */
130 memcpy(fb->tail, block, len);