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