ringbuf.c 57 KB

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