1
0

ring_buffer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <stdint.h>
  34. #include <string.h>
  35. #define RINGBUF_IRQ_SAFE
  36. #ifdef RINGBUF_IRQ_SAFE
  37. #include <cmsis.h>
  38. #define INIT_CRITICAL() uint32_t priMask = __get_PRIMASK()
  39. #define ENTER_CRITICAL() __set_PRIMASK(1)
  40. #define LEAVE_CRITICAL() __set_PRIMASK(priMask)
  41. #else
  42. #define INIT_CRITICAL()
  43. #define ENTER_CRITICAL()
  44. #define LEAVE_CRITICAL()
  45. #endif
  46. typedef struct
  47. {
  48. uint8_t *pBuf;
  49. uint32_t size;
  50. uint32_t cnt;
  51. uint32_t rNdx;
  52. uint32_t wNdx;
  53. } ring_buffer_t, RINGBUFF_T;
  54. #ifndef MIN
  55. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  56. #endif /* ifndef MIN */
  57. /**
  58. * @brief Create and initialize a ring buffer
  59. * @param pRB : pointer to ring buffer instance
  60. * @param pBuffer: pointer to the buffer for ring buffer data
  61. * @param size: The size of buffer pointed by pBuffer
  62. * @return >=0:Success ; <0:Failed
  63. */
  64. int32_t RingBuf_Init(ring_buffer_t *pRB, uint8_t *pBuffer, uint32_t size);
  65. /**
  66. * @brief Write new data to buffer
  67. * @param pRB : pointer to the ring buffer instance
  68. * @param pcData: point to data array that will be written to ring buffer
  69. * @param dataBytes: bytes to write
  70. * @return >=0:Bytes written ; <0:Failed
  71. * @remark This function updates the ring buffer
  72. */
  73. int32_t RingBuf_Write(ring_buffer_t* pRB, const uint8_t *pcData, uint32_t dataBytes);
  74. /**
  75. * @brief Write 1 new byte data to buffer
  76. * @param pRB : pointer to the ring buffer instance
  77. * @param pcData: point to data byte that will be written to ring buffer
  78. * @return 1:success; otherwise failed
  79. * @remark This function updates the ring buffer. Optimized for byte-by-byte write
  80. */
  81. int32_t RingBuf_Write1Byte(ring_buffer_t* pRB, const uint8_t *pcData);
  82. /**
  83. * @brief Read (copy and remove) data from ring buffer
  84. * @param pRB : pointer to the ring buffer instance
  85. * @param pData : pointer to data array that receives read data
  86. * @param dataBytes: bytes to copy
  87. * @return >=0:Bytes read ; <0:Failed
  88. * @remark This function updates the ring buffer.
  89. */
  90. int32_t RingBuf_Read(ring_buffer_t* pRB, uint8_t *pData, uint32_t dataBytes);
  91. /**
  92. * @brief Read (copy and remove) 1 oldest byte data from buffer
  93. * @param pRB : pointer to the ring buffer instance
  94. * @param pData: point to data byte that will receive the oldest byte
  95. * @return 1:success ; otherwise failed
  96. * @remark This function updates the ring buffer. Optimized for byte-by-byte read
  97. */
  98. int32_t RingBuf_Read1Byte(ring_buffer_t* pRB, uint8_t *pData);
  99. /**
  100. * @brief Copy but does NOT remove data from ring buffer
  101. * @param pRB : pointer to the ring buffer instance
  102. * @param pData : pointer to data array that receives read data
  103. * @param dataBytes: bytes to read
  104. * @return >=0:Read bytes ; <0:Failed
  105. */
  106. int32_t RingBuf_Copy(ring_buffer_t* pRB, uint8_t *pData, uint32_t dataBytes);
  107. /**
  108. * @brief Get data pointer to oldest byte in ring buffer, and contigous byte count
  109. * @param pRB : pointer to the ring buffer instance
  110. * @param ppData : pointer to pointer variable that will be updated to point to oldest byte
  111. * @param contiguous_bytes: Congigous bytes until roll back
  112. * @return >=0:Contiuous bytes until roll back or whole data (if roll back won't happen) ; <0:Failed
  113. * @remak Use this function if performance is critical since it does NOT copy data
  114. * Use RingBuf_Free() to free (remove) data after use
  115. */
  116. int32_t RingBuf_Peek(ring_buffer_t* pRB, uint8_t **ppData);
  117. /**
  118. * @brief Free (remove) data from ring buffer
  119. * @param pRB : pointer to the ring buffer instance
  120. * @param bytesToFree : Bytes to free (remove)
  121. * @remak Use this function to free data after data get from RingBuf_Peek() is used
  122. */
  123. int32_t RingBuf_Free(ring_buffer_t* pRB, uint32_t bytesToFree);
  124. /**
  125. * @brief Get free bytes of ring buffer
  126. * @param pRB : pointer to the ring buffer instance
  127. * @return >=0:Free bytes ; <0:Failed
  128. */
  129. int32_t RingBuf_GetFreeBytes(ring_buffer_t* pRB);
  130. /**
  131. * @brief Get free bytes of ring buffer
  132. * @param pRB : pointer to the ring buffer instance
  133. * @return >=0:Used bytes ; <0:Failed
  134. */
  135. int32_t RingBuf_GetUsedBytes(ring_buffer_t* pRB);
  136. /**
  137. * @}
  138. */
  139. #endif /* __RING_BUFFER_H_ */