queue.c 79 KB

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