ringbuf.c 55 KB

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