2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 #ifndef INC_FREERTOS_H
\r
51 #error "#include FreeRTOS.h" must appear in source files before "#include task.h"
\r
59 #include "portable.h"
\r
66 /*-----------------------------------------------------------
\r
67 * MACROS AND DEFINITIONS
\r
68 *----------------------------------------------------------*/
\r
70 #define tskKERNEL_VERSION_NUMBER "V6.0.0"
\r
75 * Type by which tasks are referenced. For example, a call to xTaskCreate
\r
76 * returns (via a pointer parameter) an xTaskHandle variable that can then
\r
77 * be used as a parameter to vTaskDelete to delete the task.
\r
79 * \page xTaskHandle xTaskHandle
\r
82 typedef void * xTaskHandle;
\r
85 * Used internally only.
\r
87 typedef struct xTIME_OUT
\r
89 portBASE_TYPE xOverflowCount;
\r
90 portTickType xTimeOnEntering;
\r
94 * Defines the memory ranges allocated to the task when an MPU is used.
\r
96 typedef struct xMEMORY_REGION
\r
98 void *pvBaseAddress;
\r
99 unsigned long ulLengthInBytes;
\r
100 unsigned long ulParameters;
\r
104 * Parameters required to create an MPU protected task.
\r
106 typedef struct xTASK_PARAMTERS
\r
108 pdTASK_CODE pvTaskCode;
\r
109 const signed char * const pcName;
\r
110 unsigned short usStackDepth;
\r
111 void *pvParameters;
\r
112 unsigned portBASE_TYPE uxPriority;
\r
113 portSTACK_TYPE *puxStackBuffer;
\r
114 xMemoryRegion xRegions[ portNUM_CONFIGURABLE_REGIONS ];
\r
118 * Defines the priority used by the idle task. This must not be modified.
\r
120 * \ingroup TaskUtils
\r
122 #define tskIDLE_PRIORITY ( ( unsigned portBASE_TYPE ) 0 )
\r
127 * Macro for forcing a context switch.
\r
129 * \page taskYIELD taskYIELD
\r
130 * \ingroup SchedulerControl
\r
132 #define taskYIELD() portYIELD()
\r
137 * Macro to mark the start of a critical code region. Preemptive context
\r
138 * switches cannot occur when in a critical region.
\r
140 * NOTE: This may alter the stack (depending on the portable implementation)
\r
141 * so must be used with care!
\r
143 * \page taskENTER_CRITICAL taskENTER_CRITICAL
\r
144 * \ingroup SchedulerControl
\r
146 #define taskENTER_CRITICAL() portENTER_CRITICAL()
\r
151 * Macro to mark the end of a critical code region. Preemptive context
\r
152 * switches cannot occur when in a critical region.
\r
154 * NOTE: This may alter the stack (depending on the portable implementation)
\r
155 * so must be used with care!
\r
157 * \page taskEXIT_CRITICAL taskEXIT_CRITICAL
\r
158 * \ingroup SchedulerControl
\r
160 #define taskEXIT_CRITICAL() portEXIT_CRITICAL()
\r
165 * Macro to disable all maskable interrupts.
\r
167 * \page taskDISABLE_INTERRUPTS taskDISABLE_INTERRUPTS
\r
168 * \ingroup SchedulerControl
\r
170 #define taskDISABLE_INTERRUPTS() portDISABLE_INTERRUPTS()
\r
175 * Macro to enable microcontroller interrupts.
\r
177 * \page taskENABLE_INTERRUPTS taskENABLE_INTERRUPTS
\r
178 * \ingroup SchedulerControl
\r
180 #define taskENABLE_INTERRUPTS() portENABLE_INTERRUPTS()
\r
182 /* Definitions returned by xTaskGetSchedulerState(). */
\r
183 #define taskSCHEDULER_NOT_STARTED 0
\r
184 #define taskSCHEDULER_RUNNING 1
\r
185 #define taskSCHEDULER_SUSPENDED 2
\r
187 /*-----------------------------------------------------------
\r
188 * TASK CREATION API
\r
189 *----------------------------------------------------------*/
\r
194 portBASE_TYPE xTaskCreate(
\r
195 pdTASK_CODE pvTaskCode,
\r
196 const char * const pcName,
\r
197 unsigned short usStackDepth,
\r
198 void *pvParameters,
\r
199 unsigned portBASE_TYPE uxPriority,
\r
200 xTaskHandle *pvCreatedTask
\r
203 * Create a new task and add it to the list of tasks that are ready to run.
\r
205 * xTaskCreate() can only be used to create a task that has unrestricted
\r
206 * access to the entire microcontroller memory map. Systems that include MPU
\r
207 * support can alternatively create an MPU constrained task using
\r
208 * xTaskCreateRestricted().
\r
210 * @param pvTaskCode Pointer to the task entry function. Tasks
\r
211 * must be implemented to never return (i.e. continuous loop).
\r
213 * @param pcName A descriptive name for the task. This is mainly used to
\r
214 * facilitate debugging. Max length defined by tskMAX_TASK_NAME_LEN - default
\r
217 * @param usStackDepth The size of the task stack specified as the number of
\r
218 * variables the stack can hold - not the number of bytes. For example, if
\r
219 * the stack is 16 bits wide and usStackDepth is defined as 100, 200 bytes
\r
220 * will be allocated for stack storage.
\r
222 * @param pvParameters Pointer that will be used as the parameter for the task
\r
225 * @param uxPriority The priority at which the task should run. Systems that
\r
226 * include MPU support can optionally create tasks in a privileged (system)
\r
227 * mode by setting bit portPRIVILEGE_BIT of the priority parameter. For
\r
228 * example, to create a privileged task at priority 2 the uxPriority parameter
\r
229 * should be set to ( 2 | portPRIVILEGE_BIT ).
\r
231 * @param pvCreatedTask Used to pass back a handle by which the created task
\r
232 * can be referenced.
\r
234 * @return pdPASS if the task was successfully created and added to a ready
\r
235 * list, otherwise an error code defined in the file errors. h
\r
239 // Task to be created.
\r
240 void vTaskCode( void * pvParameters )
\r
244 // Task code goes here.
\r
248 // Function that creates a task.
\r
249 void vOtherFunction( void )
\r
251 static unsigned char ucParameterToPass;
\r
252 xTaskHandle xHandle;
\r
254 // Create the task, storing the handle. Note that the passed parameter ucParameterToPass
\r
255 // must exist for the lifetime of the task, so in this case is declared static. If it was just an
\r
256 // an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
\r
257 // the new time attempts to access it.
\r
258 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
\r
260 // Use the handle to delete the task.
\r
261 vTaskDelete( xHandle );
\r
264 * \defgroup xTaskCreate xTaskCreate
\r
267 #define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ) )
\r
272 portBASE_TYPE xTaskCreateRestricted( xTaskParameters *pxTaskDefinition, xTaskHandle pxCreatedTask );</pre>
\r
274 * xTaskCreateRestricted() should only be used in systems that include an MPU
\r
277 * Create a new task and add it to the list of tasks that are ready to run.
\r
278 * The function parameters define the memory regions and associated access
\r
279 * permissions allocated to the task.
\r
281 * @param pxTaskDefinition Pointer to a structure that contains a member
\r
282 * for each of the normal xTaskCreate() parameters (see the xTaskCreate() API
\r
283 * documentation) plus an optional stack buffer and the memory region
\r
286 * @param pcName A descriptive name for the task. This is mainly used to
\r
287 * facilitate debugging. Max length defined by tskMAX_TASK_NAME_LEN - default
\r
290 * @param pxCreatedTask Used to pass back a handle by which the created task
\r
291 * can be referenced.
\r
293 * @return pdPASS if the task was successfully created and added to a ready
\r
294 * list, otherwise an error code defined in the file errors. h
\r
298 // Create an xTaskParameters structure that defines the task to be created.
\r
299 static const xTaskParameters xCheckTaskParameters =
\r
301 vATask, // pvTaskCode - the function that implements the task.
\r
302 "ATask", // pcName - just a text name for the task to assist debugging.
\r
303 100, // usStackDepth - the stack size DEFINED IN WORDS.
\r
304 NULL, // pvParameters - passed into the task function as the function parameters.
\r
305 ( 1UL | portPRIVILEGE_BIT ),// uxPriority - task priority, set the portPRIVILEGE_BIT if the task should run in a privileged state.
\r
306 cStackBuffer,// puxStackBuffer - the buffer to be used as the task stack.
\r
308 // xRegions - Allocate up to three separate memory regions for access by
\r
309 // the task, with appropriate access permissions. Different processors have
\r
310 // different memory alignment requirements - refer to the FreeRTOS documentation
\r
311 // for full information.
\r
313 // Base address Length Parameters
\r
314 { cReadWriteArray, 32, portMPU_REGION_READ_WRITE },
\r
315 { cReadOnlyArray, 32, portMPU_REGION_READ_ONLY },
\r
316 { cPrivilegedOnlyAccessArray, 128, portMPU_REGION_PRIVILEGED_READ_WRITE }
\r
322 xTaskHandle xHandle;
\r
324 // Create a task from the const structure defined above. The task handle
\r
325 // is requested (the second parameter is not NULL) but in this case just for
\r
326 // demonstration purposes as its not actually used.
\r
327 xTaskCreateRestricted( &xRegTest1Parameters, &xHandle );
\r
329 // Start the scheduler.
\r
330 vTaskStartScheduler();
\r
332 // Will only get here if there was insufficient memory to create the idle
\r
337 * \defgroup xTaskCreateRestricted xTaskCreateRestricted
\r
340 #define xTaskCreateRestricted( x, pxCreatedTask ) xTaskGenericCreate( ((x)->pvTaskCode), ((x)->pcName), ((x)->usStackDepth), ((x)->pvParameters), ((x)->uxPriority), (pxCreatedTask), ((x)->puxStackBuffer), ((x)->xRegions) )
\r
345 void vTaskAllocateMPURegions( xTaskHandle xTask, const xMemoryRegion * const pxRegions );</pre>
\r
347 * Memory regions are assigned to a restricted task when the task is created by
\r
348 * a call to xTaskCreateRestricted(). These regions can be redefined using
\r
349 * vTaskAllocateMPURegions().
\r
351 * @param xTask The handle of the task being updated.
\r
353 * @param xRegions A pointer to an xMemoryRegion structure that contains the
\r
354 * new memory region definitions.
\r
358 // Define an array of xMemoryRegion structures that configures an MPU region
\r
359 // allowing read/write access for 1024 bytes starting at the beginning of the
\r
360 // ucOneKByte array. The other two of the maximum 3 definable regions are
\r
361 // unused so set to zero.
\r
362 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
\r
364 // Base address Length Parameters
\r
365 { ucOneKByte, 1024, portMPU_REGION_READ_WRITE },
\r
370 void vATask( void *pvParameters )
\r
372 // This task was created such that it has access to certain regions of
\r
373 // memory as defined by the MPU configuration. At some point it is
\r
374 // desired that these MPU regions are replaced with that defined in the
\r
375 // xAltRegions const struct above. Use a call to vTaskAllocateMPURegions()
\r
376 // for this purpose. NULL is used as the task handle to indicate that this
\r
377 // function should modify the MPU regions of the calling task.
\r
378 vTaskAllocateMPURegions( NULL, xAltRegions );
\r
380 // Now the task can continue its function, but from this point on can only
\r
381 // access its stack and the ucOneKByte array (unless any other statically
\r
382 // defined or shared regions have been declared elsewhere).
\r
385 * \defgroup xTaskCreateRestricted xTaskCreateRestricted
\r
388 void vTaskAllocateMPURegions( xTaskHandle xTask, const xMemoryRegion * const pxRegions ) PRIVILEGED_FUNCTION;
\r
392 * <pre>void vTaskDelete( xTaskHandle pxTask );</pre>
\r
394 * INCLUDE_vTaskDelete must be defined as 1 for this function to be available.
\r
395 * See the configuration section for more information.
\r
397 * Remove a task from the RTOS real time kernels management. The task being
\r
398 * deleted will be removed from all ready, blocked, suspended and event lists.
\r
400 * NOTE: The idle task is responsible for freeing the kernel allocated
\r
401 * memory from tasks that have been deleted. It is therefore important that
\r
402 * the idle task is not starved of microcontroller processing time if your
\r
403 * application makes any calls to vTaskDelete (). Memory allocated by the
\r
404 * task code is not automatically freed, and should be freed before the task
\r
407 * See the demo application file death.c for sample code that utilises
\r
410 * @param pxTask The handle of the task to be deleted. Passing NULL will
\r
411 * cause the calling task to be deleted.
\r
415 void vOtherFunction( void )
\r
417 xTaskHandle xHandle;
\r
419 // Create the task, storing the handle.
\r
420 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
\r
422 // Use the handle to delete the task.
\r
423 vTaskDelete( xHandle );
\r
426 * \defgroup vTaskDelete vTaskDelete
\r
429 void vTaskDelete( xTaskHandle pxTask ) PRIVILEGED_FUNCTION;
\r
432 /*-----------------------------------------------------------
\r
434 *----------------------------------------------------------*/
\r
438 * <pre>void vTaskDelay( portTickType xTicksToDelay );</pre>
\r
440 * Delay a task for a given number of ticks. The actual time that the
\r
441 * task remains blocked depends on the tick rate. The constant
\r
442 * portTICK_RATE_MS can be used to calculate real time from the tick
\r
443 * rate - with the resolution of one tick period.
\r
445 * INCLUDE_vTaskDelay must be defined as 1 for this function to be available.
\r
446 * See the configuration section for more information.
\r
449 * vTaskDelay() specifies a time at which the task wishes to unblock relative to
\r
450 * the time at which vTaskDelay() is called. For example, specifying a block
\r
451 * period of 100 ticks will cause the task to unblock 100 ticks after
\r
452 * vTaskDelay() is called. vTaskDelay() does not therefore provide a good method
\r
453 * of controlling the frequency of a cyclical task as the path taken through the
\r
454 * code, as well as other task and interrupt activity, will effect the frequency
\r
455 * at which vTaskDelay() gets called and therefore the time at which the task
\r
456 * next executes. See vTaskDelayUntil() for an alternative API function designed
\r
457 * to facilitate fixed frequency execution. It does this by specifying an
\r
458 * absolute time (rather than a relative time) at which the calling task should
\r
461 * @param xTicksToDelay The amount of time, in tick periods, that
\r
462 * the calling task should block.
\r
466 void vTaskFunction( void * pvParameters )
\r
468 void vTaskFunction( void * pvParameters )
\r
470 // Block for 500ms.
\r
471 const portTickType xDelay = 500 / portTICK_RATE_MS;
\r
475 // Simply toggle the LED every 500ms, blocking between each toggle.
\r
477 vTaskDelay( xDelay );
\r
481 * \defgroup vTaskDelay vTaskDelay
\r
482 * \ingroup TaskCtrl
\r
484 void vTaskDelay( portTickType xTicksToDelay ) PRIVILEGED_FUNCTION;
\r
488 * <pre>void vTaskDelayUntil( portTickType *pxPreviousWakeTime, portTickType xTimeIncrement );</pre>
\r
490 * INCLUDE_vTaskDelayUntil must be defined as 1 for this function to be available.
\r
491 * See the configuration section for more information.
\r
493 * Delay a task until a specified time. This function can be used by cyclical
\r
494 * tasks to ensure a constant execution frequency.
\r
496 * This function differs from vTaskDelay () in one important aspect: vTaskDelay () will
\r
497 * cause a task to block for the specified number of ticks from the time vTaskDelay () is
\r
498 * called. It is therefore difficult to use vTaskDelay () by itself to generate a fixed
\r
499 * execution frequency as the time between a task starting to execute and that task
\r
500 * calling vTaskDelay () may not be fixed [the task may take a different path though the
\r
501 * code between calls, or may get interrupted or preempted a different number of times
\r
502 * each time it executes].
\r
504 * Whereas vTaskDelay () specifies a wake time relative to the time at which the function
\r
505 * is called, vTaskDelayUntil () specifies the absolute (exact) time at which it wishes to
\r
508 * The constant portTICK_RATE_MS can be used to calculate real time from the tick
\r
509 * rate - with the resolution of one tick period.
\r
511 * @param pxPreviousWakeTime Pointer to a variable that holds the time at which the
\r
512 * task was last unblocked. The variable must be initialised with the current time
\r
513 * prior to its first use (see the example below). Following this the variable is
\r
514 * automatically updated within vTaskDelayUntil ().
\r
516 * @param xTimeIncrement The cycle time period. The task will be unblocked at
\r
517 * time *pxPreviousWakeTime + xTimeIncrement. Calling vTaskDelayUntil with the
\r
518 * same xTimeIncrement parameter value will cause the task to execute with
\r
519 * a fixed interface period.
\r
523 // Perform an action every 10 ticks.
\r
524 void vTaskFunction( void * pvParameters )
\r
526 portTickType xLastWakeTime;
\r
527 const portTickType xFrequency = 10;
\r
529 // Initialise the xLastWakeTime variable with the current time.
\r
530 xLastWakeTime = xTaskGetTickCount ();
\r
533 // Wait for the next cycle.
\r
534 vTaskDelayUntil( &xLastWakeTime, xFrequency );
\r
536 // Perform action here.
\r
540 * \defgroup vTaskDelayUntil vTaskDelayUntil
\r
541 * \ingroup TaskCtrl
\r
543 void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement ) PRIVILEGED_FUNCTION;
\r
547 * <pre>unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask );</pre>
\r
549 * INCLUDE_xTaskPriorityGet must be defined as 1 for this function to be available.
\r
550 * See the configuration section for more information.
\r
552 * Obtain the priority of any task.
\r
554 * @param pxTask Handle of the task to be queried. Passing a NULL
\r
555 * handle results in the priority of the calling task being returned.
\r
557 * @return The priority of pxTask.
\r
561 void vAFunction( void )
\r
563 xTaskHandle xHandle;
\r
565 // Create a task, storing the handle.
\r
566 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
\r
570 // Use the handle to obtain the priority of the created task.
\r
571 // It was created with tskIDLE_PRIORITY, but may have changed
\r
573 if( uxTaskPriorityGet( xHandle ) != tskIDLE_PRIORITY )
\r
575 // The task has changed it's priority.
\r
580 // Is our priority higher than the created task?
\r
581 if( uxTaskPriorityGet( xHandle ) < uxTaskPriorityGet( NULL ) )
\r
583 // Our priority (obtained using NULL handle) is higher.
\r
587 * \defgroup uxTaskPriorityGet uxTaskPriorityGet
\r
588 * \ingroup TaskCtrl
\r
590 unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask ) PRIVILEGED_FUNCTION;
\r
594 * <pre>void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority );</pre>
\r
596 * INCLUDE_vTaskPrioritySet must be defined as 1 for this function to be available.
\r
597 * See the configuration section for more information.
\r
599 * Set the priority of any task.
\r
601 * A context switch will occur before the function returns if the priority
\r
602 * being set is higher than the currently executing task.
\r
604 * @param pxTask Handle to the task for which the priority is being set.
\r
605 * Passing a NULL handle results in the priority of the calling task being set.
\r
607 * @param uxNewPriority The priority to which the task will be set.
\r
611 void vAFunction( void )
\r
613 xTaskHandle xHandle;
\r
615 // Create a task, storing the handle.
\r
616 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
\r
620 // Use the handle to raise the priority of the created task.
\r
621 vTaskPrioritySet( xHandle, tskIDLE_PRIORITY + 1 );
\r
625 // Use a NULL handle to raise our priority to the same value.
\r
626 vTaskPrioritySet( NULL, tskIDLE_PRIORITY + 1 );
\r
629 * \defgroup vTaskPrioritySet vTaskPrioritySet
\r
630 * \ingroup TaskCtrl
\r
632 void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority ) PRIVILEGED_FUNCTION;
\r
636 * <pre>void vTaskSuspend( xTaskHandle pxTaskToSuspend );</pre>
\r
638 * INCLUDE_vTaskSuspend must be defined as 1 for this function to be available.
\r
639 * See the configuration section for more information.
\r
641 * Suspend any task. When suspended a task will never get any microcontroller
\r
642 * processing time, no matter what its priority.
\r
644 * Calls to vTaskSuspend are not accumulative -
\r
645 * i.e. calling vTaskSuspend () twice on the same task still only requires one
\r
646 * call to vTaskResume () to ready the suspended task.
\r
648 * @param pxTaskToSuspend Handle to the task being suspended. Passing a NULL
\r
649 * handle will cause the calling task to be suspended.
\r
653 void vAFunction( void )
\r
655 xTaskHandle xHandle;
\r
657 // Create a task, storing the handle.
\r
658 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
\r
662 // Use the handle to suspend the created task.
\r
663 vTaskSuspend( xHandle );
\r
667 // The created task will not run during this period, unless
\r
668 // another task calls vTaskResume( xHandle ).
\r
673 // Suspend ourselves.
\r
674 vTaskSuspend( NULL );
\r
676 // We cannot get here unless another task calls vTaskResume
\r
677 // with our handle as the parameter.
\r
680 * \defgroup vTaskSuspend vTaskSuspend
\r
681 * \ingroup TaskCtrl
\r
683 void vTaskSuspend( xTaskHandle pxTaskToSuspend ) PRIVILEGED_FUNCTION;
\r
687 * <pre>void vTaskResume( xTaskHandle pxTaskToResume );</pre>
\r
689 * INCLUDE_vTaskSuspend must be defined as 1 for this function to be available.
\r
690 * See the configuration section for more information.
\r
692 * Resumes a suspended task.
\r
694 * A task that has been suspended by one of more calls to vTaskSuspend ()
\r
695 * will be made available for running again by a single call to
\r
698 * @param pxTaskToResume Handle to the task being readied.
\r
702 void vAFunction( void )
\r
704 xTaskHandle xHandle;
\r
706 // Create a task, storing the handle.
\r
707 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
\r
711 // Use the handle to suspend the created task.
\r
712 vTaskSuspend( xHandle );
\r
716 // The created task will not run during this period, unless
\r
717 // another task calls vTaskResume( xHandle ).
\r
722 // Resume the suspended task ourselves.
\r
723 vTaskResume( xHandle );
\r
725 // The created task will once again get microcontroller processing
\r
726 // time in accordance with it priority within the system.
\r
729 * \defgroup vTaskResume vTaskResume
\r
730 * \ingroup TaskCtrl
\r
732 void vTaskResume( xTaskHandle pxTaskToResume ) PRIVILEGED_FUNCTION;
\r
736 * <pre>void xTaskResumeFromISR( xTaskHandle pxTaskToResume );</pre>
\r
738 * INCLUDE_xTaskResumeFromISR must be defined as 1 for this function to be
\r
739 * available. See the configuration section for more information.
\r
741 * An implementation of vTaskResume() that can be called from within an ISR.
\r
743 * A task that has been suspended by one of more calls to vTaskSuspend ()
\r
744 * will be made available for running again by a single call to
\r
745 * xTaskResumeFromISR ().
\r
747 * @param pxTaskToResume Handle to the task being readied.
\r
749 * \defgroup vTaskResumeFromISR vTaskResumeFromISR
\r
750 * \ingroup TaskCtrl
\r
752 portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume ) PRIVILEGED_FUNCTION;
\r
754 /*-----------------------------------------------------------
\r
755 * SCHEDULER CONTROL
\r
756 *----------------------------------------------------------*/
\r
760 * <pre>void vTaskStartScheduler( void );</pre>
\r
762 * Starts the real time kernel tick processing. After calling the kernel
\r
763 * has control over which tasks are executed and when. This function
\r
764 * does not return until an executing task calls vTaskEndScheduler ().
\r
766 * At least one task should be created via a call to xTaskCreate ()
\r
767 * before calling vTaskStartScheduler (). The idle task is created
\r
768 * automatically when the first application task is created.
\r
770 * See the demo application file main.c for an example of creating
\r
771 * tasks and starting the kernel.
\r
775 void vAFunction( void )
\r
777 // Create at least one task before starting the kernel.
\r
778 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
780 // Start the real time kernel with preemption.
\r
781 vTaskStartScheduler ();
\r
783 // Will not get here unless a task calls vTaskEndScheduler ()
\r
787 * \defgroup vTaskStartScheduler vTaskStartScheduler
\r
788 * \ingroup SchedulerControl
\r
790 void vTaskStartScheduler( void ) PRIVILEGED_FUNCTION;
\r
794 * <pre>void vTaskEndScheduler( void );</pre>
\r
796 * Stops the real time kernel tick. All created tasks will be automatically
\r
797 * deleted and multitasking (either preemptive or cooperative) will
\r
798 * stop. Execution then resumes from the point where vTaskStartScheduler ()
\r
799 * was called, as if vTaskStartScheduler () had just returned.
\r
801 * See the demo application file main. c in the demo/PC directory for an
\r
802 * example that uses vTaskEndScheduler ().
\r
804 * vTaskEndScheduler () requires an exit function to be defined within the
\r
805 * portable layer (see vPortEndScheduler () in port. c for the PC port). This
\r
806 * performs hardware specific operations such as stopping the kernel tick.
\r
808 * vTaskEndScheduler () will cause all of the resources allocated by the
\r
809 * kernel to be freed - but will not free resources allocated by application
\r
814 void vTaskCode( void * pvParameters )
\r
818 // Task code goes here.
\r
820 // At some point we want to end the real time kernel processing
\r
822 vTaskEndScheduler ();
\r
826 void vAFunction( void )
\r
828 // Create at least one task before starting the kernel.
\r
829 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
831 // Start the real time kernel with preemption.
\r
832 vTaskStartScheduler ();
\r
834 // Will only get here when the vTaskCode () task has called
\r
835 // vTaskEndScheduler (). When we get here we are back to single task
\r
840 * \defgroup vTaskEndScheduler vTaskEndScheduler
\r
841 * \ingroup SchedulerControl
\r
843 void vTaskEndScheduler( void ) PRIVILEGED_FUNCTION;
\r
847 * <pre>void vTaskSuspendAll( void );</pre>
\r
849 * Suspends all real time kernel activity while keeping interrupts (including the
\r
850 * kernel tick) enabled.
\r
852 * After calling vTaskSuspendAll () the calling task will continue to execute
\r
853 * without risk of being swapped out until a call to xTaskResumeAll () has been
\r
856 * API functions that have the potential to cause a context switch (for example,
\r
857 * vTaskDelayUntil(), xQueueSend(), etc.) must not be called while the scheduler
\r
862 void vTask1( void * pvParameters )
\r
866 // Task code goes here.
\r
870 // At some point the task wants to perform a long operation during
\r
871 // which it does not want to get swapped out. It cannot use
\r
872 // taskENTER_CRITICAL ()/taskEXIT_CRITICAL () as the length of the
\r
873 // operation may cause interrupts to be missed - including the
\r
876 // Prevent the real time kernel swapping out the task.
\r
877 vTaskSuspendAll ();
\r
879 // Perform the operation here. There is no need to use critical
\r
880 // sections as we have all the microcontroller processing time.
\r
881 // During this time interrupts will still operate and the kernel
\r
882 // tick count will be maintained.
\r
886 // The operation is complete. Restart the kernel.
\r
891 * \defgroup vTaskSuspendAll vTaskSuspendAll
\r
892 * \ingroup SchedulerControl
\r
894 void vTaskSuspendAll( void ) PRIVILEGED_FUNCTION;
\r
898 * <pre>char xTaskResumeAll( void );</pre>
\r
900 * Resumes real time kernel activity following a call to vTaskSuspendAll ().
\r
901 * After a call to vTaskSuspendAll () the kernel will take control of which
\r
902 * task is executing at any time.
\r
904 * @return If resuming the scheduler caused a context switch then pdTRUE is
\r
905 * returned, otherwise pdFALSE is returned.
\r
909 void vTask1( void * pvParameters )
\r
913 // Task code goes here.
\r
917 // At some point the task wants to perform a long operation during
\r
918 // which it does not want to get swapped out. It cannot use
\r
919 // taskENTER_CRITICAL ()/taskEXIT_CRITICAL () as the length of the
\r
920 // operation may cause interrupts to be missed - including the
\r
923 // Prevent the real time kernel swapping out the task.
\r
924 vTaskSuspendAll ();
\r
926 // Perform the operation here. There is no need to use critical
\r
927 // sections as we have all the microcontroller processing time.
\r
928 // During this time interrupts will still operate and the real
\r
929 // time kernel tick count will be maintained.
\r
933 // The operation is complete. Restart the kernel. We want to force
\r
934 // a context switch - but there is no point if resuming the scheduler
\r
935 // caused a context switch already.
\r
936 if( !xTaskResumeAll () )
\r
943 * \defgroup xTaskResumeAll xTaskResumeAll
\r
944 * \ingroup SchedulerControl
\r
946 signed portBASE_TYPE xTaskResumeAll( void ) PRIVILEGED_FUNCTION;
\r
950 * <pre>signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask );</pre>
\r
952 * Utility task that simply returns pdTRUE if the task referenced by xTask is
\r
953 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
\r
954 * is in any other state.
\r
957 signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
\r
959 /*-----------------------------------------------------------
\r
961 *----------------------------------------------------------*/
\r
965 * <PRE>volatile portTickType xTaskGetTickCount( void );</PRE>
\r
967 * @return The count of ticks since vTaskStartScheduler was called.
\r
969 * \page xTaskGetTickCount xTaskGetTickCount
\r
970 * \ingroup TaskUtils
\r
972 portTickType xTaskGetTickCount( void ) PRIVILEGED_FUNCTION;
\r
976 * <PRE>unsigned short uxTaskGetNumberOfTasks( void );</PRE>
\r
978 * @return The number of tasks that the real time kernel is currently managing.
\r
979 * This includes all ready, blocked and suspended tasks. A task that
\r
980 * has been deleted but not yet freed by the idle task will also be
\r
981 * included in the count.
\r
983 * \page uxTaskGetNumberOfTasks uxTaskGetNumberOfTasks
\r
984 * \ingroup TaskUtils
\r
986 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void ) PRIVILEGED_FUNCTION;
\r
990 * <PRE>void vTaskList( char *pcWriteBuffer );</PRE>
\r
992 * configUSE_TRACE_FACILITY must be defined as 1 for this function to be
\r
993 * available. See the configuration section for more information.
\r
995 * NOTE: This function will disable interrupts for its duration. It is
\r
996 * not intended for normal application runtime use but as a debug aid.
\r
998 * Lists all the current tasks, along with their current state and stack
\r
999 * usage high water mark.
\r
1001 * Tasks are reported as blocked ('B'), ready ('R'), deleted ('D') or
\r
1002 * suspended ('S').
\r
1004 * @param pcWriteBuffer A buffer into which the above mentioned details
\r
1005 * will be written, in ascii form. This buffer is assumed to be large
\r
1006 * enough to contain the generated report. Approximately 40 bytes per
\r
1007 * task should be sufficient.
\r
1009 * \page vTaskList vTaskList
\r
1010 * \ingroup TaskUtils
\r
1012 void vTaskList( signed char *pcWriteBuffer ) PRIVILEGED_FUNCTION;
\r
1016 * <PRE>void vTaskGetRunTimeStats( char *pcWriteBuffer );</PRE>
\r
1018 * configGENERATE_RUN_TIME_STATS must be defined as 1 for this function
\r
1019 * to be available. The application must also then provide definitions
\r
1020 * for portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() and
\r
1021 * portGET_RUN_TIME_COUNTER_VALUE to configure a peripheral timer/counter
\r
1022 * and return the timers current count value respectively. The counter
\r
1023 * should be at least 10 times the frequency of the tick count.
\r
1025 * NOTE: This function will disable interrupts for its duration. It is
\r
1026 * not intended for normal application runtime use but as a debug aid.
\r
1028 * Setting configGENERATE_RUN_TIME_STATS to 1 will result in a total
\r
1029 * accumulated execution time being stored for each task. The resolution
\r
1030 * of the accumulated time value depends on the frequency of the timer
\r
1031 * configured by the portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() macro.
\r
1032 * Calling vTaskGetRunTimeStats() writes the total execution time of each
\r
1033 * task into a buffer, both as an absolute count value and as a percentage
\r
1034 * of the total system execution time.
\r
1036 * @param pcWriteBuffer A buffer into which the execution times will be
\r
1037 * written, in ascii form. This buffer is assumed to be large enough to
\r
1038 * contain the generated report. Approximately 40 bytes per task should
\r
1041 * \page vTaskGetRunTimeStats vTaskGetRunTimeStats
\r
1042 * \ingroup TaskUtils
\r
1044 void vTaskGetRunTimeStats( signed char *pcWriteBuffer ) PRIVILEGED_FUNCTION;
\r
1048 * <PRE>void vTaskStartTrace( char * pcBuffer, unsigned portBASE_TYPE uxBufferSize );</PRE>
\r
1050 * Starts a real time kernel activity trace. The trace logs the identity of
\r
1051 * which task is running when.
\r
1053 * The trace file is stored in binary format. A separate DOS utility called
\r
1054 * convtrce.exe is used to convert this into a tab delimited text file which
\r
1055 * can be viewed and plotted in a spread sheet.
\r
1057 * @param pcBuffer The buffer into which the trace will be written.
\r
1059 * @param ulBufferSize The size of pcBuffer in bytes. The trace will continue
\r
1060 * until either the buffer in full, or ulTaskEndTrace () is called.
\r
1062 * \page vTaskStartTrace vTaskStartTrace
\r
1063 * \ingroup TaskUtils
\r
1065 void vTaskStartTrace( signed char * pcBuffer, unsigned long ulBufferSize ) PRIVILEGED_FUNCTION;
\r
1069 * <PRE>unsigned long ulTaskEndTrace( void );</PRE>
\r
1071 * Stops a kernel activity trace. See vTaskStartTrace ().
\r
1073 * @return The number of bytes that have been written into the trace buffer.
\r
1075 * \page usTaskEndTrace usTaskEndTrace
\r
1076 * \ingroup TaskUtils
\r
1078 unsigned long ulTaskEndTrace( void ) PRIVILEGED_FUNCTION;
\r
1082 * <PRE>unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask );</PRE>
\r
1084 * INCLUDE_uxTaskGetStackHighWaterMark must be set to 1 in FreeRTOSConfig.h for
\r
1085 * this function to be available.
\r
1087 * Returns the high water mark of the stack associated with xTask. That is,
\r
1088 * the minimum free stack space there has been (in bytes) since the task
\r
1089 * started. The smaller the returned number the closer the task has come
\r
1090 * to overflowing its stack.
\r
1092 * @param xTask Handle of the task associated with the stack to be checked.
\r
1093 * Set xTask to NULL to check the stack of the calling task.
\r
1095 * @return The smallest amount of free stack space there has been (in bytes)
\r
1096 * since the task referenced by xTask was created.
\r
1098 unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
\r
1102 * <pre>void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction );</pre>
\r
1104 * Sets pxHookFunction to be the task hook function used by the task xTask.
\r
1105 * Passing xTask as NULL has the effect of setting the calling tasks hook
\r
1108 void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction ) PRIVILEGED_FUNCTION;
\r
1112 * <pre>void xTaskGetApplicationTaskTag( xTaskHandle xTask );</pre>
\r
1114 * Returns the pxHookFunction value assigned to the task xTask.
\r
1116 pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
\r
1120 * <pre>portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction );</pre>
\r
1122 * Calls the hook function associated with xTask. Passing xTask as NULL has
\r
1123 * the effect of calling the Running tasks (the calling task) hook function.
\r
1125 * pvParameter is passed to the hook function for the task to interpret as it
\r
1128 portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter ) PRIVILEGED_FUNCTION;
\r
1131 /*-----------------------------------------------------------
\r
1132 * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
\r
1133 *----------------------------------------------------------*/
\r
1136 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
\r
1137 * INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
\r
1138 * AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
\r
1140 * Called from the real time kernel tick (either preemptive or cooperative),
\r
1141 * this increments the tick count and checks if any tasks that are blocked
\r
1142 * for a finite period required removing from a blocked list and placing on
\r
1145 void vTaskIncrementTick( void ) PRIVILEGED_FUNCTION;
\r
1148 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
\r
1149 * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
\r
1151 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
\r
1153 * Removes the calling task from the ready list and places it both
\r
1154 * on the list of tasks waiting for a particular event, and the
\r
1155 * list of delayed tasks. The task will be removed from both lists
\r
1156 * and replaced on the ready list should either the event occur (and
\r
1157 * there be no higher priority tasks waiting on the same event) or
\r
1158 * the delay period expires.
\r
1160 * @param pxEventList The list containing tasks that are blocked waiting
\r
1161 * for the event to occur.
\r
1163 * @param xTicksToWait The maximum amount of time that the task should wait
\r
1164 * for the event to occur. This is specified in kernel ticks,the constant
\r
1165 * portTICK_RATE_MS can be used to convert kernel ticks into a real time
\r
1168 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
1171 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
\r
1172 * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
\r
1174 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
\r
1176 * Removes a task from both the specified event list and the list of blocked
\r
1177 * tasks, and places it on a ready queue.
\r
1179 * xTaskRemoveFromEventList () will be called if either an event occurs to
\r
1180 * unblock a task, or the block timeout period expires.
\r
1182 * @return pdTRUE if the task being removed has a higher priority than the task
\r
1183 * making the call, otherwise pdFALSE.
\r
1185 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList ) PRIVILEGED_FUNCTION;
\r
1188 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
\r
1189 * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
\r
1191 * INCLUDE_vTaskCleanUpResources and INCLUDE_vTaskSuspend must be defined as 1
\r
1192 * for this function to be available.
\r
1193 * See the configuration section for more information.
\r
1195 * Empties the ready and delayed queues of task control blocks, freeing the
\r
1196 * memory allocated for the task control block and task stacks as it goes.
\r
1198 void vTaskCleanUpResources( void ) PRIVILEGED_FUNCTION;
\r
1201 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
\r
1202 * INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
\r
1203 * AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
\r
1205 * Sets the pointer to the current TCB to the TCB of the highest priority task
\r
1206 * that is ready to run.
\r
1208 void vTaskSwitchContext( void ) PRIVILEGED_FUNCTION;
\r
1211 * Return the handle of the calling task.
\r
1213 xTaskHandle xTaskGetCurrentTaskHandle( void ) PRIVILEGED_FUNCTION;
\r
1216 * Capture the current time status for future reference.
\r
1218 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut ) PRIVILEGED_FUNCTION;
\r
1221 * Compare the time status now with that previously captured to see if the
\r
1222 * timeout has expired.
\r
1224 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait ) PRIVILEGED_FUNCTION;
\r
1227 * Shortcut used by the queue implementation to prevent unnecessary call to
\r
1230 void vTaskMissedYield( void ) PRIVILEGED_FUNCTION;
\r
1233 * Returns the scheduler state as taskSCHEDULER_RUNNING,
\r
1234 * taskSCHEDULER_NOT_STARTED or taskSCHEDULER_SUSPENDED.
\r
1236 portBASE_TYPE xTaskGetSchedulerState( void ) PRIVILEGED_FUNCTION;
\r
1239 * Raises the priority of the mutex holder to that of the calling task should
\r
1240 * the mutex holder have a priority less than the calling task.
\r
1242 void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder ) PRIVILEGED_FUNCTION;
\r
1245 * Set the priority of a task back to its proper priority in the case that it
\r
1246 * inherited a higher priority while it was holding a semaphore.
\r
1248 void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder ) PRIVILEGED_FUNCTION;
\r
1251 * Generic version of the task creation function which is in turn called by the
\r
1252 * xTaskCreate() and xTaskCreateProtected() macros.
\r
1254 signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pvTaskCode, 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;
\r
1256 #ifdef __cplusplus
\r
1259 #endif /* TASK_H */
\r