uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
task.h
Go to the documentation of this file.
1 /*
2  FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
3  All rights reserved
4 
5  VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6 
7  ***************************************************************************
8  * *
9  * FreeRTOS provides completely free yet professionally developed, *
10  * robust, strictly quality controlled, supported, and cross *
11  * platform software that has become a de facto standard. *
12  * *
13  * Help yourself get started quickly and support the FreeRTOS *
14  * project by purchasing a FreeRTOS tutorial book, reference *
15  * manual, or both from: http://www.FreeRTOS.org/Documentation *
16  * *
17  * Thank you! *
18  * *
19  ***************************************************************************
20 
21  This file is part of the FreeRTOS distribution.
22 
23  FreeRTOS is free software; you can redistribute it and/or modify it under
24  the terms of the GNU General Public License (version 2) as published by the
25  Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
26 
27  >>! NOTE: The modification to the GPL is included to allow you to distribute
28  >>! a combined work that includes FreeRTOS without being obliged to provide
29  >>! the source code for proprietary components outside of the FreeRTOS
30  >>! kernel.
31 
32  FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
33  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34  FOR A PARTICULAR PURPOSE. Full license text is available from the following
35  link: http://www.freertos.org/a00114.html
36 
37  1 tab == 4 spaces!
38 
39  ***************************************************************************
40  * *
41  * Having a problem? Start by reading the FAQ "My application does *
42  * not run, what could be wrong?" *
43  * *
44  * http://www.FreeRTOS.org/FAQHelp.html *
45  * *
46  ***************************************************************************
47 
48  http://www.FreeRTOS.org - Documentation, books, training, latest versions,
49  license and Real Time Engineers Ltd. contact details.
50 
51  http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
52  including FreeRTOS+Trace - an indispensable productivity tool, a DOS
53  compatible FAT file system, and our tiny thread aware UDP/IP stack.
54 
55  http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
56  Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
57  licenses offer ticketed support, indemnification and middleware.
58 
59  http://www.SafeRTOS.com - High Integrity Systems also provide a safety
60  engineered and independently SIL3 certified version for use in safety and
61  mission critical applications that require provable dependability.
62 
63  1 tab == 4 spaces!
64 */
65 
66 
67 #ifndef INC_TASK_H
68 #define INC_TASK_H
69 
70 #ifndef INC_FREERTOS_H
71  #error "include FreeRTOS.h must appear in source files before include task.h"
72 #endif
73 
74 #include "list.h"
75 
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 
80 /*-----------------------------------------------------------
81  * MACROS AND DEFINITIONS
82  *----------------------------------------------------------*/
83 
84 #define tskKERNEL_VERSION_NUMBER "V7.5.3"
85 
96 typedef void * xTaskHandle;
97 
98 /* Task states returned by eTaskGetState. */
99 typedef enum
100 {
101  eRunning = 0, /* A task is querying the state of itself, so must be running. */
102  eReady, /* The task being queried is in a read or pending ready list. */
103  eBlocked, /* The task being queried is in the Blocked state. */
104  eSuspended, /* The task being queried is in the Suspended state, or is in the Blocked state with an infinite time out. */
105  eDeleted /* The task being queried has been deleted, but its TCB has not yet been freed. */
106 } eTaskState;
107 
108 /*
109  * Used internally only.
110  */
111 typedef struct xTIME_OUT
112 {
115 } xTimeOutType;
116 
117 /*
118  * Defines the memory ranges allocated to the task when an MPU is used.
119  */
120 typedef struct xMEMORY_REGION
121 {
123  unsigned long ulLengthInBytes;
124  unsigned long ulParameters;
125 } xMemoryRegion;
126 
127 /*
128  * Parameters required to create an MPU protected task.
129  */
130 typedef struct xTASK_PARAMTERS
131 {
133  const signed char * const pcName;
134  unsigned short usStackDepth;
140 
141 /* Used with the uxTaskGetSystemState() function to return the state of each task
142 in the system. */
143 typedef struct xTASK_STATUS
144 {
145  xTaskHandle xHandle; /* The handle of the task to which the rest of the information in the structure relates. */
146  const signed char *pcTaskName; /* A pointer to the task's name. This value will be invalid if the task was deleted since the structure was populated! */
147  unsigned portBASE_TYPE xTaskNumber; /* A number unique to the task. */
148  eTaskState eCurrentState; /* The state in which the task existed when the structure was populated. */
149  unsigned portBASE_TYPE uxCurrentPriority; /* The priority at which the task was running (may be inherited) when the structure was populated. */
150  unsigned portBASE_TYPE uxBasePriority; /* The priority to which the task will return if the task's current priority has been inherited to avoid unbounded priority inversion when obtaining a mutex. Only valid if configUSE_MUTEXES is defined as 1 in FreeRTOSConfig.h. */
151  unsigned long ulRunTimeCounter; /* The total run time allocated to the task so far, as defined by the run time stats clock. See http://www.freertos.org/rtos-run-time-stats.html. Only valid when configGENERATE_RUN_TIME_STATS is defined as 1 in FreeRTOSConfig.h. */
152  unsigned short usStackHighWaterMark; /* The minimum amount of stack space that has remained for the task since the task was created. The closer this value is to zero the closer the task has come to overflowing its stack. */
154 
155 /* Possible return values for eTaskConfirmSleepModeStatus(). */
156 typedef enum
157 {
158  eAbortSleep = 0, /* A task has been made ready or a context switch pended since portSUPPORESS_TICKS_AND_SLEEP() was called - abort entering a sleep mode. */
159  eStandardSleep, /* Enter a sleep mode that will not last any longer than the expected idle time. */
160  eNoTasksWaitingTimeout /* No tasks are waiting for a timeout so it is safe to enter a sleep mode that can only be exited by an external interrupt. */
162 
163 
164 /*
165  * Defines the priority used by the idle task. This must not be modified.
166  *
167  * \ingroup TaskUtils
168  */
169 #define tskIDLE_PRIORITY ( ( unsigned portBASE_TYPE ) 0U )
170 
179 #define taskYIELD() portYIELD()
180 
193 #define taskENTER_CRITICAL() portENTER_CRITICAL()
194 
207 #define taskEXIT_CRITICAL() portEXIT_CRITICAL()
208 
217 #define taskDISABLE_INTERRUPTS() portDISABLE_INTERRUPTS()
218 
227 #define taskENABLE_INTERRUPTS() portENABLE_INTERRUPTS()
228 
229 /* Definitions returned by xTaskGetSchedulerState(). */
230 #define taskSCHEDULER_NOT_STARTED ( ( portBASE_TYPE ) 0 )
231 #define taskSCHEDULER_RUNNING ( ( portBASE_TYPE ) 1 )
232 #define taskSCHEDULER_SUSPENDED ( ( portBASE_TYPE ) 2 )
233 
234 /*-----------------------------------------------------------
235  * TASK CREATION API
236  *----------------------------------------------------------*/
237 
314 #define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ) )
315 
383 #define xTaskCreateRestricted( x, pxCreatedTask ) xTaskGenericCreate( ((x)->pvTaskCode), ((x)->pcName), ((x)->usStackDepth), ((x)->pvParameters), ((x)->uxPriority), (pxCreatedTask), ((x)->puxStackBuffer), ((x)->xRegions) )
384 
431 void vTaskAllocateMPURegions( xTaskHandle xTask, const xMemoryRegion * const pxRegions ) PRIVILEGED_FUNCTION;
432 
472 void vTaskDelete( xTaskHandle xTaskToDelete ) PRIVILEGED_FUNCTION;
473 
474 /*-----------------------------------------------------------
475  * TASK CONTROL API
476  *----------------------------------------------------------*/
477 
526 void vTaskDelay( portTickType xTicksToDelay ) PRIVILEGED_FUNCTION;
527 
585 void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement ) PRIVILEGED_FUNCTION;
586 
632 unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
633 
650 eTaskState eTaskGetState( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
651 
692 void vTaskPrioritySet( xTaskHandle xTask, unsigned portBASE_TYPE uxNewPriority ) PRIVILEGED_FUNCTION;
693 
743 void vTaskSuspend( xTaskHandle xTaskToSuspend ) PRIVILEGED_FUNCTION;
744 
792 void vTaskResume( xTaskHandle xTaskToResume ) PRIVILEGED_FUNCTION;
793 
812 portBASE_TYPE xTaskResumeFromISR( xTaskHandle xTaskToResume ) PRIVILEGED_FUNCTION;
813 
814 /*-----------------------------------------------------------
815  * SCHEDULER CONTROL
816  *----------------------------------------------------------*/
817 
850 void vTaskStartScheduler( void ) PRIVILEGED_FUNCTION;
851 
903 void vTaskEndScheduler( void ) PRIVILEGED_FUNCTION;
904 
954 void vTaskSuspendAll( void ) PRIVILEGED_FUNCTION;
955 
1006 signed portBASE_TYPE xTaskResumeAll( void ) PRIVILEGED_FUNCTION;
1007 
1017 signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
1018 
1019 /*-----------------------------------------------------------
1020  * TASK UTILITIES
1021  *----------------------------------------------------------*/
1022 
1032 portTickType xTaskGetTickCount( void ) PRIVILEGED_FUNCTION;
1033 
1048 portTickType xTaskGetTickCountFromISR( void ) PRIVILEGED_FUNCTION;
1049 
1062 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void ) PRIVILEGED_FUNCTION;
1063 
1076 signed char *pcTaskGetTaskName( xTaskHandle xTaskToQuery );
1077 
1096 unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
1097 
1098 /* When using trace macros it is sometimes necessary to include tasks.h before
1099 FreeRTOS.h. When this is done pdTASK_HOOK_CODE will not yet have been defined,
1100 so the following two prototypes will cause a compilation error. This can be
1101 fixed by simply guarding against the inclusion of these two prototypes unless
1102 they are explicitly required by the configUSE_APPLICATION_TASK_TAG configuration
1103 constant. */
1104 #ifdef configUSE_APPLICATION_TASK_TAG
1105  #if configUSE_APPLICATION_TASK_TAG == 1
1106 
1114  void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction ) PRIVILEGED_FUNCTION;
1115 
1122  pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
1123  #endif /* configUSE_APPLICATION_TASK_TAG ==1 */
1124 #endif /* ifdef configUSE_APPLICATION_TASK_TAG */
1125 
1136 portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter ) PRIVILEGED_FUNCTION;
1137 
1145 xTaskHandle xTaskGetIdleTaskHandle( void );
1146 
1244 unsigned portBASE_TYPE uxTaskGetSystemState( xTaskStatusType *pxTaskStatusArray, unsigned portBASE_TYPE uxArraySize, unsigned long *pulTotalRunTime );
1245 
1291 void vTaskList( signed char *pcWriteBuffer ) PRIVILEGED_FUNCTION;
1292 
1345 void vTaskGetRunTimeStats( signed char *pcWriteBuffer ) PRIVILEGED_FUNCTION;
1346 
1347 /*-----------------------------------------------------------
1348  * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
1349  *----------------------------------------------------------*/
1350 
1351 /*
1352  * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
1353  * INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
1354  * AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1355  *
1356  * Called from the real time kernel tick (either preemptive or cooperative),
1357  * this increments the tick count and checks if any tasks that are blocked
1358  * for a finite period required removing from a blocked list and placing on
1359  * a ready list. If a non-zero value is returned then a context switch is
1360  * required because either:
1361  * + A task was removed from a blocked list because its timeout had expired,
1362  * or
1363  * + Time slicing is in use and there is a task of equal priority to the
1364  * currently running task.
1365  */
1366 portBASE_TYPE xTaskIncrementTick( void ) PRIVILEGED_FUNCTION;
1367 
1368 /*
1369  * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
1370  * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1371  *
1372  * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
1373  *
1374  * Removes the calling task from the ready list and places it both
1375  * on the list of tasks waiting for a particular event, and the
1376  * list of delayed tasks. The task will be removed from both lists
1377  * and replaced on the ready list should either the event occur (and
1378  * there be no higher priority tasks waiting on the same event) or
1379  * the delay period expires.
1380  *
1381  * @param pxEventList The list containing tasks that are blocked waiting
1382  * for the event to occur.
1383  *
1384  * @param xTicksToWait The maximum amount of time that the task should wait
1385  * for the event to occur. This is specified in kernel ticks,the constant
1386  * portTICK_RATE_MS can be used to convert kernel ticks into a real time
1387  * period.
1388  */
1389 void vTaskPlaceOnEventList( xList * const pxEventList, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
1390 
1391 /*
1392  * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
1393  * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1394  *
1395  * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
1396  *
1397  * This function performs nearly the same function as vTaskPlaceOnEventList().
1398  * The difference being that this function does not permit tasks to block
1399  * indefinitely, whereas vTaskPlaceOnEventList() does.
1400  *
1401  * @return pdTRUE if the task being removed has a higher priority than the task
1402  * making the call, otherwise pdFALSE.
1403  */
1404 void vTaskPlaceOnEventListRestricted( xList * const pxEventList, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
1405 
1406 /*
1407  * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
1408  * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1409  *
1410  * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
1411  *
1412  * Removes a task from both the specified event list and the list of blocked
1413  * tasks, and places it on a ready queue.
1414  *
1415  * xTaskRemoveFromEventList () will be called if either an event occurs to
1416  * unblock a task, or the block timeout period expires.
1417  *
1418  * @return pdTRUE if the task being removed has a higher priority than the task
1419  * making the call, otherwise pdFALSE.
1420  */
1421 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList ) PRIVILEGED_FUNCTION;
1422 
1423 /*
1424  * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
1425  * INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
1426  * AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1427  *
1428  * Sets the pointer to the current TCB to the TCB of the highest priority task
1429  * that is ready to run.
1430  */
1431 void vTaskSwitchContext( void ) PRIVILEGED_FUNCTION;
1432 
1433 /*
1434  * Return the handle of the calling task.
1435  */
1436 xTaskHandle xTaskGetCurrentTaskHandle( void ) PRIVILEGED_FUNCTION;
1437 
1438 /*
1439  * Capture the current time status for future reference.
1440  */
1441 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut ) PRIVILEGED_FUNCTION;
1442 
1443 /*
1444  * Compare the time status now with that previously captured to see if the
1445  * timeout has expired.
1446  */
1447 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait ) PRIVILEGED_FUNCTION;
1448 
1449 /*
1450  * Shortcut used by the queue implementation to prevent unnecessary call to
1451  * taskYIELD();
1452  */
1453 void vTaskMissedYield( void ) PRIVILEGED_FUNCTION;
1454 
1455 /*
1456  * Returns the scheduler state as taskSCHEDULER_RUNNING,
1457  * taskSCHEDULER_NOT_STARTED or taskSCHEDULER_SUSPENDED.
1458  */
1459 portBASE_TYPE xTaskGetSchedulerState( void ) PRIVILEGED_FUNCTION;
1460 
1461 /*
1462  * Raises the priority of the mutex holder to that of the calling task should
1463  * the mutex holder have a priority less than the calling task.
1464  */
1465 void vTaskPriorityInherit( xTaskHandle const pxMutexHolder ) PRIVILEGED_FUNCTION;
1466 
1467 /*
1468  * Set the priority of a task back to its proper priority in the case that it
1469  * inherited a higher priority while it was holding a semaphore.
1470  */
1471 void vTaskPriorityDisinherit( xTaskHandle const pxMutexHolder ) PRIVILEGED_FUNCTION;
1472 
1473 /*
1474  * Generic version of the task creation function which is in turn called by the
1475  * xTaskCreate() and xTaskCreateRestricted() macros.
1476  */
1477 signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions ) PRIVILEGED_FUNCTION;
1478 
1479 /*
1480  * Get the uxTCBNumber assigned to the task referenced by the xTask parameter.
1481  */
1482 unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask );
1483 
1484 /*
1485  * Set the uxTCBNumber of the task referenced by the xTask parameter to
1486  * ucHandle.
1487  */
1488 void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle );
1489 
1490 /*
1491  * If tickless mode is being used, or a low power mode is implemented, then
1492  * the tick interrupt will not execute during idle periods. When this is the
1493  * case, the tick count value maintained by the scheduler needs to be kept up
1494  * to date with the actual execution time by being skipped forward by the by
1495  * a time equal to the idle period.
1496  */
1497 void vTaskStepTick( portTickType xTicksToJump );
1498 
1499 /*
1500  * Provided for use within portSUPPRESS_TICKS_AND_SLEEP() to allow the port
1501  * specific sleep function to determine if it is ok to proceed with the sleep,
1502  * and if it is ok to proceed, if it is ok to sleep indefinitely.
1503  *
1504  * This function is necessary because portSUPPRESS_TICKS_AND_SLEEP() is only
1505  * called with the scheduler suspended, not from within a critical section. It
1506  * is therefore possible for an interrupt to request a context switch between
1507  * portSUPPRESS_TICKS_AND_SLEEP() and the low power mode actually being
1508  * entered. eTaskConfirmSleepModeStatus() should be called from a short
1509  * critical section between the timer being stopped and the sleep mode being
1510  * entered to ensure it is ok to proceed into the sleep mode.
1511  */
1513 
1514 #ifdef __cplusplus
1515 }
1516 #endif
1517 #endif /* INC_TASK_H */
1518 
1519 
1520