ringbuf.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "freertos/semphr.h"
  18. #include "freertos/ringbuf.h"
  19. //32-bit alignment macros
  20. #define rbALIGN_SIZE( xSize ) ( ( xSize + portBYTE_ALIGNMENT_MASK ) & ~portBYTE_ALIGNMENT_MASK )
  21. #define rbCHECK_ALIGNED( pvPtr ) ( ( ( UBaseType_t ) pvPtr & portBYTE_ALIGNMENT_MASK ) == 0 )
  22. //Ring buffer flags
  23. #define rbALLOW_SPLIT_FLAG ( ( UBaseType_t ) 1 ) //The ring buffer allows items to be split
  24. #define rbBYTE_BUFFER_FLAG ( ( UBaseType_t ) 2 ) //The ring buffer is a byte buffer
  25. #define rbBUFFER_FULL_FLAG ( ( UBaseType_t ) 4 ) //The ring buffer is currently full (write pointer == free pointer)
  26. //Item flags
  27. #define rbITEM_FREE_FLAG ( ( UBaseType_t ) 1 ) //Item has been retrieved and returned by application, free to overwrite
  28. #define rbITEM_DUMMY_DATA_FLAG ( ( UBaseType_t ) 2 ) //Data from here to end of the ring buffer is dummy data. Restart reading at start of head of the buffer
  29. #define rbITEM_SPLIT_FLAG ( ( UBaseType_t ) 4 ) //Valid for RINGBUF_TYPE_ALLOWSPLIT, indicating that rest of the data is wrapped around
  30. typedef struct {
  31. //This size of this structure must be 32-bit aligned
  32. size_t xItemLen;
  33. UBaseType_t uxItemFlags;
  34. } ItemHeader_t;
  35. #define rbHEADER_SIZE sizeof(ItemHeader_t)
  36. typedef struct Ringbuffer_t Ringbuffer_t;
  37. typedef BaseType_t (*CheckItemFitsFunction_t)(Ringbuffer_t *pxRingbuffer, size_t xItemSize);
  38. typedef void (*CopyItemFunction_t)(Ringbuffer_t *pxRingbuffer, const uint8_t *pcItem, size_t xItemSize);
  39. typedef BaseType_t (*CheckItemAvailFunction_t) (Ringbuffer_t *pxRingbuffer);
  40. typedef void *(*GetItemFunction_t)(Ringbuffer_t *pxRingbuffer, BaseType_t *pxIsSplit, size_t xMaxSize, size_t *pxItemSize);
  41. typedef void (*ReturnItemFunction_t)(Ringbuffer_t *pxRingbuffer, uint8_t *pvItem);
  42. typedef size_t (*GetCurMaxSizeFunction_t)(Ringbuffer_t *pxRingbuffer);
  43. struct Ringbuffer_t {
  44. size_t xSize; //Size of the data storage
  45. UBaseType_t uxRingbufferFlags; //Flags to indicate the type and status of ring buffer
  46. size_t xMaxItemSize; //Maximum item size
  47. CheckItemFitsFunction_t xCheckItemFits; //Function to check if item can currently fit in ring buffer
  48. CopyItemFunction_t vCopyItem; //Function to copy item to ring buffer
  49. GetItemFunction_t pvGetItem; //Function to get item from ring buffer
  50. ReturnItemFunction_t vReturnItem; //Function to return item to ring buffer
  51. GetCurMaxSizeFunction_t xGetCurMaxSize; //Function to get current free size
  52. uint8_t *pucWrite; //Write Pointer. Points to where the next item should be written
  53. uint8_t *pucRead; //Read Pointer. Points to where the next item should be read from
  54. uint8_t *pucFree; //Free Pointer. Points to the last item that has yet to be returned to the ring buffer
  55. uint8_t *pucHead; //Pointer to the start of the ring buffer storage area
  56. uint8_t *pucTail; //Pointer to the end of the ring buffer storage area
  57. BaseType_t xItemsWaiting; //Number of items/bytes(for byte buffers) currently in ring buffer that have not yet been read
  58. SemaphoreHandle_t xFreeSpaceSemaphore; //Binary semaphore, wakes up writing threads when more free space becomes available or when another thread times out attempting to write
  59. SemaphoreHandle_t xItemsBufferedSemaphore; //Binary semaphore, indicates there are new packets in the circular buffer. See remark.
  60. portMUX_TYPE mux; //Spinlock required for SMP
  61. };
  62. /*
  63. Remark: A counting semaphore for items_buffered_sem would be more logical, but counting semaphores in
  64. FreeRTOS need a maximum count, and allocate more memory the larger the maximum count is. Here, we
  65. would need to set the maximum to the maximum amount of times a null-byte unit first in the buffer,
  66. which is quite high and so would waste a fair amount of memory.
  67. */
  68. /* ------------------------------------------------ Static Declarations ------------------------------------------ */
  69. /*
  70. * WARNING: All of the following static functions (except generic functions)
  71. * ARE NOT THREAD SAFE. Therefore they should only be called within a critical
  72. * section (using spin locks)
  73. */
  74. //Calculate current amount of free space (in bytes) in the ring buffer
  75. static size_t prvGetFreeSize(Ringbuffer_t *pxRingbuffer);
  76. //Checks if an item/data is currently available for retrieval
  77. static BaseType_t prvCheckItemAvail(Ringbuffer_t *pxRingbuffer);
  78. //Checks if an item will currently fit in a no-split/allow-split ring buffer
  79. static BaseType_t prvCheckItemFitsDefault( Ringbuffer_t *pxRingbuffer, size_t xItemSize);
  80. //Checks if an item will currently fit in a byte buffer
  81. static BaseType_t prvCheckItemFitsByteBuffer( Ringbuffer_t *pxRingbuffer, size_t xItemSize);
  82. //Copies an item to a no-split ring buffer. Only call this function after calling prvCheckItemFitsDefault()
  83. static void prvCopyItemNoSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize);
  84. //Copies an item to a allow-split ring buffer. Only call this function after calling prvCheckItemFitsDefault()
  85. static void prvCopyItemAllowSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize);
  86. //Copies an item to a byte buffer. Only call this function after calling prvCheckItemFitsByteBuffer()
  87. static void prvCopyItemByteBuf(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize);
  88. //Retrieve item from no-split/allow-split ring buffer. *pxIsSplit is set to pdTRUE if the retrieved item is split
  89. static void *prvGetItemDefault(Ringbuffer_t *pxRingbuffer, BaseType_t *pxIsSplit, size_t xUnusedParam, size_t *pxItemSize);
  90. //Retrieve data from byte buffer. If xMaxSize is 0, all continuous data is retrieved
  91. static void *prvGetItemByteBuf(Ringbuffer_t *pxRingbuffer, BaseType_t *pxUnusedParam ,size_t xMaxSize, size_t *pxItemSize);
  92. //Return an item to a split/no-split ring buffer
  93. static void prvReturnItemDefault(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem);
  94. //Return data to a byte buffer
  95. static void prvReturnItemByteBuf(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem);
  96. //Get the maximum size an item that can currently have if sent to a no-split ring buffer
  97. static size_t prvGetCurMaxSizeNoSplit(Ringbuffer_t *pxRingbuffer);
  98. //Get the maximum size an item that can currently have if sent to a allow-split ring buffer
  99. static size_t prvGetCurMaxSizeAllowSplit(Ringbuffer_t *pxRingbuffer);
  100. //Get the maximum size an item that can currently have if sent to a byte buffer
  101. static size_t prvGetCurMaxSizeByteBuf(Ringbuffer_t *pxRingbuffer);
  102. /**
  103. * Generic function used to retrieve an item/data from ring buffers. If called on
  104. * an allow-split buffer, and pvItem2 and xItemSize2 are not NULL, both parts of
  105. * a split item will be retrieved. xMaxSize will only take effect if called on
  106. * byte buffers.
  107. */
  108. static BaseType_t prvReceiveGeneric(Ringbuffer_t *pxRingbuffer, void **pvItem1, void **pvItem2, size_t *xItemSize1, size_t *xItemSize2, size_t xMaxSize, TickType_t xTicksToWait);
  109. //Generic function used to retrieve an item/data from ring buffers in an ISR
  110. static BaseType_t prvReceiveGenericFromISR(Ringbuffer_t *pxRingbuffer, void **pvItem1, void **pvItem2, size_t *xItemSize1, size_t *xItemSize2, size_t xMaxSize);
  111. /* ------------------------------------------------ Static Definitions ------------------------------------------- */
  112. static size_t prvGetFreeSize(Ringbuffer_t *pxRingbuffer)
  113. {
  114. size_t xReturn;
  115. if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
  116. xReturn = 0;
  117. } else {
  118. BaseType_t xFreeSize = pxRingbuffer->pucFree - pxRingbuffer->pucWrite;
  119. //Check if xFreeSize has underflowed
  120. if (xFreeSize <= 0) {
  121. xFreeSize += pxRingbuffer->xSize;
  122. }
  123. xReturn = xFreeSize;
  124. }
  125. configASSERT(xReturn <= pxRingbuffer->xSize);
  126. return xReturn;
  127. }
  128. static BaseType_t prvCheckItemFitsDefault( Ringbuffer_t *pxRingbuffer, size_t xItemSize)
  129. {
  130. //Check arguments and buffer state
  131. configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucWrite)); //pucWrite is always aligned in no-split ring buffers
  132. configASSERT(pxRingbuffer->pucWrite >= pxRingbuffer->pucHead && pxRingbuffer->pucWrite < pxRingbuffer->pucTail); //Check write pointer is within bounds
  133. size_t xTotalItemSize = rbALIGN_SIZE(xItemSize) + rbHEADER_SIZE; //Rounded up aligned item size with header
  134. if (pxRingbuffer->pucWrite == pxRingbuffer->pucFree) {
  135. //Buffer is either complete empty or completely full
  136. return (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) ? pdFALSE : pdTRUE;
  137. }
  138. if (pxRingbuffer->pucFree > pxRingbuffer->pucWrite) {
  139. //Free space does not wrap around
  140. return (xTotalItemSize <= pxRingbuffer->pucFree - pxRingbuffer->pucWrite) ? pdTRUE : pdFALSE;
  141. }
  142. //Free space wraps around
  143. if (xTotalItemSize <= pxRingbuffer->pucTail - pxRingbuffer->pucWrite) {
  144. return pdTRUE; //Item fits without wrapping around
  145. }
  146. //Check if item fits by wrapping
  147. if (pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG) {
  148. //Allow split wrapping incurs an extra header
  149. return (xTotalItemSize + rbHEADER_SIZE <= pxRingbuffer->xSize - (pxRingbuffer->pucWrite - pxRingbuffer->pucFree)) ? pdTRUE : pdFALSE;
  150. } else {
  151. return (xTotalItemSize <= pxRingbuffer->pucFree - pxRingbuffer->pucHead) ? pdTRUE : pdFALSE;
  152. }
  153. }
  154. static BaseType_t prvCheckItemFitsByteBuffer( Ringbuffer_t *pxRingbuffer, size_t xItemSize)
  155. {
  156. //Check arguments and buffer state
  157. configASSERT(pxRingbuffer->pucWrite >= pxRingbuffer->pucHead && pxRingbuffer->pucWrite < pxRingbuffer->pucTail); //Check write pointer is within bounds
  158. if (pxRingbuffer->pucWrite == pxRingbuffer->pucFree) {
  159. //Buffer is either complete empty or completely full
  160. return (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) ? pdFALSE : pdTRUE;
  161. }
  162. if (pxRingbuffer->pucFree > pxRingbuffer->pucWrite) {
  163. //Free space does not wrap around
  164. return (xItemSize <= pxRingbuffer->pucFree - pxRingbuffer->pucWrite) ? pdTRUE : pdFALSE;
  165. }
  166. //Free space wraps around
  167. return (xItemSize <= pxRingbuffer->xSize - (pxRingbuffer->pucWrite - pxRingbuffer->pucFree)) ? pdTRUE : pdFALSE;
  168. }
  169. static void prvCopyItemNoSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize)
  170. {
  171. //Check arguments and buffer state
  172. size_t xAlignedItemSize = rbALIGN_SIZE(xItemSize); //Rounded up aligned item size
  173. size_t xRemLen = pxRingbuffer->pucTail - pxRingbuffer->pucWrite; //Length from pucWrite until end of buffer
  174. configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucWrite)); //pucWrite is always aligned in no-split ring buffers
  175. configASSERT(pxRingbuffer->pucWrite >= pxRingbuffer->pucHead && pxRingbuffer->pucWrite < pxRingbuffer->pucTail); //Check write pointer is within bounds
  176. configASSERT(xRemLen >= rbHEADER_SIZE); //Remaining length must be able to at least fit an item header
  177. //If remaining length can't fit item, set as dummy data and wrap around
  178. if (xRemLen < xAlignedItemSize + rbHEADER_SIZE) {
  179. ItemHeader_t *pxDummy = (ItemHeader_t *)pxRingbuffer->pucWrite;
  180. pxDummy->uxItemFlags = rbITEM_DUMMY_DATA_FLAG; //Set remaining length as dummy data
  181. pxDummy->xItemLen = 0; //Dummy data should have no length
  182. pxRingbuffer->pucWrite = pxRingbuffer->pucHead; //Reset write pointer to wrap around
  183. }
  184. //Item should be guaranteed to fit at this point. Set item header and copy data
  185. ItemHeader_t *pxHeader = (ItemHeader_t *)pxRingbuffer->pucWrite;
  186. pxHeader->xItemLen = xItemSize;
  187. pxHeader->uxItemFlags = 0;
  188. pxRingbuffer->pucWrite += rbHEADER_SIZE; //Advance pucWrite past header
  189. memcpy(pxRingbuffer->pucWrite, pucItem, xItemSize);
  190. pxRingbuffer->xItemsWaiting++;
  191. pxRingbuffer->pucWrite += xAlignedItemSize; //Advance pucWrite past item to next aligned address
  192. //If current remaining length can't fit a header, wrap around write pointer
  193. if (pxRingbuffer->pucTail - pxRingbuffer->pucWrite < rbHEADER_SIZE) {
  194. pxRingbuffer->pucWrite = pxRingbuffer->pucHead; //Wrap around pucWrite
  195. }
  196. //Check if buffer is full
  197. if (pxRingbuffer->pucWrite == pxRingbuffer->pucFree) {
  198. //Mark the buffer as full to distinguish with an empty buffer
  199. pxRingbuffer->uxRingbufferFlags |= rbBUFFER_FULL_FLAG;
  200. }
  201. }
  202. static void prvCopyItemAllowSplit(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize)
  203. {
  204. //Check arguments and buffer state
  205. size_t xAlignedItemSize = rbALIGN_SIZE(xItemSize); //Rounded up aligned item size
  206. size_t xRemLen = pxRingbuffer->pucTail - pxRingbuffer->pucWrite; //Length from pucWrite until end of buffer
  207. configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucWrite)); //pucWrite is always aligned in split ring buffers
  208. configASSERT(pxRingbuffer->pucWrite >= pxRingbuffer->pucHead && pxRingbuffer->pucWrite < pxRingbuffer->pucTail); //Check write pointer is within bounds
  209. configASSERT(xRemLen >= rbHEADER_SIZE); //Remaining length must be able to at least fit an item header
  210. //Split item if necessary
  211. if (xRemLen < xAlignedItemSize + rbHEADER_SIZE) {
  212. //Write first part of the item
  213. ItemHeader_t *pxFirstHeader = (ItemHeader_t *)pxRingbuffer->pucWrite;
  214. pxFirstHeader->uxItemFlags = 0;
  215. pxFirstHeader->xItemLen = xRemLen - rbHEADER_SIZE; //Fill remaining length with first part
  216. pxRingbuffer->pucWrite += rbHEADER_SIZE; //Advance pucWrite past header
  217. xRemLen -= rbHEADER_SIZE;
  218. if (xRemLen > 0) {
  219. memcpy(pxRingbuffer->pucWrite, pucItem, xRemLen);
  220. pxRingbuffer->xItemsWaiting++;
  221. //Update item arguments to account for data already copied
  222. pucItem += xRemLen;
  223. xItemSize -= xRemLen;
  224. xAlignedItemSize -= xRemLen;
  225. pxFirstHeader->uxItemFlags |= rbITEM_SPLIT_FLAG; //There must be more data
  226. } else {
  227. //Remaining length was only large enough to fit header
  228. pxFirstHeader->uxItemFlags |= rbITEM_DUMMY_DATA_FLAG; //Item will completely be stored in 2nd part
  229. }
  230. pxRingbuffer->pucWrite = pxRingbuffer->pucHead; //Reset write pointer to start of buffer
  231. }
  232. //Item (whole or second part) should be guaranteed to fit at this point
  233. ItemHeader_t *pxSecondHeader = (ItemHeader_t *)pxRingbuffer->pucWrite;
  234. pxSecondHeader->xItemLen = xItemSize;
  235. pxSecondHeader->uxItemFlags = 0;
  236. pxRingbuffer->pucWrite += rbHEADER_SIZE; //Advance write pointer past header
  237. memcpy(pxRingbuffer->pucWrite, pucItem, xItemSize);
  238. pxRingbuffer->xItemsWaiting++;
  239. pxRingbuffer->pucWrite += xAlignedItemSize; //Advance pucWrite past item to next aligned address
  240. //If current remaining length can't fit a header, wrap around write pointer
  241. if (pxRingbuffer->pucTail - pxRingbuffer->pucWrite < rbHEADER_SIZE) {
  242. pxRingbuffer->pucWrite = pxRingbuffer->pucHead; //Wrap around pucWrite
  243. }
  244. //Check if buffer is full
  245. if (pxRingbuffer->pucWrite == pxRingbuffer->pucFree) {
  246. //Mark the buffer as full to distinguish with an empty buffer
  247. pxRingbuffer->uxRingbufferFlags |= rbBUFFER_FULL_FLAG;
  248. }
  249. }
  250. static void prvCopyItemByteBuf(Ringbuffer_t *pxRingbuffer, const uint8_t *pucItem, size_t xItemSize)
  251. {
  252. //Check arguments and buffer state
  253. configASSERT(pxRingbuffer->pucWrite >= pxRingbuffer->pucHead && pxRingbuffer->pucWrite < pxRingbuffer->pucTail); //Check write pointer is within bounds
  254. size_t xRemLen = pxRingbuffer->pucTail - pxRingbuffer->pucWrite; //Length from pucWrite until end of buffer
  255. if (xRemLen < xItemSize) {
  256. //Copy as much as possible into remaining length
  257. memcpy(pxRingbuffer->pucWrite, pucItem, xRemLen);
  258. pxRingbuffer->xItemsWaiting += xRemLen;
  259. //Update item arguments to account for data already written
  260. pucItem += xRemLen;
  261. xItemSize -= xRemLen;
  262. pxRingbuffer->pucWrite = pxRingbuffer->pucHead; //Reset write pointer to start of buffer
  263. }
  264. //Copy all or remaining portion of the item
  265. memcpy(pxRingbuffer->pucWrite, pucItem, xItemSize);
  266. pxRingbuffer->xItemsWaiting += xItemSize;
  267. pxRingbuffer->pucWrite += xItemSize;
  268. //Wrap around pucWrite if it reaches the end
  269. if (pxRingbuffer->pucWrite == pxRingbuffer->pucTail) {
  270. pxRingbuffer->pucWrite = pxRingbuffer->pucHead;
  271. }
  272. //Check if buffer is full
  273. if (pxRingbuffer->pucWrite == pxRingbuffer->pucFree) {
  274. pxRingbuffer->uxRingbufferFlags |= rbBUFFER_FULL_FLAG; //Mark the buffer as full to avoid confusion with an empty buffer
  275. }
  276. }
  277. static BaseType_t prvCheckItemAvail(Ringbuffer_t *pxRingbuffer)
  278. {
  279. if ((pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) && pxRingbuffer->pucRead != pxRingbuffer->pucFree) {
  280. return pdFALSE; //Byte buffers do not allow multiple retrievals before return
  281. }
  282. if ((pxRingbuffer->xItemsWaiting > 0) && ((pxRingbuffer->pucRead != pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG))) {
  283. return pdTRUE; //Items/data available for retrieval
  284. } else {
  285. return pdFALSE; //No items/data available for retrieval
  286. }
  287. }
  288. static void *prvGetItemDefault(Ringbuffer_t *pxRingbuffer, BaseType_t *pxIsSplit, size_t xUnusedParam, size_t *pxItemSize)
  289. {
  290. //Check arguments and buffer state
  291. ItemHeader_t *pxHeader = (ItemHeader_t *)pxRingbuffer->pucRead;
  292. configASSERT(pxIsSplit != NULL);
  293. configASSERT((pxRingbuffer->xItemsWaiting > 0) && ((pxRingbuffer->pucRead != pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG))); //Check there are items to be read
  294. configASSERT(rbCHECK_ALIGNED(pxRingbuffer->pucRead)); //pucRead is always aligned in split ring buffers
  295. configASSERT(pxRingbuffer->pucRead >= pxRingbuffer->pucHead && pxRingbuffer->pucRead < pxRingbuffer->pucTail); //Check read pointer is within bounds
  296. configASSERT((pxHeader->xItemLen <= pxRingbuffer->xMaxItemSize) || (pxHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG));
  297. uint8_t *pcReturn;
  298. //Wrap around if dummy data (dummy data indicates wrap around in no-split buffers)
  299. if (pxHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) {
  300. pxRingbuffer->pucRead = pxRingbuffer->pucHead;
  301. //Check for errors with the next item
  302. pxHeader = (ItemHeader_t *)pxRingbuffer->pucRead;
  303. configASSERT(pxHeader->xItemLen <= pxRingbuffer->xMaxItemSize);
  304. }
  305. pcReturn = pxRingbuffer->pucRead + rbHEADER_SIZE; //Get pointer to part of item containing data (point past the header)
  306. if (pxHeader->xItemLen == 0) {
  307. //Inclusive of pucTail for special case where item of zero length just fits at the end of the buffer
  308. configASSERT(pcReturn >= pxRingbuffer->pucHead && pcReturn <= pxRingbuffer->pucTail);
  309. } else {
  310. //Exclusive of pucTali if length is larger than zero, pcReturn should never point to pucTail
  311. configASSERT(pcReturn >= pxRingbuffer->pucHead && pcReturn < pxRingbuffer->pucTail);
  312. }
  313. *pxItemSize = pxHeader->xItemLen; //Get length of item
  314. pxRingbuffer->xItemsWaiting --; //Update item count
  315. *pxIsSplit = (pxHeader->uxItemFlags & rbITEM_SPLIT_FLAG) ? pdTRUE : pdFALSE;
  316. pxRingbuffer->pucRead += rbHEADER_SIZE + rbALIGN_SIZE(pxHeader->xItemLen); //Update pucRead
  317. //Check if pucRead requires wrap around
  318. if ((pxRingbuffer->pucTail - pxRingbuffer->pucRead) < rbHEADER_SIZE) {
  319. pxRingbuffer->pucRead = pxRingbuffer->pucHead;
  320. }
  321. return (void *)pcReturn;
  322. }
  323. static void *prvGetItemByteBuf(Ringbuffer_t *pxRingbuffer, BaseType_t *pxUnusedParam ,size_t xMaxSize, size_t *pxItemSize)
  324. {
  325. //Check arguments and buffer state
  326. configASSERT((pxRingbuffer->xItemsWaiting > 0) && ((pxRingbuffer->pucRead != pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG))); //Check there are items to be read
  327. configASSERT(pxRingbuffer->pucRead >= pxRingbuffer->pucHead && pxRingbuffer->pucRead < pxRingbuffer->pucTail); //Check read pointer is within bounds
  328. configASSERT(pxRingbuffer->pucRead == pxRingbuffer->pucFree);
  329. uint8_t *ret = pxRingbuffer->pucRead;
  330. if ((pxRingbuffer->pucRead > pxRingbuffer->pucWrite) || (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG)) { //Available data wraps around
  331. //Return contiguous piece from read pointer until buffer tail, or xMaxSize
  332. if (xMaxSize == 0 || pxRingbuffer->pucTail - pxRingbuffer->pucRead <= xMaxSize) {
  333. //All contiguous data from read pointer to tail
  334. *pxItemSize = pxRingbuffer->pucTail - pxRingbuffer->pucRead;
  335. pxRingbuffer->xItemsWaiting -= pxRingbuffer->pucTail - pxRingbuffer->pucRead;
  336. pxRingbuffer->pucRead = pxRingbuffer->pucHead; //Wrap around read pointer
  337. } else {
  338. //Return xMaxSize amount of data
  339. *pxItemSize = xMaxSize;
  340. pxRingbuffer->xItemsWaiting -= xMaxSize;
  341. pxRingbuffer->pucRead += xMaxSize; //Advance read pointer past retrieved data
  342. }
  343. } else { //Available data is contiguous between read and write pointer
  344. if (xMaxSize == 0 || pxRingbuffer->pucWrite - pxRingbuffer->pucRead <= xMaxSize) {
  345. //Return all contiguous data from read to write pointer
  346. *pxItemSize = pxRingbuffer->pucWrite - pxRingbuffer->pucRead;
  347. pxRingbuffer->xItemsWaiting -= pxRingbuffer->pucWrite - pxRingbuffer->pucRead;
  348. pxRingbuffer->pucRead = pxRingbuffer->pucWrite;
  349. } else {
  350. //Return xMaxSize data from read pointer
  351. *pxItemSize = xMaxSize;
  352. pxRingbuffer->xItemsWaiting -= xMaxSize;
  353. pxRingbuffer->pucRead += xMaxSize; //Advance read pointer past retrieved data
  354. }
  355. }
  356. return (void *)ret;
  357. }
  358. static void prvReturnItemDefault(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem)
  359. {
  360. //Check arguments and buffer state
  361. configASSERT(rbCHECK_ALIGNED(pucItem));
  362. configASSERT(pucItem >= pxRingbuffer->pucHead);
  363. configASSERT(pucItem <= pxRingbuffer->pucTail); //Inclusive of pucTail in the case of zero length item at the very end
  364. //Get and check header of the item
  365. ItemHeader_t *pxCurHeader = (ItemHeader_t *)(pucItem - rbHEADER_SIZE);
  366. configASSERT(pxCurHeader->xItemLen <= pxRingbuffer->xMaxItemSize);
  367. configASSERT((pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) == 0); //Dummy items should never have been read
  368. configASSERT((pxCurHeader->uxItemFlags & rbITEM_FREE_FLAG) == 0); //Indicates item has already been returned before
  369. pxCurHeader->uxItemFlags &= ~rbITEM_SPLIT_FLAG; //Clear wrap flag if set (not strictly necessary)
  370. pxCurHeader->uxItemFlags |= rbITEM_FREE_FLAG; //Mark as free
  371. /*
  372. * Items might not be returned in the order they were retrieved. Move the free pointer
  373. * up to the next item that has not been marked as free (by free flag) or up
  374. * till the read pointer. When advancing the free pointer, items that have already been
  375. * freed or items with dummy data should be skipped over
  376. */
  377. pxCurHeader = (ItemHeader_t *)pxRingbuffer->pucFree;
  378. //Skip over Items that have already been freed or are dummy items
  379. while (((pxCurHeader->uxItemFlags & rbITEM_FREE_FLAG) || (pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG)) && pxRingbuffer->pucFree != pxRingbuffer->pucRead) {
  380. if (pxCurHeader->uxItemFlags & rbITEM_DUMMY_DATA_FLAG) {
  381. pxCurHeader->uxItemFlags |= rbITEM_FREE_FLAG; //Mark as freed (not strictly necessary but adds redundancy)
  382. pxRingbuffer->pucFree = pxRingbuffer->pucHead; //Wrap around due to dummy data
  383. } else {
  384. //Item with data that has already been freed, advance free pointer past this item
  385. size_t xAlignedItemSize = rbALIGN_SIZE(pxCurHeader->xItemLen);
  386. pxRingbuffer->pucFree += xAlignedItemSize + rbHEADER_SIZE;
  387. //Redundancy check to ensure free pointer has not overshot buffer bounds
  388. configASSERT(pxRingbuffer->pucFree <= pxRingbuffer->pucHead + pxRingbuffer->xSize);
  389. }
  390. //Check if pucRead requires wrap around
  391. if ((pxRingbuffer->pucTail - pxRingbuffer->pucFree) < rbHEADER_SIZE) {
  392. pxRingbuffer->pucFree = pxRingbuffer->pucHead;
  393. }
  394. pxCurHeader = (ItemHeader_t *)pxRingbuffer->pucFree; //Update header to point to item
  395. }
  396. //Check if the buffer full flag should be reset
  397. if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
  398. if (pxRingbuffer->pucFree != pxRingbuffer->pucWrite) {
  399. pxRingbuffer->uxRingbufferFlags &= ~rbBUFFER_FULL_FLAG;
  400. } else if (pxRingbuffer->pucFree == pxRingbuffer->pucWrite && pxRingbuffer->pucFree == pxRingbuffer->pucRead) {
  401. //Special case where a full buffer is completely freed in one go
  402. pxRingbuffer->uxRingbufferFlags &= ~rbBUFFER_FULL_FLAG;
  403. }
  404. }
  405. }
  406. static void prvReturnItemByteBuf(Ringbuffer_t *pxRingbuffer, uint8_t *pucItem)
  407. {
  408. //Check pointer points to address inside buffer
  409. configASSERT((uint8_t *)pucItem >= pxRingbuffer->pucHead);
  410. configASSERT((uint8_t *)pucItem < pxRingbuffer->pucTail);
  411. //Free the read memory. Simply moves free pointer to read pointer as byte buffers do not allow multiple outstanding reads
  412. pxRingbuffer->pucFree = pxRingbuffer->pucRead;
  413. //If buffer was full before, reset full flag as free pointer has moved
  414. if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
  415. pxRingbuffer->uxRingbufferFlags &= ~rbBUFFER_FULL_FLAG;
  416. }
  417. }
  418. static size_t prvGetCurMaxSizeNoSplit(Ringbuffer_t *pxRingbuffer)
  419. {
  420. BaseType_t xFreeSize;
  421. //Check if buffer is full
  422. if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
  423. return 0;
  424. }
  425. if (pxRingbuffer->pucWrite < pxRingbuffer->pucFree) {
  426. //Free space is contiguous between pucWrite and pucFree
  427. xFreeSize = pxRingbuffer->pucFree - pxRingbuffer->pucWrite;
  428. } else {
  429. //Free space wraps around (or overlapped at pucHead), select largest
  430. //contiguous free space as no-split items require contiguous space
  431. size_t xSize1 = pxRingbuffer->pucTail - pxRingbuffer->pucWrite;
  432. size_t xSize2 = pxRingbuffer->pucFree - pxRingbuffer->pucHead;
  433. xFreeSize = (xSize1 > xSize2) ? xSize1 : xSize2;
  434. }
  435. //No-split ring buffer items need space for a header
  436. xFreeSize -= rbHEADER_SIZE;
  437. //Limit free size to be within bounds
  438. if (xFreeSize > pxRingbuffer->xMaxItemSize) {
  439. xFreeSize = pxRingbuffer->xMaxItemSize;
  440. } else if (xFreeSize < 0) {
  441. //Occurs when free space is less than header size
  442. xFreeSize = 0;
  443. }
  444. return xFreeSize;
  445. }
  446. static size_t prvGetCurMaxSizeAllowSplit(Ringbuffer_t *pxRingbuffer)
  447. {
  448. BaseType_t xFreeSize;
  449. //Check if buffer is full
  450. if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
  451. return 0;
  452. }
  453. if (pxRingbuffer->pucWrite == pxRingbuffer->pucHead && pxRingbuffer->pucFree == pxRingbuffer->pucHead) {
  454. //Check for special case where pucWrite and pucFree are both at pucHead
  455. xFreeSize = pxRingbuffer->xSize - rbHEADER_SIZE;
  456. } else if (pxRingbuffer->pucWrite < pxRingbuffer->pucFree) {
  457. //Free space is contiguous between pucWrite and pucFree, requires single header
  458. xFreeSize = (pxRingbuffer->pucFree - pxRingbuffer->pucWrite) - rbHEADER_SIZE;
  459. } else {
  460. //Free space wraps around, requires two headers
  461. xFreeSize = (pxRingbuffer->pucFree - pxRingbuffer->pucHead) +
  462. (pxRingbuffer->pucTail - pxRingbuffer->pucWrite) -
  463. (rbHEADER_SIZE * 2);
  464. }
  465. //Limit free size to be within bounds
  466. if (xFreeSize > pxRingbuffer->xMaxItemSize) {
  467. xFreeSize = pxRingbuffer->xMaxItemSize;
  468. } else if (xFreeSize < 0) {
  469. xFreeSize = 0;
  470. }
  471. return xFreeSize;
  472. }
  473. static size_t prvGetCurMaxSizeByteBuf(Ringbuffer_t *pxRingbuffer)
  474. {
  475. BaseType_t xFreeSize;
  476. //Check if buffer is full
  477. if (pxRingbuffer->uxRingbufferFlags & rbBUFFER_FULL_FLAG) {
  478. return 0;
  479. }
  480. /*
  481. * Return whatever space is available depending on relative positions of the free
  482. * pointer and write pointer. There is no overhead of headers in this mode
  483. */
  484. xFreeSize = pxRingbuffer->pucFree - pxRingbuffer->pucWrite;
  485. if (xFreeSize <= 0) {
  486. xFreeSize += pxRingbuffer->xSize;
  487. }
  488. return xFreeSize;
  489. }
  490. static BaseType_t prvReceiveGeneric(Ringbuffer_t *pxRingbuffer, void **pvItem1, void **pvItem2, size_t *xItemSize1, size_t *xItemSize2, size_t xMaxSize, TickType_t xTicksToWait)
  491. {
  492. BaseType_t xReturn = pdFALSE;
  493. BaseType_t xReturnSemaphore = pdFALSE;
  494. TickType_t xTicksEnd = xTaskGetTickCount() + xTicksToWait;
  495. TickType_t xTicksRemaining = xTicksToWait;
  496. while (xTicksRemaining <= xTicksToWait) { //xTicksToWait will underflow once xTaskGetTickCount() > ticks_end
  497. //Block until more free space becomes available or timeout
  498. if (xSemaphoreTake(pxRingbuffer->xItemsBufferedSemaphore, xTicksRemaining) != pdTRUE) {
  499. xReturn = pdFALSE; //Timed out attempting to get semaphore
  500. break;
  501. }
  502. //Semaphore obtained, check if item can be retrieved
  503. portENTER_CRITICAL(&pxRingbuffer->mux);
  504. if (prvCheckItemAvail(pxRingbuffer) == pdTRUE) {
  505. //Item is available for retrieval
  506. BaseType_t xIsSplit;
  507. if (pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) {
  508. //Second argument (pxIsSplit) is unused for byte buffers
  509. *pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, NULL, xMaxSize, xItemSize1);
  510. } else {
  511. //Third argument (xMaxSize) is unused for no-split/allow-split buffers
  512. *pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize1);
  513. }
  514. //Check for item split if configured to do so
  515. if ((pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG) && (pvItem2 != NULL) && (xItemSize2 != NULL)) {
  516. if (xIsSplit == pdTRUE) {
  517. *pvItem2 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize2);
  518. configASSERT(*pvItem2 < *pvItem1); //Check wrap around has occurred
  519. configASSERT(xIsSplit == pdFALSE); //Second part should not have wrapped flag
  520. } else {
  521. *pvItem2 = NULL;
  522. }
  523. }
  524. xReturn = pdTRUE;
  525. if (pxRingbuffer->xItemsWaiting > 0) {
  526. xReturnSemaphore = pdTRUE;
  527. }
  528. portEXIT_CRITICAL(&pxRingbuffer->mux);
  529. break;
  530. }
  531. //No item available for retrieval, adjust ticks and take the semaphore again
  532. if (xTicksToWait != portMAX_DELAY) {
  533. xTicksRemaining = xTicksEnd - xTaskGetTickCount();
  534. }
  535. portEXIT_CRITICAL(&pxRingbuffer->mux);
  536. /*
  537. * Gap between critical section and re-acquiring of the semaphore. If
  538. * semaphore is given now, priority inversion might occur (see docs)
  539. */
  540. }
  541. if (xReturnSemaphore == pdTRUE) {
  542. xSemaphoreGive(pxRingbuffer->xItemsBufferedSemaphore); //Give semaphore back so other tasks can retrieve
  543. }
  544. return xReturn;
  545. }
  546. static BaseType_t prvReceiveGenericFromISR(Ringbuffer_t *pxRingbuffer, void **pvItem1, void **pvItem2, size_t *xItemSize1, size_t *xItemSize2, size_t xMaxSize)
  547. {
  548. BaseType_t xReturn = pdFALSE;
  549. BaseType_t xReturnSemaphore = pdFALSE;
  550. portENTER_CRITICAL_ISR(&pxRingbuffer->mux);
  551. if(prvCheckItemAvail(pxRingbuffer) == pdTRUE) {
  552. BaseType_t xIsSplit;
  553. if (pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) {
  554. //Second argument (pxIsSplit) is unused for byte buffers
  555. *pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, NULL, xMaxSize, xItemSize1);
  556. } else {
  557. //Third argument (xMaxSize) is unused for no-split/allow-split buffers
  558. *pvItem1 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize1);
  559. }
  560. //Check for item split if configured to do so
  561. if ((pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG) && pvItem2 != NULL && xItemSize2 != NULL) {
  562. if (xIsSplit == pdTRUE) {
  563. *pvItem2 = pxRingbuffer->pvGetItem(pxRingbuffer, &xIsSplit, 0, xItemSize2);
  564. configASSERT(*pvItem2 < *pvItem1); //Check wrap around has occurred
  565. configASSERT(xIsSplit == pdFALSE); //Second part should not have wrapped flag
  566. } else {
  567. *pvItem2 = NULL;
  568. }
  569. }
  570. xReturn = pdTRUE;
  571. if (pxRingbuffer->xItemsWaiting > 0) {
  572. xReturnSemaphore = pdTRUE;
  573. }
  574. }
  575. portEXIT_CRITICAL_ISR(&pxRingbuffer->mux);
  576. if (xReturnSemaphore == pdTRUE) {
  577. xSemaphoreGiveFromISR(pxRingbuffer->xItemsBufferedSemaphore, NULL); //Give semaphore back so other tasks can retrieve
  578. }
  579. return xReturn;
  580. }
  581. /* ------------------------------------------------- Public Definitions -------------------------------------------- */
  582. RingbufHandle_t xRingbufferCreate(size_t xBufferSize, ringbuf_type_t xBufferType)
  583. {
  584. //Allocate memory
  585. Ringbuffer_t *pxRingbuffer = calloc(1, sizeof(Ringbuffer_t));
  586. if (pxRingbuffer == NULL) {
  587. goto err;
  588. }
  589. if (xBufferType != RINGBUF_TYPE_BYTEBUF) {
  590. xBufferSize = rbALIGN_SIZE(xBufferSize); //xBufferSize is rounded up for no-split/allow-split buffers
  591. }
  592. pxRingbuffer->pucHead = malloc(xBufferSize);
  593. if (pxRingbuffer->pucHead == NULL) {
  594. goto err;
  595. }
  596. //Initialize values
  597. pxRingbuffer->xSize = xBufferSize;
  598. pxRingbuffer->pucTail = pxRingbuffer->pucHead + xBufferSize;
  599. pxRingbuffer->pucFree = pxRingbuffer->pucHead;
  600. pxRingbuffer->pucRead = pxRingbuffer->pucHead;
  601. pxRingbuffer->pucWrite = pxRingbuffer->pucHead;
  602. pxRingbuffer->xItemsWaiting = 0;
  603. pxRingbuffer->xFreeSpaceSemaphore = xSemaphoreCreateBinary();
  604. pxRingbuffer->xItemsBufferedSemaphore = xSemaphoreCreateBinary();
  605. pxRingbuffer->uxRingbufferFlags = 0;
  606. //Initialize type dependent values and function pointers
  607. if (xBufferType == RINGBUF_TYPE_NOSPLIT) {
  608. pxRingbuffer->xCheckItemFits = prvCheckItemFitsDefault;
  609. pxRingbuffer->vCopyItem = prvCopyItemNoSplit;
  610. pxRingbuffer->pvGetItem = prvGetItemDefault;
  611. pxRingbuffer->vReturnItem = prvReturnItemDefault;
  612. /*
  613. * Buffer lengths are always aligned. No-split buffer (read/write/free)
  614. * pointers are also always aligned. Therefore worse case scenario is
  615. * the write pointer is at the most aligned halfway point.
  616. */
  617. pxRingbuffer->xMaxItemSize = rbALIGN_SIZE(pxRingbuffer->xSize / 2) - rbHEADER_SIZE;
  618. pxRingbuffer->xGetCurMaxSize = prvGetCurMaxSizeNoSplit;
  619. } else if (xBufferType == RINGBUF_TYPE_ALLOWSPLIT) {
  620. pxRingbuffer->uxRingbufferFlags |= rbALLOW_SPLIT_FLAG;
  621. pxRingbuffer->xCheckItemFits = prvCheckItemFitsDefault;
  622. pxRingbuffer->vCopyItem = prvCopyItemAllowSplit;
  623. pxRingbuffer->pvGetItem = prvGetItemDefault;
  624. pxRingbuffer->vReturnItem = prvReturnItemDefault;
  625. //Worst case an item is split into two, incurring two headers of overhead
  626. pxRingbuffer->xMaxItemSize = pxRingbuffer->xSize - (sizeof(ItemHeader_t) * 2);
  627. pxRingbuffer->xGetCurMaxSize = prvGetCurMaxSizeAllowSplit;
  628. } else if (xBufferType == RINGBUF_TYPE_BYTEBUF) {
  629. pxRingbuffer->uxRingbufferFlags |= rbBYTE_BUFFER_FLAG;
  630. pxRingbuffer->xCheckItemFits = prvCheckItemFitsByteBuffer;
  631. pxRingbuffer->vCopyItem = prvCopyItemByteBuf;
  632. pxRingbuffer->pvGetItem = prvGetItemByteBuf;
  633. pxRingbuffer->vReturnItem = prvReturnItemByteBuf;
  634. //Byte buffers do not incur any overhead
  635. pxRingbuffer->xMaxItemSize = pxRingbuffer->xSize;
  636. pxRingbuffer->xGetCurMaxSize = prvGetCurMaxSizeByteBuf;
  637. } else {
  638. //Unsupported type
  639. configASSERT(0);
  640. }
  641. if (pxRingbuffer->xFreeSpaceSemaphore == NULL || pxRingbuffer->xItemsBufferedSemaphore == NULL) {
  642. goto err;
  643. }
  644. xSemaphoreGive(pxRingbuffer->xFreeSpaceSemaphore);
  645. vPortCPUInitializeMutex(&pxRingbuffer->mux);
  646. return (RingbufHandle_t)pxRingbuffer;
  647. err:
  648. //Some error has happened. Free/destroy all allocated things and return NULL.
  649. if (pxRingbuffer) {
  650. free(pxRingbuffer->pucHead);
  651. if (pxRingbuffer->xFreeSpaceSemaphore) {
  652. vSemaphoreDelete(pxRingbuffer->xFreeSpaceSemaphore);
  653. }
  654. if (pxRingbuffer->xItemsBufferedSemaphore) {
  655. vSemaphoreDelete(pxRingbuffer->xItemsBufferedSemaphore);
  656. }
  657. }
  658. free(pxRingbuffer);
  659. return NULL;
  660. }
  661. RingbufHandle_t xRingbufferCreateNoSplit(size_t xItemSize, size_t xItemNum)
  662. {
  663. return xRingbufferCreate((rbALIGN_SIZE(xItemSize) + rbHEADER_SIZE) * xItemNum, RINGBUF_TYPE_NOSPLIT);
  664. }
  665. BaseType_t xRingbufferSend(RingbufHandle_t xRingbuffer, const void *pvItem, size_t xItemSize, TickType_t xTicksToWait)
  666. {
  667. //Check arguments
  668. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  669. configASSERT(pxRingbuffer);
  670. configASSERT(pvItem != NULL || xItemSize == 0);
  671. if (xItemSize > pxRingbuffer->xMaxItemSize) {
  672. return pdFALSE; //Data will never ever fit in the queue.
  673. }
  674. if ((pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) && xItemSize == 0) {
  675. return pdTRUE; //Sending 0 bytes to byte buffer has no effect
  676. }
  677. //Attempt to send an item
  678. BaseType_t xReturn = pdFALSE;
  679. BaseType_t xReturnSemaphore = pdFALSE;
  680. TickType_t xTicksEnd = xTaskGetTickCount() + xTicksToWait;
  681. TickType_t xTicksRemaining = xTicksToWait;
  682. while (xTicksRemaining <= xTicksToWait) { //xTicksToWait will underflow once xTaskGetTickCount() > ticks_end
  683. //Block until more free space becomes available or timeout
  684. if (xSemaphoreTake(pxRingbuffer->xFreeSpaceSemaphore, xTicksRemaining) != pdTRUE) {
  685. xReturn = pdFALSE;
  686. break;
  687. }
  688. //Semaphore obtained, check if item can fit
  689. portENTER_CRITICAL(&pxRingbuffer->mux);
  690. if(pxRingbuffer->xCheckItemFits(pxRingbuffer, xItemSize) == pdTRUE) {
  691. //Item will fit, copy item
  692. pxRingbuffer->vCopyItem(pxRingbuffer, pvItem, xItemSize);
  693. xReturn = pdTRUE;
  694. //Check if the free semaphore should be returned to allow other tasks to send
  695. if (prvGetFreeSize(pxRingbuffer) > 0) {
  696. xReturnSemaphore = pdTRUE;
  697. }
  698. portEXIT_CRITICAL(&pxRingbuffer->mux);
  699. break;
  700. }
  701. //Item doesn't fit, adjust ticks and take the semaphore again
  702. if (xTicksToWait != portMAX_DELAY) {
  703. xTicksRemaining = xTicksEnd - xTaskGetTickCount();
  704. }
  705. portEXIT_CRITICAL(&pxRingbuffer->mux);
  706. /*
  707. * Gap between critical section and re-acquiring of the semaphore. If
  708. * semaphore is given now, priority inversion might occur (see docs)
  709. */
  710. }
  711. if (xReturn == pdTRUE) {
  712. //Indicate item was successfully sent
  713. xSemaphoreGive(pxRingbuffer->xItemsBufferedSemaphore);
  714. }
  715. if (xReturnSemaphore == pdTRUE) {
  716. xSemaphoreGive(pxRingbuffer->xFreeSpaceSemaphore); //Give back semaphore so other tasks can send
  717. }
  718. return xReturn;
  719. }
  720. BaseType_t xRingbufferSendFromISR(RingbufHandle_t xRingbuffer, const void *pvItem, size_t xItemSize, BaseType_t *pxHigherPriorityTaskWoken)
  721. {
  722. //Check arguments
  723. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  724. configASSERT(pxRingbuffer);
  725. configASSERT(pvItem != NULL || xItemSize == 0);
  726. if (xItemSize > pxRingbuffer->xMaxItemSize) {
  727. return pdFALSE; //Data will never ever fit in the queue.
  728. }
  729. if ((pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG) && xItemSize == 0) {
  730. return pdTRUE; //Sending 0 bytes to byte buffer has no effect
  731. }
  732. //Attempt to send an item
  733. BaseType_t xReturn;
  734. BaseType_t xReturnSemaphore = pdFALSE;
  735. portENTER_CRITICAL_ISR(&pxRingbuffer->mux);
  736. if (pxRingbuffer->xCheckItemFits(xRingbuffer, xItemSize) == pdTRUE) {
  737. pxRingbuffer->vCopyItem(xRingbuffer, pvItem, xItemSize);
  738. xReturn = pdTRUE;
  739. //Check if the free semaphore should be returned to allow other tasks to send
  740. if (prvGetFreeSize(pxRingbuffer) > 0) {
  741. xReturnSemaphore = pdTRUE;
  742. }
  743. } else {
  744. xReturn = pdFALSE;
  745. }
  746. portEXIT_CRITICAL_ISR(&pxRingbuffer->mux);
  747. if (xReturn == pdTRUE) {
  748. //Indicate item was successfully sent
  749. xSemaphoreGiveFromISR(pxRingbuffer->xItemsBufferedSemaphore, pxHigherPriorityTaskWoken);
  750. }
  751. if (xReturnSemaphore == pdTRUE) {
  752. xSemaphoreGiveFromISR(pxRingbuffer->xFreeSpaceSemaphore, pxHigherPriorityTaskWoken); //Give back semaphore so other tasks can send
  753. }
  754. return xReturn;
  755. }
  756. void *xRingbufferReceive(RingbufHandle_t xRingbuffer, size_t *pxItemSize, TickType_t xTicksToWait)
  757. {
  758. //Check arguments
  759. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  760. configASSERT(pxRingbuffer);
  761. //Attempt to retrieve an item
  762. void *pvTempItem;
  763. size_t xTempSize;
  764. if (prvReceiveGeneric(pxRingbuffer, &pvTempItem, NULL, &xTempSize, NULL, 0, xTicksToWait) == pdTRUE) {
  765. if (pxItemSize != NULL) {
  766. *pxItemSize = xTempSize;
  767. }
  768. return pvTempItem;
  769. } else {
  770. return NULL;
  771. }
  772. }
  773. void *xRingbufferReceiveFromISR(RingbufHandle_t xRingbuffer, size_t *pxItemSize)
  774. {
  775. //Check arguments
  776. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  777. configASSERT(pxRingbuffer);
  778. //Attempt to retrieve an item
  779. void *pvTempItem;
  780. size_t xTempSize;
  781. if (prvReceiveGenericFromISR(pxRingbuffer, &pvTempItem, NULL, &xTempSize, NULL, 0) == pdTRUE) {
  782. if (pxItemSize != NULL) {
  783. *pxItemSize = xTempSize;
  784. }
  785. return pvTempItem;
  786. } else {
  787. return NULL;
  788. }
  789. }
  790. BaseType_t xRingbufferReceiveSplit(RingbufHandle_t xRingbuffer, void **ppvHeadItem, void **ppvTailItem, size_t *pxHeadItemSize, size_t *pxTailItemSize, TickType_t xTicksToWait)
  791. {
  792. //Check arguments
  793. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  794. configASSERT(pxRingbuffer);
  795. configASSERT(pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG);
  796. configASSERT(ppvHeadItem != NULL && ppvTailItem != NULL);
  797. //Attempt to retrieve multiple items
  798. void *pvTempHeadItem, *pvTempTailItem;
  799. size_t xTempHeadSize, xTempTailSize;
  800. if (prvReceiveGeneric(pxRingbuffer, &pvTempHeadItem, &pvTempTailItem, &xTempHeadSize, &xTempTailSize, 0, xTicksToWait) == pdTRUE) {
  801. //At least one item was retrieved
  802. *ppvHeadItem = pvTempHeadItem;
  803. if(pxHeadItemSize != NULL){
  804. *pxHeadItemSize = xTempHeadSize;
  805. }
  806. //Check to see if a second item was also retrieved
  807. if (pvTempTailItem != NULL) {
  808. *ppvTailItem = pvTempTailItem;
  809. if (pxTailItemSize != NULL) {
  810. *pxTailItemSize = xTempTailSize;
  811. }
  812. } else {
  813. *ppvTailItem = NULL;
  814. }
  815. return pdTRUE;
  816. } else {
  817. //No items retrieved
  818. *ppvHeadItem = NULL;
  819. *ppvTailItem = NULL;
  820. return pdFALSE;
  821. }
  822. }
  823. BaseType_t xRingbufferReceiveSplitFromISR(RingbufHandle_t xRingbuffer, void **ppvHeadItem, void **ppvTailItem, size_t *pxHeadItemSize, size_t *pxTailItemSize)
  824. {
  825. //Check arguments
  826. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  827. configASSERT(pxRingbuffer);
  828. configASSERT(pxRingbuffer->uxRingbufferFlags & rbALLOW_SPLIT_FLAG);
  829. configASSERT(ppvHeadItem != NULL && ppvTailItem != NULL);
  830. //Attempt to retrieve multiple items
  831. void *pvTempHeadItem = NULL, *pvTempTailItem = NULL;
  832. size_t xTempHeadSize, xTempTailSize;
  833. if (prvReceiveGenericFromISR(pxRingbuffer, &pvTempHeadItem, &pvTempTailItem, &xTempHeadSize, &xTempTailSize, 0) == pdTRUE) {
  834. //At least one item was received
  835. *ppvHeadItem = pvTempHeadItem;
  836. if (pxHeadItemSize != NULL) {
  837. *pxHeadItemSize = xTempHeadSize;
  838. }
  839. //Check to see if a second item was also retrieved
  840. if (pvTempTailItem != NULL) {
  841. *ppvTailItem = pvTempTailItem;
  842. if (pxTailItemSize != NULL) {
  843. *pxTailItemSize = xTempTailSize;
  844. }
  845. } else {
  846. *ppvTailItem = NULL;
  847. }
  848. return pdTRUE;
  849. } else {
  850. *ppvHeadItem = NULL;
  851. *ppvTailItem = NULL;
  852. return pdFALSE;
  853. }
  854. }
  855. void *xRingbufferReceiveUpTo(RingbufHandle_t xRingbuffer, size_t *pxItemSize, TickType_t xTicksToWait, size_t xMaxSize)
  856. {
  857. //Check arguments
  858. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  859. configASSERT(pxRingbuffer);
  860. configASSERT(pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG); //This function should only be called for byte buffers
  861. if (xMaxSize == 0) {
  862. return NULL;
  863. }
  864. //Attempt to retrieve up to xMaxSize bytes
  865. void *pvTempItem;
  866. size_t xTempSize;
  867. if (prvReceiveGeneric(pxRingbuffer, &pvTempItem, NULL, &xTempSize, NULL, xMaxSize, xTicksToWait) == pdTRUE) {
  868. if (pxItemSize != NULL) {
  869. *pxItemSize = xTempSize;
  870. }
  871. return pvTempItem;
  872. } else {
  873. return NULL;
  874. }
  875. }
  876. void *xRingbufferReceiveUpToFromISR(RingbufHandle_t xRingbuffer, size_t *pxItemSize, size_t xMaxSize)
  877. {
  878. //Check arguments
  879. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  880. configASSERT(pxRingbuffer);
  881. configASSERT(pxRingbuffer->uxRingbufferFlags & rbBYTE_BUFFER_FLAG); //This function should only be called for byte buffers
  882. if (xMaxSize == 0) {
  883. return NULL;
  884. }
  885. //Attempt to retrieve up to xMaxSize bytes
  886. void *pvTempItem;
  887. size_t xTempSize;
  888. if (prvReceiveGenericFromISR(pxRingbuffer, &pvTempItem, NULL, &xTempSize, NULL, xMaxSize) == pdTRUE) {
  889. if (pxItemSize != NULL) {
  890. *pxItemSize = xTempSize;
  891. }
  892. return pvTempItem;
  893. } else {
  894. return NULL;
  895. }
  896. }
  897. void vRingbufferReturnItem(RingbufHandle_t xRingbuffer, void *pvItem)
  898. {
  899. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  900. configASSERT(pxRingbuffer);
  901. configASSERT(pvItem != NULL);
  902. portENTER_CRITICAL(&pxRingbuffer->mux);
  903. pxRingbuffer->vReturnItem(pxRingbuffer, (uint8_t *)pvItem);
  904. portEXIT_CRITICAL(&pxRingbuffer->mux);
  905. xSemaphoreGive(pxRingbuffer->xFreeSpaceSemaphore);
  906. }
  907. void vRingbufferReturnItemFromISR(RingbufHandle_t xRingbuffer, void *pvItem, BaseType_t *pxHigherPriorityTaskWoken)
  908. {
  909. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  910. configASSERT(pxRingbuffer);
  911. configASSERT(pvItem != NULL);
  912. portENTER_CRITICAL_ISR(&pxRingbuffer->mux);
  913. pxRingbuffer->vReturnItem(pxRingbuffer, (uint8_t *)pvItem);
  914. portEXIT_CRITICAL_ISR(&pxRingbuffer->mux);
  915. xSemaphoreGiveFromISR(pxRingbuffer->xFreeSpaceSemaphore, pxHigherPriorityTaskWoken);
  916. }
  917. void vRingbufferDelete(RingbufHandle_t xRingbuffer)
  918. {
  919. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  920. configASSERT(pxRingbuffer);
  921. if (pxRingbuffer) {
  922. free(pxRingbuffer->pucHead);
  923. if (pxRingbuffer->xFreeSpaceSemaphore) {
  924. vSemaphoreDelete(pxRingbuffer->xFreeSpaceSemaphore);
  925. }
  926. if (pxRingbuffer->xItemsBufferedSemaphore) {
  927. vSemaphoreDelete(pxRingbuffer->xItemsBufferedSemaphore);
  928. }
  929. }
  930. free(pxRingbuffer);
  931. }
  932. size_t xRingbufferGetMaxItemSize(RingbufHandle_t xRingbuffer)
  933. {
  934. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  935. configASSERT(pxRingbuffer);
  936. return pxRingbuffer->xMaxItemSize;
  937. }
  938. size_t xRingbufferGetCurFreeSize(RingbufHandle_t xRingbuffer)
  939. {
  940. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  941. configASSERT(pxRingbuffer);
  942. size_t xFreeSize;
  943. portENTER_CRITICAL(&pxRingbuffer->mux);
  944. xFreeSize = pxRingbuffer->xGetCurMaxSize(pxRingbuffer);
  945. portEXIT_CRITICAL(&pxRingbuffer->mux);
  946. return xFreeSize;
  947. }
  948. BaseType_t xRingbufferAddToQueueSetRead(RingbufHandle_t xRingbuffer, QueueSetHandle_t xQueueSet)
  949. {
  950. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  951. configASSERT(pxRingbuffer);
  952. BaseType_t xReturn;
  953. portENTER_CRITICAL(&pxRingbuffer->mux);
  954. //Cannot add semaphore to queue set if semaphore is not empty. Temporarily hold semaphore
  955. BaseType_t xHoldSemaphore = xSemaphoreTake(pxRingbuffer->xItemsBufferedSemaphore, 0);
  956. xReturn = xQueueAddToSet(pxRingbuffer->xItemsBufferedSemaphore, xQueueSet);
  957. if (xHoldSemaphore == pdTRUE) {
  958. //Return semaphore if temporarily held
  959. configASSERT(xSemaphoreGive(pxRingbuffer->xItemsBufferedSemaphore) == pdTRUE);
  960. }
  961. portEXIT_CRITICAL(&pxRingbuffer->mux);
  962. return xReturn;
  963. }
  964. BaseType_t xRingbufferCanRead(RingbufHandle_t xRingbuffer, QueueSetMemberHandle_t xMember)
  965. {
  966. //Check if the selected queue set member is the ring buffer's read semaphore
  967. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  968. configASSERT(pxRingbuffer);
  969. return (pxRingbuffer->xItemsBufferedSemaphore == xMember) ? pdTRUE : pdFALSE;
  970. }
  971. BaseType_t xRingbufferRemoveFromQueueSetRead(RingbufHandle_t xRingbuffer, QueueSetHandle_t xQueueSet)
  972. {
  973. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  974. configASSERT(pxRingbuffer);
  975. BaseType_t xReturn;
  976. portENTER_CRITICAL(&pxRingbuffer->mux);
  977. //Cannot remove semaphore from queue set if semaphore is not empty. Temporarily hold semaphore
  978. BaseType_t xHoldSemaphore = xSemaphoreTake(pxRingbuffer->xItemsBufferedSemaphore, 0);
  979. xReturn = xQueueRemoveFromSet(pxRingbuffer->xItemsBufferedSemaphore, xQueueSet);
  980. if (xHoldSemaphore == pdTRUE) {
  981. //Return semaphore if temporarily held
  982. configASSERT(xSemaphoreGive(pxRingbuffer->xItemsBufferedSemaphore) == pdTRUE);
  983. }
  984. portEXIT_CRITICAL(&pxRingbuffer->mux);
  985. return xReturn;
  986. }
  987. void vRingbufferGetInfo(RingbufHandle_t xRingbuffer, UBaseType_t *uxFree, UBaseType_t *uxRead, UBaseType_t *uxWrite, UBaseType_t *uxItemsWaiting)
  988. {
  989. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  990. configASSERT(pxRingbuffer);
  991. portENTER_CRITICAL(&pxRingbuffer->mux);
  992. if (uxFree != NULL) {
  993. *uxFree = (UBaseType_t)(pxRingbuffer->pucFree - pxRingbuffer->pucHead);
  994. }
  995. if (uxRead != NULL) {
  996. *uxRead = (UBaseType_t)(pxRingbuffer->pucRead - pxRingbuffer->pucHead);
  997. }
  998. if (uxWrite != NULL) {
  999. *uxWrite = (UBaseType_t)(pxRingbuffer->pucWrite - pxRingbuffer->pucHead);
  1000. }
  1001. if (uxItemsWaiting != NULL) {
  1002. *uxItemsWaiting = (UBaseType_t)(pxRingbuffer->xItemsWaiting);
  1003. }
  1004. portEXIT_CRITICAL(&pxRingbuffer->mux);
  1005. }
  1006. void xRingbufferPrintInfo(RingbufHandle_t xRingbuffer)
  1007. {
  1008. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  1009. configASSERT(pxRingbuffer);
  1010. printf("Rb size:%d\tfree: %d\trptr: %d\tfreeptr: %d\twptr: %d\n",
  1011. pxRingbuffer->xSize, prvGetFreeSize(pxRingbuffer),
  1012. pxRingbuffer->pucRead - pxRingbuffer->pucHead,
  1013. pxRingbuffer->pucFree - pxRingbuffer->pucHead,
  1014. pxRingbuffer->pucWrite - pxRingbuffer->pucHead);
  1015. }
  1016. /* --------------------------------- Deprecated Functions ------------------------------ */
  1017. //Todo: Remove the following deprecated functions in next release
  1018. bool xRingbufferIsNextItemWrapped(RingbufHandle_t xRingbuffer)
  1019. {
  1020. //This function is deprecated, use xRingbufferReceiveSplit() instead
  1021. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  1022. configASSERT(pxRingbuffer);
  1023. bool is_wrapped;
  1024. portENTER_CRITICAL(&pxRingbuffer->mux);
  1025. ItemHeader_t *xHeader = (ItemHeader_t *)pxRingbuffer->pucRead;
  1026. is_wrapped = xHeader->uxItemFlags & rbITEM_SPLIT_FLAG;
  1027. portEXIT_CRITICAL(&pxRingbuffer->mux);
  1028. return is_wrapped;
  1029. }
  1030. BaseType_t xRingbufferAddToQueueSetWrite(RingbufHandle_t xRingbuffer, QueueSetHandle_t xQueueSet)
  1031. {
  1032. //This function is deprecated. QueueSetWrite no longer supported
  1033. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  1034. configASSERT(pxRingbuffer);
  1035. BaseType_t xReturn;
  1036. portENTER_CRITICAL(&pxRingbuffer->mux);
  1037. //Cannot add semaphore to queue set if semaphore is not empty. Temporary hold semaphore
  1038. BaseType_t xHoldSemaphore = xSemaphoreTake(pxRingbuffer->xFreeSpaceSemaphore, 0);
  1039. xReturn = xQueueAddToSet(pxRingbuffer->xFreeSpaceSemaphore, xQueueSet);
  1040. if (xHoldSemaphore == pdTRUE) {
  1041. //Return semaphore is temporarily held
  1042. configASSERT(xSemaphoreGive(pxRingbuffer->xFreeSpaceSemaphore) == pdTRUE);
  1043. }
  1044. portEXIT_CRITICAL(&pxRingbuffer->mux);
  1045. return xReturn;
  1046. }
  1047. BaseType_t xRingbufferRemoveFromQueueSetWrite(RingbufHandle_t xRingbuffer, QueueSetHandle_t xQueueSet)
  1048. {
  1049. //This function is deprecated. QueueSetWrite no longer supported
  1050. Ringbuffer_t *pxRingbuffer = (Ringbuffer_t *)xRingbuffer;
  1051. configASSERT(pxRingbuffer);
  1052. BaseType_t xReturn;
  1053. portENTER_CRITICAL(&pxRingbuffer->mux);
  1054. //Cannot remove semaphore from queue set if semaphore is not empty. Temporary hold semaphore
  1055. BaseType_t xHoldSemaphore = xSemaphoreTake(pxRingbuffer->xFreeSpaceSemaphore, 0);
  1056. xReturn = xQueueRemoveFromSet(pxRingbuffer->xFreeSpaceSemaphore, xQueueSet);
  1057. if (xHoldSemaphore == pdTRUE) {
  1058. //Return semaphore is temporarily held
  1059. configASSERT(xSemaphoreGive(pxRingbuffer->xFreeSpaceSemaphore) == pdTRUE);
  1060. }
  1061. portEXIT_CRITICAL(&pxRingbuffer->mux);
  1062. return xReturn;
  1063. }