ringbuf.c 65 KB

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