ring_buffer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * @brief Common ring buffer support functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __RING_BUFFER_H_
  32. #define __RING_BUFFER_H_
  33. #include "lpc_types.h"
  34. /** @defgroup Ring_Buffer CHIP: Simple ring buffer implementation
  35. * @ingroup CHIP_Common
  36. * @{
  37. */
  38. /**
  39. * @brief Ring buffer structure
  40. */
  41. typedef struct {
  42. void *data;
  43. int count;
  44. int itemSz;
  45. uint32_t head;
  46. uint32_t tail;
  47. } RINGBUFF_T;
  48. /**
  49. * @def RB_VHEAD(rb)
  50. * volatile typecasted head index
  51. */
  52. #define RB_VHEAD(rb) (*(volatile uint32_t *) &(rb)->head)
  53. /**
  54. * @def RB_VTAIL(rb)
  55. * volatile typecasted tail index
  56. */
  57. #define RB_VTAIL(rb) (*(volatile uint32_t *) &(rb)->tail)
  58. /**
  59. * @brief Initialize ring buffer
  60. * @param RingBuff : Pointer to ring buffer to initialize
  61. * @param buffer : Pointer to buffer to associate with RingBuff
  62. * @param itemSize : Size of each buffer item size
  63. * @param count : Size of ring buffer
  64. * @note Memory pointed by @a buffer must have correct alignment of
  65. * @a itemSize, and @a count must be a power of 2 and must at
  66. * least be 2 or greater.
  67. * @return Nothing
  68. */
  69. int RingBuffer_Init(RINGBUFF_T *RingBuff, void *buffer, int itemSize, int count);
  70. /**
  71. * @brief Resets the ring buffer to empty
  72. * @param RingBuff : Pointer to ring buffer
  73. * @return Nothing
  74. */
  75. STATIC INLINE void RingBuffer_Flush(RINGBUFF_T *RingBuff)
  76. {
  77. RingBuff->head = RingBuff->tail = 0;
  78. }
  79. /**
  80. * @brief Return size the ring buffer
  81. * @param RingBuff : Pointer to ring buffer
  82. * @return Size of the ring buffer in bytes
  83. */
  84. STATIC INLINE int RingBuffer_GetSize(RINGBUFF_T *RingBuff)
  85. {
  86. return RingBuff->count;
  87. }
  88. /**
  89. * @brief Return number of items in the ring buffer
  90. * @param RingBuff : Pointer to ring buffer
  91. * @return Number of items in the ring buffer
  92. */
  93. STATIC INLINE int RingBuffer_GetCount(RINGBUFF_T *RingBuff)
  94. {
  95. return RB_VHEAD(RingBuff) - RB_VTAIL(RingBuff);
  96. }
  97. /**
  98. * @brief Return number of free items in the ring buffer
  99. * @param RingBuff : Pointer to ring buffer
  100. * @return Number of free items in the ring buffer
  101. */
  102. STATIC INLINE int RingBuffer_GetFree(RINGBUFF_T *RingBuff)
  103. {
  104. return RingBuff->count - RingBuffer_GetCount(RingBuff);
  105. }
  106. /**
  107. * @brief Return number of items in the ring buffer
  108. * @param RingBuff : Pointer to ring buffer
  109. * @return 1 if the ring buffer is full, otherwise 0
  110. */
  111. STATIC INLINE int RingBuffer_IsFull(RINGBUFF_T *RingBuff)
  112. {
  113. return (RingBuffer_GetCount(RingBuff) >= RingBuff->count);
  114. }
  115. /**
  116. * @brief Return empty status of ring buffer
  117. * @param RingBuff : Pointer to ring buffer
  118. * @return 1 if the ring buffer is empty, otherwise 0
  119. */
  120. STATIC INLINE int RingBuffer_IsEmpty(RINGBUFF_T *RingBuff)
  121. {
  122. return RB_VHEAD(RingBuff) == RB_VTAIL(RingBuff);
  123. }
  124. /**
  125. * @brief Insert a single item into ring buffer
  126. * @param RingBuff : Pointer to ring buffer
  127. * @param data : pointer to item
  128. * @return 1 when successfully inserted,
  129. * 0 on error (Buffer not initialized using
  130. * RingBuffer_Init() or attempted to insert
  131. * when buffer is full)
  132. */
  133. int RingBuffer_Insert(RINGBUFF_T *RingBuff, const void *data);
  134. /**
  135. * @brief Insert an array of items into ring buffer
  136. * @param RingBuff : Pointer to ring buffer
  137. * @param data : Pointer to first element of the item array
  138. * @param num : Number of items in the array
  139. * @return number of items successfully inserted,
  140. * 0 on error (Buffer not initialized using
  141. * RingBuffer_Init() or attempted to insert
  142. * when buffer is full)
  143. */
  144. int RingBuffer_InsertMult(RINGBUFF_T *RingBuff, const void *data, int num);
  145. /**
  146. * @brief Pop an item from the ring buffer
  147. * @param RingBuff : Pointer to ring buffer
  148. * @param data : Pointer to memory where popped item be stored
  149. * @return 1 when item popped successfuly onto @a data,
  150. * 0 When error (Buffer not initialized using
  151. * RingBuffer_Init() or attempted to pop item when
  152. * the buffer is empty)
  153. */
  154. int RingBuffer_Pop(RINGBUFF_T *RingBuff, void *data);
  155. /**
  156. * @brief Pop an array of items from the ring buffer
  157. * @param RingBuff : Pointer to ring buffer
  158. * @param data : Pointer to memory where popped items be stored
  159. * @param num : Max number of items array @a data can hold
  160. * @return Number of items popped onto @a data,
  161. * 0 on error (Buffer not initialized using RingBuffer_Init()
  162. * or attempted to pop when the buffer is empty)
  163. */
  164. int RingBuffer_PopMult(RINGBUFF_T *RingBuff, void *data, int num);
  165. /**
  166. * @}
  167. */
  168. #endif /* __RING_BUFFER_H_ */