ringbuf.c 64 KB

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