3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  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. 
  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 
  32  *  \brief Lightweight ring (circular) buffer, for fast insertion/deletion of bytes. 
  34  *  Lightweight ring buffer, for fast insertion/deletion. Multiple buffers can be created of 
  35  *  different sizes to suit different needs. 
  37  *  Note that for each buffer, insertion and removal operations may occur at the same time (via 
  38  *  a multi-threaded 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 occurring at the same point in time, atomic (mutex) locking should be used. 
  43 /** \ingroup Group_MiscDrivers 
  44  *  \defgroup Group_RingBuff Generic Byte Ring Buffer - LUFA/Drivers/Misc/RingBuffer.h 
  45  *  \brief Lightweight ring buffer, for fast insertion/deletion of bytes. 
  47  *  \section Sec_Dependencies Module Source Dependencies 
  48  *  The following files must be built with any user project that uses this module: 
  51  *  \section Sec_ModDescription Module Description 
  52  *  Lightweight ring buffer, for fast insertion/deletion. Multiple buffers can be created of 
  53  *  different sizes to suit different needs. 
  55  *  Note that for each buffer, insertion and removal operations may occur at the same time (via 
  56  *  a multi-threaded ISR based system) however the same kind of operation (two or more insertions 
  57  *  or deletions) must not overlap. If there is possibility of two or more of the same kind of 
  58  *  operating occurring at the same point in time, atomic (mutex) locking should be used. 
  60  *  \section Sec_ExampleUsage Example Usage 
  61  *  The following snippet is an example of how this module may be used within a typical 
  65  *      // Create the buffer structure and its underlying storage array 
  66  *      RingBuffer_t Buffer; 
  67  *      uint8_t      BufferData[128]; 
  69  *      // Initialize the buffer with the created storage array 
  70  *      RingBuffer_InitBuffer(&Buffer, BufferData, sizeof(BufferData)); 
  72  *      // Insert some data into the buffer 
  73  *      RingBuffer_Insert(Buffer, 'H'); 
  74  *      RingBuffer_Insert(Buffer, 'E'); 
  75  *      RingBuffer_Insert(Buffer, 'L'); 
  76  *      RingBuffer_Insert(Buffer, 'L'); 
  77  *      RingBuffer_Insert(Buffer, 'O'); 
  79  *      // Cache the number of stored bytes in the buffer 
  80  *      uint16_t BufferCount = RingBuffer_GetCount(&Buffer); 
  82  *      // Printer stored data length 
  83  *      printf("Buffer Length: %d, Buffer Data: \r\n", BufferCount); 
  85  *      // Print contents of the buffer one character at a time 
  86  *      while (BufferCount--) 
  87  *        putc(RingBuffer_Remove(&Buffer)); 
  93 #ifndef __RING_BUFFER_H__ 
  94 #define __RING_BUFFER_H__ 
  97                 #include "../../Common/Common.h" 
  99         /* Enable C linkage for C++ Compilers: */ 
 100                 #if defined(__cplusplus) 
 105                 /** \brief Ring Buffer Management Structure. 
 107                  *  Type define for a new ring buffer object. Buffers should be initialized via a call to 
 108                  *  \ref RingBuffer_InitBuffer() before use. 
 112                         uint8_t* In
; /**< Current storage location in the circular buffer. */ 
 113                         uint8_t* Out
; /**< Current retrieval location in the circular buffer. */ 
 114                         uint8_t* Start
; /**< Pointer to the start of the buffer's underlying storage array. */ 
 115                         uint8_t* End
; /**< Pointer to the end of the buffer's underlying storage array. */ 
 116                         uint16_t Size
; /**< Size of the buffer's underlying storage array. */ 
 117                         uint16_t Count
; /**< Number of bytes currently stored in the buffer. */ 
 120         /* Inline Functions: */ 
 121                 /** Initializes a ring buffer ready for use. Buffers must be initialized via this function 
 122                  *  before any operations are called upon them. Already initialized buffers may be reset 
 123                  *  by re-initializing them using this function. 
 125                  *  \param[out] Buffer   Pointer to a ring buffer structure to initialize. 
 126                  *  \param[out] DataPtr  Pointer to a global array that will hold the data stored into the ring buffer. 
 127                  *  \param[out] Size     Maximum number of bytes that can be stored in the underlying data array. 
 129                 static inline void RingBuffer_InitBuffer(RingBuffer_t
* Buffer
, uint8_t* const DataPtr
, const uint16_t Size
) 
 130                                                          ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2); 
 131                 static inline void RingBuffer_InitBuffer(RingBuffer_t
* Buffer
, uint8_t* const DataPtr
, const uint16_t Size
) 
 133                         GCC_FORCE_POINTER_ACCESS(Buffer
); 
 135                         uint_reg_t CurrentGlobalInt 
= GetGlobalInterruptMask(); 
 136                         GlobalInterruptDisable(); 
 138                         Buffer
->In     
= DataPtr
; 
 139                         Buffer
->Out    
= DataPtr
; 
 140                         Buffer
->Start  
= &DataPtr
[0]; 
 141                         Buffer
->End    
= &DataPtr
[Size
]; 
 145                         SetGlobalInterruptMask(CurrentGlobalInt
); 
 148                 /** Retrieves the current number of bytes stored in a particular buffer. This value is computed 
 149                  *  by entering an atomic lock on the buffer, so that the buffer cannot be modified while the 
 150                  *  computation takes place. This value should be cached when reading out the contents of the buffer, 
 151                  *  so that as small a time as possible is spent in an atomic lock. 
 153                  *  \note The value returned by this function is guaranteed to only be the minimum number of bytes 
 154                  *        stored in the given buffer; this value may change as other threads write new data, thus 
 155                  *        the returned number should be used only to determine how many successive reads may safely 
 156                  *        be performed on the buffer. 
 158                  *  \param[in] Buffer  Pointer to a ring buffer structure whose count is to be computed. 
 160                  *  \return Number of bytes currently stored in the buffer. 
 162                 static inline uint16_t RingBuffer_GetCount(RingBuffer_t
* const Buffer
) ATTR_WARN_UNUSED_RESULT 
ATTR_NON_NULL_PTR_ARG(1); 
 163                 static inline uint16_t RingBuffer_GetCount(RingBuffer_t
* const Buffer
) 
 167                         uint_reg_t CurrentGlobalInt 
= GetGlobalInterruptMask(); 
 168                         GlobalInterruptDisable(); 
 170                         Count 
= Buffer
->Count
; 
 172                         SetGlobalInterruptMask(CurrentGlobalInt
); 
 176                 /** Retrieves the free space in a particular buffer. This value is computed by entering an atomic lock 
 177                  *  on the buffer, so that the buffer cannot be modified while the computation takes place. 
 179                  *  \note The value returned by this function is guaranteed to only be the maximum number of bytes 
 180                  *        free in the given buffer; this value may change as other threads write new data, thus 
 181                  *        the returned number should be used only to determine how many successive writes may safely 
 182                  *        be performed on the buffer when there is a single writer thread. 
 184                  *  \param[in] Buffer  Pointer to a ring buffer structure whose free count is to be computed. 
 186                  *  \return Number of free bytes in the buffer. 
 188                 static inline uint16_t RingBuffer_GetFreeCount(RingBuffer_t
* const Buffer
) ATTR_WARN_UNUSED_RESULT 
ATTR_NON_NULL_PTR_ARG(1); 
 189                 static inline uint16_t RingBuffer_GetFreeCount(RingBuffer_t
* const Buffer
) 
 191                         return (Buffer
->Size 
- RingBuffer_GetCount(Buffer
)); 
 194                 /** Atomically determines if the specified ring buffer contains any data. This should 
 195                  *  be tested before removing data from the buffer, to ensure that the buffer does not 
 198                  *  If the data is to be removed in a loop, store the total number of bytes stored in the 
 199                  *  buffer (via a call to the \ref RingBuffer_GetCount() function) in a temporary variable 
 200                  *  to reduce the time spent in atomicity locks. 
 202                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to insert into. 
 204                  *  \return Boolean \c true if the buffer contains no free space, false otherwise. 
 206                 static inline bool RingBuffer_IsEmpty(RingBuffer_t
* const Buffer
) ATTR_WARN_UNUSED_RESULT 
ATTR_NON_NULL_PTR_ARG(1); 
 207                 static inline bool RingBuffer_IsEmpty(RingBuffer_t
* const Buffer
) 
 209                         return (RingBuffer_GetCount(Buffer
) == 0); 
 212                 /** Atomically determines if the specified ring buffer contains any free space. This should 
 213                  *  be tested before storing data to the buffer, to ensure that no data is lost due to a 
 216                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to insert into. 
 218                  *  \return Boolean \c true if the buffer contains no free space, false otherwise. 
 220                 static inline bool RingBuffer_IsFull(RingBuffer_t
* const Buffer
) ATTR_WARN_UNUSED_RESULT 
ATTR_NON_NULL_PTR_ARG(1); 
 221                 static inline bool RingBuffer_IsFull(RingBuffer_t
* const Buffer
) 
 223                         return (RingBuffer_GetCount(Buffer
) == Buffer
->Size
); 
 226                 /** Inserts an element into the ring buffer. 
 228                  *  \warning Only one execution thread (main program thread or an ISR) may insert into a single buffer 
 229                  *           otherwise data corruption may occur. Insertion and removal may occur from different execution 
 232                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to insert into. 
 233                  *  \param[in]     Data    Data element to insert into the buffer. 
 235                 static inline void RingBuffer_Insert(RingBuffer_t
* Buffer
, const uint8_t Data
) ATTR_NON_NULL_PTR_ARG(1); 
 236                 static inline void RingBuffer_Insert(RingBuffer_t
* Buffer
, const uint8_t Data
) 
 238                         GCC_FORCE_POINTER_ACCESS(Buffer
); 
 242                         if (++Buffer
->In 
== Buffer
->End
) 
 243                           Buffer
->In 
= Buffer
->Start
; 
 245                         uint_reg_t CurrentGlobalInt 
= GetGlobalInterruptMask(); 
 246                         GlobalInterruptDisable(); 
 250                         SetGlobalInterruptMask(CurrentGlobalInt
); 
 253                 /** Removes an element from the ring buffer. 
 255                  *  \warning Only one execution thread (main program thread or an ISR) may remove from a single buffer 
 256                  *           otherwise data corruption may occur. Insertion and removal may occur from different execution 
 259                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to retrieve from. 
 261                  *  \return Next data element stored in the buffer. 
 263                 static inline uint8_t RingBuffer_Remove(RingBuffer_t
* Buffer
) ATTR_NON_NULL_PTR_ARG(1); 
 264                 static inline uint8_t RingBuffer_Remove(RingBuffer_t
* Buffer
) 
 266                         GCC_FORCE_POINTER_ACCESS(Buffer
); 
 268                         uint8_t Data 
= *Buffer
->Out
; 
 270                         if (++Buffer
->Out 
== Buffer
->End
) 
 271                           Buffer
->Out 
= Buffer
->Start
; 
 273                         uint_reg_t CurrentGlobalInt 
= GetGlobalInterruptMask(); 
 274                         GlobalInterruptDisable(); 
 278                         SetGlobalInterruptMask(CurrentGlobalInt
); 
 283                 /** Returns the next element stored in the ring buffer, without removing it. 
 285                  *  \param[in,out] Buffer  Pointer to a ring buffer structure to retrieve from. 
 287                  *  \return Next data element stored in the buffer. 
 289                 static inline uint8_t RingBuffer_Peek(RingBuffer_t
* const Buffer
) ATTR_WARN_UNUSED_RESULT 
ATTR_NON_NULL_PTR_ARG(1); 
 290                 static inline uint8_t RingBuffer_Peek(RingBuffer_t
* const Buffer
) 
 295         /* Disable C linkage for C++ Compilers: */ 
 296                 #if defined(__cplusplus)