tasks.c 38 KB

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