4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 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 General pourpose FIFO buffer implemented with a ring buffer
15 * \li \c begin points to the first buffer element;
16 * \li \c end points to the last buffer element (unlike the STL convention);
17 * \li \c head points to the element to be extracted next;
18 * \li \c tail points to the location following the last insertion;
19 * \li when any of the pointers advances beyond \c end, it is reset
24 * +-----------------------------------+
25 * | empty | valid data | empty |
26 * +-----------------------------------+
32 * The buffer is EMPTY when \c head and \c tail point to the same location:
33 * \code head == tail \endcode
35 * The buffer is FULL when \c tail points to the location immediately
37 * \code tail == head - 1 \endcode
39 * The buffer is also FULL when \c tail points to the last buffer
40 * location and head points to the first one:
41 * \code head == begin && tail == end \endcode
46 *#* Revision 1.16 2004/09/06 21:39:08 bernie
47 *#* Simplify code using ATOMIC().
49 *#* Revision 1.15 2004/08/29 22:05:16 bernie
50 *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
52 *#* Revision 1.14 2004/08/25 14:12:09 rasky
53 *#* Aggiornato il comment block dei log RCS
55 *#* Revision 1.13 2004/08/24 13:16:11 bernie
56 *#* Add type-size definitions for preprocessor.
58 *#* Revision 1.12 2004/08/02 20:20:29 aleph
59 *#* Merge from project_ks
61 *#* Revision 1.11 2004/07/30 14:15:53 rasky
62 *#* Nuovo supporto unificato per detect della CPU
64 *#* Revision 1.10 2004/07/29 22:57:09 bernie
67 *#* Revision 1.9 2004/07/20 23:54:27 bernie
68 *#* fifo_flush_locked(): New function;
69 *#* Revamp documentation.
71 *#* Revision 1.8 2004/07/20 23:47:39 bernie
72 *#* Finally remove redundant protos.
74 *#* Revision 1.7 2004/07/20 23:46:29 bernie
75 *#* Finally remove redundant protos.
77 *#* Revision 1.6 2004/06/06 17:18:04 bernie
78 *#* Remove redundant declaration of fifo_isempty_locked().
80 *#* Revision 1.5 2004/06/06 16:50:35 bernie
81 *#* Import fixes for race conditions from project_ks.
83 *#* Revision 1.4 2004/06/06 16:11:17 bernie
84 *#* Protect MetroWerks specific pragmas with #ifdef's
91 #include <drv/kdebug.h>
93 typedef struct FIFOBuffer
95 unsigned char * volatile head;
96 unsigned char * volatile tail;
102 #define ASSERT_VALID_FIFO(fifo) \
104 ASSERT((fifo)->head >= (fifo)->begin); \
105 ASSERT((fifo)->head <= (fifo)->end); \
106 ASSERT((fifo)->tail >= (fifo)->begin); \
107 ASSERT((fifo)->tail <= (fifo)->end); \
112 * Check whether the fifo is empty
114 * \note Calling fifo_isempty() is safe while a concurrent
115 * execution context is calling fifo_push() or fifo_pop()
116 * only if the CPU can atomically update a pointer
117 * (which the AVR and other 8-bit processors can't do).
119 * \sa fifo_isempty_locked
121 INLINE bool fifo_isempty(const FIFOBuffer *fb)
123 //ASSERT_VALID_FIFO(fb);
124 return fb->head == fb->tail;
129 * Check whether the fifo is full
131 * \note Calling fifo_isfull() is safe while a concurrent
132 * execution context is calling fifo_pop() and the
133 * CPU can update a pointer atomically.
134 * It is NOT safe when the other context calls
136 * This limitation is not usually problematic in a
137 * consumer/producer scenario because the
138 * fifo_isfull() and fifo_push() are usually called
139 * in the producer context.
141 INLINE bool fifo_isfull(const FIFOBuffer *fb)
143 //ASSERT_VALID_FIFO(fb);
145 ((fb->head == fb->begin) && (fb->tail == fb->end))
146 || (fb->tail == fb->head - 1);
151 * Pop a character from the fifo buffer.
153 * \note Calling \c fifo_push() on a full buffer is undefined.
154 * The caller must make sure the buffer has at least
155 * one free slot before calling this function.
157 * \note It is safe to call fifo_pop() and fifo_push() from
158 * concurrent contexts, unless the CPU can't update
159 * a pointer atomically (which the AVR and other 8-bit
160 * processors can't do).
162 * \sa fifo_push_locked
164 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
167 #pragma interrupt called
169 //ASSERT_VALID_FIFO(fb);
171 /* Write at tail position */
174 if (UNLIKELY(fb->tail == fb->end))
175 /* wrap tail around */
176 fb->tail = fb->begin;
178 /* Move tail forward */
184 * Pop a character from the fifo buffer.
186 * \note Calling \c fifo_pop() on an empty buffer is undefined.
187 * The caller must make sure the buffer contains at least
188 * one character before calling this function.
190 * \note It is safe to call fifo_pop() and fifo_push() from
191 * concurrent contexts.
193 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
196 #pragma interrupt called
198 //ASSERT_VALID_FIFO(fb);
200 if (UNLIKELY(fb->head == fb->end))
202 /* wrap head around */
203 fb->head = fb->begin;
207 /* move head forward */
208 return *(fb->head++);
213 * Make the fifo empty, discarding all its current contents.
215 INLINE void fifo_flush(FIFOBuffer *fb)
217 //ASSERT_VALID_FIFO(fb);
222 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
225 * 16/32bit CPUs that can update a pointer with a single write
226 * operation, no need to disable interrupts.
228 #define fifo_isempty_locked(fb) fifo_isempty((fb))
229 #define fifo_push_locked(fb, c) fifo_push((fb), (c))
230 #define fifo_pop_locked(fb) fifo_pop((fb))
231 #define fifo_flush_locked(fb) fifo_flush((fb))
233 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
236 * Similar to fifo_isempty(), but with stronger guarantees for
237 * concurrent access between user and interrupt code.
239 * \note This is actually only needed for 8-bit processors.
243 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
246 ATOMIC(result = fifo_isempty(fb));
252 * Similar to fifo_push(), but with stronger guarantees for
253 * concurrent access between user and interrupt code.
255 * \note This is actually only needed for 8-bit processors.
259 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
261 ATOMIC(fifo_push(fb, c));
264 /* Probably not really needed, but hard to prove. */
265 INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
268 ATOMIC(c = fifo_pop(fb));
273 * Similar to fifo_flush(), but with stronger guarantees for
274 * concurrent access between user and interrupt code.
276 * \note This is actually only needed for 8-bit processors.
280 INLINE void fifo_flush_locked(FIFOBuffer *fb)
282 ATOMIC(fifo_flush(fb));
285 #endif /* CPU_REG_BITS < BITS_PER_PTR */
289 * Thread safe version of fifo_isfull()
291 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
294 ATOMIC(result = fifo_isfull(_fb));
300 * FIFO Initialization.
302 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
304 fb->head = fb->tail = fb->begin = buf;
305 fb->end = buf + size - 1;
312 * UNTESTED: if uncommented, to be moved in fifobuf.c
314 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
318 /* Se c'e' spazio da tail alla fine del buffer */
319 if (fb->tail >= fb->head)
321 freelen = fb->end - fb->tail + 1;
323 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
326 /* Scrivi quello che entra fino alla fine del buffer */
327 memcpy(fb->tail, block, freelen);
330 fb->tail = fb->begin;
334 /* Scrivi tutto il blocco */
335 memcpy(fb->tail, block, len);
343 while (!(freelen = fb->head - fb->tail - 1))
344 Delay(FIFO_POLLDELAY);
346 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
349 /* Scrivi quello che entra fino alla fine del buffer */
350 memcpy(fb->tail, block, freelen);
357 /* Scrivi tutto il blocco */
358 memcpy(fb->tail, block, len);
366 #endif /* MWARE_FIFO_H */