queue.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606
  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. }
  278. #endif /* configASSERT_DEFINED */
  279. /* The address of a statically allocated queue was passed in, use it.
  280. The address of a statically allocated storage area was also passed in
  281. but is already set. */
  282. 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. */
  283. if( pxNewQueue != NULL )
  284. {
  285. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  286. {
  287. /* Queues can be allocated wither statically or dynamically, so
  288. note this queue was allocated statically in case the queue is
  289. later deleted. */
  290. pxNewQueue->ucStaticallyAllocated = pdTRUE;
  291. }
  292. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  293. prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
  294. }
  295. return pxNewQueue;
  296. }
  297. #endif /* configSUPPORT_STATIC_ALLOCATION */
  298. /*-----------------------------------------------------------*/
  299. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  300. QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType )
  301. {
  302. Queue_t *pxNewQueue;
  303. size_t xQueueSizeInBytes;
  304. uint8_t *pucQueueStorage;
  305. configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
  306. if( uxItemSize == ( UBaseType_t ) 0 )
  307. {
  308. /* There is not going to be a queue storage area. */
  309. xQueueSizeInBytes = ( size_t ) 0;
  310. }
  311. else
  312. {
  313. /* Allocate enough space to hold the maximum number of items that
  314. can be in the queue at any time. */
  315. xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  316. }
  317. pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );
  318. if( pxNewQueue != NULL )
  319. {
  320. /* Jump past the queue structure to find the location of the queue
  321. storage area. */
  322. pucQueueStorage = ( ( uint8_t * ) pxNewQueue ) + sizeof( Queue_t );
  323. #if( configSUPPORT_STATIC_ALLOCATION == 1 )
  324. {
  325. /* Queues can be created either statically or dynamically, so
  326. note this task was created dynamically in case it is later
  327. deleted. */
  328. pxNewQueue->ucStaticallyAllocated = pdFALSE;
  329. }
  330. #endif /* configSUPPORT_STATIC_ALLOCATION */
  331. prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
  332. }
  333. return pxNewQueue;
  334. }
  335. #endif /* configSUPPORT_STATIC_ALLOCATION */
  336. /*-----------------------------------------------------------*/
  337. static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue )
  338. {
  339. /* Remove compiler warnings about unused parameters should
  340. configUSE_TRACE_FACILITY not be set to 1. */
  341. ( void ) ucQueueType;
  342. if( uxItemSize == ( UBaseType_t ) 0 )
  343. {
  344. /* No RAM was allocated for the queue storage area, but PC head cannot
  345. be set to NULL because NULL is used as a key to say the queue is used as
  346. a mutex. Therefore just set pcHead to point to the queue as a benign
  347. value that is known to be within the memory map. */
  348. pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
  349. }
  350. else
  351. {
  352. /* Set the head to the start of the queue storage area. */
  353. pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
  354. }
  355. /* Initialise the queue members as described where the queue type is
  356. defined. */
  357. pxNewQueue->uxLength = uxQueueLength;
  358. pxNewQueue->uxItemSize = uxItemSize;
  359. ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
  360. #if ( configUSE_TRACE_FACILITY == 1 )
  361. {
  362. pxNewQueue->ucQueueType = ucQueueType;
  363. }
  364. #endif /* configUSE_TRACE_FACILITY */
  365. #if( configUSE_QUEUE_SETS == 1 )
  366. {
  367. pxNewQueue->pxQueueSetContainer = NULL;
  368. }
  369. #endif /* configUSE_QUEUE_SETS */
  370. traceQUEUE_CREATE( pxNewQueue );
  371. }
  372. /*-----------------------------------------------------------*/
  373. #if( configUSE_MUTEXES == 1 )
  374. static void prvInitialiseMutex( Queue_t *pxNewQueue )
  375. {
  376. if( pxNewQueue != NULL )
  377. {
  378. /* The queue create function will set all the queue structure members
  379. correctly for a generic queue, but this function is creating a
  380. mutex. Overwrite those members that need to be set differently -
  381. in particular the information required for priority inheritance. */
  382. pxNewQueue->pxMutexHolder = NULL;
  383. pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
  384. /* In case this is a recursive mutex. */
  385. pxNewQueue->u.uxRecursiveCallCount = 0;
  386. vPortCPUInitializeMutex(&pxNewQueue->mux);
  387. traceCREATE_MUTEX( pxNewQueue );
  388. /* Start with the semaphore in the expected state. */
  389. ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
  390. }
  391. else
  392. {
  393. traceCREATE_MUTEX_FAILED();
  394. }
  395. }
  396. #endif /* configUSE_MUTEXES */
  397. /*-----------------------------------------------------------*/
  398. #if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  399. QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
  400. {
  401. Queue_t *pxNewQueue;
  402. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  403. pxNewQueue = ( Queue_t * ) xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
  404. prvInitialiseMutex( pxNewQueue );
  405. return pxNewQueue;
  406. }
  407. #endif /* configUSE_MUTEXES */
  408. /*-----------------------------------------------------------*/
  409. #if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  410. QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType, StaticQueue_t *pxStaticQueue )
  411. {
  412. Queue_t *pxNewQueue;
  413. const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
  414. /* Prevent compiler warnings about unused parameters if
  415. configUSE_TRACE_FACILITY does not equal 1. */
  416. ( void ) ucQueueType;
  417. pxNewQueue = ( Queue_t * ) xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
  418. prvInitialiseMutex( pxNewQueue );
  419. return pxNewQueue;
  420. }
  421. #endif /* configUSE_MUTEXES */
  422. /*-----------------------------------------------------------*/
  423. #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
  424. void* xQueueGetMutexHolder( QueueHandle_t xSemaphore )
  425. {
  426. Queue_t * const pxQueue = ( Queue_t * ) xSemaphore;
  427. void *pxReturn;
  428. /* This function is called by xSemaphoreGetMutexHolder(), and should not
  429. be called directly. Note: This is a good way of determining if the
  430. calling task is the mutex holder, but not a good way of determining the
  431. identity of the mutex holder, as the holder may change between the
  432. following critical section exiting and the function returning. */
  433. taskENTER_CRITICAL(&pxQueue->mux);
  434. {
  435. if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
  436. {
  437. pxReturn = ( void * ) ( ( Queue_t * ) xSemaphore )->pxMutexHolder;
  438. }
  439. else
  440. {
  441. pxReturn = NULL;
  442. }
  443. }
  444. taskEXIT_CRITICAL(&pxQueue->mux);
  445. return pxReturn;
  446. } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
  447. #endif
  448. /*-----------------------------------------------------------*/
  449. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  450. BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
  451. {
  452. BaseType_t xReturn;
  453. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  454. configASSERT( pxMutex );
  455. /* If this is the task that holds the mutex then pxMutexHolder will not
  456. change outside of this task. If this task does not hold the mutex then
  457. pxMutexHolder can never coincidentally equal the tasks handle, and as
  458. this is the only condition we are interested in it does not matter if
  459. pxMutexHolder is accessed simultaneously by another task. Therefore no
  460. mutual exclusion is required to test the pxMutexHolder variable. */
  461. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Not a redundant cast as TaskHandle_t is a typedef. */
  462. {
  463. traceGIVE_MUTEX_RECURSIVE( pxMutex );
  464. /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
  465. the task handle, therefore no underflow check is required. Also,
  466. uxRecursiveCallCount is only modified by the mutex holder, and as
  467. there can only be one, no mutual exclusion is required to modify the
  468. uxRecursiveCallCount member. */
  469. ( pxMutex->u.uxRecursiveCallCount )--;
  470. /* Have we unwound the call count? */
  471. if( pxMutex->u.uxRecursiveCallCount == ( UBaseType_t ) 0 )
  472. {
  473. /* Return the mutex. This will automatically unblock any other
  474. task that might be waiting to access the mutex. */
  475. ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
  476. }
  477. else
  478. {
  479. mtCOVERAGE_TEST_MARKER();
  480. }
  481. xReturn = pdPASS;
  482. }
  483. else
  484. {
  485. /* The mutex cannot be given because the calling task is not the
  486. holder. */
  487. xReturn = pdFAIL;
  488. traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
  489. }
  490. return xReturn;
  491. }
  492. #endif /* configUSE_RECURSIVE_MUTEXES */
  493. /*-----------------------------------------------------------*/
  494. #if ( configUSE_RECURSIVE_MUTEXES == 1 )
  495. BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex, TickType_t xTicksToWait )
  496. {
  497. BaseType_t xReturn;
  498. Queue_t * const pxMutex = ( Queue_t * ) xMutex;
  499. configASSERT( pxMutex );
  500. /* Comments regarding mutual exclusion as per those within
  501. xQueueGiveMutexRecursive(). */
  502. traceTAKE_MUTEX_RECURSIVE( pxMutex );
  503. if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  504. {
  505. ( pxMutex->u.uxRecursiveCallCount )++;
  506. xReturn = pdPASS;
  507. }
  508. else
  509. {
  510. xReturn = xQueueGenericReceive( pxMutex, NULL, xTicksToWait, pdFALSE );
  511. /* pdPASS will only be returned if the mutex was successfully
  512. obtained. The calling task may have entered the Blocked state
  513. before reaching here. */
  514. if( xReturn == pdPASS )
  515. {
  516. ( pxMutex->u.uxRecursiveCallCount )++;
  517. }
  518. else
  519. {
  520. traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
  521. }
  522. }
  523. return xReturn;
  524. }
  525. #endif /* configUSE_RECURSIVE_MUTEXES */
  526. /*-----------------------------------------------------------*/
  527. #if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  528. QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount, StaticQueue_t *pxStaticQueue )
  529. {
  530. QueueHandle_t xHandle;
  531. configASSERT( uxMaxCount != 0 );
  532. configASSERT( uxInitialCount <= uxMaxCount );
  533. xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  534. if( xHandle != NULL )
  535. {
  536. ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
  537. traceCREATE_COUNTING_SEMAPHORE();
  538. }
  539. else
  540. {
  541. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  542. }
  543. return xHandle;
  544. }
  545. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  546. /*-----------------------------------------------------------*/
  547. #if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  548. QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount )
  549. {
  550. QueueHandle_t xHandle;
  551. configASSERT( uxMaxCount != 0 );
  552. configASSERT( uxInitialCount <= uxMaxCount );
  553. xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
  554. if( xHandle != NULL )
  555. {
  556. ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
  557. traceCREATE_COUNTING_SEMAPHORE();
  558. }
  559. else
  560. {
  561. traceCREATE_COUNTING_SEMAPHORE_FAILED();
  562. }
  563. configASSERT( xHandle );
  564. return xHandle;
  565. }
  566. #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  567. /*-----------------------------------------------------------*/
  568. BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition )
  569. {
  570. BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
  571. TimeOut_t xTimeOut;
  572. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  573. configASSERT( pxQueue );
  574. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  575. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  576. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  577. {
  578. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  579. }
  580. #endif
  581. /* This function relaxes the coding standard somewhat to allow return
  582. statements within the function itself. This is done in the interest
  583. of execution time efficiency. */
  584. for( ;; )
  585. {
  586. taskENTER_CRITICAL(&pxQueue->mux);
  587. {
  588. /* Is there room on the queue now? The running task must be
  589. the highest priority task wanting to access the queue. If
  590. the head item in the queue is to be overwritten then it does
  591. not matter if the queue is full. */
  592. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  593. {
  594. traceQUEUE_SEND( pxQueue );
  595. xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  596. #if ( configUSE_QUEUE_SETS == 1 )
  597. {
  598. if( pxQueue->pxQueueSetContainer != NULL )
  599. {
  600. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
  601. {
  602. /* The queue is a member of a queue set, and posting
  603. to the queue set caused a higher priority task to
  604. unblock. A context switch is required. */
  605. queueYIELD_IF_USING_PREEMPTION();
  606. }
  607. else
  608. {
  609. mtCOVERAGE_TEST_MARKER();
  610. }
  611. }
  612. else
  613. {
  614. /* If there was a task waiting for data to arrive on the
  615. queue then unblock it now. */
  616. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  617. {
  618. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  619. {
  620. /* The unblocked task has a priority higher than
  621. our own so yield immediately. Yes it is ok to
  622. do this from within the critical section - the
  623. kernel takes care of that. */
  624. queueYIELD_IF_USING_PREEMPTION();
  625. }
  626. else
  627. {
  628. mtCOVERAGE_TEST_MARKER();
  629. }
  630. }
  631. else if( xYieldRequired != pdFALSE )
  632. {
  633. /* This path is a special case that will only get
  634. executed if the task was holding multiple mutexes
  635. and the mutexes were given back in an order that is
  636. different to that in which they were taken. */
  637. queueYIELD_IF_USING_PREEMPTION();
  638. }
  639. else
  640. {
  641. mtCOVERAGE_TEST_MARKER();
  642. }
  643. }
  644. }
  645. #else /* configUSE_QUEUE_SETS */
  646. {
  647. /* If there was a task waiting for data to arrive on the
  648. queue then unblock it now. */
  649. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  650. {
  651. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  652. {
  653. /* The unblocked task has a priority higher than
  654. our own so yield immediately. Yes it is ok to do
  655. this from within the critical section - the kernel
  656. takes care of that. */
  657. queueYIELD_IF_USING_PREEMPTION();
  658. }
  659. else
  660. {
  661. mtCOVERAGE_TEST_MARKER();
  662. }
  663. }
  664. else if( xYieldRequired != pdFALSE )
  665. {
  666. /* This path is a special case that will only get
  667. executed if the task was holding multiple mutexes and
  668. the mutexes were given back in an order that is
  669. different to that in which they were taken. */
  670. queueYIELD_IF_USING_PREEMPTION();
  671. }
  672. else
  673. {
  674. mtCOVERAGE_TEST_MARKER();
  675. }
  676. }
  677. #endif /* configUSE_QUEUE_SETS */
  678. taskEXIT_CRITICAL(&pxQueue->mux);
  679. return pdPASS;
  680. }
  681. else
  682. {
  683. if( xTicksToWait == ( TickType_t ) 0 )
  684. {
  685. /* The queue was full and no block time is specified (or
  686. the block time has expired) so leave now. */
  687. taskEXIT_CRITICAL(&pxQueue->mux);
  688. /* Return to the original privilege level before exiting
  689. the function. */
  690. traceQUEUE_SEND_FAILED( pxQueue );
  691. return errQUEUE_FULL;
  692. }
  693. else if( xEntryTimeSet == pdFALSE )
  694. {
  695. /* The queue was full and a block time was specified so
  696. configure the timeout structure. */
  697. vTaskSetTimeOutState( &xTimeOut );
  698. xEntryTimeSet = pdTRUE;
  699. }
  700. else
  701. {
  702. /* Entry time was already set. */
  703. mtCOVERAGE_TEST_MARKER();
  704. }
  705. }
  706. }
  707. taskEXIT_CRITICAL(&pxQueue->mux);
  708. /* Interrupts and other tasks can send to and receive from the queue
  709. now the critical section has been exited. */
  710. taskENTER_CRITICAL(&pxQueue->mux);
  711. /* Update the timeout state to see if it has expired yet. */
  712. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  713. {
  714. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  715. {
  716. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  717. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  718. /* Resuming the scheduler will move tasks from the pending
  719. ready list into the ready list - so it is feasible that this
  720. task is already in a ready list before it yields - in which
  721. case the yield will not cause a context switch unless there
  722. is also a higher priority task in the pending ready list. */
  723. taskEXIT_CRITICAL(&pxQueue->mux);
  724. portYIELD_WITHIN_API();
  725. }
  726. else
  727. {
  728. /* Try again. */
  729. taskEXIT_CRITICAL(&pxQueue->mux);
  730. }
  731. }
  732. else
  733. {
  734. /* The timeout has expired. */
  735. taskEXIT_CRITICAL(&pxQueue->mux);
  736. /* Return to the original privilege level before exiting the
  737. function. */
  738. traceQUEUE_SEND_FAILED( pxQueue );
  739. return errQUEUE_FULL;
  740. }
  741. }
  742. }
  743. /*-----------------------------------------------------------*/
  744. #if ( configUSE_ALTERNATIVE_API == 1 )
  745. BaseType_t xQueueAltGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, BaseType_t xCopyPosition )
  746. {
  747. BaseType_t xEntryTimeSet = pdFALSE;
  748. TimeOut_t xTimeOut;
  749. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  750. configASSERT( pxQueue );
  751. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  752. for( ;; )
  753. {
  754. taskENTER_CRITICAL(&pxQueue->mux);
  755. {
  756. /* Is there room on the queue now? To be running we must be
  757. the highest priority task wanting to access the queue. */
  758. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  759. {
  760. traceQUEUE_SEND( pxQueue );
  761. prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  762. /* If there was a task waiting for data to arrive on the
  763. queue then unblock it now. */
  764. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  765. {
  766. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
  767. {
  768. /* The unblocked task has a priority higher than
  769. our own so yield immediately. */
  770. taskEXIT_CRITICAL(&pxQueue->mux);
  771. portYIELD_WITHIN_API();
  772. taskENTER_CRITICAL(&pxQueue->mux);
  773. }
  774. else
  775. {
  776. mtCOVERAGE_TEST_MARKER();
  777. }
  778. }
  779. else
  780. {
  781. mtCOVERAGE_TEST_MARKER();
  782. }
  783. taskEXIT_CRITICAL(&pxQueue->mux);
  784. return pdPASS;
  785. }
  786. else
  787. {
  788. if( xTicksToWait == ( TickType_t ) 0 )
  789. {
  790. taskEXIT_CRITICAL(&pxQueue->mux);
  791. return errQUEUE_FULL;
  792. }
  793. else if( xEntryTimeSet == pdFALSE )
  794. {
  795. vTaskSetTimeOutState( &xTimeOut );
  796. xEntryTimeSet = pdTRUE;
  797. }
  798. }
  799. }
  800. taskEXIT_CRITICAL(&pxQueue->mux);
  801. taskENTER_CRITICAL(&pxQueue->mux);
  802. {
  803. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  804. {
  805. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  806. {
  807. traceBLOCKING_ON_QUEUE_SEND( pxQueue );
  808. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
  809. taskEXIT_CRITICAL(&pxQueue->mux);
  810. portYIELD_WITHIN_API();
  811. taskENTER_CRITICAL(&pxQueue->mux);
  812. }
  813. else
  814. {
  815. mtCOVERAGE_TEST_MARKER();
  816. }
  817. }
  818. else
  819. {
  820. taskEXIT_CRITICAL(&pxQueue->mux);
  821. traceQUEUE_SEND_FAILED( pxQueue );
  822. return errQUEUE_FULL;
  823. }
  824. }
  825. taskEXIT_CRITICAL(&pxQueue->mux);
  826. }
  827. }
  828. #endif /* configUSE_ALTERNATIVE_API */
  829. /*-----------------------------------------------------------*/
  830. #if ( configUSE_ALTERNATIVE_API == 1 )
  831. BaseType_t xQueueAltGenericReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait, BaseType_t xJustPeeking )
  832. {
  833. BaseType_t xEntryTimeSet = pdFALSE;
  834. TimeOut_t xTimeOut;
  835. int8_t *pcOriginalReadPosition;
  836. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  837. configASSERT( pxQueue );
  838. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  839. UNTESTED_FUNCTION();
  840. for( ;; )
  841. {
  842. taskENTER_CRITICAL();
  843. {
  844. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  845. {
  846. /* Remember our read position in case we are just peeking. */
  847. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  848. prvCopyDataFromQueue( pxQueue, pvBuffer );
  849. if( xJustPeeking == pdFALSE )
  850. {
  851. traceQUEUE_RECEIVE( pxQueue );
  852. /* Data is actually being removed (not just peeked). */
  853. --( pxQueue->uxMessagesWaiting );
  854. #if ( configUSE_MUTEXES == 1 )
  855. {
  856. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  857. {
  858. /* Record the information required to implement
  859. priority inheritance should it become necessary. */
  860. pxQueue->pxMutexHolder = ( int8_t * ) xTaskGetCurrentTaskHandle();
  861. }
  862. else
  863. {
  864. mtCOVERAGE_TEST_MARKER();
  865. }
  866. }
  867. #endif
  868. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  869. {
  870. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  871. {
  872. portYIELD_WITHIN_API();
  873. }
  874. else
  875. {
  876. mtCOVERAGE_TEST_MARKER();
  877. }
  878. }
  879. }
  880. else
  881. {
  882. traceQUEUE_PEEK( pxQueue );
  883. /* The data is not being removed, so reset our read
  884. pointer. */
  885. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  886. /* The data is being left in the queue, so see if there are
  887. any other tasks waiting for the data. */
  888. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  889. {
  890. /* Tasks that are removed from the event list will get added to
  891. the pending ready list as the scheduler is still suspended. */
  892. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  893. {
  894. /* The task waiting has a higher priority than this task. */
  895. portYIELD_WITHIN_API();
  896. }
  897. else
  898. {
  899. mtCOVERAGE_TEST_MARKER();
  900. }
  901. }
  902. else
  903. {
  904. mtCOVERAGE_TEST_MARKER();
  905. }
  906. }
  907. taskEXIT_CRITICAL();
  908. return pdPASS;
  909. }
  910. else
  911. {
  912. if( xTicksToWait == ( TickType_t ) 0 )
  913. {
  914. taskEXIT_CRITICAL();
  915. traceQUEUE_RECEIVE_FAILED( pxQueue );
  916. return errQUEUE_EMPTY;
  917. }
  918. else if( xEntryTimeSet == pdFALSE )
  919. {
  920. vTaskSetTimeOutState( &xTimeOut );
  921. xEntryTimeSet = pdTRUE;
  922. }
  923. }
  924. }
  925. taskEXIT_CRITICAL();
  926. taskENTER_CRITICAL();
  927. {
  928. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  929. {
  930. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  931. {
  932. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  933. #if ( configUSE_MUTEXES == 1 )
  934. {
  935. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  936. {
  937. taskENTER_CRITICAL();
  938. {
  939. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  940. }
  941. taskEXIT_CRITICAL();
  942. }
  943. else
  944. {
  945. mtCOVERAGE_TEST_MARKER();
  946. }
  947. }
  948. #endif
  949. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  950. portYIELD_WITHIN_API();
  951. }
  952. else
  953. {
  954. mtCOVERAGE_TEST_MARKER();
  955. }
  956. }
  957. else
  958. {
  959. taskEXIT_CRITICAL();
  960. traceQUEUE_RECEIVE_FAILED( pxQueue );
  961. return errQUEUE_EMPTY;
  962. }
  963. }
  964. taskEXIT_CRITICAL();
  965. }
  966. }
  967. #endif /* configUSE_ALTERNATIVE_API */
  968. /*-----------------------------------------------------------*/
  969. BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t * const pxHigherPriorityTaskWoken, const BaseType_t xCopyPosition )
  970. {
  971. BaseType_t xReturn;
  972. UBaseType_t uxSavedInterruptStatus;
  973. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  974. configASSERT( pxQueue );
  975. configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  976. configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
  977. /* RTOS ports that support interrupt nesting have the concept of a maximum
  978. system call (or maximum API call) interrupt priority. Interrupts that are
  979. above the maximum system call priority are kept permanently enabled, even
  980. when the RTOS kernel is in a critical section, but cannot make any calls to
  981. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  982. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  983. failure if a FreeRTOS API function is called from an interrupt that has been
  984. assigned a priority above the configured maximum system call priority.
  985. Only FreeRTOS functions that end in FromISR can be called from interrupts
  986. that have been assigned a priority at or (logically) below the maximum
  987. system call interrupt priority. FreeRTOS maintains a separate interrupt
  988. safe API to ensure interrupt entry is as fast and as simple as possible.
  989. More information (albeit Cortex-M specific) is provided on the following
  990. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  991. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  992. /* Similar to xQueueGenericSend, except without blocking if there is no room
  993. in the queue. Also don't directly wake a task that was blocked on a queue
  994. read, instead return a flag to say whether a context switch is required or
  995. not (i.e. has a task with a higher priority than us been woken by this
  996. post). */
  997. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  998. {
  999. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1000. if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
  1001. {
  1002. traceQUEUE_SEND_FROM_ISR( pxQueue );
  1003. /* A task can only have an inherited priority if it is a mutex
  1004. holder - and if there is a mutex holder then the mutex cannot be
  1005. given from an ISR. Therefore, unlike the xQueueGenericGive()
  1006. function, there is no need to determine the need for priority
  1007. disinheritance here or to clear the mutex holder TCB member. */
  1008. ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
  1009. #if ( configUSE_QUEUE_SETS == 1 )
  1010. {
  1011. if( pxQueue->pxQueueSetContainer != NULL )
  1012. {
  1013. if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
  1014. {
  1015. /* The queue is a member of a queue set, and posting
  1016. to the queue set caused a higher priority task to
  1017. unblock. A context switch is required. */
  1018. if( pxHigherPriorityTaskWoken != NULL )
  1019. {
  1020. *pxHigherPriorityTaskWoken = pdTRUE;
  1021. }
  1022. else
  1023. {
  1024. mtCOVERAGE_TEST_MARKER();
  1025. }
  1026. }
  1027. else
  1028. {
  1029. mtCOVERAGE_TEST_MARKER();
  1030. }
  1031. }
  1032. else
  1033. {
  1034. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1035. {
  1036. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1037. {
  1038. /* The task waiting has a higher priority so
  1039. record that a context switch is required. */
  1040. if( pxHigherPriorityTaskWoken != NULL )
  1041. {
  1042. *pxHigherPriorityTaskWoken = pdTRUE;
  1043. }
  1044. else
  1045. {
  1046. mtCOVERAGE_TEST_MARKER();
  1047. }
  1048. }
  1049. else
  1050. {
  1051. mtCOVERAGE_TEST_MARKER();
  1052. }
  1053. }
  1054. else
  1055. {
  1056. mtCOVERAGE_TEST_MARKER();
  1057. }
  1058. }
  1059. }
  1060. #else /* configUSE_QUEUE_SETS */
  1061. {
  1062. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1063. {
  1064. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1065. {
  1066. /* The task waiting has a higher priority so record that a
  1067. context switch is required. */
  1068. if( pxHigherPriorityTaskWoken != NULL )
  1069. {
  1070. *pxHigherPriorityTaskWoken = pdTRUE;
  1071. }
  1072. else
  1073. {
  1074. mtCOVERAGE_TEST_MARKER();
  1075. }
  1076. }
  1077. else
  1078. {
  1079. mtCOVERAGE_TEST_MARKER();
  1080. }
  1081. }
  1082. else
  1083. {
  1084. mtCOVERAGE_TEST_MARKER();
  1085. }
  1086. }
  1087. #endif /* configUSE_QUEUE_SETS */
  1088. xReturn = pdPASS;
  1089. }
  1090. else
  1091. {
  1092. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  1093. xReturn = errQUEUE_FULL;
  1094. }
  1095. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1096. }
  1097. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1098. return xReturn;
  1099. }
  1100. /*-----------------------------------------------------------*/
  1101. BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken )
  1102. {
  1103. BaseType_t xReturn;
  1104. UBaseType_t uxSavedInterruptStatus;
  1105. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1106. configASSERT( pxQueue );
  1107. /* xQueueGenericSendFromISR() should be used in the item size is not 0. */
  1108. configASSERT( pxQueue->uxItemSize == 0 );
  1109. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1110. system call (or maximum API call) interrupt priority. Interrupts that are
  1111. above the maximum system call priority are kept permanently enabled, even
  1112. when the RTOS kernel is in a critical section, but cannot make any calls to
  1113. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1114. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1115. failure if a FreeRTOS API function is called from an interrupt that has been
  1116. assigned a priority above the configured maximum system call priority.
  1117. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1118. that have been assigned a priority at or (logically) below the maximum
  1119. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1120. safe API to ensure interrupt entry is as fast and as simple as possible.
  1121. More information (albeit Cortex-M specific) is provided on the following
  1122. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1123. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1124. /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
  1125. item size is 0. Don't directly wake a task that was blocked on a queue
  1126. read, instead return a flag to say whether a context switch is required or
  1127. not (i.e. has a task with a higher priority than us been woken by this
  1128. post). */
  1129. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1130. {
  1131. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1132. /* When the queue is used to implement a semaphore no data is ever
  1133. moved through the queue but it is still valid to see if the queue 'has
  1134. space'. */
  1135. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1136. {
  1137. traceQUEUE_SEND_FROM_ISR( pxQueue );
  1138. /* A task can only have an inherited priority if it is a mutex
  1139. holder - and if there is a mutex holder then the mutex cannot be
  1140. given from an ISR. Therefore, unlike the xQueueGenericGive()
  1141. function, there is no need to determine the need for priority
  1142. disinheritance here or to clear the mutex holder TCB member. */
  1143. ++( pxQueue->uxMessagesWaiting );
  1144. #if ( configUSE_QUEUE_SETS == 1 )
  1145. {
  1146. if( pxQueue->pxQueueSetContainer != NULL )
  1147. {
  1148. if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) == pdTRUE )
  1149. {
  1150. /* The semaphore is a member of a queue set, and
  1151. posting to the queue set caused a higher priority
  1152. task to unblock. A context switch is required. */
  1153. if( pxHigherPriorityTaskWoken != NULL )
  1154. {
  1155. *pxHigherPriorityTaskWoken = pdTRUE;
  1156. }
  1157. else
  1158. {
  1159. mtCOVERAGE_TEST_MARKER();
  1160. }
  1161. }
  1162. else
  1163. {
  1164. mtCOVERAGE_TEST_MARKER();
  1165. }
  1166. }
  1167. else
  1168. {
  1169. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1170. {
  1171. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1172. {
  1173. /* The task waiting has a higher priority so
  1174. record that a context switch is required. */
  1175. if( pxHigherPriorityTaskWoken != NULL )
  1176. {
  1177. *pxHigherPriorityTaskWoken = pdTRUE;
  1178. }
  1179. else
  1180. {
  1181. mtCOVERAGE_TEST_MARKER();
  1182. }
  1183. }
  1184. else
  1185. {
  1186. mtCOVERAGE_TEST_MARKER();
  1187. }
  1188. }
  1189. else
  1190. {
  1191. mtCOVERAGE_TEST_MARKER();
  1192. }
  1193. }
  1194. }
  1195. #else /* configUSE_QUEUE_SETS */
  1196. {
  1197. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1198. {
  1199. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1200. {
  1201. /* The task waiting has a higher priority so record that a
  1202. context switch is required. */
  1203. if( pxHigherPriorityTaskWoken != NULL )
  1204. {
  1205. *pxHigherPriorityTaskWoken = pdTRUE;
  1206. }
  1207. else
  1208. {
  1209. mtCOVERAGE_TEST_MARKER();
  1210. }
  1211. }
  1212. else
  1213. {
  1214. mtCOVERAGE_TEST_MARKER();
  1215. }
  1216. }
  1217. else
  1218. {
  1219. mtCOVERAGE_TEST_MARKER();
  1220. }
  1221. }
  1222. #endif /* configUSE_QUEUE_SETS */
  1223. xReturn = pdPASS;
  1224. }
  1225. else
  1226. {
  1227. traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
  1228. xReturn = errQUEUE_FULL;
  1229. }
  1230. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1231. }
  1232. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1233. return xReturn;
  1234. }
  1235. /*-----------------------------------------------------------*/
  1236. BaseType_t xQueueGenericReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait, const BaseType_t xJustPeeking )
  1237. {
  1238. BaseType_t xEntryTimeSet = pdFALSE;
  1239. TimeOut_t xTimeOut;
  1240. int8_t *pcOriginalReadPosition;
  1241. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1242. configASSERT( pxQueue );
  1243. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1244. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  1245. {
  1246. configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
  1247. }
  1248. #endif
  1249. /* This function relaxes the coding standard somewhat to allow return
  1250. statements within the function itself. This is done in the interest
  1251. of execution time efficiency. */
  1252. for( ;; )
  1253. {
  1254. taskENTER_CRITICAL(&pxQueue->mux);
  1255. {
  1256. /* Is there data in the queue now? To be running the calling task
  1257. must be the highest priority task wanting to access the queue. */
  1258. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1259. {
  1260. /* Remember the read position in case the queue is only being
  1261. peeked. */
  1262. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1263. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1264. if( xJustPeeking == pdFALSE )
  1265. {
  1266. traceQUEUE_RECEIVE( pxQueue );
  1267. /* Actually removing data, not just peeking. */
  1268. --( pxQueue->uxMessagesWaiting );
  1269. #if ( configUSE_MUTEXES == 1 )
  1270. {
  1271. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1272. {
  1273. /* Record the information required to implement
  1274. priority inheritance should it become necessary. */
  1275. pxQueue->pxMutexHolder = ( int8_t * ) pvTaskIncrementMutexHeldCount(); /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
  1276. }
  1277. else
  1278. {
  1279. mtCOVERAGE_TEST_MARKER();
  1280. }
  1281. }
  1282. #endif /* configUSE_MUTEXES */
  1283. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1284. {
  1285. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
  1286. {
  1287. queueYIELD_IF_USING_PREEMPTION();
  1288. }
  1289. else
  1290. {
  1291. mtCOVERAGE_TEST_MARKER();
  1292. }
  1293. }
  1294. else
  1295. {
  1296. mtCOVERAGE_TEST_MARKER();
  1297. }
  1298. }
  1299. else
  1300. {
  1301. traceQUEUE_PEEK( pxQueue );
  1302. /* The data is not being removed, so reset the read
  1303. pointer. */
  1304. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1305. /* The data is being left in the queue, so see if there are
  1306. any other tasks waiting for the data. */
  1307. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1308. {
  1309. /* Tasks that are removed from the event list will get added to
  1310. the pending ready list as the scheduler is still suspended. */
  1311. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1312. {
  1313. /* The task waiting has a higher priority than this task. */
  1314. queueYIELD_IF_USING_PREEMPTION();
  1315. }
  1316. else
  1317. {
  1318. mtCOVERAGE_TEST_MARKER();
  1319. }
  1320. }
  1321. else
  1322. {
  1323. mtCOVERAGE_TEST_MARKER();
  1324. }
  1325. }
  1326. taskEXIT_CRITICAL(&pxQueue->mux);
  1327. return pdPASS;
  1328. }
  1329. else
  1330. {
  1331. if( xTicksToWait == ( TickType_t ) 0 )
  1332. {
  1333. /* The queue was empty and no block time is specified (or
  1334. the block time has expired) so leave now. */
  1335. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1336. taskEXIT_CRITICAL(&pxQueue->mux);
  1337. return errQUEUE_EMPTY;
  1338. }
  1339. else if( xEntryTimeSet == pdFALSE )
  1340. {
  1341. /* The queue was empty and a block time was specified so
  1342. configure the timeout structure. */
  1343. vTaskSetTimeOutState( &xTimeOut );
  1344. xEntryTimeSet = pdTRUE;
  1345. }
  1346. else
  1347. {
  1348. /* Entry time was already set. */
  1349. mtCOVERAGE_TEST_MARKER();
  1350. }
  1351. }
  1352. }
  1353. taskEXIT_CRITICAL(&pxQueue->mux);
  1354. /* Interrupts and other tasks can send to and receive from the queue
  1355. now the critical section has been exited. */
  1356. taskENTER_CRITICAL(&pxQueue->mux);
  1357. /* Update the timeout state to see if it has expired yet. */
  1358. if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
  1359. {
  1360. if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
  1361. {
  1362. traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
  1363. #if ( configUSE_MUTEXES == 1 )
  1364. {
  1365. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1366. {
  1367. vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
  1368. }
  1369. else
  1370. {
  1371. mtCOVERAGE_TEST_MARKER();
  1372. }
  1373. }
  1374. #endif
  1375. vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  1376. taskEXIT_CRITICAL(&pxQueue->mux);
  1377. portYIELD_WITHIN_API();
  1378. }
  1379. else
  1380. {
  1381. /* Try again. */
  1382. taskEXIT_CRITICAL(&pxQueue->mux);
  1383. }
  1384. }
  1385. else
  1386. {
  1387. taskEXIT_CRITICAL(&pxQueue->mux);
  1388. traceQUEUE_RECEIVE_FAILED( pxQueue );
  1389. return errQUEUE_EMPTY;
  1390. }
  1391. }
  1392. }
  1393. /*-----------------------------------------------------------*/
  1394. BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void * const pvBuffer, BaseType_t * const pxHigherPriorityTaskWoken )
  1395. {
  1396. BaseType_t xReturn;
  1397. UBaseType_t uxSavedInterruptStatus;
  1398. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1399. configASSERT( pxQueue );
  1400. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1401. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1402. system call (or maximum API call) interrupt priority. Interrupts that are
  1403. above the maximum system call priority are kept permanently enabled, even
  1404. when the RTOS kernel is in a critical section, but cannot make any calls to
  1405. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1406. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1407. failure if a FreeRTOS API function is called from an interrupt that has been
  1408. assigned a priority above the configured maximum system call priority.
  1409. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1410. that have been assigned a priority at or (logically) below the maximum
  1411. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1412. safe API to ensure interrupt entry is as fast and as simple as possible.
  1413. More information (albeit Cortex-M specific) is provided on the following
  1414. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1415. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1416. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1417. {
  1418. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1419. /* Cannot block in an ISR, so check there is data available. */
  1420. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1421. {
  1422. traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
  1423. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1424. --( pxQueue->uxMessagesWaiting );
  1425. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1426. {
  1427. if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1428. {
  1429. /* The task waiting has a higher priority than us so
  1430. force a context switch. */
  1431. if( pxHigherPriorityTaskWoken != NULL )
  1432. {
  1433. *pxHigherPriorityTaskWoken = pdTRUE;
  1434. }
  1435. else
  1436. {
  1437. mtCOVERAGE_TEST_MARKER();
  1438. }
  1439. }
  1440. else
  1441. {
  1442. mtCOVERAGE_TEST_MARKER();
  1443. }
  1444. }
  1445. else
  1446. {
  1447. mtCOVERAGE_TEST_MARKER();
  1448. }
  1449. xReturn = pdPASS;
  1450. }
  1451. else
  1452. {
  1453. xReturn = pdFAIL;
  1454. traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
  1455. }
  1456. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1457. }
  1458. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1459. return xReturn;
  1460. }
  1461. /*-----------------------------------------------------------*/
  1462. BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, void * const pvBuffer )
  1463. {
  1464. BaseType_t xReturn;
  1465. UBaseType_t uxSavedInterruptStatus;
  1466. int8_t *pcOriginalReadPosition;
  1467. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1468. configASSERT( pxQueue );
  1469. configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
  1470. configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
  1471. /* RTOS ports that support interrupt nesting have the concept of a maximum
  1472. system call (or maximum API call) interrupt priority. Interrupts that are
  1473. above the maximum system call priority are kept permanently enabled, even
  1474. when the RTOS kernel is in a critical section, but cannot make any calls to
  1475. FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  1476. then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1477. failure if a FreeRTOS API function is called from an interrupt that has been
  1478. assigned a priority above the configured maximum system call priority.
  1479. Only FreeRTOS functions that end in FromISR can be called from interrupts
  1480. that have been assigned a priority at or (logically) below the maximum
  1481. system call interrupt priority. FreeRTOS maintains a separate interrupt
  1482. safe API to ensure interrupt entry is as fast and as simple as possible.
  1483. More information (albeit Cortex-M specific) is provided on the following
  1484. link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
  1485. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1486. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  1487. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1488. {
  1489. /* Cannot block in an ISR, so check there is data available. */
  1490. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1491. {
  1492. traceQUEUE_PEEK_FROM_ISR( pxQueue );
  1493. /* Remember the read position so it can be reset as nothing is
  1494. actually being removed from the queue. */
  1495. pcOriginalReadPosition = pxQueue->u.pcReadFrom;
  1496. prvCopyDataFromQueue( pxQueue, pvBuffer );
  1497. pxQueue->u.pcReadFrom = pcOriginalReadPosition;
  1498. xReturn = pdPASS;
  1499. }
  1500. else
  1501. {
  1502. xReturn = pdFAIL;
  1503. traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
  1504. }
  1505. }
  1506. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1507. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  1508. return xReturn;
  1509. }
  1510. /*-----------------------------------------------------------*/
  1511. UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
  1512. {
  1513. UBaseType_t uxReturn;
  1514. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1515. configASSERT( xQueue );
  1516. taskENTER_CRITICAL(&pxQueue->mux);
  1517. {
  1518. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1519. }
  1520. taskEXIT_CRITICAL(&pxQueue->mux);
  1521. return uxReturn;
  1522. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1523. /*-----------------------------------------------------------*/
  1524. UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
  1525. {
  1526. UBaseType_t uxReturn;
  1527. Queue_t *pxQueue;
  1528. pxQueue = ( Queue_t * ) xQueue;
  1529. configASSERT( pxQueue );
  1530. taskENTER_CRITICAL(&pxQueue->mux);
  1531. {
  1532. uxReturn = pxQueue->uxLength - pxQueue->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 uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
  1539. {
  1540. UBaseType_t uxReturn;
  1541. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1542. configASSERT( xQueue );
  1543. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1544. uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
  1545. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1546. return uxReturn;
  1547. } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
  1548. /*-----------------------------------------------------------*/
  1549. void vQueueDelete( QueueHandle_t xQueue )
  1550. {
  1551. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1552. configASSERT( pxQueue );
  1553. traceQUEUE_DELETE( pxQueue );
  1554. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  1555. {
  1556. vQueueUnregisterQueue( pxQueue );
  1557. }
  1558. #endif
  1559. #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
  1560. {
  1561. /* The queue can only have been allocated dynamically - free it
  1562. again. */
  1563. vPortFree( pxQueue );
  1564. }
  1565. #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  1566. {
  1567. /* The queue could have been allocated statically or dynamically, so
  1568. check before attempting to free the memory. */
  1569. if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
  1570. {
  1571. vPortFree( pxQueue );
  1572. }
  1573. else
  1574. {
  1575. mtCOVERAGE_TEST_MARKER();
  1576. }
  1577. }
  1578. #else
  1579. {
  1580. /* The queue must have been statically allocated, so is not going to be
  1581. deleted. Avoid compiler warnings about the unused parameter. */
  1582. ( void ) pxQueue;
  1583. }
  1584. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  1585. }
  1586. /*-----------------------------------------------------------*/
  1587. #if ( configUSE_TRACE_FACILITY == 1 )
  1588. UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
  1589. {
  1590. return ( ( Queue_t * ) xQueue )->uxQueueNumber;
  1591. }
  1592. #endif /* configUSE_TRACE_FACILITY */
  1593. /*-----------------------------------------------------------*/
  1594. #if ( configUSE_TRACE_FACILITY == 1 )
  1595. void vQueueSetQueueNumber( QueueHandle_t xQueue, UBaseType_t uxQueueNumber )
  1596. {
  1597. ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
  1598. }
  1599. #endif /* configUSE_TRACE_FACILITY */
  1600. /*-----------------------------------------------------------*/
  1601. #if ( configUSE_TRACE_FACILITY == 1 )
  1602. uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
  1603. {
  1604. return ( ( Queue_t * ) xQueue )->ucQueueType;
  1605. }
  1606. #endif /* configUSE_TRACE_FACILITY */
  1607. /*-----------------------------------------------------------*/
  1608. //This routine assumes the queue has already been locked.
  1609. static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition )
  1610. {
  1611. BaseType_t xReturn = pdFALSE;
  1612. if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
  1613. {
  1614. #if ( configUSE_MUTEXES == 1 )
  1615. {
  1616. if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
  1617. {
  1618. /* The mutex is no longer being held. */
  1619. xReturn = xTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
  1620. pxQueue->pxMutexHolder = NULL;
  1621. }
  1622. else
  1623. {
  1624. mtCOVERAGE_TEST_MARKER();
  1625. }
  1626. }
  1627. #endif /* configUSE_MUTEXES */
  1628. }
  1629. else if( xPosition == queueSEND_TO_BACK )
  1630. {
  1631. ( 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. */
  1632. pxQueue->pcWriteTo += pxQueue->uxItemSize;
  1633. if( pxQueue->pcWriteTo >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1634. {
  1635. pxQueue->pcWriteTo = pxQueue->pcHead;
  1636. }
  1637. else
  1638. {
  1639. mtCOVERAGE_TEST_MARKER();
  1640. }
  1641. }
  1642. else
  1643. {
  1644. ( void ) memcpy( ( void * ) pxQueue->u.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  1645. pxQueue->u.pcReadFrom -= pxQueue->uxItemSize;
  1646. if( pxQueue->u.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
  1647. {
  1648. pxQueue->u.pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
  1649. }
  1650. else
  1651. {
  1652. mtCOVERAGE_TEST_MARKER();
  1653. }
  1654. if( xPosition == queueOVERWRITE )
  1655. {
  1656. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1657. {
  1658. /* An item is not being added but overwritten, so subtract
  1659. one from the recorded number of items in the queue so when
  1660. one is added again below the number of recorded items remains
  1661. correct. */
  1662. --( pxQueue->uxMessagesWaiting );
  1663. }
  1664. else
  1665. {
  1666. mtCOVERAGE_TEST_MARKER();
  1667. }
  1668. }
  1669. else
  1670. {
  1671. mtCOVERAGE_TEST_MARKER();
  1672. }
  1673. }
  1674. ++( pxQueue->uxMessagesWaiting );
  1675. return xReturn;
  1676. }
  1677. /*-----------------------------------------------------------*/
  1678. static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer )
  1679. {
  1680. if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
  1681. {
  1682. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1683. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
  1684. {
  1685. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1686. }
  1687. else
  1688. {
  1689. mtCOVERAGE_TEST_MARKER();
  1690. }
  1691. ( 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. */
  1692. }
  1693. }
  1694. /*-----------------------------------------------------------*/
  1695. static BaseType_t prvIsQueueEmpty( Queue_t *pxQueue )
  1696. {
  1697. BaseType_t xReturn;
  1698. //No lock needed: we read a base type.
  1699. {
  1700. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1701. {
  1702. xReturn = pdTRUE;
  1703. }
  1704. else
  1705. {
  1706. xReturn = pdFALSE;
  1707. }
  1708. }
  1709. return xReturn;
  1710. }
  1711. /*-----------------------------------------------------------*/
  1712. BaseType_t xQueueIsQueueEmptyFromISR( QueueHandle_t xQueue )
  1713. {
  1714. BaseType_t xReturn;
  1715. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1716. configASSERT( xQueue );
  1717. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1718. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1719. {
  1720. xReturn = pdTRUE;
  1721. }
  1722. else
  1723. {
  1724. xReturn = pdFALSE;
  1725. }
  1726. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1727. return xReturn;
  1728. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  1729. /*-----------------------------------------------------------*/
  1730. static BaseType_t prvIsQueueFull( Queue_t *pxQueue )
  1731. {
  1732. BaseType_t xReturn;
  1733. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1734. {
  1735. if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
  1736. {
  1737. xReturn = pdTRUE;
  1738. }
  1739. else
  1740. {
  1741. xReturn = pdFALSE;
  1742. }
  1743. }
  1744. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1745. return xReturn;
  1746. }
  1747. /*-----------------------------------------------------------*/
  1748. BaseType_t xQueueIsQueueFullFromISR( QueueHandle_t xQueue )
  1749. {
  1750. BaseType_t xReturn;
  1751. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1752. configASSERT( xQueue );
  1753. taskENTER_CRITICAL_ISR(&pxQueue->mux);
  1754. if( ( ( Queue_t * ) xQueue )->uxMessagesWaiting == ( ( Queue_t * ) xQueue )->uxLength )
  1755. {
  1756. xReturn = pdTRUE;
  1757. }
  1758. else
  1759. {
  1760. xReturn = pdFALSE;
  1761. }
  1762. taskEXIT_CRITICAL_ISR(&pxQueue->mux);
  1763. return xReturn;
  1764. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  1765. /*-----------------------------------------------------------*/
  1766. #if ( configUSE_CO_ROUTINES == 1 )
  1767. BaseType_t xQueueCRSend( QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait )
  1768. {
  1769. BaseType_t xReturn;
  1770. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1771. UNTESTED_FUNCTION();
  1772. /* If the queue is already full we may have to block. A critical section
  1773. is required to prevent an interrupt removing something from the queue
  1774. between the check to see if the queue is full and blocking on the queue. */
  1775. portDISABLE_INTERRUPTS();
  1776. {
  1777. if( prvIsQueueFull( pxQueue ) != pdFALSE )
  1778. {
  1779. /* The queue is full - do we want to block or just leave without
  1780. posting? */
  1781. if( xTicksToWait > ( TickType_t ) 0 )
  1782. {
  1783. /* As this is called from a coroutine we cannot block directly, but
  1784. return indicating that we need to block. */
  1785. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
  1786. portENABLE_INTERRUPTS();
  1787. return errQUEUE_BLOCKED;
  1788. }
  1789. else
  1790. {
  1791. portENABLE_INTERRUPTS();
  1792. return errQUEUE_FULL;
  1793. }
  1794. }
  1795. }
  1796. portENABLE_INTERRUPTS();
  1797. portDISABLE_INTERRUPTS();
  1798. {
  1799. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1800. {
  1801. /* There is room in the queue, copy the data into the queue. */
  1802. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1803. xReturn = pdPASS;
  1804. /* Were any co-routines waiting for data to become available? */
  1805. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1806. {
  1807. /* In this instance the co-routine could be placed directly
  1808. into the ready list as we are within a critical section.
  1809. Instead the same pending ready list mechanism is used as if
  1810. the event were caused from within an interrupt. */
  1811. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1812. {
  1813. /* The co-routine waiting has a higher priority so record
  1814. that a yield might be appropriate. */
  1815. xReturn = errQUEUE_YIELD;
  1816. }
  1817. else
  1818. {
  1819. mtCOVERAGE_TEST_MARKER();
  1820. }
  1821. }
  1822. else
  1823. {
  1824. mtCOVERAGE_TEST_MARKER();
  1825. }
  1826. }
  1827. else
  1828. {
  1829. xReturn = errQUEUE_FULL;
  1830. }
  1831. }
  1832. portENABLE_INTERRUPTS();
  1833. return xReturn;
  1834. }
  1835. #endif /* configUSE_CO_ROUTINES */
  1836. /*-----------------------------------------------------------*/
  1837. #if ( configUSE_CO_ROUTINES == 1 )
  1838. BaseType_t xQueueCRReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait )
  1839. {
  1840. BaseType_t xReturn;
  1841. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1842. /* If the queue is already empty we may have to block. A critical section
  1843. is required to prevent an interrupt adding something to the queue
  1844. between the check to see if the queue is empty and blocking on the queue. */
  1845. portDISABLE_INTERRUPTS();
  1846. {
  1847. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
  1848. {
  1849. /* There are no messages in the queue, do we want to block or just
  1850. leave with nothing? */
  1851. if( xTicksToWait > ( TickType_t ) 0 )
  1852. {
  1853. /* As this is a co-routine we cannot block directly, but return
  1854. indicating that we need to block. */
  1855. vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
  1856. portENABLE_INTERRUPTS();
  1857. return errQUEUE_BLOCKED;
  1858. }
  1859. else
  1860. {
  1861. portENABLE_INTERRUPTS();
  1862. return errQUEUE_FULL;
  1863. }
  1864. }
  1865. else
  1866. {
  1867. mtCOVERAGE_TEST_MARKER();
  1868. }
  1869. }
  1870. portENABLE_INTERRUPTS();
  1871. portDISABLE_INTERRUPTS();
  1872. {
  1873. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1874. {
  1875. /* Data is available from the queue. */
  1876. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1877. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  1878. {
  1879. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1880. }
  1881. else
  1882. {
  1883. mtCOVERAGE_TEST_MARKER();
  1884. }
  1885. --( pxQueue->uxMessagesWaiting );
  1886. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1887. xReturn = pdPASS;
  1888. /* Were any co-routines waiting for space to become available? */
  1889. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1890. {
  1891. /* In this instance the co-routine could be placed directly
  1892. into the ready list as we are within a critical section.
  1893. Instead the same pending ready list mechanism is used as if
  1894. the event were caused from within an interrupt. */
  1895. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1896. {
  1897. xReturn = errQUEUE_YIELD;
  1898. }
  1899. else
  1900. {
  1901. mtCOVERAGE_TEST_MARKER();
  1902. }
  1903. }
  1904. else
  1905. {
  1906. mtCOVERAGE_TEST_MARKER();
  1907. }
  1908. }
  1909. else
  1910. {
  1911. xReturn = pdFAIL;
  1912. }
  1913. }
  1914. portENABLE_INTERRUPTS();
  1915. return xReturn;
  1916. }
  1917. #endif /* configUSE_CO_ROUTINES */
  1918. /*-----------------------------------------------------------*/
  1919. #if ( configUSE_CO_ROUTINES == 1 )
  1920. BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t xCoRoutinePreviouslyWoken )
  1921. {
  1922. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1923. /* Cannot block within an ISR so if there is no space on the queue then
  1924. exit without doing anything. */
  1925. if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
  1926. {
  1927. prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
  1928. /* We only want to wake one co-routine per ISR, so check that a
  1929. co-routine has not already been woken. */
  1930. if( xCoRoutinePreviouslyWoken == pdFALSE )
  1931. {
  1932. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
  1933. {
  1934. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
  1935. {
  1936. return pdTRUE;
  1937. }
  1938. else
  1939. {
  1940. mtCOVERAGE_TEST_MARKER();
  1941. }
  1942. }
  1943. else
  1944. {
  1945. mtCOVERAGE_TEST_MARKER();
  1946. }
  1947. }
  1948. else
  1949. {
  1950. mtCOVERAGE_TEST_MARKER();
  1951. }
  1952. }
  1953. else
  1954. {
  1955. mtCOVERAGE_TEST_MARKER();
  1956. }
  1957. return xCoRoutinePreviouslyWoken;
  1958. }
  1959. #endif /* configUSE_CO_ROUTINES */
  1960. /*-----------------------------------------------------------*/
  1961. #if ( configUSE_CO_ROUTINES == 1 )
  1962. BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer, BaseType_t *pxCoRoutineWoken )
  1963. {
  1964. BaseType_t xReturn;
  1965. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  1966. /* We cannot block from an ISR, so check there is data available. If
  1967. not then just leave without doing anything. */
  1968. if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
  1969. {
  1970. /* Copy the data from the queue. */
  1971. pxQueue->u.pcReadFrom += pxQueue->uxItemSize;
  1972. if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )
  1973. {
  1974. pxQueue->u.pcReadFrom = pxQueue->pcHead;
  1975. }
  1976. else
  1977. {
  1978. mtCOVERAGE_TEST_MARKER();
  1979. }
  1980. --( pxQueue->uxMessagesWaiting );
  1981. ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
  1982. if( ( *pxCoRoutineWoken ) == pdFALSE )
  1983. {
  1984. if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
  1985. {
  1986. if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
  1987. {
  1988. *pxCoRoutineWoken = pdTRUE;
  1989. }
  1990. else
  1991. {
  1992. mtCOVERAGE_TEST_MARKER();
  1993. }
  1994. }
  1995. else
  1996. {
  1997. mtCOVERAGE_TEST_MARKER();
  1998. }
  1999. }
  2000. else
  2001. {
  2002. mtCOVERAGE_TEST_MARKER();
  2003. }
  2004. xReturn = pdPASS;
  2005. }
  2006. else
  2007. {
  2008. xReturn = pdFAIL;
  2009. }
  2010. return xReturn;
  2011. }
  2012. #endif /* configUSE_CO_ROUTINES */
  2013. /*-----------------------------------------------------------*/
  2014. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2015. void vQueueAddToRegistry( QueueHandle_t xQueue, const char *pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2016. {
  2017. UBaseType_t ux;
  2018. portENTER_CRITICAL(&queue_registry_spinlock);
  2019. /* See if there is an empty space in the registry. A NULL name denotes
  2020. a free slot. */
  2021. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2022. {
  2023. if( xQueueRegistry[ ux ].pcQueueName == NULL )
  2024. {
  2025. /* Store the information on this queue. */
  2026. xQueueRegistry[ ux ].pcQueueName = pcQueueName;
  2027. xQueueRegistry[ ux ].xHandle = xQueue;
  2028. traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
  2029. break;
  2030. }
  2031. else
  2032. {
  2033. mtCOVERAGE_TEST_MARKER();
  2034. }
  2035. }
  2036. portEXIT_CRITICAL(&queue_registry_spinlock);
  2037. }
  2038. #endif /* configQUEUE_REGISTRY_SIZE */
  2039. /*-----------------------------------------------------------*/
  2040. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2041. //This function is backported from FreeRTOS v9.0.0
  2042. const char *pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2043. {
  2044. UBaseType_t ux;
  2045. const char *pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2046. portENTER_CRITICAL(&queue_registry_spinlock);
  2047. /* Note there is nothing here to protect against another task adding or
  2048. removing entries from the registry while it is being searched. */
  2049. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2050. {
  2051. if( xQueueRegistry[ ux ].xHandle == xQueue )
  2052. {
  2053. pcReturn = xQueueRegistry[ ux ].pcQueueName;
  2054. break;
  2055. }
  2056. else
  2057. {
  2058. mtCOVERAGE_TEST_MARKER();
  2059. }
  2060. }
  2061. portEXIT_CRITICAL(&queue_registry_spinlock);
  2062. return pcReturn;
  2063. }
  2064. #endif /* configQUEUE_REGISTRY_SIZE */
  2065. /*-----------------------------------------------------------*/
  2066. #if ( configQUEUE_REGISTRY_SIZE > 0 )
  2067. void vQueueUnregisterQueue( QueueHandle_t xQueue )
  2068. {
  2069. UBaseType_t ux;
  2070. portENTER_CRITICAL(&queue_registry_spinlock);
  2071. /* See if the handle of the queue being unregistered in actually in the
  2072. registry. */
  2073. for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
  2074. {
  2075. if( xQueueRegistry[ ux ].xHandle == xQueue )
  2076. {
  2077. /* Set the name to NULL to show that this slot if free again. */
  2078. xQueueRegistry[ ux ].pcQueueName = NULL;
  2079. break;
  2080. }
  2081. else
  2082. {
  2083. mtCOVERAGE_TEST_MARKER();
  2084. }
  2085. }
  2086. portEXIT_CRITICAL(&queue_registry_spinlock);
  2087. } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
  2088. #endif /* configQUEUE_REGISTRY_SIZE */
  2089. /*-----------------------------------------------------------*/
  2090. #if ( configUSE_TIMERS == 1 )
  2091. void vQueueWaitForMessageRestricted( QueueHandle_t xQueue, TickType_t xTicksToWait )
  2092. {
  2093. Queue_t * const pxQueue = ( Queue_t * ) xQueue;
  2094. /* This function should not be called by application code hence the
  2095. 'Restricted' in its name. It is not part of the public API. It is
  2096. designed for use by kernel code, and has special calling requirements.
  2097. It can result in vListInsert() being called on a list that can only
  2098. possibly ever have one item in it, so the list will be fast, but even
  2099. so it should be called with the scheduler locked and not from a critical
  2100. section. */
  2101. /* Only do anything if there are no messages in the queue. This function
  2102. will not actually cause the task to block, just place it on a blocked
  2103. list. It will not block until the scheduler is unlocked - at which
  2104. time a yield will be performed. */
  2105. taskENTER_CRITICAL(&pxQueue->mux);
  2106. if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
  2107. {
  2108. /* There is nothing in the queue, block for the specified period. */
  2109. vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
  2110. }
  2111. else
  2112. {
  2113. mtCOVERAGE_TEST_MARKER();
  2114. }
  2115. taskEXIT_CRITICAL(&pxQueue->mux);
  2116. }
  2117. #endif /* configUSE_TIMERS */
  2118. /*-----------------------------------------------------------*/
  2119. #if( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  2120. QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
  2121. {
  2122. QueueSetHandle_t pxQueue;
  2123. pxQueue = xQueueGenericCreate( uxEventQueueLength, sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
  2124. return pxQueue;
  2125. }
  2126. #endif /* configUSE_QUEUE_SETS */
  2127. /*-----------------------------------------------------------*/
  2128. #if ( configUSE_QUEUE_SETS == 1 )
  2129. BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  2130. {
  2131. BaseType_t xReturn;
  2132. //ToDo: figure out locking
  2133. // taskENTER_CRITICAL(&pxQueue->mux);
  2134. {
  2135. if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
  2136. {
  2137. /* Cannot add a queue/semaphore to more than one queue set. */
  2138. xReturn = pdFAIL;
  2139. }
  2140. else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2141. {
  2142. /* Cannot add a queue/semaphore to a queue set if there are already
  2143. items in the queue/semaphore. */
  2144. xReturn = pdFAIL;
  2145. }
  2146. else
  2147. {
  2148. ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
  2149. xReturn = pdPASS;
  2150. }
  2151. }
  2152. // taskEXIT_CRITICAL(&pxQueue->mux);
  2153. return xReturn;
  2154. }
  2155. #endif /* configUSE_QUEUE_SETS */
  2156. /*-----------------------------------------------------------*/
  2157. #if ( configUSE_QUEUE_SETS == 1 )
  2158. BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )
  2159. {
  2160. BaseType_t xReturn;
  2161. Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
  2162. if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
  2163. {
  2164. /* The queue was not a member of the set. */
  2165. xReturn = pdFAIL;
  2166. }
  2167. else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
  2168. {
  2169. /* It is dangerous to remove a queue from a set when the queue is
  2170. not empty because the queue set will still hold pending events for
  2171. the queue. */
  2172. xReturn = pdFAIL;
  2173. }
  2174. else
  2175. {
  2176. // taskENTER_CRITICAL(&pxQueue->mux);
  2177. {
  2178. /* The queue is no longer contained in the set. */
  2179. pxQueueOrSemaphore->pxQueueSetContainer = NULL;
  2180. }
  2181. // taskEXIT_CRITICAL(&pxQueue->mux);
  2182. xReturn = pdPASS;
  2183. }
  2184. return xReturn;
  2185. } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
  2186. #endif /* configUSE_QUEUE_SETS */
  2187. /*-----------------------------------------------------------*/
  2188. #if ( configUSE_QUEUE_SETS == 1 )
  2189. QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet, TickType_t const xTicksToWait )
  2190. {
  2191. QueueSetMemberHandle_t xReturn = NULL;
  2192. ( void ) xQueueGenericReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait, pdFALSE ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2193. return xReturn;
  2194. }
  2195. #endif /* configUSE_QUEUE_SETS */
  2196. /*-----------------------------------------------------------*/
  2197. #if ( configUSE_QUEUE_SETS == 1 )
  2198. QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
  2199. {
  2200. QueueSetMemberHandle_t xReturn = NULL;
  2201. ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
  2202. return xReturn;
  2203. }
  2204. #endif /* configUSE_QUEUE_SETS */
  2205. /*-----------------------------------------------------------*/
  2206. #if ( configUSE_QUEUE_SETS == 1 )
  2207. static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition )
  2208. {
  2209. Queue_t *pxQueueSetContainer = pxQueue->pxQueueSetContainer;
  2210. BaseType_t xReturn = pdFALSE;
  2211. /* This function must be called form a critical section. */
  2212. configASSERT( pxQueueSetContainer );
  2213. configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
  2214. if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
  2215. {
  2216. traceQUEUE_SEND( pxQueueSetContainer );
  2217. /* The data copied is the handle of the queue that contains data. */
  2218. xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );
  2219. if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
  2220. {
  2221. if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
  2222. {
  2223. /* The task waiting has a higher priority */
  2224. xReturn = pdTRUE;
  2225. }
  2226. else
  2227. {
  2228. mtCOVERAGE_TEST_MARKER();
  2229. }
  2230. }
  2231. else
  2232. {
  2233. mtCOVERAGE_TEST_MARKER();
  2234. }
  2235. }
  2236. else
  2237. {
  2238. mtCOVERAGE_TEST_MARKER();
  2239. }
  2240. return xReturn;
  2241. }
  2242. #endif /* configUSE_QUEUE_SETS */