Include top-level headers from cfg/ subdir.
[bertos.git] / mware / fifobuf.h
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief General pourpose FIFO buffer implemented with a ring buffer
14  *
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
20  *     back to \c begin.
21  *
22  * \code
23  *
24  *  +-----------------------------------+
25  *  |  empty  |   valid data   |  empty |
26  *  +-----------------------------------+
27  *  ^         ^                ^        ^
28  *  begin    head             tail     end
29  *
30  * \endcode
31  *
32  * The buffer is EMPTY when \c head and \c tail point to the same location:
33  *              \code head == tail \endcode
34  *
35  * The buffer is FULL when \c tail points to the location immediately
36  * after \c head:
37  *              \code tail == head - 1 \endcode
38  *
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
42  */
43
44 /*#*
45  *#* $Log$
46  *#* Revision 1.20  2005/04/11 19:10:28  bernie
47  *#* Include top-level headers from cfg/ subdir.
48  *#*
49  *#* Revision 1.19  2004/12/08 08:30:12  bernie
50  *#* Add missing header.
51  *#*
52  *#* Revision 1.18  2004/11/16 21:55:12  bernie
53  *#* Workaround for a known fifobuf bug.
54  *#*
55  *#* Revision 1.17  2004/09/14 20:57:00  bernie
56  *#* Use debug.h instead of kdebug.h.
57  *#*
58  *#* Revision 1.16  2004/09/06 21:39:08  bernie
59  *#* Simplify code using ATOMIC().
60  *#*
61  *#* Revision 1.15  2004/08/29 22:05:16  bernie
62  *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
63  *#*
64  *#* Revision 1.14  2004/08/25 14:12:09  rasky
65  *#* Aggiornato il comment block dei log RCS
66  *#*
67  *#* Revision 1.13  2004/08/24 13:16:11  bernie
68  *#* Add type-size definitions for preprocessor.
69  *#*
70  *#* Revision 1.12  2004/08/02 20:20:29  aleph
71  *#* Merge from project_ks
72  *#*
73  *#* Revision 1.11  2004/07/30 14:15:53  rasky
74  *#* Nuovo supporto unificato per detect della CPU
75  *#*
76  *#* Revision 1.10  2004/07/29 22:57:09  bernie
77  *#* Doxygen fix.
78  *#*
79  *#* Revision 1.9  2004/07/20 23:54:27  bernie
80  *#* fifo_flush_locked(): New function;
81  *#* Revamp documentation.
82  *#*
83  *#* Revision 1.8  2004/07/20 23:47:39  bernie
84  *#* Finally remove redundant protos.
85  *#*
86  *#* Revision 1.7  2004/07/20 23:46:29  bernie
87  *#* Finally remove redundant protos.
88  *#*
89  *#* Revision 1.6  2004/06/06 17:18:04  bernie
90  *#* Remove redundant declaration of fifo_isempty_locked().
91  *#*
92  *#* Revision 1.5  2004/06/06 16:50:35  bernie
93  *#* Import fixes for race conditions from project_ks.
94  *#*
95  *#* Revision 1.4  2004/06/06 16:11:17  bernie
96  *#* Protect MetroWerks specific pragmas with #ifdef's
97  *#*/
98
99 #ifndef MWARE_FIFO_H
100 #define MWARE_FIFO_H
101
102 #include <cfg/cpu.h>
103 #include <cfg/debug.h>
104
105 typedef struct FIFOBuffer
106 {
107         unsigned char * volatile head;
108         unsigned char * volatile tail;
109         unsigned char *begin;
110         unsigned char *end;
111 } FIFOBuffer;
112
113
114 #define ASSERT_VALID_FIFO(fifo) \
115         ATOMIC( \
116                 ASSERT((fifo)->head >= (fifo)->begin); \
117                 ASSERT((fifo)->head <= (fifo)->end); \
118                 ASSERT((fifo)->tail >= (fifo)->begin); \
119                 ASSERT((fifo)->tail <= (fifo)->end); \
120         )
121
122
123 /*!
124  * Check whether the fifo is empty
125  *
126  * \note Calling fifo_isempty() is safe while a concurrent
127  *       execution context is calling fifo_push() or fifo_pop()
128  *       only if the CPU can atomically update a pointer
129  *       (which the AVR and other 8-bit processors can't do).
130  *
131  * \sa fifo_isempty_locked
132  */
133 INLINE bool fifo_isempty(const FIFOBuffer *fb)
134 {
135         //ASSERT_VALID_FIFO(fb);
136         return fb->head == fb->tail;
137 }
138
139
140 /*!
141  * Check whether the fifo is full
142  *
143  * \note Calling fifo_isfull() is safe while a concurrent
144  *       execution context is calling fifo_pop() and the
145  *       CPU can update a pointer atomically.
146  *       It is NOT safe when the other context calls
147  *       fifo_push().
148  *       This limitation is not usually problematic in a
149  *       consumer/producer scenario because the
150  *       fifo_isfull() and fifo_push() are usually called
151  *       in the producer context.
152  */
153 INLINE bool fifo_isfull(const FIFOBuffer *fb)
154 {
155         //ASSERT_VALID_FIFO(fb);
156         return
157                 ((fb->head == fb->begin) && (fb->tail == fb->end))
158                 || (fb->tail == fb->head - 1);
159 }
160
161
162 /*!
163  * Pop a character from the fifo buffer.
164  *
165  * \note Calling \c fifo_push() on a full buffer is undefined.
166  *       The caller must make sure the buffer has at least
167  *       one free slot before calling this function.
168  *
169  * \note It is safe to call fifo_pop() and fifo_push() from
170  *       concurrent contexts, unless the CPU can't update
171  *       a pointer atomically (which the AVR and other 8-bit
172  *       processors can't do).
173  *
174  * \sa fifo_push_locked
175  */
176 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
177 {
178 #ifdef __MWERKS__
179 #pragma interrupt called
180 #endif
181         //ASSERT_VALID_FIFO(fb);
182
183         /* Write at tail position */
184         *(fb->tail) = c;
185
186         if (UNLIKELY(fb->tail == fb->end))
187                 /* wrap tail around */
188                 fb->tail = fb->begin;
189         else
190                 /* Move tail forward */
191                 fb->tail++;
192 }
193
194
195 /*!
196  * Pop a character from the fifo buffer.
197  *
198  * \note Calling \c fifo_pop() on an empty buffer is undefined.
199  *       The caller must make sure the buffer contains at least
200  *       one character before calling this function.
201  *
202  * \note It is safe to call fifo_pop() and fifo_push() from
203  *       concurrent contexts.
204  */
205 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
206 {
207 #ifdef __MWERKS__
208 #pragma interrupt called
209 #endif
210         //ASSERT_VALID_FIFO(fb);
211
212         if (UNLIKELY(fb->head == fb->end))
213         {
214                 /* wrap head around */
215                 fb->head = fb->begin;
216                 return *(fb->end);
217         }
218         else
219                 /* move head forward */
220                 return *(fb->head++);
221 }
222
223
224 /*!
225  * Make the fifo empty, discarding all its current contents.
226  */
227 INLINE void fifo_flush(FIFOBuffer *fb)
228 {
229         //ASSERT_VALID_FIFO(fb);
230         fb->head = fb->tail;
231 }
232
233
234 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
235
236         /*
237          * 16/32bit CPUs that can update a pointer with a single write
238          * operation, no need to disable interrupts.
239          */
240         #define fifo_isempty_locked(fb) fifo_isempty((fb))
241         #define fifo_push_locked(fb, c) fifo_push((fb), (c))
242         #define fifo_pop_locked(fb)     fifo_pop((fb))
243         #define fifo_flush_locked(fb)   fifo_flush((fb))
244
245 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
246
247         /*!
248          * Similar to fifo_isempty(), but with stronger guarantees for
249          * concurrent access between user and interrupt code.
250          *
251          * \note This is actually only needed for 8-bit processors.
252          *
253          * \sa fifo_isempty()
254          */
255         INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
256         {
257                 bool result;
258                 ATOMIC(result = fifo_isempty(fb));
259                 return result;
260         }
261
262
263         /*!
264          * Similar to fifo_push(), but with stronger guarantees for
265          * concurrent access between user and interrupt code.
266          *
267          * \note This is actually only needed for 8-bit processors.
268          *
269          * \sa fifo_push()
270          */
271         INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
272         {
273                 ATOMIC(fifo_push(fb, c));
274         }
275
276         /* Probably not really needed, but hard to prove. */
277         INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
278         {
279                 unsigned char c;
280                 ATOMIC(c = fifo_pop(fb));
281                 return c;
282         }
283
284         /*!
285          * Similar to fifo_flush(), but with stronger guarantees for
286          * concurrent access between user and interrupt code.
287          *
288          * \note This is actually only needed for 8-bit processors.
289          *
290          * \sa fifo_flush()
291          */
292         INLINE void fifo_flush_locked(FIFOBuffer *fb)
293         {
294                 ATOMIC(fifo_flush(fb));
295         }
296
297 #endif /* CPU_REG_BITS < BITS_PER_PTR */
298
299
300 /*!
301  * Thread safe version of fifo_isfull()
302  */
303 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
304 {
305         bool result;
306         ATOMIC(result = fifo_isfull(_fb));
307         return result;
308 }
309
310
311 /*!
312  * FIFO Initialization.
313  */
314 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
315 {
316         /* FIFO buffers have a known bug with 1-byte buffers. */
317         ASSERT(size > 1);
318
319         fb->head = fb->tail = fb->begin = buf;
320         fb->end = buf + size - 1;
321 }
322
323
324 #if 0
325
326 /*
327  * UNTESTED: if uncommented, to be moved in fifobuf.c
328  */
329 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
330 {
331         size_t freelen;
332
333         /* Se c'e' spazio da tail alla fine del buffer */
334         if (fb->tail >= fb->head)
335         {
336                 freelen = fb->end - fb->tail + 1;
337
338                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
339                 if (freelen < len)
340                 {
341                         /* Scrivi quello che entra fino alla fine del buffer */
342                         memcpy(fb->tail, block, freelen);
343                         block += freelen;
344                         len -= freelen;
345                         fb->tail = fb->begin;
346                 }
347                 else
348                 {
349                         /* Scrivi tutto il blocco */
350                         memcpy(fb->tail, block, len);
351                         fb->tail += len;
352                         return;
353                 }
354         }
355
356         for(;;)
357         {
358                 while (!(freelen = fb->head - fb->tail - 1))
359                         Delay(FIFO_POLLDELAY);
360
361                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
362                 if (freelen < len)
363                 {
364                         /* Scrivi quello che entra fino alla fine del buffer */
365                         memcpy(fb->tail, block, freelen);
366                         block += freelen;
367                         len -= freelen;
368                         fb->tail += freelen;
369                 }
370                 else
371                 {
372                         /* Scrivi tutto il blocco */
373                         memcpy(fb->tail, block, len);
374                         fb->tail += len;
375                         return;
376                 }
377         }
378 }
379 #endif
380
381 #endif /* MWARE_FIFO_H */
382