queue.c 122 KB

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