queue.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  1. /*
  2. FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /*
  56. ToDo: The multicore implementation of this uses taskENTER_CRITICAL etc to make sure the
  57. queue structures aren't accessed by another processor or core. It would be useful to have
  58. IRQs be able to schedule stuff while doing task-related stuff, meaning we have to convert
  59. the taskENTER_CRITICAL stuff to a lock + a scheduler suspend instead.
  60. */
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include "rom/ets_sys.h"
  64. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  65. all the API functions to use the MPU wrappers. That should only be done when
  66. task.h is included from an application file. */
  67. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  68. #include "FreeRTOS.h"
  69. #include "task.h"
  70. #include "queue.h"
  71. #if ( configUSE_CO_ROUTINES == 1 )
  72. #include "croutine.h"
  73. #endif
  74. /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
  75. MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
  76. header files above, but not in this file, in order to generate the correct
  77. privileged Vs unprivileged linkage and placement. */
  78. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
  79. /* When the Queue_t structure is used to represent a base queue its pcHead and
  80. pcTail members are used as pointers into the queue storage area. When the
  81. Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
  82. not necessary, and the pcHead pointer is set to NULL to indicate that the
  83. pcTail pointer actually points to the mutex holder (if any). Map alternative
  84. names to the pcHead and pcTail structure members to ensure the readability of
  85. the code is maintained despite this dual use of two structure members. An
  86. alternative implementation would be to use a union, but use of a union is
  87. against the coding standard (although an exception to the standard has been
  88. permitted where the dual use also significantly changes the type of the
  89. structure member). */
  90. #define pxMutexHolder pcTail
  91. #define uxQueueType pcHead
  92. #define queueQUEUE_IS_MUTEX NULL
  93. /* Semaphores do not actually store or copy data, so have an item size of
  94. zero. */
  95. #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
  96. #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
  97. #if( configUSE_PREEMPTION == 0 )
  98. /* If the cooperative scheduler is being used then a yield should not be
  99. performed just because a higher priority task has been woken. */
  100. #define queueYIELD_IF_USING_PREEMPTION()
  101. #else
  102. #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
  103. #endif
  104. /*
  105. * Definition of the queue used by the scheduler.
  106. * Items are queued by copy, not reference. See the following link for the
  107. * rationale: http://www.freertos.org/Embedded-RTOS-Queues.html
  108. */
  109. typedef struct QueueDefinition
  110. {
  111. int8_t *pcHead; /*< Points to the beginning of the queue storage area. */
  112. int8_t *pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
  113. int8_t *pcWriteTo; /*< Points to the free next place in the storage area. */
  114. union /* Use of a union is an exception to the coding standard to ensure two mutually exclusive structure members don't appear simultaneously (wasting RAM). */
  115. {
  116. int8_t *pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. */
  117. UBaseType_t uxRecursiveCallCount;/*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */
  118. } u;
  119. List_t xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
  120. List_t xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
  121. volatile UBaseType_t uxMessagesWaiting;/*< The number of items currently in the queue. */
  122. UBaseType_t uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
  123. UBaseType_t uxItemSize; /*< The size of each items that the queue will hold. */
  124. #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  125. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
  126. #endif
  127. #if ( configUSE_QUEUE_SETS == 1 )
  128. struct QueueDefinition *pxQueueSetContainer;
  129. #endif
  130. #if ( configUSE_TRACE_FACILITY == 1 )
  131. UBaseType_t uxQueueNumber;
  132. uint8_t ucQueueType;
  133. #endif
  134. portMUX_TYPE mux; //Mutex required due to SMP
  135. } xQUEUE;
  136. /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
  137. name below to enable the use of older kernel aware debuggers. */
  138. typedef xQUEUE Queue_t;
  139. #if __GNUC_PREREQ(4, 6)
  140. _Static_assert(sizeof(StaticQueue_t) == sizeof(Queue_t), "StaticQueue_t != Queue_t");
  141. #endif
  142. /*-----------------------------------------------------------*/
  143. /*
  144. * The queue registry is just a means for kernel aware debuggers to locate
  145. * queue structures. It has no other purpose so is an optional component.
  146. */
  147. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  148. /* The type stored within the queue registry array. This allows a name
  149. to be assigned to each queue making kernel aware debugging a little
  150. more user friendly. */
  151. typedef struct QUEUE_REGISTRY_ITEM
  152. {
  153. const char *pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  154. QueueHandle_t xHandle;
  155. } xQueueRegistryItem;
  156. /* The old xQueueRegistryItem name is maintained above then typedefed to the
  157. new xQueueRegistryItem name below to enable the use of older kernel aware
  158. debuggers. */
  159. typedef xQueueRegistryItem QueueRegistryItem_t;
  160. /* The queue registry is simply an array of QueueRegistryItem_t structures.
  161. The pcQueueName member of a structure being NULL is indicative of the
  162. array position being vacant. */
  163. QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
  164. //Need to add queue registry mutex to protect against simultaneous access
  165. static portMUX_TYPE queue_registry_spinlock = portMUX_INITIALIZER_UNLOCKED;
  166. #endif /* configQUEUE_REGISTRY_SIZE */
  167. /*
  168. * Uses a critical section to determine if there is any data in a queue.
  169. *
  170. * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
  171. */
  172. static BaseType_t prvIsQueueEmpty( Queue_t *pxQueue ) PRIVILEGED_FUNCTION;
  173. /*
  174. * Uses a critical section to determine if there is any space in a queue.
  175. *
  176. * @return pdTRUE if there is no space, otherwise pdFALSE;
  177. */
  178. static BaseType_t prvIsQueueFull( Queue_t *pxQueue ) PRIVILEGED_FUNCTION;
  179. /*
  180. * Copies an item into the queue, either at the front of the queue or the
  181. * back of the queue.
  182. */
  183. static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
  184. /*
  185. * Copies an item out of a queue.
  186. */
  187. static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer ) PRIVILEGED_FUNCTION;
  188. #if ( configUSE_QUEUE_SETS == 1 )
  189. /*
  190. * Checks to see if a queue is a member of a queue set, and if so, notifies
  191. * the queue set that the queue contains data.
  192. */
  193. static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition ) PRIVILEGED_FUNCTION;
  194. #endif
  195. /*
  196. * Called after a Queue_t structure has been allocated either statically or
  197. * dynamically to fill in the structure's members.
  198. */
  199. static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;
  200. /*
  201. * Mutexes are a special type of queue. When a mutex is created, first the
  202. * queue is created, then prvInitialiseMutex() is called to configure the queue
  203. * as a mutex.
  204. */
  205. #if( configUSE_MUTEXES == 1 )
  206. static void prvInitialiseMutex( Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;
  207. #endif
  208. BaseType_t xQueueGenericReset( QueueHandle_t xQueue, BaseType_t xNewQueue )
  209. {
  210. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  211. configASSERT( pxQueue );
  212. if ( xNewQueue == pdTRUE )
  213. {
  214. vPortCPUInitializeMutex(&pxQueue->mux);
  215. }
  216. taskENTER_CRITICAL(&pxQueue->mux);
  217. {
  218. pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
  219. pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
  220. pxQueue->pcWriteTo = pxQueue->pcHead;
  221. pxQueue->u.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( UBaseType_t ) 1U ) * pxQueue->uxItemSize );
  222. if( xNewQueue == pdFALSE )
  223. {
  224. /* If there are tasks blocked waiting to read from the queue, then
  225. the tasks will remain blocked as after this function exits the queue
  226. will still be empty. If there are tasks blocked waiting to write to
  227. the queue, then one should be unblocked as after this function exits
  228. it will be possible to write to it. */
  229. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  230. {
  231. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  232. {
  233. queueYIELD_IF_USING_PREEMPTION();
  234. }
  235. else
  236. {
  237. mtCOVERAGE_TEST_MARKER();
  238. }
  239. }
  240. else
  241. {
  242. mtCOVERAGE_TEST_MARKER();
  243. }
  244. }
  245. else
  246. {
  247. /* Ensure the event queues start in the correct state. */
  248. vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
  249. vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
  250. }
  251. }
  252. taskEXIT_CRITICAL(&pxQueue->mux);
  253. /* A value is returned for calling semantic consistency with previous
  254. versions. */
  255. return pdPASS;
  256. }
  257. /*-----------------------------------------------------------*/
  258. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  259. QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, StaticQueue_t *pxStaticQueue, const uint8_t ucQueueType )
  260. {
  261. Queue_t *pxNewQueue;
  262. configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
  263. /* The StaticQueue_t structure and the queue storage area must be
  264. supplied. */
  265. configASSERT( pxStaticQueue != NULL );
  266. /* A queue storage area should be provided if the item size is not 0, and
  267. should not be provided if the item size is 0. */
  268. configASSERT( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) );
  269. configASSERT( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) );
  270. #if( configASSERT_DEFINED == 1 )
  271. {
  272. /* Sanity check that the size of the structure used to declare a
  273. variable of type StaticQueue_t or StaticSemaphore_t equals the size of
  274. the real queue and semaphore structures. */
  275. volatile size_t xSize = sizeof( StaticQueue_t );
  276. configASSERT( xSize == sizeof( Queue_t ) );
  277. ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
  278. }
  279. #endif /* configASSERT_DEFINED */
  280. /* The address of a statically allocated queue was passed in, use it.
  281. The address of a statically allocated storage area was also passed in
  282. but is already set. */
  283. pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
  284. if( pxNewQueue != NULL )
  285. {
  286. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  287. {
  288. /* Queues can be allocated wither statically or dynamically, so
  289. note this queue was allocated statically in case the queue is
  290. later deleted. */
  291. pxNewQueue->ucStaticallyAllocated = pdTRUE;
  292. }
  293. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  294. prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
  295. }
  296. return pxNewQueue;
  297. }
  298. #endif /* configSUPPORT_STATIC_ALLOCATION */
  299. /*-----------------------------------------------------------*/
  300. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  301. QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType )
  302. {
  303. Queue_t *pxNewQueue;
  304. size_t xQueueSizeInBytes;
  305. uint8_t *pucQueueStorage;
  306. configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
  307. if( uxItemSize == ( UBaseType_t ) 0 )
  308. {
  309. /* There is not going to be a queue storage area. */
  310. xQueueSizeInBytes = ( size_t ) 0;
  311. }
  312. else
  313. {
  314. /* Allocate enough space to hold the maximum number of items that
  315. can be in the queue at any time. */
  316. xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  317. }
  318. /* Check for multiplication overflow. */
  319. configASSERT( ( uxItemSize == 0 ) || ( uxQueueLength == ( xQueueSizeInBytes / uxItemSize ) ) );
  320. /* Check for addition overflow. */
  321. configASSERT( ( sizeof( Queue_t ) + xQueueSizeInBytes ) > xQueueSizeInBytes );
  322. /* Allocate the queue and storage area. Justification for MISRA
  323. deviation as follows: pvPortMalloc() always ensures returned memory
  324. blocks are aligned per the requirements of the MCU stack. In this case
  325. pvPortMalloc() must return a pointer that is guaranteed to meet the
  326. alignment requirements of the Queue_t structure - which in this case
  327. is an int8_t *. Therefore, whenever the stack alignment requirements
  328. are greater than or equal to the pointer to char requirements the cast
  329. is safe. In other cases alignment requirements are not strict (one or
  330. two bytes). */
  331. pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
  332. if( pxNewQueue != NULL )
  333. {
  334. /* Jump past the queue structure to find the location of the queue
  335. storage area. */
  336. pucQueueStorage = ( ( uint8_t * ) pxNewQueue ) + sizeof( Queue_t );
  337. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  338. {
  339. /* Queues can be created either statically or dynamically, so
  340. note this task was created dynamically in case it is later
  341. deleted. */
  342. pxNewQueue->ucStaticallyAllocated = pdFALSE;
  343. }
  344. #endif /* configSUPPORT_STATIC_ALLOCATION */
  345. prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
  346. }
  347. return pxNewQueue;
  348. }
  349. #endif /* configSUPPORT_STATIC_ALLOCATION */
  350. /*-----------------------------------------------------------*/
  351. static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue )
  352. {
  353. /* Remove compiler warnings about unused parameters should
  354. configUSE_TRACE_FACILITY not be set to 1. */
  355. ( void ) ucQueueType;
  356. if( uxItemSize == ( UBaseType_t ) 0 )
  357. {
  358. /* No RAM was allocated for the queue storage area, but PC head cannot
  359. be set to NULL because NULL is used as a key to say the queue is used as
  360. a mutex. Therefore just set pcHead to point to the queue as a benign
  361. value that is known to be within the memory map. */
  362. pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
  363. }
  364. else
  365. {
  366. /* Set the head to the start of the queue storage area. */
  367. pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
  368. }
  369. /* Initialise the queue members as described where the queue type is
  370. defined. */
  371. pxNewQueue->uxLength = uxQueueLength;
  372. pxNewQueue->uxItemSize = uxItemSize;
  373. ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
  374. #if ( configUSE_TRACE_FACILITY == 1 )
  375. {
  376. pxNewQueue->ucQueueType = ucQueueType;
  377. }
  378. #endif /* configUSE_TRACE_FACILITY */
  379. #if( configUSE_QUEUE_SETS == 1 )
  380. {
  381. pxNewQueue->pxQueueSetContainer = NULL;
  382. }
  383. #endif /* configUSE_QUEUE_SETS */
  384. traceQUEUE_CREATE( pxNewQueue );
  385. }
  386. /*-----------------------------------------------------------*/
  387. #if( configUSE_MUTEXES == 1 )
  388. static void prvInitialiseMutex( Queue_t *pxNewQueue )
  389. {
  390. if( pxNewQueue != NULL )
  391. {
  392. /* The queue create function will set all the queue structure members
  393. correctly for a generic queue, but this function is creating a
  394. mutex. Overwrite those members that need to be set differently -
  395. in particular the information required for priority inheritance. */
  396. pxNewQueue->pxMutexHolder = NULL;
  397. pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
  398. /* In case this is a recursive mutex. */
  399. pxNewQueue->u.uxRecursiveCallCount = 0;
  400. vPortCPUInitializeMutex(&pxNewQueue->mux);
  401. traceCREATE_MUTEX( pxNewQueue );
  402. /* Start with the semaphore in the expected state. */
  403. ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
  404. }
  405. else
  406. {
  407. traceCREATE_MUTEX_FAILED();
  408. }
  409. }
  410. #endif /* configUSE_MUTEXES */
  411. /*-----------------------------------------------------------*/
  412. #if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  413. QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
  414. {
  415. Queue_t *pxNewQueue;
  416. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  417. pxNewQueue = ( Queue_t * ) xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
  418. prvInitialiseMutex( pxNewQueue );
  419. return pxNewQueue;
  420. }
  421. #endif /* configUSE_MUTEXES */
  422. /*-----------------------------------------------------------*/
  423. #if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  424. QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType, StaticQueue_t *pxStaticQueue )
  425. {
  426. Queue_t *pxNewQueue;
  427. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  428. /* Prevent compiler warnings about unused parameters if
  429. configUSE_TRACE_FACILITY does not equal 1. */
  430. ( void ) ucQueueType;
  431. pxNewQueue = ( Queue_t * ) xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
  432. prvInitialiseMutex( pxNewQueue );
  433. return pxNewQueue;
  434. }
  435. #endif /* configUSE_MUTEXES */
  436. /*-----------------------------------------------------------*/
  437. #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
  438. void* xQueueGetMutexHolder( QueueHandle_t xSemaphore )
  439. {
  440. Queue_t * const pxQueue = ( Queue_t * ) xSemaphore;
  441. void *pxReturn;
  442. /* This function is called by xSemaphoreGetMutexHolder(), and should not
  443. be called directly. Note: This is a good way of determining if the
  444. calling task is the mutex holder, but not a good way of determining the
  445. identity of the mutex holder, as the holder may change between the
  446. following critical section exiting and the function returning. */
  447. taskENTER_CRITICAL(&pxQueue->mux);
  448. {
  449. if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
  450. {
  451. pxReturn = ( void * ) ( ( Queue_t * ) xSemaphore )->pxMutexHolder;
  452. }
  453. else
  454. {
  455. pxReturn = NULL;
  456. }
  457. }
  458. taskEXIT_CRITICAL(&pxQueue->mux);
  459. return pxReturn;
  460. } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
  461. #endif
  462. /*-----------------------------------------------------------*/
  463. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  464. BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
  465. {
  466. BaseType_t xReturn;
  467. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  468. configASSERT( pxMutex );
  469. /* If this is the task that holds the mutex then pxMutexHolder will not
  470. change outside of this task. If this task does not hold the mutex then
  471. pxMutexHolder can never coincidentally equal the tasks handle, and as
  472. this is the only condition we are interested in it does not matter if
  473. pxMutexHolder is accessed simultaneously by another task. Therefore no
  474. mutual exclusion is required to test the pxMutexHolder variable. */
  475. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Not a redundant cast as TaskHandle_t is a typedef. */
  476. {
  477. traceGIVE_MUTEX_RECURSIVE( pxMutex );
  478. /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
  479. the task handle, therefore no underflow check is required. Also,
  480. uxRecursiveCallCount is only modified by the mutex holder, and as
  481. there can only be one, no mutual exclusion is required to modify the
  482. uxRecursiveCallCount member. */
  483. ( pxMutex->u.uxRecursiveCallCount )--;
  484. /* Have we unwound the call count? */
  485. if( pxMutex->u.uxRecursiveCallCount == ( UBaseType_t ) 0 )
  486. {
  487. /* Return the mutex. This will automatically unblock any other
  488. task that might be waiting to access the mutex. */
  489. ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
  490. }
  491. else
  492. {
  493. mtCOVERAGE_TEST_MARKER();
  494. }
  495. xReturn = pdPASS;
  496. }
  497. else
  498. {
  499. /* The mutex cannot be given because the calling task is not the
  500. holder. */
  501. xReturn = pdFAIL;
  502. traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
  503. }
  504. return xReturn;
  505. }
  506. #endif /* configUSE_RECURSIVE_MUTEXES */
  507. /*-----------------------------------------------------------*/
  508. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  509. BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex, TickType_t xTicksToWait )
  510. {
  511. BaseType_t xReturn;
  512. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  513. configASSERT( pxMutex );
  514. /* Comments regarding mutual exclusion as per those within
  515. xQueueGiveMutexRecursive(). */
  516. traceTAKE_MUTEX_RECURSIVE( pxMutex );
  517. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  518. {
  519. ( pxMutex->u.uxRecursiveCallCount )++;
  520. xReturn = pdPASS;
  521. }
  522. else
  523. {
  524. xReturn = xQueueGenericReceive( pxMutex, NULL, xTicksToWait, pdFALSE );
  525. /* pdPASS will only be returned if the mutex was successfully
  526. obtained. The calling task may have entered the Blocked state
  527. before reaching here. */
  528. if( xReturn == pdPASS )
  529. {
  530. ( pxMutex->u.uxRecursiveCallCount )++;
  531. }
  532. else
  533. {
  534. traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
  535. }
  536. }
  537. return xReturn;
  538. }
  539. #endif /* configUSE_RECURSIVE_MUTEXES */
  540. /*-----------------------------------------------------------*/
  541. #if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  542. QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount, StaticQueue_t *pxStaticQueue )
  543. {
  544. QueueHandle_t xHandle;
  545. configASSERT( uxMaxCount != 0 );
  546. configASSERT( uxInitialCount <= uxMaxCount );
  547. xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  548. if( xHandle != NULL )
  549. {
  550. ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
  551. traceCREATE_COUNTING_SEMAPHORE();
  552. }
  553. else
  554. {
  555. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  556. }
  557. return xHandle;
  558. }
  559. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  560. /*-----------------------------------------------------------*/
  561. #if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  562. QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount )
  563. {
  564. QueueHandle_t xHandle;
  565. configASSERT( uxMaxCount != 0 );
  566. configASSERT( uxInitialCount <= uxMaxCount );
  567. xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  568. if( xHandle != NULL )
  569. {
  570. ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
  571. traceCREATE_COUNTING_SEMAPHORE();
  572. }
  573. else
  574. {
  575. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  576. }
  577. configASSERT( xHandle );
  578. return xHandle;
  579. }
  580. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  581. /*-----------------------------------------------------------*/
  582. BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition )
  583. {
  584. BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
  585. TimeOut_t xTimeOut;
  586. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  587. configASSERT( pxQueue );
  588. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  589. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  590. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  591. {
  592. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  593. }
  594. #endif
  595. /* This function relaxes the coding standard somewhat to allow return
  596. statements within the function itself. This is done in the interest
  597. of execution time efficiency. */
  598. for( ;; )
  599. {
  600. taskENTER_CRITICAL(&pxQueue->mux);
  601. {
  602. /* Is there room on the queue now? The running task must be
  603. the highest priority task wanting to access the queue. If
  604. the head item in the queue is to be overwritten then it does
  605. not matter if the queue is full. */
  606. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  607. {
  608. traceQUEUE_SEND( pxQueue );
  609. xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  610. #if ( configUSE_QUEUE_SETS == 1 )
  611. {
  612. if( pxQueue->pxQueueSetContainer != NULL )
  613. {
  614. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
  615. {
  616. /* The queue is a member of a queue set, and posting
  617. to the queue set caused a higher priority task to
  618. unblock. A context switch is required. */
  619. queueYIELD_IF_USING_PREEMPTION();
  620. }
  621. else
  622. {
  623. mtCOVERAGE_TEST_MARKER();
  624. }
  625. }
  626. else
  627. {
  628. /* If there was a task waiting for data to arrive on the
  629. queue then unblock it now. */
  630. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  631. {
  632. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  633. {
  634. /* The unblocked task has a priority higher than
  635. our own so yield immediately. Yes it is ok to
  636. do this from within the critical section - the
  637. kernel takes care of that. */
  638. queueYIELD_IF_USING_PREEMPTION();
  639. }
  640. else
  641. {
  642. mtCOVERAGE_TEST_MARKER();
  643. }
  644. }
  645. else if( xYieldRequired != pdFALSE )
  646. {
  647. /* This path is a special case that will only get
  648. executed if the task was holding multiple mutexes
  649. and the mutexes were given back in an order that is
  650. different to that in which they were taken. */
  651. queueYIELD_IF_USING_PREEMPTION();
  652. }
  653. else
  654. {
  655. mtCOVERAGE_TEST_MARKER();
  656. }
  657. }
  658. }
  659. #else /* configUSE_QUEUE_SETS */
  660. {
  661. /* If there was a task waiting for data to arrive on the
  662. queue then unblock it now. */
  663. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  664. {
  665. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  666. {
  667. /* The unblocked task has a priority higher than
  668. our own so yield immediately. Yes it is ok to do
  669. this from within the critical section - the kernel
  670. takes care of that. */
  671. queueYIELD_IF_USING_PREEMPTION();
  672. }
  673. else
  674. {
  675. mtCOVERAGE_TEST_MARKER();
  676. }
  677. }
  678. else if( xYieldRequired != pdFALSE )
  679. {
  680. /* This path is a special case that will only get
  681. executed if the task was holding multiple mutexes and
  682. the mutexes were given back in an order that is
  683. different to that in which they were taken. */
  684. queueYIELD_IF_USING_PREEMPTION();
  685. }
  686. else
  687. {
  688. mtCOVERAGE_TEST_MARKER();
  689. }
  690. }
  691. #endif /* configUSE_QUEUE_SETS */
  692. taskEXIT_CRITICAL(&pxQueue->mux);
  693. return pdPASS;
  694. }
  695. else
  696. {
  697. if( xTicksToWait == ( TickType_t ) 0 )
  698. {
  699. /* The queue was full and no block time is specified (or
  700. the block time has expired) so leave now. */
  701. taskEXIT_CRITICAL(&pxQueue->mux);
  702. /* Return to the original privilege level before exiting
  703. the function. */
  704. traceQUEUE_SEND_FAILED( pxQueue );
  705. return errQUEUE_FULL;
  706. }
  707. else if( xEntryTimeSet == pdFALSE )
  708. {
  709. /* The queue was full and a block time was specified so
  710. configure the timeout structure. */
  711. vTaskSetTimeOutState( &xTimeOut );
  712. xEntryTimeSet = pdTRUE;
  713. }
  714. else
  715. {
  716. /* Entry time was already set. */
  717. mtCOVERAGE_TEST_MARKER();
  718. }
  719. }
  720. }
  721. taskEXIT_CRITICAL(&pxQueue->mux);
  722. /* Interrupts and other tasks can send to and receive from the queue
  723. now the critical section has been exited. */
  724. taskENTER_CRITICAL(&pxQueue->mux);
  725. /* Update the timeout state to see if it has expired yet. */
  726. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  727. {
  728. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  729. {
  730. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  731. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  732. /* Resuming the scheduler will move tasks from the pending
  733. ready list into the ready list - so it is feasible that this
  734. task is already in a ready list before it yields - in which
  735. case the yield will not cause a context switch unless there
  736. is also a higher priority task in the pending ready list. */
  737. taskEXIT_CRITICAL(&pxQueue->mux);
  738. portYIELD_WITHIN_API();
  739. }
  740. else
  741. {
  742. /* Try again. */
  743. taskEXIT_CRITICAL(&pxQueue->mux);
  744. }
  745. }
  746. else
  747. {
  748. /* The timeout has expired. */
  749. taskEXIT_CRITICAL(&pxQueue->mux);
  750. /* Return to the original privilege level before exiting the
  751. function. */
  752. traceQUEUE_SEND_FAILED( pxQueue );
  753. return errQUEUE_FULL;
  754. }
  755. }
  756. }
  757. /*-----------------------------------------------------------*/
  758. #if ( configUSE_ALTERNATIVE_API == 1 )
  759. BaseType_t xQueueAltGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, BaseType_t xCopyPosition )
  760. {
  761. BaseType_t xEntryTimeSet = pdFALSE;
  762. TimeOut_t xTimeOut;
  763. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  764. configASSERT( pxQueue );
  765. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  766. for( ;; )
  767. {
  768. taskENTER_CRITICAL(&pxQueue->mux);
  769. {
  770. /* Is there room on the queue now? To be running we must be
  771. the highest priority task wanting to access the queue. */
  772. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  773. {
  774. traceQUEUE_SEND( pxQueue );
  775. prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  776. /* If there was a task waiting for data to arrive on the
  777. queue then unblock it now. */
  778. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  779. {
  780. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  781. {
  782. /* The unblocked task has a priority higher than
  783. our own so yield immediately. */
  784. taskEXIT_CRITICAL(&pxQueue->mux);
  785. portYIELD_WITHIN_API();
  786. taskENTER_CRITICAL(&pxQueue->mux);
  787. }
  788. else
  789. {
  790. mtCOVERAGE_TEST_MARKER();
  791. }
  792. }
  793. else
  794. {
  795. mtCOVERAGE_TEST_MARKER();
  796. }
  797. taskEXIT_CRITICAL(&pxQueue->mux);
  798. return pdPASS;
  799. }
  800. else
  801. {
  802. if( xTicksToWait == ( TickType_t ) 0 )
  803. {
  804. taskEXIT_CRITICAL(&pxQueue->mux);
  805. return errQUEUE_FULL;
  806. }
  807. else if( xEntryTimeSet == pdFALSE )
  808. {
  809. vTaskSetTimeOutState( &xTimeOut );
  810. xEntryTimeSet = pdTRUE;
  811. }
  812. }
  813. }
  814. taskEXIT_CRITICAL(&pxQueue->mux);
  815. taskENTER_CRITICAL(&pxQueue->mux);
  816. {
  817. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  818. {
  819. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  820. {
  821. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  822. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  823. taskEXIT_CRITICAL(&pxQueue->mux);
  824. portYIELD_WITHIN_API();
  825. taskENTER_CRITICAL(&pxQueue->mux);
  826. }
  827. else
  828. {
  829. mtCOVERAGE_TEST_MARKER();
  830. }
  831. }
  832. else
  833. {
  834. taskEXIT_CRITICAL(&pxQueue->mux);
  835. traceQUEUE_SEND_FAILED( pxQueue );
  836. return errQUEUE_FULL;
  837. }
  838. }
  839. taskEXIT_CRITICAL(&pxQueue->mux);
  840. }
  841. }
  842. #endif /* configUSE_ALTERNATIVE_API */
  843. /*-----------------------------------------------------------*/
  844. #if ( configUSE_ALTERNATIVE_API == 1 )
  845. BaseType_t xQueueAltGenericReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait, BaseType_t xJustPeeking )
  846. {
  847. BaseType_t xEntryTimeSet = pdFALSE;
  848. TimeOut_t xTimeOut;
  849. int8_t *pcOriginalReadPosition;
  850. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  851. configASSERT( pxQueue );
  852. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  853. UNTESTED_FUNCTION();
  854. for( ;; )
  855. {
  856. taskENTER_CRITICAL();
  857. {
  858. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  859. {
  860. /* Remember our read position in case we are just peeking. */
  861. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  862. prvCopyDataFromQueue( pxQueue, pvBuffer );
  863. if( xJustPeeking == pdFALSE )
  864. {
  865. traceQUEUE_RECEIVE( pxQueue );
  866. /* Data is actually being removed (not just peeked). */
  867. --( pxQueue->uxMessagesWaiting );
  868. #if ( configUSE_MUTEXES == 1 )
  869. {
  870. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  871. {
  872. /* Record the information required to implement
  873. priority inheritance should it become necessary. */
  874. pxQueue->pxMutexHolder = ( int8_t * ) xTaskGetCurrentTaskHandle();
  875. }
  876. else
  877. {
  878. mtCOVERAGE_TEST_MARKER();
  879. }
  880. }
  881. #endif
  882. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  883. {
  884. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  885. {
  886. portYIELD_WITHIN_API();
  887. }
  888. else
  889. {
  890. mtCOVERAGE_TEST_MARKER();
  891. }
  892. }
  893. }
  894. else
  895. {
  896. traceQUEUE_PEEK( pxQueue );
  897. /* The data is not being removed, so reset our read
  898. pointer. */
  899. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  900. /* The data is being left in the queue, so see if there are
  901. any other tasks waiting for the data. */
  902. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  903. {
  904. /* Tasks that are removed from the event list will get added to
  905. the pending ready list as the scheduler is still suspended. */
  906. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  907. {
  908. /* The task waiting has a higher priority than this task. */
  909. portYIELD_WITHIN_API();
  910. }
  911. else
  912. {
  913. mtCOVERAGE_TEST_MARKER();
  914. }
  915. }
  916. else
  917. {
  918. mtCOVERAGE_TEST_MARKER();
  919. }
  920. }
  921. taskEXIT_CRITICAL();
  922. return pdPASS;
  923. }
  924. else
  925. {
  926. if( xTicksToWait == ( TickType_t ) 0 )
  927. {
  928. taskEXIT_CRITICAL();
  929. traceQUEUE_RECEIVE_FAILED( pxQueue );
  930. return errQUEUE_EMPTY;
  931. }
  932. else if( xEntryTimeSet == pdFALSE )
  933. {
  934. vTaskSetTimeOutState( &xTimeOut );
  935. xEntryTimeSet = pdTRUE;
  936. }
  937. }
  938. }
  939. taskEXIT_CRITICAL();
  940. taskENTER_CRITICAL();
  941. {
  942. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  943. {
  944. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  945. {
  946. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  947. #if ( configUSE_MUTEXES == 1 )
  948. {
  949. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  950. {
  951. taskENTER_CRITICAL();
  952. {
  953. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  954. }
  955. taskEXIT_CRITICAL();
  956. }
  957. else
  958. {
  959. mtCOVERAGE_TEST_MARKER();
  960. }
  961. }
  962. #endif
  963. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  964. portYIELD_WITHIN_API();
  965. }
  966. else
  967. {
  968. mtCOVERAGE_TEST_MARKER();
  969. }
  970. }
  971. else
  972. {
  973. taskEXIT_CRITICAL();
  974. traceQUEUE_RECEIVE_FAILED( pxQueue );
  975. return errQUEUE_EMPTY;
  976. }
  977. }
  978. taskEXIT_CRITICAL();
  979. }
  980. }
  981. #endif /* configUSE_ALTERNATIVE_API */
  982. /*-----------------------------------------------------------*/
  983. BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t * const pxHigherPriorityTaskWoken, const BaseType_t xCopyPosition )
  984. {
  985. BaseType_t xReturn;
  986. UBaseType_t uxSavedInterruptStatus;
  987. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  988. configASSERT( pxQueue );
  989. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  990. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  991. /* RTOS ports that support interrupt nesting have the concept of a maximum
  992. system call (or maximum API call) interrupt priority. Interrupts that are
  993. above the maximum system call priority are kept permanently enabled, even
  994. when the RTOS kernel is in a critical section, but cannot make any calls to
  995. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  996. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  997. failure if a FreeRTOS API function is called from an interrupt that has been
  998. assigned a priority above the configured maximum system call priority.
  999. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1000. that have been assigned a priority at or (logically) below the maximum
  1001. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1002. safe API to ensure interrupt entry is as fast and as simple as possible.
  1003. More information (albeit Cortex-M specific) is provided on the following
  1004. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1005. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1006. /* Similar to xQueueGenericSend, except without blocking if there is no room
  1007. in the queue. Also don't directly wake a task that was blocked on a queue
  1008. read, instead return a flag to say whether a context switch is required or
  1009. not (i.e. has a task with a higher priority than us been woken by this
  1010. post). */
  1011. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1012. {
  1013. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1014. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  1015. {
  1016. traceQUEUE_SEND_FROM_ISR( pxQueue );
  1017. /* A task can only have an inherited priority if it is a mutex
  1018. holder - and if there is a mutex holder then the mutex cannot be
  1019. given from an ISR. Therefore, unlike the xQueueGenericGive()
  1020. function, there is no need to determine the need for priority
  1021. disinheritance here or to clear the mutex holder TCB member. */
  1022. ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  1023. #if ( configUSE_QUEUE_SETS == 1 )
  1024. {
  1025. if( pxQueue->pxQueueSetContainer != NULL )
  1026. {
  1027. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
  1028. {
  1029. /* The queue is a member of a queue set, and posting
  1030. to the queue set caused a higher priority task to
  1031. unblock. A context switch is required. */
  1032. if( pxHigherPriorityTaskWoken != NULL )
  1033. {
  1034. *pxHigherPriorityTaskWoken = pdTRUE;
  1035. }
  1036. else
  1037. {
  1038. mtCOVERAGE_TEST_MARKER();
  1039. }
  1040. }
  1041. else
  1042. {
  1043. mtCOVERAGE_TEST_MARKER();
  1044. }
  1045. }
  1046. else
  1047. {
  1048. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1049. {
  1050. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1051. {
  1052. /* The task waiting has a higher priority so
  1053. record that a context switch is required. */
  1054. if( pxHigherPriorityTaskWoken != NULL )
  1055. {
  1056. *pxHigherPriorityTaskWoken = pdTRUE;
  1057. }
  1058. else
  1059. {
  1060. mtCOVERAGE_TEST_MARKER();
  1061. }
  1062. }
  1063. else
  1064. {
  1065. mtCOVERAGE_TEST_MARKER();
  1066. }
  1067. }
  1068. else
  1069. {
  1070. mtCOVERAGE_TEST_MARKER();
  1071. }
  1072. }
  1073. }
  1074. #else /* configUSE_QUEUE_SETS */
  1075. {
  1076. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1077. {
  1078. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1079. {
  1080. /* The task waiting has a higher priority so record that a
  1081. context switch is required. */
  1082. if( pxHigherPriorityTaskWoken != NULL )
  1083. {
  1084. *pxHigherPriorityTaskWoken = pdTRUE;
  1085. }
  1086. else
  1087. {
  1088. mtCOVERAGE_TEST_MARKER();
  1089. }
  1090. }
  1091. else
  1092. {
  1093. mtCOVERAGE_TEST_MARKER();
  1094. }
  1095. }
  1096. else
  1097. {
  1098. mtCOVERAGE_TEST_MARKER();
  1099. }
  1100. }
  1101. #endif /* configUSE_QUEUE_SETS */
  1102. xReturn = pdPASS;
  1103. }
  1104. else
  1105. {
  1106. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  1107. xReturn = errQUEUE_FULL;
  1108. }
  1109. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1110. }
  1111. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1112. return xReturn;
  1113. }
  1114. /*-----------------------------------------------------------*/
  1115. BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken )
  1116. {
  1117. BaseType_t xReturn;
  1118. UBaseType_t uxSavedInterruptStatus;
  1119. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1120. configASSERT( pxQueue );
  1121. /* xQueueGenericSendFromISR() should be used in the item size is not 0. */
  1122. configASSERT( pxQueue->uxItemSize == 0 );
  1123. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1124. system call (or maximum API call) interrupt priority. Interrupts that are
  1125. above the maximum system call priority are kept permanently enabled, even
  1126. when the RTOS kernel is in a critical section, but cannot make any calls to
  1127. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1128. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1129. failure if a FreeRTOS API function is called from an interrupt that has been
  1130. assigned a priority above the configured maximum system call priority.
  1131. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1132. that have been assigned a priority at or (logically) below the maximum
  1133. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1134. safe API to ensure interrupt entry is as fast and as simple as possible.
  1135. More information (albeit Cortex-M specific) is provided on the following
  1136. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1137. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1138. /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
  1139. item size is 0. Don't directly wake a task that was blocked on a queue
  1140. read, instead return a flag to say whether a context switch is required or
  1141. not (i.e. has a task with a higher priority than us been woken by this
  1142. post). */
  1143. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1144. {
  1145. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1146. /* When the queue is used to implement a semaphore no data is ever
  1147. moved through the queue but it is still valid to see if the queue 'has
  1148. space'. */
  1149. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1150. {
  1151. traceQUEUE_SEND_FROM_ISR( pxQueue );
  1152. /* A task can only have an inherited priority if it is a mutex
  1153. holder - and if there is a mutex holder then the mutex cannot be
  1154. given from an ISR. Therefore, unlike the xQueueGenericGive()
  1155. function, there is no need to determine the need for priority
  1156. disinheritance here or to clear the mutex holder TCB member. */
  1157. ++( pxQueue->uxMessagesWaiting );
  1158. #if ( configUSE_QUEUE_SETS == 1 )
  1159. {
  1160. if( pxQueue->pxQueueSetContainer != NULL )
  1161. {
  1162. if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) == pdTRUE )
  1163. {
  1164. /* The semaphore is a member of a queue set, and
  1165. posting to the queue set caused a higher priority
  1166. task to unblock. A context switch is required. */
  1167. if( pxHigherPriorityTaskWoken != NULL )
  1168. {
  1169. *pxHigherPriorityTaskWoken = pdTRUE;
  1170. }
  1171. else
  1172. {
  1173. mtCOVERAGE_TEST_MARKER();
  1174. }
  1175. }
  1176. else
  1177. {
  1178. mtCOVERAGE_TEST_MARKER();
  1179. }
  1180. }
  1181. else
  1182. {
  1183. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1184. {
  1185. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1186. {
  1187. /* The task waiting has a higher priority so
  1188. record that a context switch is required. */
  1189. if( pxHigherPriorityTaskWoken != NULL )
  1190. {
  1191. *pxHigherPriorityTaskWoken = pdTRUE;
  1192. }
  1193. else
  1194. {
  1195. mtCOVERAGE_TEST_MARKER();
  1196. }
  1197. }
  1198. else
  1199. {
  1200. mtCOVERAGE_TEST_MARKER();
  1201. }
  1202. }
  1203. else
  1204. {
  1205. mtCOVERAGE_TEST_MARKER();
  1206. }
  1207. }
  1208. }
  1209. #else /* configUSE_QUEUE_SETS */
  1210. {
  1211. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1212. {
  1213. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1214. {
  1215. /* The task waiting has a higher priority so record that a
  1216. context switch is required. */
  1217. if( pxHigherPriorityTaskWoken != NULL )
  1218. {
  1219. *pxHigherPriorityTaskWoken = pdTRUE;
  1220. }
  1221. else
  1222. {
  1223. mtCOVERAGE_TEST_MARKER();
  1224. }
  1225. }
  1226. else
  1227. {
  1228. mtCOVERAGE_TEST_MARKER();
  1229. }
  1230. }
  1231. else
  1232. {
  1233. mtCOVERAGE_TEST_MARKER();
  1234. }
  1235. }
  1236. #endif /* configUSE_QUEUE_SETS */
  1237. xReturn = pdPASS;
  1238. }
  1239. else
  1240. {
  1241. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  1242. xReturn = errQUEUE_FULL;
  1243. }
  1244. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1245. }
  1246. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1247. return xReturn;
  1248. }
  1249. /*-----------------------------------------------------------*/
  1250. BaseType_t xQueueGenericReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait, const BaseType_t xJustPeeking )
  1251. {
  1252. BaseType_t xEntryTimeSet = pdFALSE;
  1253. TimeOut_t xTimeOut;
  1254. int8_t *pcOriginalReadPosition;
  1255. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1256. configASSERT( pxQueue );
  1257. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1258. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  1259. {
  1260. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  1261. }
  1262. #endif
  1263. /* This function relaxes the coding standard somewhat to allow return
  1264. statements within the function itself. This is done in the interest
  1265. of execution time efficiency. */
  1266. for( ;; )
  1267. {
  1268. taskENTER_CRITICAL(&pxQueue->mux);
  1269. {
  1270. /* Is there data in the queue now? To be running the calling task
  1271. must be the highest priority task wanting to access the queue. */
  1272. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1273. {
  1274. /* Remember the read position in case the queue is only being
  1275. peeked. */
  1276. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1277. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1278. if( xJustPeeking == pdFALSE )
  1279. {
  1280. traceQUEUE_RECEIVE( pxQueue );
  1281. /* Actually removing data, not just peeking. */
  1282. --( pxQueue->uxMessagesWaiting );
  1283. #if ( configUSE_MUTEXES == 1 )
  1284. {
  1285. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1286. {
  1287. /* Record the information required to implement
  1288. priority inheritance should it become necessary. */
  1289. pxQueue->pxMutexHolder = ( int8_t * ) pvTaskIncrementMutexHeldCount(); /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  1290. }
  1291. else
  1292. {
  1293. mtCOVERAGE_TEST_MARKER();
  1294. }
  1295. }
  1296. #endif /* configUSE_MUTEXES */
  1297. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1298. {
  1299. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  1300. {
  1301. queueYIELD_IF_USING_PREEMPTION();
  1302. }
  1303. else
  1304. {
  1305. mtCOVERAGE_TEST_MARKER();
  1306. }
  1307. }
  1308. else
  1309. {
  1310. mtCOVERAGE_TEST_MARKER();
  1311. }
  1312. }
  1313. else
  1314. {
  1315. traceQUEUE_PEEK( pxQueue );
  1316. /* The data is not being removed, so reset the read
  1317. pointer. */
  1318. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1319. /* The data is being left in the queue, so see if there are
  1320. any other tasks waiting for the data. */
  1321. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1322. {
  1323. /* Tasks that are removed from the event list will get added to
  1324. the pending ready list as the scheduler is still suspended. */
  1325. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1326. {
  1327. /* The task waiting has a higher priority than this task. */
  1328. queueYIELD_IF_USING_PREEMPTION();
  1329. }
  1330. else
  1331. {
  1332. mtCOVERAGE_TEST_MARKER();
  1333. }
  1334. }
  1335. else
  1336. {
  1337. mtCOVERAGE_TEST_MARKER();
  1338. }
  1339. }
  1340. taskEXIT_CRITICAL(&pxQueue->mux);
  1341. return pdPASS;
  1342. }
  1343. else
  1344. {
  1345. if( xTicksToWait == ( TickType_t ) 0 )
  1346. {
  1347. /* The queue was empty and no block time is specified (or
  1348. the block time has expired) so leave now. */
  1349. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1350. taskEXIT_CRITICAL(&pxQueue->mux);
  1351. return errQUEUE_EMPTY;
  1352. }
  1353. else if( xEntryTimeSet == pdFALSE )
  1354. {
  1355. /* The queue was empty and a block time was specified so
  1356. configure the timeout structure. */
  1357. vTaskSetTimeOutState( &xTimeOut );
  1358. xEntryTimeSet = pdTRUE;
  1359. }
  1360. else
  1361. {
  1362. /* Entry time was already set. */
  1363. mtCOVERAGE_TEST_MARKER();
  1364. }
  1365. }
  1366. }
  1367. taskEXIT_CRITICAL(&pxQueue->mux);
  1368. /* Interrupts and other tasks can send to and receive from the queue
  1369. now the critical section has been exited. */
  1370. taskENTER_CRITICAL(&pxQueue->mux);
  1371. /* Update the timeout state to see if it has expired yet. */
  1372. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  1373. {
  1374. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1375. {
  1376. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  1377. #if ( configUSE_MUTEXES == 1 )
  1378. {
  1379. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1380. {
  1381. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  1382. }
  1383. else
  1384. {
  1385. mtCOVERAGE_TEST_MARKER();
  1386. }
  1387. }
  1388. #endif
  1389. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1390. taskEXIT_CRITICAL(&pxQueue->mux);
  1391. portYIELD_WITHIN_API();
  1392. }
  1393. else
  1394. {
  1395. /* Try again. */
  1396. taskEXIT_CRITICAL(&pxQueue->mux);
  1397. }
  1398. }
  1399. else
  1400. {
  1401. taskEXIT_CRITICAL(&pxQueue->mux);
  1402. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1403. return errQUEUE_EMPTY;
  1404. }
  1405. }
  1406. }
  1407. /*-----------------------------------------------------------*/
  1408. BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void * const pvBuffer, BaseType_t * const pxHigherPriorityTaskWoken )
  1409. {
  1410. BaseType_t xReturn;
  1411. UBaseType_t uxSavedInterruptStatus;
  1412. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1413. configASSERT( pxQueue );
  1414. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1415. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1416. system call (or maximum API call) interrupt priority. Interrupts that are
  1417. above the maximum system call priority are kept permanently enabled, even
  1418. when the RTOS kernel is in a critical section, but cannot make any calls to
  1419. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1420. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1421. failure if a FreeRTOS API function is called from an interrupt that has been
  1422. assigned a priority above the configured maximum system call priority.
  1423. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1424. that have been assigned a priority at or (logically) below the maximum
  1425. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1426. safe API to ensure interrupt entry is as fast and as simple as possible.
  1427. More information (albeit Cortex-M specific) is provided on the following
  1428. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1429. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1430. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1431. {
  1432. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1433. /* Cannot block in an ISR, so check there is data available. */
  1434. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1435. {
  1436. traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
  1437. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1438. --( pxQueue->uxMessagesWaiting );
  1439. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1440. {
  1441. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1442. {
  1443. /* The task waiting has a higher priority than us so
  1444. force a context switch. */
  1445. if( pxHigherPriorityTaskWoken != NULL )
  1446. {
  1447. *pxHigherPriorityTaskWoken = pdTRUE;
  1448. }
  1449. else
  1450. {
  1451. mtCOVERAGE_TEST_MARKER();
  1452. }
  1453. }
  1454. else
  1455. {
  1456. mtCOVERAGE_TEST_MARKER();
  1457. }
  1458. }
  1459. else
  1460. {
  1461. mtCOVERAGE_TEST_MARKER();
  1462. }
  1463. xReturn = pdPASS;
  1464. }
  1465. else
  1466. {
  1467. xReturn = pdFAIL;
  1468. traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
  1469. }
  1470. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1471. }
  1472. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1473. return xReturn;
  1474. }
  1475. /*-----------------------------------------------------------*/
  1476. BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, void * const pvBuffer )
  1477. {
  1478. BaseType_t xReturn;
  1479. UBaseType_t uxSavedInterruptStatus;
  1480. int8_t *pcOriginalReadPosition;
  1481. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1482. configASSERT( pxQueue );
  1483. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1484. configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
  1485. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1486. system call (or maximum API call) interrupt priority. Interrupts that are
  1487. above the maximum system call priority are kept permanently enabled, even
  1488. when the RTOS kernel is in a critical section, but cannot make any calls to
  1489. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1490. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1491. failure if a FreeRTOS API function is called from an interrupt that has been
  1492. assigned a priority above the configured maximum system call priority.
  1493. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1494. that have been assigned a priority at or (logically) below the maximum
  1495. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1496. safe API to ensure interrupt entry is as fast and as simple as possible.
  1497. More information (albeit Cortex-M specific) is provided on the following
  1498. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1499. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1500. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1501. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1502. {
  1503. /* Cannot block in an ISR, so check there is data available. */
  1504. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1505. {
  1506. traceQUEUE_PEEK_FROM_ISR( pxQueue );
  1507. /* Remember the read position so it can be reset as nothing is
  1508. actually being removed from the queue. */
  1509. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1510. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1511. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1512. xReturn = pdPASS;
  1513. }
  1514. else
  1515. {
  1516. xReturn = pdFAIL;
  1517. traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
  1518. }
  1519. }
  1520. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1521. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1522. return xReturn;
  1523. }
  1524. /*-----------------------------------------------------------*/
  1525. UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
  1526. {
  1527. UBaseType_t uxReturn;
  1528. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1529. configASSERT( xQueue );
  1530. taskENTER_CRITICAL(&pxQueue->mux);
  1531. {
  1532. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1533. }
  1534. taskEXIT_CRITICAL(&pxQueue->mux);
  1535. return uxReturn;
  1536. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1537. /*-----------------------------------------------------------*/
  1538. UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
  1539. {
  1540. UBaseType_t uxReturn;
  1541. Queue_t *pxQueue;
  1542. pxQueue = ( Queue_t * ) xQueue;
  1543. configASSERT( pxQueue );
  1544. taskENTER_CRITICAL(&pxQueue->mux);
  1545. {
  1546. uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
  1547. }
  1548. taskEXIT_CRITICAL(&pxQueue->mux);
  1549. return uxReturn;
  1550. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1551. /*-----------------------------------------------------------*/
  1552. UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
  1553. {
  1554. UBaseType_t uxReturn;
  1555. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1556. configASSERT( xQueue );
  1557. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1558. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1559. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1560. return uxReturn;
  1561. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1562. /*-----------------------------------------------------------*/
  1563. void vQueueDelete( QueueHandle_t xQueue )
  1564. {
  1565. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1566. configASSERT( pxQueue );
  1567. traceQUEUE_DELETE( pxQueue );
  1568. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  1569. {
  1570. vQueueUnregisterQueue( pxQueue );
  1571. }
  1572. #endif
  1573. #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  1574. {
  1575. /* The queue can only have been allocated dynamically - free it
  1576. again. */
  1577. vPortFree( pxQueue );
  1578. }
  1579. #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  1580. {
  1581. /* The queue could have been allocated statically or dynamically, so
  1582. check before attempting to free the memory. */
  1583. if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  1584. {
  1585. vPortFree( pxQueue );
  1586. }
  1587. else
  1588. {
  1589. mtCOVERAGE_TEST_MARKER();
  1590. }
  1591. }
  1592. #else
  1593. {
  1594. /* The queue must have been statically allocated, so is not going to be
  1595. deleted. Avoid compiler warnings about the unused parameter. */
  1596. ( void ) pxQueue;
  1597. }
  1598. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  1599. }
  1600. /*-----------------------------------------------------------*/
  1601. #if ( configUSE_TRACE_FACILITY == 1 )
  1602. UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
  1603. {
  1604. return ( ( Queue_t * ) xQueue )->uxQueueNumber;
  1605. }
  1606. #endif /* configUSE_TRACE_FACILITY */
  1607. /*-----------------------------------------------------------*/
  1608. #if ( configUSE_TRACE_FACILITY == 1 )
  1609. void vQueueSetQueueNumber( QueueHandle_t xQueue, UBaseType_t uxQueueNumber )
  1610. {
  1611. ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
  1612. }
  1613. #endif /* configUSE_TRACE_FACILITY */
  1614. /*-----------------------------------------------------------*/
  1615. #if ( configUSE_TRACE_FACILITY == 1 )
  1616. uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
  1617. {
  1618. return ( ( Queue_t * ) xQueue )->ucQueueType;
  1619. }
  1620. #endif /* configUSE_TRACE_FACILITY */
  1621. /*-----------------------------------------------------------*/
  1622. //This routine assumes the queue has already been locked.
  1623. static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition )
  1624. {
  1625. BaseType_t xReturn = pdFALSE;
  1626. if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
  1627. {
  1628. #if ( configUSE_MUTEXES == 1 )
  1629. {
  1630. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1631. {
  1632. /* The mutex is no longer being held. */
  1633. xReturn = xTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
  1634. pxQueue->pxMutexHolder = NULL;
  1635. }
  1636. else
  1637. {
  1638. mtCOVERAGE_TEST_MARKER();
  1639. }
  1640. }
  1641. #endif /* configUSE_MUTEXES */
  1642. }
  1643. else if( xPosition == queueSEND_TO_BACK )
  1644. {
  1645. ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0. */
  1646. pxQueue->pcWriteTo += pxQueue->uxItemSize;
  1647. if( pxQueue->pcWriteTo >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1648. {
  1649. pxQueue->pcWriteTo = pxQueue->pcHead;
  1650. }
  1651. else
  1652. {
  1653. mtCOVERAGE_TEST_MARKER();
  1654. }
  1655. }
  1656. else
  1657. {
  1658. ( void ) memcpy( ( void * ) pxQueue->u.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  1659. pxQueue->u.pcReadFrom -= pxQueue->uxItemSize;
  1660. if( pxQueue->u.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1661. {
  1662. pxQueue->u.pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
  1663. }
  1664. else
  1665. {
  1666. mtCOVERAGE_TEST_MARKER();
  1667. }
  1668. if( xPosition == queueOVERWRITE )
  1669. {
  1670. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1671. {
  1672. /* An item is not being added but overwritten, so subtract
  1673. one from the recorded number of items in the queue so when
  1674. one is added again below the number of recorded items remains
  1675. correct. */
  1676. --( pxQueue->uxMessagesWaiting );
  1677. }
  1678. else
  1679. {
  1680. mtCOVERAGE_TEST_MARKER();
  1681. }
  1682. }
  1683. else
  1684. {
  1685. mtCOVERAGE_TEST_MARKER();
  1686. }
  1687. }
  1688. ++( pxQueue->uxMessagesWaiting );
  1689. return xReturn;
  1690. }
  1691. /*-----------------------------------------------------------*/
  1692. static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer )
  1693. {
  1694. if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
  1695. {
  1696. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1697. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
  1698. {
  1699. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1700. }
  1701. else
  1702. {
  1703. mtCOVERAGE_TEST_MARKER();
  1704. }
  1705. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 MISRA exception as the casts are only redundant for some ports. Also previous logic ensures a null pointer can only be passed to memcpy() when the count is 0. */
  1706. }
  1707. }
  1708. /*-----------------------------------------------------------*/
  1709. static BaseType_t prvIsQueueEmpty( Queue_t *pxQueue )
  1710. {
  1711. BaseType_t xReturn;
  1712. //No lock needed: we read a base type.
  1713. {
  1714. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1715. {
  1716. xReturn = pdTRUE;
  1717. }
  1718. else
  1719. {
  1720. xReturn = pdFALSE;
  1721. }
  1722. }
  1723. return xReturn;
  1724. }
  1725. /*-----------------------------------------------------------*/
  1726. BaseType_t xQueueIsQueueEmptyFromISR( QueueHandle_t xQueue )
  1727. {
  1728. BaseType_t xReturn;
  1729. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1730. configASSERT( xQueue );
  1731. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1732. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1733. {
  1734. xReturn = pdTRUE;
  1735. }
  1736. else
  1737. {
  1738. xReturn = pdFALSE;
  1739. }
  1740. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1741. return xReturn;
  1742. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  1743. /*-----------------------------------------------------------*/
  1744. static BaseType_t prvIsQueueFull( Queue_t *pxQueue )
  1745. {
  1746. BaseType_t xReturn;
  1747. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1748. {
  1749. if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
  1750. {
  1751. xReturn = pdTRUE;
  1752. }
  1753. else
  1754. {
  1755. xReturn = pdFALSE;
  1756. }
  1757. }
  1758. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1759. return xReturn;
  1760. }
  1761. /*-----------------------------------------------------------*/
  1762. BaseType_t xQueueIsQueueFullFromISR( QueueHandle_t xQueue )
  1763. {
  1764. BaseType_t xReturn;
  1765. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1766. configASSERT( xQueue );
  1767. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1768. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( ( Queue_t * ) xQueue )->uxLength )
  1769. {
  1770. xReturn = pdTRUE;
  1771. }
  1772. else
  1773. {
  1774. xReturn = pdFALSE;
  1775. }
  1776. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1777. return xReturn;
  1778. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  1779. /*-----------------------------------------------------------*/
  1780. #if ( configUSE_CO_ROUTINES == 1 )
  1781. BaseType_t xQueueCRSend( QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait )
  1782. {
  1783. BaseType_t xReturn;
  1784. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1785. UNTESTED_FUNCTION();
  1786. /* If the queue is already full we may have to block. A critical section
  1787. is required to prevent an interrupt removing something from the queue
  1788. between the check to see if the queue is full and blocking on the queue. */
  1789. portDISABLE_INTERRUPTS();
  1790. {
  1791. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  1792. {
  1793. /* The queue is full - do we want to block or just leave without
  1794. posting? */
  1795. if( xTicksToWait > ( TickType_t ) 0 )
  1796. {
  1797. /* As this is called from a coroutine we cannot block directly, but
  1798. return indicating that we need to block. */
  1799. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
  1800. portENABLE_INTERRUPTS();
  1801. return errQUEUE_BLOCKED;
  1802. }
  1803. else
  1804. {
  1805. portENABLE_INTERRUPTS();
  1806. return errQUEUE_FULL;
  1807. }
  1808. }
  1809. }
  1810. portENABLE_INTERRUPTS();
  1811. portDISABLE_INTERRUPTS();
  1812. {
  1813. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1814. {
  1815. /* There is room in the queue, copy the data into the queue. */
  1816. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1817. xReturn = pdPASS;
  1818. /* Were any co-routines waiting for data to become available? */
  1819. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1820. {
  1821. /* In this instance the co-routine could be placed directly
  1822. into the ready list as we are within a critical section.
  1823. Instead the same pending ready list mechanism is used as if
  1824. the event were caused from within an interrupt. */
  1825. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1826. {
  1827. /* The co-routine waiting has a higher priority so record
  1828. that a yield might be appropriate. */
  1829. xReturn = errQUEUE_YIELD;
  1830. }
  1831. else
  1832. {
  1833. mtCOVERAGE_TEST_MARKER();
  1834. }
  1835. }
  1836. else
  1837. {
  1838. mtCOVERAGE_TEST_MARKER();
  1839. }
  1840. }
  1841. else
  1842. {
  1843. xReturn = errQUEUE_FULL;
  1844. }
  1845. }
  1846. portENABLE_INTERRUPTS();
  1847. return xReturn;
  1848. }
  1849. #endif /* configUSE_CO_ROUTINES */
  1850. /*-----------------------------------------------------------*/
  1851. #if ( configUSE_CO_ROUTINES == 1 )
  1852. BaseType_t xQueueCRReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait )
  1853. {
  1854. BaseType_t xReturn;
  1855. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1856. /* If the queue is already empty we may have to block. A critical section
  1857. is required to prevent an interrupt adding something to the queue
  1858. between the check to see if the queue is empty and blocking on the queue. */
  1859. portDISABLE_INTERRUPTS();
  1860. {
  1861. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1862. {
  1863. /* There are no messages in the queue, do we want to block or just
  1864. leave with nothing? */
  1865. if( xTicksToWait > ( TickType_t ) 0 )
  1866. {
  1867. /* As this is a co-routine we cannot block directly, but return
  1868. indicating that we need to block. */
  1869. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
  1870. portENABLE_INTERRUPTS();
  1871. return errQUEUE_BLOCKED;
  1872. }
  1873. else
  1874. {
  1875. portENABLE_INTERRUPTS();
  1876. return errQUEUE_FULL;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. mtCOVERAGE_TEST_MARKER();
  1882. }
  1883. }
  1884. portENABLE_INTERRUPTS();
  1885. portDISABLE_INTERRUPTS();
  1886. {
  1887. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1888. {
  1889. /* Data is available from the queue. */
  1890. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1891. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  1892. {
  1893. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1894. }
  1895. else
  1896. {
  1897. mtCOVERAGE_TEST_MARKER();
  1898. }
  1899. --( pxQueue->uxMessagesWaiting );
  1900. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1901. xReturn = pdPASS;
  1902. /* Were any co-routines waiting for space to become available? */
  1903. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1904. {
  1905. /* In this instance the co-routine could be placed directly
  1906. into the ready list as we are within a critical section.
  1907. Instead the same pending ready list mechanism is used as if
  1908. the event were caused from within an interrupt. */
  1909. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1910. {
  1911. xReturn = errQUEUE_YIELD;
  1912. }
  1913. else
  1914. {
  1915. mtCOVERAGE_TEST_MARKER();
  1916. }
  1917. }
  1918. else
  1919. {
  1920. mtCOVERAGE_TEST_MARKER();
  1921. }
  1922. }
  1923. else
  1924. {
  1925. xReturn = pdFAIL;
  1926. }
  1927. }
  1928. portENABLE_INTERRUPTS();
  1929. return xReturn;
  1930. }
  1931. #endif /* configUSE_CO_ROUTINES */
  1932. /*-----------------------------------------------------------*/
  1933. #if ( configUSE_CO_ROUTINES == 1 )
  1934. BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t xCoRoutinePreviouslyWoken )
  1935. {
  1936. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1937. /* Cannot block within an ISR so if there is no space on the queue then
  1938. exit without doing anything. */
  1939. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1940. {
  1941. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1942. /* We only want to wake one co-routine per ISR, so check that a
  1943. co-routine has not already been woken. */
  1944. if( xCoRoutinePreviouslyWoken == pdFALSE )
  1945. {
  1946. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1947. {
  1948. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1949. {
  1950. return pdTRUE;
  1951. }
  1952. else
  1953. {
  1954. mtCOVERAGE_TEST_MARKER();
  1955. }
  1956. }
  1957. else
  1958. {
  1959. mtCOVERAGE_TEST_MARKER();
  1960. }
  1961. }
  1962. else
  1963. {
  1964. mtCOVERAGE_TEST_MARKER();
  1965. }
  1966. }
  1967. else
  1968. {
  1969. mtCOVERAGE_TEST_MARKER();
  1970. }
  1971. return xCoRoutinePreviouslyWoken;
  1972. }
  1973. #endif /* configUSE_CO_ROUTINES */
  1974. /*-----------------------------------------------------------*/
  1975. #if ( configUSE_CO_ROUTINES == 1 )
  1976. BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer, BaseType_t *pxCoRoutineWoken )
  1977. {
  1978. BaseType_t xReturn;
  1979. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1980. /* We cannot block from an ISR, so check there is data available. If
  1981. not then just leave without doing anything. */
  1982. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1983. {
  1984. /* Copy the data from the queue. */
  1985. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1986. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  1987. {
  1988. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1989. }
  1990. else
  1991. {
  1992. mtCOVERAGE_TEST_MARKER();
  1993. }
  1994. --( pxQueue->uxMessagesWaiting );
  1995. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1996. if( ( *pxCoRoutineWoken ) == pdFALSE )
  1997. {
  1998. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1999. {
  2000. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  2001. {
  2002. *pxCoRoutineWoken = pdTRUE;
  2003. }
  2004. else
  2005. {
  2006. mtCOVERAGE_TEST_MARKER();
  2007. }
  2008. }
  2009. else
  2010. {
  2011. mtCOVERAGE_TEST_MARKER();
  2012. }
  2013. }
  2014. else
  2015. {
  2016. mtCOVERAGE_TEST_MARKER();
  2017. }
  2018. xReturn = pdPASS;
  2019. }
  2020. else
  2021. {
  2022. xReturn = pdFAIL;
  2023. }
  2024. return xReturn;
  2025. }
  2026. #endif /* configUSE_CO_ROUTINES */
  2027. /*-----------------------------------------------------------*/
  2028. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2029. void vQueueAddToRegistry( QueueHandle_t xQueue, const char *pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2030. {
  2031. UBaseType_t ux;
  2032. portENTER_CRITICAL(&queue_registry_spinlock);
  2033. /* See if there is an empty space in the registry. A NULL name denotes
  2034. a free slot. */
  2035. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2036. {
  2037. if( xQueueRegistry[ ux ].pcQueueName == NULL )
  2038. {
  2039. /* Store the information on this queue. */
  2040. xQueueRegistry[ ux ].pcQueueName = pcQueueName;
  2041. xQueueRegistry[ ux ].xHandle = xQueue;
  2042. traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
  2043. break;
  2044. }
  2045. else
  2046. {
  2047. mtCOVERAGE_TEST_MARKER();
  2048. }
  2049. }
  2050. portEXIT_CRITICAL(&queue_registry_spinlock);
  2051. }
  2052. #endif /* configQUEUE_REGISTRY_SIZE */
  2053. /*-----------------------------------------------------------*/
  2054. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2055. //This function is backported from FreeRTOS v9.0.0
  2056. const char *pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2057. {
  2058. UBaseType_t ux;
  2059. const char *pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2060. portENTER_CRITICAL(&queue_registry_spinlock);
  2061. /* Note there is nothing here to protect against another task adding or
  2062. removing entries from the registry while it is being searched. */
  2063. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2064. {
  2065. if( xQueueRegistry[ ux ].xHandle == xQueue )
  2066. {
  2067. pcReturn = xQueueRegistry[ ux ].pcQueueName;
  2068. break;
  2069. }
  2070. else
  2071. {
  2072. mtCOVERAGE_TEST_MARKER();
  2073. }
  2074. }
  2075. portEXIT_CRITICAL(&queue_registry_spinlock);
  2076. return pcReturn;
  2077. }
  2078. #endif /* configQUEUE_REGISTRY_SIZE */
  2079. /*-----------------------------------------------------------*/
  2080. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2081. void vQueueUnregisterQueue( QueueHandle_t xQueue )
  2082. {
  2083. UBaseType_t ux;
  2084. portENTER_CRITICAL(&queue_registry_spinlock);
  2085. /* See if the handle of the queue being unregistered in actually in the
  2086. registry. */
  2087. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2088. {
  2089. if( xQueueRegistry[ ux ].xHandle == xQueue )
  2090. {
  2091. /* Set the name to NULL to show that this slot if free again. */
  2092. xQueueRegistry[ ux ].pcQueueName = NULL;
  2093. break;
  2094. }
  2095. else
  2096. {
  2097. mtCOVERAGE_TEST_MARKER();
  2098. }
  2099. }
  2100. portEXIT_CRITICAL(&queue_registry_spinlock);
  2101. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  2102. #endif /* configQUEUE_REGISTRY_SIZE */
  2103. /*-----------------------------------------------------------*/
  2104. #if ( configUSE_TIMERS == 1 )
  2105. void vQueueWaitForMessageRestricted( QueueHandle_t xQueue, TickType_t xTicksToWait )
  2106. {
  2107. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  2108. /* This function should not be called by application code hence the
  2109. 'Restricted' in its name. It is not part of the public API. It is
  2110. designed for use by kernel code, and has special calling requirements.
  2111. It can result in vListInsert() being called on a list that can only
  2112. possibly ever have one item in it, so the list will be fast, but even
  2113. so it should be called with the scheduler locked and not from a critical
  2114. section. */
  2115. /* Only do anything if there are no messages in the queue. This function
  2116. will not actually cause the task to block, just place it on a blocked
  2117. list. It will not block until the scheduler is unlocked - at which
  2118. time a yield will be performed. */
  2119. taskENTER_CRITICAL(&pxQueue->mux);
  2120. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
  2121. {
  2122. /* There is nothing in the queue, block for the specified period. */
  2123. vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  2124. }
  2125. else
  2126. {
  2127. mtCOVERAGE_TEST_MARKER();
  2128. }
  2129. taskEXIT_CRITICAL(&pxQueue->mux);
  2130. }
  2131. #endif /* configUSE_TIMERS */
  2132. /*-----------------------------------------------------------*/
  2133. #if( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  2134. QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
  2135. {
  2136. QueueSetHandle_t pxQueue;
  2137. pxQueue = xQueueGenericCreate( uxEventQueueLength, sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
  2138. return pxQueue;
  2139. }
  2140. #endif /* configUSE_QUEUE_SETS */
  2141. /*-----------------------------------------------------------*/
  2142. #if ( configUSE_QUEUE_SETS == 1 )
  2143. BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  2144. {
  2145. BaseType_t xReturn;
  2146. taskENTER_CRITICAL(&(((Queue_t * )xQueueOrSemaphore)->mux));
  2147. {
  2148. if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
  2149. {
  2150. /* Cannot add a queue/semaphore to more than one queue set. */
  2151. xReturn = pdFAIL;
  2152. }
  2153. else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2154. {
  2155. /* Cannot add a queue/semaphore to a queue set if there are already
  2156. items in the queue/semaphore. */
  2157. xReturn = pdFAIL;
  2158. }
  2159. else
  2160. {
  2161. ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
  2162. xReturn = pdPASS;
  2163. }
  2164. }
  2165. taskEXIT_CRITICAL(&(((Queue_t * )xQueueOrSemaphore)->mux));
  2166. return xReturn;
  2167. }
  2168. #endif /* configUSE_QUEUE_SETS */
  2169. /*-----------------------------------------------------------*/
  2170. #if ( configUSE_QUEUE_SETS == 1 )
  2171. BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  2172. {
  2173. BaseType_t xReturn;
  2174. Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
  2175. if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
  2176. {
  2177. /* The queue was not a member of the set. */
  2178. xReturn = pdFAIL;
  2179. }
  2180. else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2181. {
  2182. /* It is dangerous to remove a queue from a set when the queue is
  2183. not empty because the queue set will still hold pending events for
  2184. the queue. */
  2185. xReturn = pdFAIL;
  2186. }
  2187. else
  2188. {
  2189. taskENTER_CRITICAL(&(pxQueueOrSemaphore->mux));
  2190. {
  2191. /* The queue is no longer contained in the set. */
  2192. pxQueueOrSemaphore->pxQueueSetContainer = NULL;
  2193. }
  2194. taskEXIT_CRITICAL(&(pxQueueOrSemaphore->mux));
  2195. xReturn = pdPASS;
  2196. }
  2197. return xReturn;
  2198. } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
  2199. #endif /* configUSE_QUEUE_SETS */
  2200. /*-----------------------------------------------------------*/
  2201. #if ( configUSE_QUEUE_SETS == 1 )
  2202. QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet, TickType_t const xTicksToWait )
  2203. {
  2204. QueueSetMemberHandle_t xReturn = NULL;
  2205. ( void ) xQueueGenericReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait, pdFALSE ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2206. return xReturn;
  2207. }
  2208. #endif /* configUSE_QUEUE_SETS */
  2209. /*-----------------------------------------------------------*/
  2210. #if ( configUSE_QUEUE_SETS == 1 )
  2211. QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
  2212. {
  2213. QueueSetMemberHandle_t xReturn = NULL;
  2214. ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2215. return xReturn;
  2216. }
  2217. #endif /* configUSE_QUEUE_SETS */
  2218. /*-----------------------------------------------------------*/
  2219. #if ( configUSE_QUEUE_SETS == 1 )
  2220. static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition )
  2221. {
  2222. Queue_t *pxQueueSetContainer = pxQueue->pxQueueSetContainer;
  2223. BaseType_t xReturn = pdFALSE;
  2224. /*
  2225. * This function is called with a Queue's / Semaphore's spinlock already
  2226. * acquired. Acquiring the Queue set's spinlock is still necessary.
  2227. */
  2228. configASSERT( pxQueueSetContainer );
  2229. //Acquire the Queue set's spinlock
  2230. portENTER_CRITICAL(&(pxQueueSetContainer->mux));
  2231. configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
  2232. if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
  2233. {
  2234. traceQUEUE_SEND( pxQueueSetContainer );
  2235. /* The data copied is the handle of the queue that contains data. */
  2236. xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );
  2237. if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
  2238. {
  2239. if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
  2240. {
  2241. /* The task waiting has a higher priority */
  2242. xReturn = pdTRUE;
  2243. }
  2244. else
  2245. {
  2246. mtCOVERAGE_TEST_MARKER();
  2247. }
  2248. }
  2249. else
  2250. {
  2251. mtCOVERAGE_TEST_MARKER();
  2252. }
  2253. }
  2254. else
  2255. {
  2256. mtCOVERAGE_TEST_MARKER();
  2257. }
  2258. //Release the Queue set's spinlock
  2259. portEXIT_CRITICAL(&(pxQueueSetContainer->mux));
  2260. return xReturn;
  2261. }
  2262. #endif /* configUSE_QUEUE_SETS */