Combined page and word ISP programming mode code in the AVRISP-MKII clone project...
[pub/USBasp.git] / Projects / XPLAINBridge / Lib / LightweightRingBuff.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Ultra lightweight ring buffer, for fast insertion/deletion. This uses inlined functions
34 * for maximum speed. All buffers created with this library must be of the same size, however
35 * multiple independant buffers can be created.
36 *
37 * Note that for each buffer, insertion and removal operations may occur at the same time (via
38 * a multithreaded ISR based system) however the same kind of operation (two or more insertions
39 * or deletions) must not overlap. If there is possibility of two or more of the same kind of
40 * operating occuring at the same point in time, atomic (mutex) locking should be used.
41 */
42
43 #ifndef _ULW_RING_BUFF_H_
44 #define _ULW_RING_BUFF_H_
45
46 /* Includes: */
47 #include <util/atomic.h>
48
49 #include <stdint.h>
50 #include <stdbool.h>
51
52 /* Defines: */
53 /** Size of each ring buffer, in data elements - must be between 1 and 255. */
54 #define BUFFER_SIZE 255
55
56 /** Type of data to store into the buffer. */
57 #define RingBuff_Data_t uint8_t
58
59 /** Datatype which may be used to store the count of data stored in a buffer, retrieved
60 * via a call to \ref RingBuffer_GetCount().
61 */
62 #if (BUFFER_SIZE <= 0xFF)
63 #define RingBuff_Count_t uint8_t
64 #else
65 #define RingBuff_Count_t uint16_t
66 #endif
67
68 /* Type Defines: */
69 /** Type define for a new ring buffer object. Buffers should be initialized via a call to
70 * \ref RingBuffer_InitBuffer() before use.
71 */
72 typedef struct
73 {
74 RingBuff_Data_t Buffer[BUFFER_SIZE]; /**< Internal ring buffer data, referenced by the buffer pointers. */
75 RingBuff_Data_t* In; /**< Current storage location in the circular buffer */
76 RingBuff_Data_t* Out; /**< Current retrieval location in the circular buffer */
77 RingBuff_Count_t Count;
78 } RingBuff_t;
79
80 /* Inline Functions: */
81 /** Initializes a ring buffer ready for use. Buffers must be initialized via this function
82 * before any operations are called upon them. Already initialized buffers may be reset
83 * by re-initializing them using this function.
84 *
85 * \param[out] Buffer Pointer to a ring buffer structure to initialize
86 */
87 static inline void RingBuffer_InitBuffer(RingBuff_t* const Buffer)
88 {
89 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
90 {
91 Buffer->In = Buffer->Buffer;
92 Buffer->Out = Buffer->Buffer;
93 }
94 }
95
96 /** Retrieves the minimum number of bytes stored in a particular buffer. This value is computed
97 * by entering an atomic lock on the buffer while the IN and OUT locations are fetched, so that
98 * the buffer cannot be modified while the computation takes place. This value should be cached
99 * when reading out the contents of the buffer, so that as small a time as possible is spent
100 * in an atomic lock.
101 *
102 * \note The value returned by this function is guaranteed to only be the minimum number of bytes
103 * stored in the given buffer; this value may change as other threads write new data and so
104 * the returned number should be used only to determine how many successive reads may safely
105 * be performed on the buffer.
106 *
107 * \param[in] Buffer Pointer to a ring buffer structure whose count is to be computed
108 */
109 static inline RingBuff_Count_t RingBuffer_GetCount(RingBuff_t* const Buffer)
110 {
111 RingBuff_Count_t Count;
112
113 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
114 {
115 Count = Buffer->Count;
116 }
117
118 return Count;
119 }
120
121 /** Atomically determines if the specified ring buffer contains any free space. This should
122 * be tested before storing data to the buffer, to ensure that no data is lost due to a
123 * buffer overrun.
124 *
125 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
126 *
127 * \return Boolean true if the buffer contains no free space, false otherwise
128 */
129 static inline bool RingBuffer_IsFull(RingBuff_t* const Buffer)
130 {
131 return (RingBuffer_GetCount(Buffer) == BUFFER_SIZE);
132 }
133
134 /** Atomically determines if the specified ring buffer contains any data. This should
135 * be tested before removing data from the buffer, to ensure that the buffer does not
136 * underflow.
137 *
138 * If the data is to be removed in a loop, store the total number of bytes stored in the
139 * buffer (via a call to the \ref RingBuffer_GetCount() function) in a temporary variable
140 * to reduce the time spent in atomicity locks.
141 *
142 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
143 *
144 * \return Boolean true if the buffer contains no free space, false otherwise
145 */
146 static inline bool RingBuffer_IsEmpty(RingBuff_t* const Buffer)
147 {
148 return (RingBuffer_GetCount(Buffer) == 0);
149 }
150
151 /** Inserts an element into the ring buffer.
152 *
153 * \note Only one execution thread (main program thread or an ISR) may insert into a single buffer
154 * otherwise data corruption may occur. Insertion and removal may occur from different execution
155 * threads.
156 *
157 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
158 * \param[in] Data Data element to insert into the buffer
159 */
160 static inline void RingBuffer_Insert(RingBuff_t* const Buffer,
161 const RingBuff_Data_t Data)
162 {
163 *Buffer->In = Data;
164
165 if (++Buffer->In == &Buffer->Buffer[BUFFER_SIZE])
166 Buffer->In = Buffer->Buffer;
167
168 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
169 {
170 Buffer->Count++;
171 }
172 }
173
174 /** Removes an element from the ring buffer.
175 *
176 * \note Only one execution thread (main program thread or an ISR) may remove from a single buffer
177 * otherwise data corruption may occur. Insertion and removal may occur from different execution
178 * threads.
179 *
180 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
181 *
182 * \return Next data element stored in the buffer
183 */
184 static inline RingBuff_Data_t RingBuffer_Remove(RingBuff_t* const Buffer)
185 {
186 RingBuff_Data_t Data = *Buffer->Out;
187
188 if (++Buffer->Out == &Buffer->Buffer[BUFFER_SIZE])
189 Buffer->Out = Buffer->Buffer;
190
191 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
192 {
193 Buffer->Count--;
194 }
195
196 return Data;
197 }
198
199 /** Returns the next element stored in the ring buffer, without removing it.
200 *
201 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
202 *
203 * \return Next data element stored in the buffer
204 */
205 static inline RingBuff_Data_t RingBuffer_Peek(RingBuff_t* const Buffer)
206 {
207 return *Buffer->Out;
208 }
209
210 #endif
211