ring_buffer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. uint8_t *bufferBase, *bufferLast;
  43. uint8_t *bufferIn, *bufferOut;
  44. int count, used, itemSize;
  45. } RINGBUFF_T;
  46. /**
  47. * @brief Initialize ring buffer
  48. * @param RingBuff : Pointer to ring buffer to initialize
  49. * @param buffer : Pointer to buffer to associate with RingBuff
  50. * @param itemSize : Size of each buffer item size (1, 2 or 4 bytes)
  51. * @param count : Size of ring buffer
  52. * @return Nothing
  53. */
  54. void RingBuffer_Init(RINGBUFF_T *RingBuff, void *buffer, int itemSize, int count);
  55. /**
  56. * @brief Return number of items in the ring buffer
  57. * @param RingBuff : Pointer to ring buffer
  58. * @return Number of items in the ring buffer
  59. */
  60. STATIC INLINE int RingBuffer_GetCount(RINGBUFF_T *RingBuff)
  61. {
  62. return RingBuff->used;
  63. }
  64. /**
  65. * @brief Return number of items in the ring buffer
  66. * @param RingBuff : Pointer to ring buffer
  67. * @return true if the ring buffer is full, otherwise false
  68. */
  69. STATIC INLINE bool RingBuffer_IsFull(RINGBUFF_T *RingBuff)
  70. {
  71. return (bool) (RingBuff->used >= RingBuff->count);
  72. }
  73. /**
  74. * @brief Return empty status of ring buffer
  75. * @param RingBuff : Pointer to ring buffer
  76. * @return true if the ring buffer is empty, otherwise false
  77. */
  78. STATIC INLINE bool RingBuffer_IsEmpty(RINGBUFF_T *RingBuff)
  79. {
  80. return (bool) (RingBuff->used == 0);
  81. }
  82. /**
  83. * @brief Insert 8-bit value in ring buffer
  84. * @param RingBuff : Pointer to ring buffer
  85. * @param data8 : Byte to insert in ring buffer
  86. * @return true if a valid byte was inserted, or false if the ring buffer was full
  87. */
  88. bool RingBuffer_Insert8(RINGBUFF_T *RingBuff, uint8_t data8);
  89. /**
  90. * @brief Insert 16-bit value in ring buffer
  91. * @param RingBuff : Pointer to ring buffer
  92. * @param data16 : 16-bit value to insert in ring buffer
  93. * @return true if valid data was inserted, or false if the ring buffer was full
  94. */
  95. bool RingBuffer_Insert16(RINGBUFF_T *RingBuff, uint16_t data16);
  96. /**
  97. * @brief Insert 32-bit value in ring buffer
  98. * @param RingBuff : Pointer to ring buffer
  99. * @param data32 : 32-bit value to insert in ring buffer
  100. * @return true if valid data was inserted, or false if the ring buffer was full
  101. */
  102. bool RingBuffer_Insert32(RINGBUFF_T *RingBuff, uint32_t data32);
  103. /**
  104. * @brief Pop a 8-bit value from the ring buffer
  105. * @param RingBuff : Pointer to ring buffer
  106. * @param data8 : Pointer to where to place value
  107. * @return true if a valid byte was popped, or false if the ring buffer was empty
  108. */
  109. bool RingBuffer_Pop8(RINGBUFF_T *RingBuff, uint8_t *data8);
  110. /**
  111. * @brief Pop a 16-bit value from the ring buffer
  112. * @param RingBuff : Pointer to ring buffer
  113. * @param data16 : Pointer to where to place value
  114. * @return true if a valid byte was popped, or false if the ring buffer was empty
  115. */
  116. bool RingBuffer_Pop16(RINGBUFF_T *RingBuff, uint16_t *data16);
  117. /**
  118. * @brief Pop a 32-bit value from the ring buffer
  119. * @param RingBuff : Pointer to ring buffer
  120. * @param data32 : Pointer to where to place value
  121. * @return true if a valid byte was popped, or false if the ring buffer was empty
  122. */
  123. bool RingBuffer_Pop32(RINGBUFF_T *RingBuff, uint32_t *data32);
  124. /**
  125. * @}
  126. */
  127. #endif /* __RING_BUFFER_H_ */