queue.c 91 KB

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