queue.c 78 KB

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