tasks.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /* Standard includes. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. /* FreeRTOS includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /* Values that can be assigned to the ucNotifyState member of the TCB. */
  35. #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
  36. #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
  37. #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
  38. /*
  39. * Several functions take a TaskHandle_t parameter that can optionally be NULL,
  40. * where NULL is used to indicate that the handle of the currently executing
  41. * task should be used in place of the parameter. This macro simply checks to
  42. * see if the parameter is NULL and returns a pointer to the appropriate TCB.
  43. */
  44. #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( xTaskGetCurrentTaskHandle() ) : ( pxHandle ) )
  45. /*
  46. * Task control block. A task control block (TCB) is allocated for each task,
  47. * and stores task state information, including a pointer to the task's context
  48. * (the task's run time environment, including register values)
  49. */
  50. typedef struct tskTaskControlBlock
  51. {
  52. struct rt_thread thread;
  53. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  54. TaskHookFunction_t pxTaskTag;
  55. #endif
  56. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  57. volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
  58. volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
  59. #endif
  60. #if ( INCLUDE_xTaskAbortDelay == 1 )
  61. uint8_t ucDelayAborted;
  62. #endif
  63. } tskTCB;
  64. typedef tskTCB TCB_t;
  65. /* Other file private variables. --------------------------------*/
  66. static volatile BaseType_t xSchedulerRunning = pdFALSE;
  67. /*-----------------------------------------------------------*/
  68. /*
  69. * Called after a Task_t structure has been allocated either statically or
  70. * dynamically to fill in the structure's members.
  71. */
  72. static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
  73. const char * const pcName,
  74. const uint32_t ulStackDepth,
  75. void * const pvParameters,
  76. UBaseType_t uxPriority,
  77. TaskHandle_t * const pxCreatedTask,
  78. TCB_t * pxNewTCB,
  79. StackType_t * const puxStackBuffer );
  80. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  81. TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
  82. const char * const pcName,
  83. const uint32_t ulStackDepth,
  84. void * const pvParameters,
  85. UBaseType_t uxPriority,
  86. StackType_t * const puxStackBuffer,
  87. StaticTask_t * const pxTaskBuffer )
  88. {
  89. TCB_t * pxNewTCB;
  90. TaskHandle_t xReturn = NULL;
  91. configASSERT( puxStackBuffer != NULL );
  92. configASSERT( pxTaskBuffer != NULL );
  93. #if ( configASSERT_DEFINED == 1 )
  94. {
  95. /* Sanity check that the size of the structure used to declare a
  96. * variable of type StaticTask_t equals the size of the real task
  97. * structure. */
  98. volatile size_t xSize = sizeof( StaticTask_t );
  99. configASSERT( xSize == sizeof( TCB_t ) );
  100. ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
  101. }
  102. #endif /* configASSERT_DEFINED */
  103. if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
  104. {
  105. pxNewTCB = ( TCB_t * ) pxTaskBuffer;
  106. prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, puxStackBuffer );
  107. rt_thread_startup( ( rt_thread_t ) pxNewTCB );
  108. }
  109. return xReturn;
  110. }
  111. #endif /* SUPPORT_STATIC_ALLOCATION */
  112. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  113. BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
  114. const char * const pcName,
  115. const configSTACK_DEPTH_TYPE usStackDepth,
  116. void * const pvParameters,
  117. UBaseType_t uxPriority,
  118. TaskHandle_t * const pxCreatedTask )
  119. {
  120. TCB_t * pxNewTCB;
  121. BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  122. void * stack_start = RT_NULL;
  123. pxNewTCB = ( TCB_t * ) RT_KERNEL_MALLOC( sizeof( TCB_t ) );
  124. if ( pxNewTCB != NULL )
  125. {
  126. stack_start = RT_KERNEL_MALLOC( usStackDepth * sizeof( StackType_t ) );
  127. if ( stack_start != RT_NULL )
  128. {
  129. prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, ( StackType_t * ) stack_start );
  130. xReturn = pdPASS;
  131. /* Mark as dynamic */
  132. #if RT_VER_NUM < 0x50000
  133. ( ( struct rt_thread * ) pxNewTCB )-> type &= ~RT_Object_Class_Static;
  134. #else
  135. ( ( struct rt_thread * ) pxNewTCB )-> parent.type &= ~RT_Object_Class_Static;
  136. #endif /* RT_VER_NUM < 0x50000 */
  137. rt_thread_startup( ( rt_thread_t ) pxNewTCB );
  138. }
  139. else
  140. {
  141. RT_KERNEL_FREE( pxNewTCB );
  142. }
  143. }
  144. return xReturn;
  145. }
  146. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  147. /*-----------------------------------------------------------*/
  148. #ifdef ESP_PLATFORM
  149. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  150. BaseType_t xTaskCreatePinnedToCore( TaskFunction_t pvTaskCode,
  151. const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  152. const uint32_t usStackDepth,
  153. void * const pvParameters,
  154. UBaseType_t uxPriority,
  155. TaskHandle_t * const pvCreatedTask,
  156. const BaseType_t xCoreID)
  157. {
  158. ( void ) xCoreID;
  159. return xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pvCreatedTask );
  160. }
  161. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  162. #endif
  163. /*-----------------------------------------------------------*/
  164. static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
  165. const char * const pcName,
  166. const uint32_t ulStackDepth,
  167. void * const pvParameters,
  168. UBaseType_t uxPriority,
  169. TaskHandle_t * const pxCreatedTask,
  170. TCB_t * pxNewTCB,
  171. StackType_t * const puxStackBuffer )
  172. {
  173. /* This is used as an array index so must ensure it's not too large. */
  174. configASSERT( uxPriority < configMAX_PRIORITIES );
  175. if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  176. {
  177. uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  178. }
  179. rt_thread_init( ( struct rt_thread * ) pxNewTCB, pcName, pxTaskCode, pvParameters,
  180. puxStackBuffer, ulStackDepth * sizeof( StackType_t ), FREERTOS_PRIORITY_TO_RTTHREAD( uxPriority ), 1 );
  181. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  182. pxNewTCB->pxTaskTag = NULL;
  183. #endif
  184. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  185. rt_memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
  186. rt_memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
  187. #endif
  188. #if ( INCLUDE_xTaskAbortDelay == 1 )
  189. pxNewTCB->ucDelayAborted = pdFALSE;
  190. #endif
  191. if ( pxCreatedTask != NULL )
  192. {
  193. *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
  194. }
  195. }
  196. /*-----------------------------------------------------------*/
  197. #if ( INCLUDE_vTaskDelete == 1 )
  198. void vTaskDelete( TaskHandle_t xTaskToDelete )
  199. {
  200. rt_thread_t thread = ( rt_thread_t ) prvGetTCBFromHandle( xTaskToDelete );
  201. if ( thread == RT_NULL )
  202. {
  203. thread = rt_thread_self();
  204. }
  205. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  206. if ( rt_object_is_systemobject( ( rt_object_t ) thread ) )
  207. #endif
  208. {
  209. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  210. rt_thread_detach( thread );
  211. #endif
  212. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  213. }
  214. else
  215. {
  216. #endif
  217. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  218. rt_thread_delete( thread );
  219. #endif
  220. }
  221. if ( thread == rt_thread_self() )
  222. {
  223. rt_schedule();
  224. }
  225. }
  226. #endif /* INCLUDE_vTaskDelete */
  227. /*-----------------------------------------------------------*/
  228. #if ( INCLUDE_xTaskDelayUntil == 1 )
  229. BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
  230. const TickType_t xTimeIncrement )
  231. {
  232. BaseType_t xShouldDelay = pdFALSE;
  233. rt_base_t level;
  234. rt_tick_t cur_tick;
  235. RT_ASSERT( pxPreviousWakeTime != RT_NULL );
  236. RT_ASSERT( xTimeIncrement > 0U );
  237. level = rt_hw_interrupt_disable();
  238. cur_tick = rt_tick_get();
  239. if (cur_tick - *pxPreviousWakeTime < xTimeIncrement)
  240. {
  241. rt_thread_delay_until( pxPreviousWakeTime, xTimeIncrement );
  242. xShouldDelay = pdTRUE;
  243. }
  244. rt_hw_interrupt_enable( level );
  245. return xShouldDelay;
  246. }
  247. #endif /* INCLUDE_xTaskDelayUntil */
  248. /*-----------------------------------------------------------*/
  249. #if ( INCLUDE_vTaskDelay == 1 )
  250. void vTaskDelay( const TickType_t xTicksToDelay )
  251. {
  252. rt_thread_delay( xTicksToDelay );
  253. }
  254. #endif /* INCLUDE_vTaskDelay */
  255. /*-----------------------------------------------------------*/
  256. #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
  257. eTaskState eTaskGetState( TaskHandle_t xTask )
  258. {
  259. eTaskState eReturn;
  260. rt_thread_t thread = ( rt_thread_t ) xTask;
  261. rt_base_t level;
  262. configASSERT( xTask );
  263. level = rt_hw_interrupt_disable();
  264. #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 2, 0))
  265. switch ( thread->stat & RT_THREAD_STAT_MASK )
  266. #else
  267. switch ( RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK )
  268. #endif
  269. {
  270. case RT_THREAD_READY:
  271. {
  272. eReturn = eReady;
  273. break;
  274. }
  275. case RT_THREAD_SUSPEND:
  276. {
  277. /* If thread timer is activated it is blocked with a timeout */
  278. if ( thread->thread_timer.parent.flag & RT_TIMER_FLAG_ACTIVATED )
  279. {
  280. eReturn = eBlocked;
  281. }
  282. /* Otherwise it is suspended or blocked with an infinite timeout */
  283. else
  284. {
  285. eReturn = eSuspended;
  286. }
  287. break;
  288. }
  289. case RT_THREAD_RUNNING:
  290. {
  291. eReturn = eRunning;
  292. break;
  293. }
  294. case RT_THREAD_CLOSE:
  295. {
  296. eReturn = eDeleted;
  297. break;
  298. }
  299. default:
  300. eReturn = eInvalid;
  301. }
  302. rt_hw_interrupt_enable( level );
  303. return eReturn;
  304. }
  305. #endif /* INCLUDE_eTaskGetState */
  306. /*-----------------------------------------------------------*/
  307. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  308. UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
  309. {
  310. UBaseType_t uxReturn;
  311. rt_thread_t thread = ( rt_thread_t ) prvGetTCBFromHandle( xTask );
  312. rt_base_t level;
  313. level = rt_hw_interrupt_disable();
  314. #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 2, 0))
  315. uxReturn = thread->current_priority;
  316. #else
  317. uxReturn = RT_SCHED_PRIV(thread).current_priority;
  318. #endif
  319. rt_hw_interrupt_enable( level );
  320. return RTTHREAD_PRIORITY_TO_FREERTOS( uxReturn );
  321. }
  322. #endif /* INCLUDE_uxTaskPriorityGet */
  323. /*-----------------------------------------------------------*/
  324. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  325. UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
  326. {
  327. return uxTaskPriorityGet( xTask );
  328. }
  329. #endif /* INCLUDE_uxTaskPriorityGet */
  330. /*-----------------------------------------------------------*/
  331. #if ( INCLUDE_vTaskPrioritySet == 1 )
  332. void vTaskPrioritySet( TaskHandle_t xTask,
  333. UBaseType_t uxNewPriority )
  334. {
  335. extern rt_thread_t rt_current_thread;
  336. rt_thread_t thread;
  337. rt_uint8_t current_priority;
  338. rt_bool_t need_schedule = RT_FALSE;
  339. rt_base_t level;
  340. configASSERT( uxNewPriority < configMAX_PRIORITIES );
  341. /* Ensure the new priority is valid. */
  342. if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  343. {
  344. uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  345. }
  346. uxNewPriority = FREERTOS_PRIORITY_TO_RTTHREAD( uxNewPriority );
  347. level = rt_hw_interrupt_disable();
  348. thread = ( rt_thread_t ) prvGetTCBFromHandle( xTask );
  349. #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 2, 0))
  350. current_priority = thread->current_priority;
  351. #else
  352. current_priority = RT_SCHED_PRIV(thread).current_priority;
  353. #endif
  354. if ( current_priority != uxNewPriority )
  355. {
  356. rt_thread_control( thread, RT_THREAD_CTRL_CHANGE_PRIORITY, &uxNewPriority);
  357. if ( uxNewPriority < current_priority )
  358. {
  359. /* The priority of a task other than the currently running task is being raised.
  360. * Need to schedule if the priority is raised above that of the running task */
  361. #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 2, 0))
  362. if ( thread != rt_current_thread && uxNewPriority <= rt_current_thread->current_priority )
  363. #else
  364. if ( thread != rt_current_thread && uxNewPriority <= RT_SCHED_PRIV(rt_current_thread).current_priority )
  365. #endif
  366. {
  367. need_schedule = RT_TRUE;
  368. }
  369. }
  370. /* Setting the priority of the running task down means
  371. * there may now be another task of higher priority that
  372. * is ready to execute. */
  373. else if ( thread == rt_current_thread )
  374. {
  375. need_schedule = RT_TRUE;
  376. }
  377. }
  378. rt_hw_interrupt_enable( level );
  379. if ( need_schedule == RT_TRUE )
  380. {
  381. rt_schedule();
  382. }
  383. }
  384. #endif /* INCLUDE_vTaskPrioritySet */
  385. /*-----------------------------------------------------------*/
  386. #if ( INCLUDE_vTaskSuspend == 1 )
  387. void vTaskSuspend( TaskHandle_t xTaskToSuspend )
  388. {
  389. rt_thread_t thread = ( rt_thread_t ) prvGetTCBFromHandle( xTaskToSuspend );
  390. if ( rt_thread_suspend( thread ) == RT_EOK )
  391. {
  392. rt_schedule();
  393. }
  394. }
  395. #endif /* INCLUDE_vTaskSuspend */
  396. /*-----------------------------------------------------------*/
  397. #if ( INCLUDE_vTaskSuspend == 1 )
  398. void vTaskResume( TaskHandle_t xTaskToResume )
  399. {
  400. rt_thread_t thread = ( rt_thread_t ) xTaskToResume;
  401. rt_bool_t need_schedule = RT_FALSE;
  402. rt_base_t level;
  403. /* It does not make sense to resume the calling task. */
  404. configASSERT( xTaskToResume );
  405. if ( thread != NULL && thread != rt_thread_self() )
  406. {
  407. level = rt_hw_interrupt_disable();
  408. /* A task with higher priority than the current running task is ready */
  409. #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 2, 0))
  410. if ( rt_thread_resume( thread ) == RT_EOK && thread->current_priority <= rt_thread_self()->current_priority )
  411. #else
  412. if ( rt_thread_resume( thread ) == RT_EOK && RT_SCHED_PRIV(thread).current_priority <= RT_SCHED_PRIV(rt_thread_self()).current_priority )
  413. #endif
  414. {
  415. need_schedule = RT_TRUE;
  416. }
  417. rt_hw_interrupt_enable( level );
  418. }
  419. if (need_schedule == RT_TRUE)
  420. {
  421. rt_schedule();
  422. }
  423. }
  424. #endif /* INCLUDE_vTaskSuspend */
  425. /*-----------------------------------------------------------*/
  426. #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
  427. BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
  428. {
  429. vTaskResume( xTaskToResume );
  430. return pdFALSE;
  431. }
  432. #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
  433. /*-----------------------------------------------------------*/
  434. void vTaskStartScheduler( void )
  435. {
  436. xSchedulerRunning = pdTRUE;
  437. #ifdef ESP_PLATFORM
  438. extern int rtthread_startup(void);
  439. rtthread_startup();
  440. #endif
  441. }
  442. /*-----------------------------------------------------------*/
  443. void vTaskEndScheduler( void )
  444. {
  445. xSchedulerRunning = pdFALSE;
  446. vPortEndScheduler();
  447. }
  448. /*----------------------------------------------------------*/
  449. void vTaskSuspendAll( void )
  450. {
  451. rt_enter_critical();
  452. }
  453. /*----------------------------------------------------------*/
  454. BaseType_t xTaskResumeAll( void )
  455. {
  456. rt_exit_critical();
  457. return pdFALSE;
  458. }
  459. /*-----------------------------------------------------------*/
  460. TickType_t xTaskGetTickCount( void )
  461. {
  462. return rt_tick_get();
  463. }
  464. /*-----------------------------------------------------------*/
  465. TickType_t xTaskGetTickCountFromISR( void )
  466. {
  467. return rt_tick_get();
  468. }
  469. /*-----------------------------------------------------------*/
  470. UBaseType_t uxTaskGetNumberOfTasks( void )
  471. {
  472. UBaseType_t uxReturn = 0;
  473. rt_base_t level;
  474. struct rt_object_information *information;
  475. struct rt_list_node *node = RT_NULL;
  476. information = rt_object_get_information( RT_Object_Class_Thread );
  477. RT_ASSERT( information != RT_NULL );
  478. level = rt_hw_interrupt_disable();
  479. rt_list_for_each( node, &( information->object_list ) )
  480. {
  481. uxReturn += 1;
  482. }
  483. rt_hw_interrupt_enable( level );
  484. return uxReturn;
  485. }
  486. /*-----------------------------------------------------------*/
  487. char * pcTaskGetName( TaskHandle_t xTaskToQuery )
  488. {
  489. rt_thread_t thread = ( rt_thread_t ) prvGetTCBFromHandle( xTaskToQuery );
  490. #if RT_VER_NUM < 0x50000
  491. return &( thread->name[ 0 ] );
  492. #else
  493. return &( thread->parent.name[ 0 ] );
  494. #endif /* RT_VER_NUM < 0x50000 */
  495. }
  496. /*-----------------------------------------------------------*/
  497. #if ( INCLUDE_xTaskGetHandle == 1 )
  498. TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
  499. {
  500. return ( TaskHandle_t ) rt_thread_find( ( char * ) pcNameToQuery );
  501. }
  502. #endif /* INCLUDE_xTaskGetHandle */
  503. /*-----------------------------------------------------------*/
  504. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  505. TaskHandle_t xTaskGetIdleTaskHandle( void )
  506. {
  507. return ( TaskHandle_t ) rt_thread_find( "tidle0" );
  508. }
  509. #endif /* INCLUDE_xTaskGetIdleTaskHandle */
  510. /*----------------------------------------------------------*/
  511. #if ( INCLUDE_xTaskAbortDelay == 1 )
  512. BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
  513. {
  514. TCB_t * pxTCB = xTask;
  515. BaseType_t xReturn;
  516. rt_thread_t thread = ( rt_thread_t ) xTask;
  517. rt_bool_t need_schedule = RT_FALSE;
  518. rt_base_t level;
  519. configASSERT( pxTCB );
  520. level = rt_hw_interrupt_disable();
  521. if ( eTaskGetState( xTask ) == eBlocked )
  522. {
  523. rt_thread_resume( thread );
  524. thread->error = -RT_ETIMEOUT;
  525. pxTCB->ucDelayAborted = pdTRUE;
  526. #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 2, 0))
  527. if ( thread->current_priority < rt_thread_self()->current_priority )
  528. #else
  529. if ( RT_SCHED_PRIV(thread).current_priority < RT_SCHED_PRIV(rt_thread_self()).current_priority )
  530. #endif
  531. {
  532. need_schedule = RT_TRUE;
  533. }
  534. xReturn = pdPASS;
  535. }
  536. else
  537. {
  538. xReturn = pdFAIL;
  539. }
  540. rt_hw_interrupt_enable( level );
  541. if ( need_schedule == RT_TRUE )
  542. {
  543. rt_schedule();
  544. }
  545. return xReturn;
  546. }
  547. #endif /* INCLUDE_xTaskAbortDelay */
  548. /*----------------------------------------------------------*/
  549. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  550. void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
  551. TaskHookFunction_t pxHookFunction )
  552. {
  553. TCB_t * xTCB = prvGetTCBFromHandle( xTask );
  554. rt_base_t level;
  555. level = rt_hw_interrupt_disable();
  556. xTCB->pxTaskTag = pxHookFunction;
  557. rt_hw_interrupt_enable( level );
  558. }
  559. #endif /* configUSE_APPLICATION_TASK_TAG */
  560. /*-----------------------------------------------------------*/
  561. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  562. TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
  563. {
  564. TaskHookFunction_t xReturn;
  565. TCB_t * xTCB = prvGetTCBFromHandle( xTask );
  566. rt_base_t level;
  567. level = rt_hw_interrupt_disable();
  568. xReturn = xTCB->pxTaskTag;
  569. rt_hw_interrupt_enable( level );
  570. return xReturn;
  571. }
  572. #endif /* configUSE_APPLICATION_TASK_TAG */
  573. /*-----------------------------------------------------------*/
  574. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  575. TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
  576. {
  577. return xTaskGetApplicationTaskTag( xTask );
  578. }
  579. #endif /* configUSE_APPLICATION_TASK_TAG */
  580. /*-----------------------------------------------------------*/
  581. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  582. BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
  583. void * pvParameter )
  584. {
  585. BaseType_t xReturn;
  586. TCB_t * xTCB = prvGetTCBFromHandle( xTask );
  587. if( xTCB->pxTaskTag != NULL )
  588. {
  589. xReturn = xTCB->pxTaskTag( pvParameter );
  590. }
  591. else
  592. {
  593. xReturn = pdFAIL;
  594. }
  595. return xReturn;
  596. }
  597. #endif /* configUSE_APPLICATION_TASK_TAG */
  598. /*-----------------------------------------------------------*/
  599. void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
  600. {
  601. rt_base_t level;
  602. configASSERT( pxTimeOut );
  603. level = rt_hw_interrupt_disable();
  604. pxTimeOut->xOverflowCount = 0;
  605. pxTimeOut->xTimeOnEntering = ( TickType_t ) rt_tick_get();
  606. rt_hw_interrupt_enable( level );
  607. }
  608. /*-----------------------------------------------------------*/
  609. void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
  610. {
  611. /* For internal use only as it does not use a critical section. */
  612. pxTimeOut->xOverflowCount = 0;
  613. pxTimeOut->xTimeOnEntering = ( TickType_t ) rt_tick_get();;
  614. }
  615. /*-----------------------------------------------------------*/
  616. BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
  617. TickType_t * const pxTicksToWait )
  618. {
  619. TCB_t * pxCurrentTCB = ( TCB_t * ) rt_thread_self();
  620. BaseType_t xReturn;
  621. rt_base_t level;
  622. configASSERT( pxTimeOut );
  623. configASSERT( pxTicksToWait );
  624. level = rt_hw_interrupt_disable();
  625. /* Minor optimisation. The tick count cannot change in this block. */
  626. const TickType_t xConstTickCount = ( TickType_t ) rt_tick_get();
  627. const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
  628. #if ( INCLUDE_xTaskAbortDelay == 1 )
  629. if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
  630. {
  631. /* The delay was aborted, which is not the same as a time out,
  632. * but has the same result. */
  633. pxCurrentTCB->ucDelayAborted = pdFALSE;
  634. xReturn = pdTRUE;
  635. }
  636. else
  637. #endif
  638. #if ( INCLUDE_vTaskSuspend == 1 )
  639. if( *pxTicksToWait == portMAX_DELAY )
  640. {
  641. /* If INCLUDE_vTaskSuspend is set to 1 and the block time
  642. * specified is the maximum block time then the task should block
  643. * indefinitely, and therefore never time out. */
  644. xReturn = pdFALSE;
  645. }
  646. else
  647. #endif
  648. if( xElapsedTime < *pxTicksToWait )
  649. {
  650. /* Not a genuine timeout. Adjust parameters for time remaining. */
  651. *pxTicksToWait -= xElapsedTime;
  652. vTaskInternalSetTimeOutState( pxTimeOut );
  653. xReturn = pdFALSE;
  654. }
  655. else
  656. {
  657. *pxTicksToWait = ( TickType_t ) 0;
  658. xReturn = pdTRUE;
  659. }
  660. rt_hw_interrupt_enable( level );
  661. return xReturn;
  662. }
  663. /*-----------------------------------------------------------*/
  664. #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) )
  665. TaskHandle_t xTaskGetCurrentTaskHandle( void )
  666. {
  667. TaskHandle_t xReturn;
  668. /* A critical section is not required as this is not called from
  669. * an interrupt and the current TCB will always be the same for any
  670. * individual execution thread. */
  671. xReturn = ( TaskHandle_t ) rt_thread_self();
  672. return xReturn;
  673. }
  674. #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
  675. /*-----------------------------------------------------------*/
  676. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) )
  677. BaseType_t xTaskGetSchedulerState( void )
  678. {
  679. BaseType_t xReturn;
  680. if( xSchedulerRunning == pdFALSE )
  681. {
  682. xReturn = taskSCHEDULER_NOT_STARTED;
  683. }
  684. else
  685. {
  686. if( rt_critical_level() == 0 )
  687. {
  688. xReturn = taskSCHEDULER_RUNNING;
  689. }
  690. else
  691. {
  692. xReturn = taskSCHEDULER_SUSPENDED;
  693. }
  694. }
  695. return xReturn;
  696. }
  697. #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) ) */
  698. /*-----------------------------------------------------------*/
  699. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  700. uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
  701. BaseType_t xClearCountOnExit,
  702. TickType_t xTicksToWait )
  703. {
  704. uint32_t ulReturn;
  705. TCB_t * pxCurrentTCB = ( TCB_t * ) rt_thread_self();
  706. rt_thread_t thread = ( rt_thread_t ) pxCurrentTCB;
  707. rt_base_t level;
  708. configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  709. level = rt_hw_interrupt_disable();
  710. /* Only block if the notification count is not already non-zero. */
  711. if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
  712. {
  713. /* Mark this task as waiting for a notification. */
  714. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
  715. if( xTicksToWait > ( TickType_t ) 0 )
  716. {
  717. rt_thread_suspend( thread );
  718. if ( ( rt_int32_t ) xTicksToWait > 0 )
  719. {
  720. rt_timer_control(&(thread->thread_timer),
  721. RT_TIMER_CTRL_SET_TIME,
  722. &xTicksToWait);
  723. rt_timer_start(&(thread->thread_timer));
  724. }
  725. rt_hw_interrupt_enable(level);
  726. rt_schedule();
  727. /* Clear thread error. */
  728. thread->error = RT_EOK;
  729. }
  730. }
  731. rt_hw_interrupt_enable( level );
  732. level = rt_hw_interrupt_disable();
  733. ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
  734. if( ulReturn != 0UL )
  735. {
  736. if( xClearCountOnExit != pdFALSE )
  737. {
  738. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
  739. }
  740. else
  741. {
  742. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
  743. }
  744. }
  745. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
  746. rt_hw_interrupt_enable( level );
  747. return ulReturn;
  748. }
  749. #endif /* configUSE_TASK_NOTIFICATIONS */
  750. /*-----------------------------------------------------------*/
  751. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  752. BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
  753. uint32_t ulBitsToClearOnEntry,
  754. uint32_t ulBitsToClearOnExit,
  755. uint32_t * pulNotificationValue,
  756. TickType_t xTicksToWait )
  757. {
  758. BaseType_t xReturn;
  759. TCB_t * pxCurrentTCB = ( TCB_t * ) rt_thread_self();
  760. rt_thread_t thread = ( rt_thread_t ) pxCurrentTCB;
  761. rt_base_t level;
  762. configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  763. level = rt_hw_interrupt_disable();
  764. /* Only block if a notification is not already pending. */
  765. if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
  766. {
  767. /* Clear bits in the task's notification value as bits may get
  768. * set by the notifying task or interrupt. This can be used to
  769. * clear the value to zero. */
  770. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
  771. /* Mark this task as waiting for a notification. */
  772. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
  773. if( xTicksToWait > ( TickType_t ) 0 )
  774. {
  775. rt_thread_suspend( thread );
  776. if ( ( rt_int32_t ) xTicksToWait > 0 )
  777. {
  778. rt_timer_control(&(thread->thread_timer),
  779. RT_TIMER_CTRL_SET_TIME,
  780. &xTicksToWait);
  781. rt_timer_start(&(thread->thread_timer));
  782. }
  783. rt_hw_interrupt_enable(level);
  784. rt_schedule();
  785. /* Clear thread error. It is not used to determine the function return value. */
  786. thread->error = RT_EOK;
  787. }
  788. else
  789. {
  790. rt_hw_interrupt_enable( level );
  791. }
  792. }
  793. else
  794. {
  795. rt_hw_interrupt_enable( level );
  796. }
  797. level = rt_hw_interrupt_disable();
  798. if( pulNotificationValue != NULL )
  799. {
  800. /* Output the current notification value, which may or may not
  801. * have changed. */
  802. *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
  803. }
  804. /* If ucNotifyValue is set then either the task never entered the
  805. * blocked state (because a notification was already pending) or the
  806. * task unblocked because of a notification. Otherwise the task
  807. * unblocked because of a timeout. */
  808. if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
  809. {
  810. /* A notification was not received. */
  811. xReturn = pdFALSE;
  812. }
  813. else
  814. {
  815. /* A notification was already pending or a notification was
  816. * received while the task was waiting. */
  817. pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
  818. xReturn = pdTRUE;
  819. }
  820. pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
  821. rt_hw_interrupt_enable( level );
  822. return xReturn;
  823. }
  824. #endif /* configUSE_TASK_NOTIFICATIONS */
  825. /*-----------------------------------------------------------*/
  826. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  827. BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
  828. UBaseType_t uxIndexToNotify,
  829. uint32_t ulValue,
  830. eNotifyAction eAction,
  831. uint32_t * pulPreviousNotificationValue )
  832. {
  833. TCB_t * pxTCB;
  834. BaseType_t xReturn = pdPASS;
  835. uint8_t ucOriginalNotifyState;
  836. rt_base_t level;
  837. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  838. configASSERT( xTaskToNotify );
  839. pxTCB = xTaskToNotify;
  840. level = rt_hw_interrupt_disable();
  841. if( pulPreviousNotificationValue != NULL )
  842. {
  843. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
  844. }
  845. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  846. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  847. switch( eAction )
  848. {
  849. case eSetBits:
  850. pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
  851. break;
  852. case eIncrement:
  853. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  854. break;
  855. case eSetValueWithOverwrite:
  856. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  857. break;
  858. case eSetValueWithoutOverwrite:
  859. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  860. {
  861. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  862. }
  863. else
  864. {
  865. /* The value could not be written to the task. */
  866. xReturn = pdFAIL;
  867. }
  868. break;
  869. case eNoAction:
  870. /* The task is being notified without its notify value being
  871. * updated. */
  872. break;
  873. default:
  874. /* Should not get here if all enums are handled.
  875. * Artificially force an assert by testing a value the
  876. * compiler can't assume is const. */
  877. configASSERT( xTaskToNotify == NULL );
  878. break;
  879. }
  880. /* If the task is in the blocked state specifically to wait for a
  881. * notification then unblock it now. */
  882. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  883. {
  884. rt_thread_resume( ( rt_thread_t ) pxTCB );
  885. #if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION < RT_VERSION_CHECK(5, 2, 0))
  886. if( ( ( rt_thread_t ) pxTCB )->current_priority < rt_thread_self()->current_priority )
  887. #else
  888. if( RT_SCHED_PRIV( ( rt_thread_t ) pxTCB ).current_priority < RT_SCHED_PRIV( rt_thread_self() ).current_priority )
  889. #endif
  890. {
  891. /* The notified task has a priority above the currently
  892. * executing task so a schedule is required. */
  893. rt_schedule();
  894. }
  895. }
  896. rt_hw_interrupt_enable( level );
  897. return xReturn;
  898. }
  899. #endif /* configUSE_TASK_NOTIFICATIONS */
  900. /*-----------------------------------------------------------*/
  901. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  902. BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
  903. UBaseType_t uxIndexToNotify,
  904. uint32_t ulValue,
  905. eNotifyAction eAction,
  906. uint32_t * pulPreviousNotificationValue,
  907. BaseType_t * pxHigherPriorityTaskWoken )
  908. {
  909. BaseType_t xReturn;
  910. xReturn = xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
  911. if ( pxHigherPriorityTaskWoken != NULL )
  912. {
  913. *pxHigherPriorityTaskWoken = pdFALSE;
  914. }
  915. return xReturn;
  916. }
  917. #endif /* configUSE_TASK_NOTIFICATIONS */
  918. /*-----------------------------------------------------------*/
  919. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  920. void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
  921. UBaseType_t uxIndexToNotify,
  922. BaseType_t * pxHigherPriorityTaskWoken )
  923. {
  924. xTaskNotifyGiveIndexed( xTaskToNotify, uxIndexToNotify );
  925. if ( pxHigherPriorityTaskWoken != NULL )
  926. {
  927. *pxHigherPriorityTaskWoken = pdFALSE;
  928. }
  929. }
  930. #endif /* configUSE_TASK_NOTIFICATIONS */
  931. /*-----------------------------------------------------------*/
  932. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  933. BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
  934. UBaseType_t uxIndexToClear )
  935. {
  936. TCB_t * pxTCB;
  937. BaseType_t xReturn;
  938. rt_base_t level;
  939. configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  940. /* If null is passed in here then it is the calling task that is having
  941. * its notification state cleared. */
  942. pxTCB = prvGetTCBFromHandle( xTask );
  943. level = rt_hw_interrupt_disable();
  944. if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
  945. {
  946. pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
  947. xReturn = pdPASS;
  948. }
  949. else
  950. {
  951. xReturn = pdFAIL;
  952. }
  953. rt_hw_interrupt_enable( level );
  954. return xReturn;
  955. }
  956. #endif /* configUSE_TASK_NOTIFICATIONS */
  957. /*-----------------------------------------------------------*/
  958. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  959. uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
  960. UBaseType_t uxIndexToClear,
  961. uint32_t ulBitsToClear )
  962. {
  963. TCB_t * pxTCB;
  964. uint32_t ulReturn;
  965. rt_base_t level;
  966. /* If null is passed in here then it is the calling task that is having
  967. * its notification state cleared. */
  968. pxTCB = prvGetTCBFromHandle( xTask );
  969. level = rt_hw_interrupt_disable();
  970. /* Return the notification as it was before the bits were cleared,
  971. * then clear the bit mask. */
  972. ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
  973. pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
  974. rt_hw_interrupt_enable( level );
  975. return ulReturn;
  976. }
  977. #endif /* configUSE_TASK_NOTIFICATIONS */
  978. /*-----------------------------------------------------------*/
  979. #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
  980. /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
  981. * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
  982. * user to determine the return type. It gets around the problem of the value
  983. * overflowing on 8-bit types without breaking backward compatibility for
  984. * applications that expect an 8-bit return type. */
  985. configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
  986. {
  987. uint32_t ulCount = 0U;
  988. rt_thread_t thread = ( rt_thread_t ) prvGetTCBFromHandle( xTask );
  989. rt_uint8_t * stack_addr = thread->stack_addr;
  990. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  991. stack_addr = stack_addr + thread->stack_size - 1;
  992. while ( *stack_addr == '#' )
  993. {
  994. ulCount += 1;
  995. stack_addr -= 1;
  996. }
  997. #else
  998. while ( *stack_addr == '#' )
  999. {
  1000. ulCount += 1;
  1001. stack_addr += 1;
  1002. }
  1003. #endif
  1004. ulCount /= ( uint32_t ) sizeof( StackType_t );
  1005. return ( configSTACK_DEPTH_TYPE ) ulCount;
  1006. }
  1007. #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
  1008. /*-----------------------------------------------------------*/
  1009. #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
  1010. UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
  1011. {
  1012. return ( UBaseType_t ) uxTaskGetStackHighWaterMark2( xTask );
  1013. }
  1014. #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
  1015. /*-----------------------------------------------------------*/
  1016. #ifdef ESP_PLATFORM
  1017. BaseType_t xTaskGetAffinity( TaskHandle_t xTask )
  1018. {
  1019. ( void ) xTask;
  1020. return 0;
  1021. }
  1022. TaskHandle_t xTaskGetCurrentTaskHandleForCPU( BaseType_t cpuid )
  1023. {
  1024. ( void ) cpuid;
  1025. return xTaskGetCurrentTaskHandle();
  1026. }
  1027. TaskHandle_t xTaskGetIdleTaskHandleForCPU( UBaseType_t cpuid )
  1028. {
  1029. ( void ) cpuid;
  1030. return xTaskGetIdleTaskHandle();
  1031. }
  1032. #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
  1033. #include "freertos_tasks_c_additions.h"
  1034. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  1035. static void freertos_tasks_c_additions_init( void )
  1036. {
  1037. FREERTOS_TASKS_C_ADDITIONS_INIT();
  1038. }
  1039. #endif
  1040. #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
  1041. /* Unimplemented */
  1042. #include "esp_log.h"
  1043. static const char *TAG = "freertos";
  1044. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
  1045. void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
  1046. BaseType_t xIndex,
  1047. void * pvValue )
  1048. {
  1049. ESP_LOGE(TAG, "vTaskSetThreadLocalStoragePointer unimplemented");
  1050. configASSERT(0);
  1051. }
  1052. void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
  1053. BaseType_t xIndex )
  1054. {
  1055. ESP_LOGE(TAG, "pvTaskGetThreadLocalStoragePointer unimplemented");
  1056. configASSERT(0);
  1057. return NULL;
  1058. }
  1059. #if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
  1060. typedef void (*TlsDeleteCallbackFunction_t)( int, void * );
  1061. void vTaskSetThreadLocalStoragePointerAndDelCallback( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue, TlsDeleteCallbackFunction_t pvDelCallback)
  1062. {
  1063. ESP_LOGE(TAG, "vTaskSetThreadLocalStoragePointerAndDelCallback unimplemented");
  1064. configASSERT(0);
  1065. }
  1066. #endif
  1067. #endif
  1068. #endif