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