]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Misc/RingBuffer.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Misc / RingBuffer.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief Lightweight ring (circular) buffer, for fast insertion/deletion of bytes.\r
33  *\r
34  *  Lightweight ring buffer, for fast insertion/deletion. Multiple buffers can be created of\r
35  *  different sizes to suit different needs.\r
36  *\r
37  *  Note that for each buffer, insertion and removal operations may occur at the same time (via\r
38  *  a multi-threaded ISR based system) however the same kind of operation (two or more insertions\r
39  *  or deletions) must not overlap. If there is possibility of two or more of the same kind of\r
40  *  operating occurring at the same point in time, atomic (mutex) locking should be used.\r
41  */\r
42 \r
43 /** \ingroup Group_MiscDrivers\r
44  *  \defgroup Group_RingBuff Generic Byte Ring Buffer - LUFA/Drivers/Misc/RingBuffer.h\r
45  *  \brief Lightweight ring buffer, for fast insertion/deletion of bytes.\r
46  *\r
47  *  \section Sec_Dependencies Module Source Dependencies\r
48  *  The following files must be built with any user project that uses this module:\r
49  *    - None\r
50  *\r
51  *  \section Sec_ModDescription Module Description\r
52  *  Lightweight ring buffer, for fast insertion/deletion. Multiple buffers can be created of\r
53  *  different sizes to suit different needs.\r
54  *\r
55  *  Note that for each buffer, insertion and removal operations may occur at the same time (via\r
56  *  a multi-threaded ISR based system) however the same kind of operation (two or more insertions\r
57  *  or deletions) must not overlap. If there is possibility of two or more of the same kind of\r
58  *  operating occurring at the same point in time, atomic (mutex) locking should be used.\r
59  *\r
60  *  \section Sec_ExampleUsage Example Usage\r
61  *  The following snippet is an example of how this module may be used within a typical\r
62  *  application.\r
63  *\r
64  *  \code\r
65  *      // Create the buffer structure and its underlying storage array\r
66  *      RingBuffer_t Buffer;\r
67  *      uint8_t      BufferData[128];\r
68  *      \r
69  *      // Initialize the buffer with the created storage array\r
70  *      RingBuffer_InitBuffer(&Buffer, BufferData, sizeof(BufferData));\r
71  *      \r
72  *      // Insert some data into the buffer\r
73  *      RingBuffer_Insert(Buffer, 'H');\r
74  *      RingBuffer_Insert(Buffer, 'E');\r
75  *      RingBuffer_Insert(Buffer, 'L');\r
76  *      RingBuffer_Insert(Buffer, 'L');\r
77  *      RingBuffer_Insert(Buffer, 'O');\r
78  *      \r
79  *      // Cache the number of stored bytes in the buffer\r
80  *      uint16_t BufferCount = RingBuffer_GetCount(&Buffer);\r
81  *      \r
82  *      // Printer stored data length\r
83  *      printf("Buffer Length: %d, Buffer Data: \r\n", BufferCount);\r
84  *      \r
85  *      // Print contents of the buffer one character at a time\r
86  *      while (BufferCount--)\r
87  *        putc(RingBuffer_Remove(&Buffer));\r
88  *  \endcode\r
89  *\r
90  *  @{\r
91  */\r
92 \r
93 #ifndef __RING_BUFFER_H__\r
94 #define __RING_BUFFER_H__\r
95 \r
96         /* Includes: */\r
97                 #include "../../Common/Common.h"\r
98 \r
99         /* Enable C linkage for C++ Compilers: */\r
100                 #if defined(__cplusplus)\r
101                         extern "C" {\r
102                 #endif\r
103 \r
104         /* Type Defines: */\r
105                 /** \brief Ring Buffer Management Structure.\r
106                  *\r
107                  *  Type define for a new ring buffer object. Buffers should be initialized via a call to\r
108                  *  \ref RingBuffer_InitBuffer() before use.\r
109                  */\r
110                 typedef struct\r
111                 {\r
112                         uint8_t* In; /**< Current storage location in the circular buffer. */\r
113                         uint8_t* Out; /**< Current retrieval location in the circular buffer. */\r
114                         uint8_t* Start; /**< Pointer to the start of the buffer's underlying storage array. */\r
115                         uint8_t* End; /**< Pointer to the end of the buffer's underlying storage array. */\r
116                         uint16_t Size; /**< Size of the buffer's underlying storage array. */\r
117                         uint16_t Count; /**< Number of bytes currently stored in the buffer. */\r
118                 } RingBuffer_t;\r
119 \r
120         /* Inline Functions: */\r
121                 /** Initializes a ring buffer ready for use. Buffers must be initialized via this function\r
122                  *  before any operations are called upon them. Already initialized buffers may be reset\r
123                  *  by re-initializing them using this function.\r
124                  *\r
125                  *  \param[out] Buffer   Pointer to a ring buffer structure to initialize.\r
126                  *  \param[out] DataPtr  Pointer to a global array that will hold the data stored into the ring buffer.\r
127                  *  \param[out] Size     Maximum number of bytes that can be stored in the underlying data array.\r
128                  */\r
129                 static inline void RingBuffer_InitBuffer(RingBuffer_t* Buffer, uint8_t* const DataPtr, const uint16_t Size)\r
130                                                          ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);\r
131                 static inline void RingBuffer_InitBuffer(RingBuffer_t* Buffer, uint8_t* const DataPtr, const uint16_t Size)\r
132                 {\r
133                         GCC_FORCE_POINTER_ACCESS(Buffer);\r
134 \r
135                         uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();\r
136                         GlobalInterruptDisable();\r
137 \r
138                         Buffer->In     = DataPtr;\r
139                         Buffer->Out    = DataPtr;\r
140                         Buffer->Start  = &DataPtr[0];\r
141                         Buffer->End    = &DataPtr[Size];\r
142                         Buffer->Size   = Size;\r
143                         Buffer->Count  = 0;\r
144 \r
145                         SetGlobalInterruptMask(CurrentGlobalInt);\r
146                 }\r
147 \r
148                 /** Retrieves the current number of bytes stored in a particular buffer. This value is computed\r
149                  *  by entering an atomic lock on the buffer, so that the buffer cannot be modified while the\r
150                  *  computation takes place. This value should be cached when reading out the contents of the buffer,\r
151                  *  so that as small a time as possible is spent in an atomic lock.\r
152                  *\r
153                  *  \note The value returned by this function is guaranteed to only be the minimum number of bytes\r
154                  *        stored in the given buffer; this value may change as other threads write new data, thus\r
155                  *        the returned number should be used only to determine how many successive reads may safely\r
156                  *        be performed on the buffer.\r
157                  *\r
158                  *  \param[in] Buffer  Pointer to a ring buffer structure whose count is to be computed.\r
159                  *\r
160                  *  \return Number of bytes currently stored in the buffer.\r
161                  */\r
162                 static inline uint16_t RingBuffer_GetCount(RingBuffer_t* const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);\r
163                 static inline uint16_t RingBuffer_GetCount(RingBuffer_t* const Buffer)\r
164                 {\r
165                         uint16_t Count;\r
166 \r
167                         uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();\r
168                         GlobalInterruptDisable();\r
169 \r
170                         Count = Buffer->Count;\r
171 \r
172                         SetGlobalInterruptMask(CurrentGlobalInt);\r
173                         return Count;\r
174                 }\r
175 \r
176                 /** Retrieves the free space in a particular buffer. This value is computed by entering an atomic lock\r
177                  *  on the buffer, so that the buffer cannot be modified while the computation takes place.\r
178                  *\r
179                  *  \note The value returned by this function is guaranteed to only be the maximum number of bytes\r
180                  *        free in the given buffer; this value may change as other threads write new data, thus\r
181                  *        the returned number should be used only to determine how many successive writes may safely\r
182                  *        be performed on the buffer when there is a single writer thread.\r
183                  *\r
184                  *  \param[in] Buffer  Pointer to a ring buffer structure whose free count is to be computed.\r
185                  *\r
186                  *  \return Number of free bytes in the buffer.\r
187                  */\r
188                 static inline uint16_t RingBuffer_GetFreeCount(RingBuffer_t* const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);\r
189                 static inline uint16_t RingBuffer_GetFreeCount(RingBuffer_t* const Buffer)\r
190                 {\r
191                         return (Buffer->Size - RingBuffer_GetCount(Buffer));\r
192                 }\r
193 \r
194                 /** Atomically determines if the specified ring buffer contains any data. This should\r
195                  *  be tested before removing data from the buffer, to ensure that the buffer does not\r
196                  *  underflow.\r
197                  *\r
198                  *  If the data is to be removed in a loop, store the total number of bytes stored in the\r
199                  *  buffer (via a call to the \ref RingBuffer_GetCount() function) in a temporary variable\r
200                  *  to reduce the time spent in atomicity locks.\r
201                  *\r
202                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to insert into.\r
203                  *\r
204                  *  \return Boolean \c true if the buffer contains no free space, false otherwise.\r
205                  */\r
206                 static inline bool RingBuffer_IsEmpty(RingBuffer_t* const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);\r
207                 static inline bool RingBuffer_IsEmpty(RingBuffer_t* const Buffer)\r
208                 {\r
209                         return (RingBuffer_GetCount(Buffer) == 0);\r
210                 }\r
211 \r
212                 /** Atomically determines if the specified ring buffer contains any free space. This should\r
213                  *  be tested before storing data to the buffer, to ensure that no data is lost due to a\r
214                  *  buffer overrun.\r
215                  *\r
216                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to insert into.\r
217                  *\r
218                  *  \return Boolean \c true if the buffer contains no free space, false otherwise.\r
219                  */\r
220                 static inline bool RingBuffer_IsFull(RingBuffer_t* const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);\r
221                 static inline bool RingBuffer_IsFull(RingBuffer_t* const Buffer)\r
222                 {\r
223                         return (RingBuffer_GetCount(Buffer) == Buffer->Size);\r
224                 }\r
225 \r
226                 /** Inserts an element into the ring buffer.\r
227                  *\r
228                  *  \warning Only one execution thread (main program thread or an ISR) may insert into a single buffer\r
229                  *           otherwise data corruption may occur. Insertion and removal may occur from different execution\r
230                  *           threads.\r
231                  *\r
232                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to insert into.\r
233                  *  \param[in]     Data    Data element to insert into the buffer.\r
234                  */\r
235                 static inline void RingBuffer_Insert(RingBuffer_t* Buffer, const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1);\r
236                 static inline void RingBuffer_Insert(RingBuffer_t* Buffer, const uint8_t Data)\r
237                 {\r
238                         GCC_FORCE_POINTER_ACCESS(Buffer);\r
239 \r
240                         *Buffer->In = Data;\r
241 \r
242                         if (++Buffer->In == Buffer->End)\r
243                           Buffer->In = Buffer->Start;\r
244 \r
245                         uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();\r
246                         GlobalInterruptDisable();\r
247 \r
248                         Buffer->Count++;\r
249 \r
250                         SetGlobalInterruptMask(CurrentGlobalInt);\r
251                 }\r
252 \r
253                 /** Removes an element from the ring buffer.\r
254                  *\r
255                  *  \warning Only one execution thread (main program thread or an ISR) may remove from a single buffer\r
256                  *           otherwise data corruption may occur. Insertion and removal may occur from different execution\r
257                  *           threads.\r
258                  *\r
259                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to retrieve from.\r
260                  *\r
261                  *  \return Next data element stored in the buffer.\r
262                  */\r
263                 static inline uint8_t RingBuffer_Remove(RingBuffer_t* Buffer) ATTR_NON_NULL_PTR_ARG(1);\r
264                 static inline uint8_t RingBuffer_Remove(RingBuffer_t* Buffer)\r
265                 {\r
266                         GCC_FORCE_POINTER_ACCESS(Buffer);\r
267 \r
268                         uint8_t Data = *Buffer->Out;\r
269 \r
270                         if (++Buffer->Out == Buffer->End)\r
271                           Buffer->Out = Buffer->Start;\r
272 \r
273                         uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();\r
274                         GlobalInterruptDisable();\r
275 \r
276                         Buffer->Count--;\r
277 \r
278                         SetGlobalInterruptMask(CurrentGlobalInt);\r
279 \r
280                         return Data;\r
281                 }\r
282 \r
283                 /** Returns the next element stored in the ring buffer, without removing it.\r
284                  *\r
285                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to retrieve from.\r
286                  *\r
287                  *  \return Next data element stored in the buffer.\r
288                  */\r
289                 static inline uint8_t RingBuffer_Peek(RingBuffer_t* const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);\r
290                 static inline uint8_t RingBuffer_Peek(RingBuffer_t* const Buffer)\r
291                 {\r
292                         return *Buffer->Out;\r
293                 }\r
294 \r
295         /* Disable C linkage for C++ Compilers: */\r
296                 #if defined(__cplusplus)\r
297                         }\r
298                 #endif\r
299 \r
300 #endif\r
301 \r
302 /** @} */\r
303 \r