queue.c 78 KB

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