queue.c 78 KB

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