ring_buffer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include "ring_buffer.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /*****************************************************************************
  36. * Public types/enumerations/variables
  37. ****************************************************************************/
  38. /*****************************************************************************
  39. * Private functions
  40. ****************************************************************************/
  41. /*****************************************************************************
  42. * Public functions
  43. ****************************************************************************/
  44. /* Initialize ring buffer */
  45. void RingBuffer_Init(RINGBUFF_T *RingBuff, void *buffer, int itemSize, int count)
  46. {
  47. RingBuff->bufferBase = RingBuff->bufferIn = RingBuff->bufferOut = buffer;
  48. RingBuff->bufferLast = RingBuff->bufferBase + (itemSize * count);
  49. RingBuff->count = count;
  50. RingBuff->itemSize = itemSize;
  51. RingBuff->used = 0;
  52. }
  53. /* Return empty status of ring buffer */
  54. bool RingBuffer_Insert8(RINGBUFF_T *RingBuff, uint8_t data8)
  55. {
  56. bool full = RingBuffer_IsFull(RingBuff);
  57. if (!full) {
  58. *RingBuff->bufferIn = data8;
  59. RingBuff->used++;
  60. RingBuff->bufferIn++;
  61. if (RingBuff->bufferIn >= RingBuff->bufferLast) {
  62. RingBuff->bufferIn = RingBuff->bufferBase;
  63. }
  64. }
  65. return (bool) !full;
  66. }
  67. /* Insert 16-bit value in ring buffer */
  68. bool RingBuffer_Insert16(RINGBUFF_T *RingBuff, uint16_t data16)
  69. {
  70. bool full = RingBuffer_IsFull(RingBuff);
  71. if (!full) {
  72. uint16_t *buff16 = (uint16_t *) RingBuff->bufferIn;
  73. *buff16 = data16;
  74. RingBuff->used++;
  75. buff16++;
  76. RingBuff->bufferIn = (uint8_t *) buff16;
  77. if (RingBuff->bufferIn >= RingBuff->bufferLast) {
  78. RingBuff->bufferIn = RingBuff->bufferBase;
  79. }
  80. }
  81. return (bool) !full;
  82. }
  83. /* Insert 32-bit value in ring buffer */
  84. bool RingBuffer_Insert32(RINGBUFF_T *RingBuff, uint32_t data32)
  85. {
  86. bool full = RingBuffer_IsFull(RingBuff);
  87. if (!full) {
  88. uint32_t *buff32 = (uint32_t *) RingBuff->bufferIn;
  89. *buff32 = data32;
  90. RingBuff->used++;
  91. buff32++;
  92. RingBuff->bufferIn = (uint8_t *) buff32;
  93. if (RingBuff->bufferIn >= RingBuff->bufferLast) {
  94. RingBuff->bufferIn = RingBuff->bufferBase;
  95. }
  96. }
  97. return (bool) !full;
  98. }
  99. /* Pop a 8-bit value from the ring buffer */
  100. bool RingBuffer_Pop8(RINGBUFF_T *RingBuff, uint8_t *data8)
  101. {
  102. bool empty = RingBuffer_IsEmpty(RingBuff);
  103. if (!empty) {
  104. *data8 = *RingBuff->bufferOut;
  105. RingBuff->used--;
  106. RingBuff->bufferOut++;
  107. if (RingBuff->bufferOut >= RingBuff->bufferLast) {
  108. RingBuff->bufferOut = RingBuff->bufferBase;
  109. }
  110. }
  111. return (bool) !empty;
  112. }
  113. /* Pop a 16-bit value from the ring buffer */
  114. bool RingBuffer_Pop16(RINGBUFF_T *RingBuff, uint16_t *data16)
  115. {
  116. bool empty = RingBuffer_IsEmpty(RingBuff);
  117. if (!empty) {
  118. uint16_t *buff16 = (uint16_t *) RingBuff->bufferOut;
  119. *data16 = *buff16;
  120. RingBuff->used--;
  121. buff16++;
  122. RingBuff->bufferOut = (uint8_t *) buff16;
  123. if (RingBuff->bufferOut >= RingBuff->bufferLast) {
  124. RingBuff->bufferOut = RingBuff->bufferBase;
  125. }
  126. }
  127. return (bool) !empty;
  128. }
  129. /* Pop a 32-bit value from the ring buffer */
  130. bool RingBuffer_Pop32(RINGBUFF_T *RingBuff, uint32_t *data32)
  131. {
  132. bool empty = RingBuffer_IsEmpty(RingBuff);
  133. if (!empty) {
  134. uint32_t *buff32 = (uint32_t *) RingBuff->bufferOut;
  135. *data32 = *buff32;
  136. RingBuff->used--;
  137. data32++;
  138. RingBuff->bufferOut = (uint8_t *) data32;
  139. if (RingBuff->bufferOut >= RingBuff->bufferLast) {
  140. RingBuff->bufferOut = RingBuff->bufferBase;
  141. }
  142. }
  143. return (bool) !empty;
  144. }