queue.c 81 KB

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