Finally remove redundant protos.
[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 punta al primo elemento del buffer,
16  * \li \c end punta all'ultimo elemento,
17  * \li \c head punta al prossimo elemento che verra' estratto,
18  * \li \c tail punta alla posizione successiva all'ultimo elemento inserito.
19  * \li quando uno dei due puntatori raggiunge @c end, viene resettato a @c begin.
20  *
21  * \code
22  *
23  *  +-----------------------------------+
24  *  |  vuoto  |   dati validi  |  vuoto |
25  *  +-----------------------------------+
26  *  ^         ^                ^        ^
27  *  begin    head             tail     end
28  *
29  * \endcode
30  *
31  * Il buffer e' VUOTO quando head e tail coincidono:
32  *              \code head == tail \endcode
33  *
34  * Il buffer e' PIENO quando tail si trova immediatamente dietro a head:
35  *              \code tail == head - 1 \endcode
36  *
37  * Il buffer e' PIENO anche quando tail e' posizionato
38  * sull'ultima posizione del buffer e head sulla prima:
39  *              \code head == begin && tail == end \endcode
40  */
41
42 /*
43  * $Log$
44  * Revision 1.7  2004/07/20 23:46:29  bernie
45  * Finally remove redundant protos.
46  *
47  * Revision 1.6  2004/06/06 17:18:04  bernie
48  * Remove redundant declaration of fifo_isempty_locked().
49  *
50  * Revision 1.5  2004/06/06 16:50:35  bernie
51  * Import fixes for race conditions from project_ks.
52  *
53  * Revision 1.4  2004/06/06 16:11:17  bernie
54  * Protect MetroWerks specific pragmas with #ifdef's
55  *
56  * Revision 1.3  2004/06/03 15:04:10  aleph
57  * Merge improvements from project_ks (mainly inlining)
58  *
59  * Revision 1.2  2004/06/03 11:27:09  bernie
60  * Add dual-license information.
61  *
62  * Revision 1.1  2004/05/23 15:43:16  bernie
63  * Import mware modules.
64  *
65  * Revision 1.3  2004/05/22 17:55:58  rasky
66  * \samp non esiste in doxygen
67  *
68  * Revision 1.2  2004/04/27 11:13:29  rasky
69  * Spostate tutte le definizioni CPU-specific da compiler.h nel nuovo file cpu.h
70  *
71  * Revision 1.1  2004/04/21 17:38:25  rasky
72  * New application
73  *
74  * Revision 1.4  2004/03/24 15:37:03  bernie
75  * Remove Copyright messages from Doxygen output
76  *
77  * Revision 1.3  2004/03/18 18:11:07  bernie
78  * Add thread-safe FIFO handling macros
79  *
80  * Revision 1.2  2004/03/01 08:00:36  bernie
81  * Fixes for Doxygen
82  *
83  * Revision 1.1  2003/12/07 04:04:20  bernie
84  * Initial project_ks framework.
85  *
86  * Revision 1.1  2003/11/21 16:36:17  aleph
87  * Rename from fifo to fifobuf to avoid conflict with BSP fifo.h header
88  *
89  * Revision 1.1  2003/11/20 22:17:41  aleph
90  * Add fifo buffer used by serial
91  *
92  */
93
94 #ifndef MWARE_FIFO_H
95 #define MWARE_FIFO_H
96
97 #include "cpu.h"
98
99 typedef struct FIFOBuffer
100 {
101         unsigned char * volatile head;
102         unsigned char * volatile tail;
103         unsigned char *begin;
104         unsigned char *end;
105 } FIFOBuffer;
106
107
108 /*!
109  * Check whether the fifo is empty
110  *
111  * \note Calling fifo_isempty() is safe while a concurrent
112  *       execution context is calling fifo_push() or fifo_pop()
113  *       only if the CPU can atomically update a pointer
114  *       (which the AVR and other 8-bit processors can't do).
115  *
116  * \sa fifo_isempty_locked
117  */
118 INLINE bool fifo_isempty(const FIFOBuffer *fb)
119 {
120         return fb->head == fb->tail;
121 }
122
123
124 /*!
125  * Check whether the fifo is full
126  *
127  * \note Calling fifo_isfull() is safe while a concurrent
128  *       execution context is calling fifo_pop() and the
129  *       CPU can update a pointer atomically.
130  *       It is NOT safe when the other context calls
131  *       fifo_push().
132  *       This limitation is not usually problematic in a
133  *       consumer/producer scenario because the
134  *       fifo_isfull() and fifo_push() are usually called
135  *       in the producer context.
136  */
137 INLINE bool fifo_isfull(const FIFOBuffer *fb)
138 {
139         return
140                 ((fb->head == fb->begin) && (fb->tail == fb->end))
141                 || (fb->tail == fb->head - 1);
142 }
143
144
145 /*!
146  * Pop a character from the fifo buffer.
147  *
148  * \note Calling \c fifo_push() on a full buffer is undefined.
149  *       The caller must make sure the buffer has at least
150  *       one free slot before calling this function.
151  *
152  * \note It is safe to call fifo_pop() and fifo_push() from
153  *       concurrent contexts, unless the CPU can't update
154  *       a pointer atomically (which the AVR and other 8-bit
155  *       processors can't do).
156  *
157  * \sa fifo_push_locked
158  */
159 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
160 {
161 #ifdef __MWERKS__
162 #pragma interrupt called
163 #endif
164         /* Write at tail position */
165         *(fb->tail) = c;
166
167         if (fb->tail == fb->end)
168                 /* wrap tail around */
169                 fb->tail = fb->begin;
170         else
171                 /* Move tail forward */
172                 fb->tail++;
173 }
174
175
176 /*!
177  * Pop a character from the fifo buffer.
178  *
179  * \note Calling \c fifo_pop() on an empty buffer is undefined.
180  *       The caller must make sure the buffer contains at least
181  *       one character before calling this function.
182  *
183  * \note It is safe to call fifo_pop() and fifo_push() from
184  *       concurrent contexts.
185  */
186 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
187 {
188 #ifdef __MWERKS__
189 #pragma interrupt called
190 #endif
191         if (fb->head == fb->end)
192         {
193                 /* wrap head around */
194                 fb->head = fb->begin;
195                 return *(fb->end);
196         }
197         else
198                 /* move head forward */
199                 return *(fb->head++);
200 }
201
202
203 /*!
204  * Make the fifo empty, discarding all its current contents.
205  */
206 INLINE void fifo_flush(FIFOBuffer *fb)
207 {
208         fb->head = fb->tail;
209 }
210
211
212 #if !defined(__AVR__)
213
214         /* No tricks needed on 16/32bit CPUs */
215 #       define fifo_isempty_locked(fb) fifo_isempty((fb))
216 #       define fifo_push_locked(fb, c) fifo_push((fb), (c))
217
218 #else /* !__AVR__ */
219
220         /*!
221          * Similar to fifo_isempty(), but with stronger guarantees for
222          * concurrent access between user and interrupt code.
223          * This is actually only needed for 8-bit processors.
224          *
225          * \sa fifo_isempty()
226          */
227         INLINE bool fifo_isempty_locked(const FIFOBuffer *fb);
228         INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
229         {
230                 bool result;
231                 cpuflags_t flags;
232
233                 DISABLE_IRQSAVE(flags);
234                 result = fifo_isempty(fb);
235                 ENABLE_IRQRESTORE(flags);
236
237                 return result;
238         }
239
240         /*!
241          * Similar to fifo_push(), but with stronger guarantees for
242          * concurrent access between user and interrupt code.
243          * This is actually only needed for 8-bit processors.
244          *
245          * \sa fifo_push()
246          */
247         INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c);
248         INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
249         {
250                 cpuflags_t flags;
251                 DISABLE_IRQSAVE(flags);
252                 fifo_push(fb, c);
253                 ENABLE_IRQRESTORE(flags);
254         }
255
256 #endif /* !__AVR__ */
257
258
259 /*!
260  * Thread safe version of fifo_isfull()
261  */
262 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
263 {
264         bool _result;
265         cpuflags_t _flags;
266
267         DISABLE_IRQSAVE(_flags);
268         _result = fifo_isfull(_fb);
269         ENABLE_IRQRESTORE(_flags);
270
271         return _result;
272 }
273
274
275
276 /*!
277  * FIFO Initialization.
278  */
279 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
280 {
281         fb->head = fb->tail = fb->begin = buf;
282         fb->end = buf + size - 1;
283 }
284
285
286
287 #if 0
288
289 /*
290  * UNTESTED: if uncommented, to be moved in fifobuf.c
291  */
292 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
293 {
294         size_t freelen;
295
296         /* Se c'e' spazio da tail alla fine del buffer */
297         if (fb->tail >= fb->head)
298         {
299                 freelen = fb->end - fb->tail + 1;
300
301                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
302                 if (freelen < len)
303                 {
304                         /* Scrivi quello che entra fino alla fine del buffer */
305                         memcpy(fb->tail, block, freelen);
306                         block += freelen;
307                         len -= freelen;
308                         fb->tail = fb->begin;
309                 }
310                 else
311                 {
312                         /* Scrivi tutto il blocco */
313                         memcpy(fb->tail, block, len);
314                         fb->tail += len;
315                         return;
316                 }
317         }
318
319         for(;;)
320         {
321                 while (!(freelen = fb->head - fb->tail - 1))
322                         Delay(FIFO_POLLDELAY);
323
324                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
325                 if (freelen < len)
326                 {
327                         /* Scrivi quello che entra fino alla fine del buffer */
328                         memcpy(fb->tail, block, freelen);
329                         block += freelen;
330                         len -= freelen;
331                         fb->tail += freelen;
332                 }
333                 else
334                 {
335                         /* Scrivi tutto il blocco */
336                         memcpy(fb->tail, block, len);
337                         fb->tail += len;
338                         return;
339                 }
340         }
341 }
342 #endif
343
344 #endif /* MWARE_FIFO_H */
345