queue.c 79 KB

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