4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
33 * \brief General pourpose FIFO buffer implemented with a ring buffer
35 * \li \c begin points to the first buffer element;
36 * \li \c end points to the last buffer element (unlike the STL convention);
37 * \li \c head points to the element to be extracted next;
38 * \li \c tail points to the location following the last insertion;
39 * \li when any of the pointers advances beyond \c end, it is reset
44 * +-----------------------------------+
45 * | empty | valid data | empty |
46 * +-----------------------------------+
52 * The buffer is EMPTY when \c head and \c tail point to the same location:
53 * \code head == tail \endcode
55 * The buffer is FULL when \c tail points to the location immediately
57 * \code tail == head - 1 \endcode
59 * The buffer is also FULL when \c tail points to the last buffer
60 * location and head points to the first one:
61 * \code head == begin && tail == end \endcode
63 * \author Bernie Innocenti <bernie@codewiz.org>
69 #include <cpu/types.h>
71 #include <cfg/debug.h>
73 typedef struct FIFOBuffer
75 unsigned char * volatile head;
76 unsigned char * volatile tail;
82 #define ASSERT_VALID_FIFO(fifo) \
84 ASSERT((fifo)->head >= (fifo)->begin); \
85 ASSERT((fifo)->head <= (fifo)->end); \
86 ASSERT((fifo)->tail >= (fifo)->begin); \
87 ASSERT((fifo)->tail <= (fifo)->end); \
92 * Check whether the fifo is empty
94 * \note Calling fifo_isempty() is safe while a concurrent
95 * execution context is calling fifo_push() or fifo_pop()
96 * only if the CPU can atomically update a pointer
97 * (which the AVR and other 8-bit processors can't do).
99 * \sa fifo_isempty_locked
101 INLINE bool fifo_isempty(const FIFOBuffer *fb)
103 //ASSERT_VALID_FIFO(fb);
104 return fb->head == fb->tail;
109 * Check whether the fifo is full
111 * \note Calling fifo_isfull() is safe while a concurrent
112 * execution context is calling fifo_pop() and the
113 * CPU can update a pointer atomically.
114 * It is NOT safe when the other context calls
116 * This limitation is not usually problematic in a
117 * consumer/producer scenario because the
118 * fifo_isfull() and fifo_push() are usually called
119 * in the producer context.
121 INLINE bool fifo_isfull(const FIFOBuffer *fb)
123 //ASSERT_VALID_FIFO(fb);
125 ((fb->head == fb->begin) && (fb->tail == fb->end))
126 || (fb->tail == fb->head - 1);
131 * Push a character on the fifo buffer.
133 * \note Calling \c fifo_push() on a full buffer is undefined.
134 * The caller must make sure the buffer has at least
135 * one free slot before calling this function.
137 * \note It is safe to call fifo_pop() and fifo_push() from
138 * concurrent contexts, unless the CPU can't update
139 * a pointer atomically (which the AVR and other 8-bit
140 * processors can't do).
142 * \sa fifo_push_locked
144 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
147 #pragma interrupt called
149 //ASSERT_VALID_FIFO(fb);
151 /* Write at tail position */
154 if (UNLIKELY(fb->tail == fb->end))
155 /* wrap tail around */
156 fb->tail = fb->begin;
158 /* Move tail forward */
164 * Pop a character from the fifo buffer.
166 * \note Calling \c fifo_pop() on an empty buffer is undefined.
167 * The caller must make sure the buffer contains at least
168 * one character before calling this function.
170 * \note It is safe to call fifo_pop() and fifo_push() from
171 * concurrent contexts.
173 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
176 #pragma interrupt called
178 //ASSERT_VALID_FIFO(fb);
180 if (UNLIKELY(fb->head == fb->end))
182 /* wrap head around */
183 fb->head = fb->begin;
187 /* move head forward */
188 return *(fb->head++);
193 * Make the fifo empty, discarding all its current contents.
195 INLINE void fifo_flush(FIFOBuffer *fb)
197 //ASSERT_VALID_FIFO(fb);
202 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
205 * 16/32bit CPUs that can update a pointer with a single write
206 * operation, no need to disable interrupts.
208 #define fifo_isempty_locked(fb) fifo_isempty((fb))
209 #define fifo_push_locked(fb, c) fifo_push((fb), (c))
210 #define fifo_pop_locked(fb) fifo_pop((fb))
211 #define fifo_flush_locked(fb) fifo_flush((fb))
213 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
216 * Similar to fifo_isempty(), but with stronger guarantees for
217 * concurrent access between user and interrupt code.
219 * \note This is actually only needed for 8-bit processors.
223 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
226 ATOMIC(result = fifo_isempty(fb));
232 * Similar to fifo_push(), but with stronger guarantees for
233 * concurrent access between user and interrupt code.
235 * \note This is actually only needed for 8-bit processors.
239 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
241 ATOMIC(fifo_push(fb, c));
244 /* Probably not really needed, but hard to prove. */
245 INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
248 ATOMIC(c = fifo_pop(fb));
253 * Similar to fifo_flush(), but with stronger guarantees for
254 * concurrent access between user and interrupt code.
256 * \note This is actually only needed for 8-bit processors.
260 INLINE void fifo_flush_locked(FIFOBuffer *fb)
262 ATOMIC(fifo_flush(fb));
265 #endif /* CPU_REG_BITS < BITS_PER_PTR */
269 * Thread safe version of fifo_isfull()
271 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
274 ATOMIC(result = fifo_isfull(_fb));
280 * FIFO Initialization.
282 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
284 /* FIFO buffers have a known bug with 1-byte buffers. */
287 fb->head = fb->tail = fb->begin = buf;
288 fb->end = buf + size - 1;
292 * \return Lenght of the FIFOBuffer \a fb.
294 INLINE size_t fifo_len(FIFOBuffer *fb)
296 return fb->end - fb->begin;
303 * UNTESTED: if uncommented, to be moved in fifobuf.c
305 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
309 /* Se c'e' spazio da tail alla fine del buffer */
310 if (fb->tail >= fb->head)
312 freelen = fb->end - fb->tail + 1;
314 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
317 /* Scrivi quello che entra fino alla fine del buffer */
318 memcpy(fb->tail, block, freelen);
321 fb->tail = fb->begin;
325 /* Scrivi tutto il blocco */
326 memcpy(fb->tail, block, len);
334 while (!(freelen = fb->head - fb->tail - 1))
335 Delay(FIFO_POLLDELAY);
337 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
340 /* Scrivi quello che entra fino alla fine del buffer */
341 memcpy(fb->tail, block, freelen);
348 /* Scrivi tutto il blocco */
349 memcpy(fb->tail, block, len);
357 #endif /* STRUCT_FIFO_H */