FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Source / include / task.h
bloba33c98728a83f0597cb76dbbbf012791f4ed8b67
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 #ifndef INC_FREERTOS_H
56 #error "#include FreeRTOS.h" must appear in source files before "#include task.h"
57 #endif
61 #ifndef TASK_H
62 #define TASK_H
64 #include "portable.h"
65 #include "list.h"
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
71 /*-----------------------------------------------------------
72 * MACROS AND DEFINITIONS
73 *----------------------------------------------------------*/
75 #define tskKERNEL_VERSION_NUMBER "V6.0.4"
77 /**
78 * task. h
80 * Type by which tasks are referenced. For example, a call to xTaskCreate
81 * returns (via a pointer parameter) an xTaskHandle variable that can then
82 * be used as a parameter to vTaskDelete to delete the task.
84 * \page xTaskHandle xTaskHandle
85 * \ingroup Tasks
87 typedef void * xTaskHandle;
90 * Used internally only.
92 typedef struct xTIME_OUT
94 portBASE_TYPE xOverflowCount;
95 portTickType xTimeOnEntering;
96 } xTimeOutType;
99 * Defines the memory ranges allocated to the task when an MPU is used.
101 typedef struct xMEMORY_REGION
103 void *pvBaseAddress;
104 unsigned long ulLengthInBytes;
105 unsigned long ulParameters;
106 } xMemoryRegion;
109 * Parameters required to create an MPU protected task.
111 typedef struct xTASK_PARAMTERS
113 pdTASK_CODE pvTaskCode;
114 const signed char * const pcName;
115 unsigned short usStackDepth;
116 void *pvParameters;
117 unsigned portBASE_TYPE uxPriority;
118 portSTACK_TYPE *puxStackBuffer;
119 xMemoryRegion xRegions[ portNUM_CONFIGURABLE_REGIONS ];
120 } xTaskParameters;
123 * Defines the priority used by the idle task. This must not be modified.
125 * \ingroup TaskUtils
127 #define tskIDLE_PRIORITY ( ( unsigned portBASE_TYPE ) 0 )
130 * task. h
132 * Macro for forcing a context switch.
134 * \page taskYIELD taskYIELD
135 * \ingroup SchedulerControl
137 #define taskYIELD() portYIELD()
140 * task. h
142 * Macro to mark the start of a critical code region. Preemptive context
143 * switches cannot occur when in a critical region.
145 * NOTE: This may alter the stack (depending on the portable implementation)
146 * so must be used with care!
148 * \page taskENTER_CRITICAL taskENTER_CRITICAL
149 * \ingroup SchedulerControl
151 #define taskENTER_CRITICAL() portENTER_CRITICAL()
154 * task. h
156 * Macro to mark the end of a critical code region. Preemptive context
157 * switches cannot occur when in a critical region.
159 * NOTE: This may alter the stack (depending on the portable implementation)
160 * so must be used with care!
162 * \page taskEXIT_CRITICAL taskEXIT_CRITICAL
163 * \ingroup SchedulerControl
165 #define taskEXIT_CRITICAL() portEXIT_CRITICAL()
168 * task. h
170 * Macro to disable all maskable interrupts.
172 * \page taskDISABLE_INTERRUPTS taskDISABLE_INTERRUPTS
173 * \ingroup SchedulerControl
175 #define taskDISABLE_INTERRUPTS() portDISABLE_INTERRUPTS()
178 * task. h
180 * Macro to enable microcontroller interrupts.
182 * \page taskENABLE_INTERRUPTS taskENABLE_INTERRUPTS
183 * \ingroup SchedulerControl
185 #define taskENABLE_INTERRUPTS() portENABLE_INTERRUPTS()
187 /* Definitions returned by xTaskGetSchedulerState(). */
188 #define taskSCHEDULER_NOT_STARTED 0
189 #define taskSCHEDULER_RUNNING 1
190 #define taskSCHEDULER_SUSPENDED 2
192 /*-----------------------------------------------------------
193 * TASK CREATION API
194 *----------------------------------------------------------*/
197 * task. h
198 *<pre>
199 portBASE_TYPE xTaskCreate(
200 pdTASK_CODE pvTaskCode,
201 const char * const pcName,
202 unsigned short usStackDepth,
203 void *pvParameters,
204 unsigned portBASE_TYPE uxPriority,
205 xTaskHandle *pvCreatedTask
206 );</pre>
208 * Create a new task and add it to the list of tasks that are ready to run.
210 * xTaskCreate() can only be used to create a task that has unrestricted
211 * access to the entire microcontroller memory map. Systems that include MPU
212 * support can alternatively create an MPU constrained task using
213 * xTaskCreateRestricted().
215 * @param pvTaskCode Pointer to the task entry function. Tasks
216 * must be implemented to never return (i.e. continuous loop).
218 * @param pcName A descriptive name for the task. This is mainly used to
219 * facilitate debugging. Max length defined by tskMAX_TASK_NAME_LEN - default
220 * is 16.
222 * @param usStackDepth The size of the task stack specified as the number of
223 * variables the stack can hold - not the number of bytes. For example, if
224 * the stack is 16 bits wide and usStackDepth is defined as 100, 200 bytes
225 * will be allocated for stack storage.
227 * @param pvParameters Pointer that will be used as the parameter for the task
228 * being created.
230 * @param uxPriority The priority at which the task should run. Systems that
231 * include MPU support can optionally create tasks in a privileged (system)
232 * mode by setting bit portPRIVILEGE_BIT of the priority parameter. For
233 * example, to create a privileged task at priority 2 the uxPriority parameter
234 * should be set to ( 2 | portPRIVILEGE_BIT ).
236 * @param pvCreatedTask Used to pass back a handle by which the created task
237 * can be referenced.
239 * @return pdPASS if the task was successfully created and added to a ready
240 * list, otherwise an error code defined in the file errors. h
242 * Example usage:
243 <pre>
244 // Task to be created.
245 void vTaskCode( void * pvParameters )
247 for( ;; )
249 // Task code goes here.
253 // Function that creates a task.
254 void vOtherFunction( void )
256 static unsigned char ucParameterToPass;
257 xTaskHandle xHandle;
259 // Create the task, storing the handle. Note that the passed parameter ucParameterToPass
260 // must exist for the lifetime of the task, so in this case is declared static. If it was just an
261 // an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
262 // the new task attempts to access it.
263 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
265 // Use the handle to delete the task.
266 vTaskDelete( xHandle );
268 </pre>
269 * \defgroup xTaskCreate xTaskCreate
270 * \ingroup Tasks
272 #define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ) )
275 * task. h
276 *<pre>
277 portBASE_TYPE xTaskCreateRestricted( xTaskParameters *pxTaskDefinition, xTaskHandle *pxCreatedTask );</pre>
279 * xTaskCreateRestricted() should only be used in systems that include an MPU
280 * implementation.
282 * Create a new task and add it to the list of tasks that are ready to run.
283 * The function parameters define the memory regions and associated access
284 * permissions allocated to the task.
286 * @param pxTaskDefinition Pointer to a structure that contains a member
287 * for each of the normal xTaskCreate() parameters (see the xTaskCreate() API
288 * documentation) plus an optional stack buffer and the memory region
289 * definitions.
291 * @param pxCreatedTask Used to pass back a handle by which the created task
292 * can be referenced.
294 * @return pdPASS if the task was successfully created and added to a ready
295 * list, otherwise an error code defined in the file errors. h
297 * Example usage:
298 <pre>
299 // Create an xTaskParameters structure that defines the task to be created.
300 static const xTaskParameters xCheckTaskParameters =
302 vATask, // pvTaskCode - the function that implements the task.
303 "ATask", // pcName - just a text name for the task to assist debugging.
304 100, // usStackDepth - the stack size DEFINED IN WORDS.
305 NULL, // pvParameters - passed into the task function as the function parameters.
306 ( 1UL | portPRIVILEGE_BIT ),// uxPriority - task priority, set the portPRIVILEGE_BIT if the task should run in a privileged state.
307 cStackBuffer,// puxStackBuffer - the buffer to be used as the task stack.
309 // xRegions - Allocate up to three separate memory regions for access by
310 // the task, with appropriate access permissions. Different processors have
311 // different memory alignment requirements - refer to the FreeRTOS documentation
312 // for full information.
314 // Base address Length Parameters
315 { cReadWriteArray, 32, portMPU_REGION_READ_WRITE },
316 { cReadOnlyArray, 32, portMPU_REGION_READ_ONLY },
317 { cPrivilegedOnlyAccessArray, 128, portMPU_REGION_PRIVILEGED_READ_WRITE }
321 int main( void )
323 xTaskHandle xHandle;
325 // Create a task from the const structure defined above. The task handle
326 // is requested (the second parameter is not NULL) but in this case just for
327 // demonstration purposes as its not actually used.
328 xTaskCreateRestricted( &xRegTest1Parameters, &xHandle );
330 // Start the scheduler.
331 vTaskStartScheduler();
333 // Will only get here if there was insufficient memory to create the idle
334 // task.
335 for( ;; );
337 </pre>
338 * \defgroup xTaskCreateRestricted xTaskCreateRestricted
339 * \ingroup Tasks
341 #define xTaskCreateRestricted( x, pxCreatedTask ) xTaskGenericCreate( ((x)->pvTaskCode), ((x)->pcName), ((x)->usStackDepth), ((x)->pvParameters), ((x)->uxPriority), (pxCreatedTask), ((x)->puxStackBuffer), ((x)->xRegions) )
344 * task. h
345 *<pre>
346 void vTaskAllocateMPURegions( xTaskHandle xTask, const xMemoryRegion * const pxRegions );</pre>
348 * Memory regions are assigned to a restricted task when the task is created by
349 * a call to xTaskCreateRestricted(). These regions can be redefined using
350 * vTaskAllocateMPURegions().
352 * @param xTask The handle of the task being updated.
354 * @param xRegions A pointer to an xMemoryRegion structure that contains the
355 * new memory region definitions.
357 * Example usage:
358 <pre>
359 // Define an array of xMemoryRegion structures that configures an MPU region
360 // allowing read/write access for 1024 bytes starting at the beginning of the
361 // ucOneKByte array. The other two of the maximum 3 definable regions are
362 // unused so set to zero.
363 static const xMemoryRegion xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
365 // Base address Length Parameters
366 { ucOneKByte, 1024, portMPU_REGION_READ_WRITE },
367 { 0, 0, 0 },
368 { 0, 0, 0 }
371 void vATask( void *pvParameters )
373 // This task was created such that it has access to certain regions of
374 // memory as defined by the MPU configuration. At some point it is
375 // desired that these MPU regions are replaced with that defined in the
376 // xAltRegions const struct above. Use a call to vTaskAllocateMPURegions()
377 // for this purpose. NULL is used as the task handle to indicate that this
378 // function should modify the MPU regions of the calling task.
379 vTaskAllocateMPURegions( NULL, xAltRegions );
381 // Now the task can continue its function, but from this point on can only
382 // access its stack and the ucOneKByte array (unless any other statically
383 // defined or shared regions have been declared elsewhere).
385 </pre>
386 * \defgroup xTaskCreateRestricted xTaskCreateRestricted
387 * \ingroup Tasks
389 void vTaskAllocateMPURegions( xTaskHandle xTask, const xMemoryRegion * const pxRegions ) PRIVILEGED_FUNCTION;
392 * task. h
393 * <pre>void vTaskDelete( xTaskHandle pxTask );</pre>
395 * INCLUDE_vTaskDelete must be defined as 1 for this function to be available.
396 * See the configuration section for more information.
398 * Remove a task from the RTOS real time kernels management. The task being
399 * deleted will be removed from all ready, blocked, suspended and event lists.
401 * NOTE: The idle task is responsible for freeing the kernel allocated
402 * memory from tasks that have been deleted. It is therefore important that
403 * the idle task is not starved of microcontroller processing time if your
404 * application makes any calls to vTaskDelete (). Memory allocated by the
405 * task code is not automatically freed, and should be freed before the task
406 * is deleted.
408 * See the demo application file death.c for sample code that utilises
409 * vTaskDelete ().
411 * @param pxTask The handle of the task to be deleted. Passing NULL will
412 * cause the calling task to be deleted.
414 * Example usage:
415 <pre>
416 void vOtherFunction( void )
418 xTaskHandle xHandle;
420 // Create the task, storing the handle.
421 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
423 // Use the handle to delete the task.
424 vTaskDelete( xHandle );
426 </pre>
427 * \defgroup vTaskDelete vTaskDelete
428 * \ingroup Tasks
430 void vTaskDelete( xTaskHandle pxTask ) PRIVILEGED_FUNCTION;
433 /*-----------------------------------------------------------
434 * TASK CONTROL API
435 *----------------------------------------------------------*/
438 * task. h
439 * <pre>void vTaskDelay( portTickType xTicksToDelay );</pre>
441 * Delay a task for a given number of ticks. The actual time that the
442 * task remains blocked depends on the tick rate. The constant
443 * portTICK_RATE_MS can be used to calculate real time from the tick
444 * rate - with the resolution of one tick period.
446 * INCLUDE_vTaskDelay must be defined as 1 for this function to be available.
447 * See the configuration section for more information.
450 * vTaskDelay() specifies a time at which the task wishes to unblock relative to
451 * the time at which vTaskDelay() is called. For example, specifying a block
452 * period of 100 ticks will cause the task to unblock 100 ticks after
453 * vTaskDelay() is called. vTaskDelay() does not therefore provide a good method
454 * of controlling the frequency of a cyclical task as the path taken through the
455 * code, as well as other task and interrupt activity, will effect the frequency
456 * at which vTaskDelay() gets called and therefore the time at which the task
457 * next executes. See vTaskDelayUntil() for an alternative API function designed
458 * to facilitate fixed frequency execution. It does this by specifying an
459 * absolute time (rather than a relative time) at which the calling task should
460 * unblock.
462 * @param xTicksToDelay The amount of time, in tick periods, that
463 * the calling task should block.
465 * Example usage:
467 void vTaskFunction( void * pvParameters )
469 void vTaskFunction( void * pvParameters )
471 // Block for 500ms.
472 const portTickType xDelay = 500 / portTICK_RATE_MS;
474 for( ;; )
476 // Simply toggle the LED every 500ms, blocking between each toggle.
477 vToggleLED();
478 vTaskDelay( xDelay );
482 * \defgroup vTaskDelay vTaskDelay
483 * \ingroup TaskCtrl
485 void vTaskDelay( portTickType xTicksToDelay ) PRIVILEGED_FUNCTION;
488 * task. h
489 * <pre>void vTaskDelayUntil( portTickType *pxPreviousWakeTime, portTickType xTimeIncrement );</pre>
491 * INCLUDE_vTaskDelayUntil must be defined as 1 for this function to be available.
492 * See the configuration section for more information.
494 * Delay a task until a specified time. This function can be used by cyclical
495 * tasks to ensure a constant execution frequency.
497 * This function differs from vTaskDelay () in one important aspect: vTaskDelay () will
498 * cause a task to block for the specified number of ticks from the time vTaskDelay () is
499 * called. It is therefore difficult to use vTaskDelay () by itself to generate a fixed
500 * execution frequency as the time between a task starting to execute and that task
501 * calling vTaskDelay () may not be fixed [the task may take a different path though the
502 * code between calls, or may get interrupted or preempted a different number of times
503 * each time it executes].
505 * Whereas vTaskDelay () specifies a wake time relative to the time at which the function
506 * is called, vTaskDelayUntil () specifies the absolute (exact) time at which it wishes to
507 * unblock.
509 * The constant portTICK_RATE_MS can be used to calculate real time from the tick
510 * rate - with the resolution of one tick period.
512 * @param pxPreviousWakeTime Pointer to a variable that holds the time at which the
513 * task was last unblocked. The variable must be initialised with the current time
514 * prior to its first use (see the example below). Following this the variable is
515 * automatically updated within vTaskDelayUntil ().
517 * @param xTimeIncrement The cycle time period. The task will be unblocked at
518 * time *pxPreviousWakeTime + xTimeIncrement. Calling vTaskDelayUntil with the
519 * same xTimeIncrement parameter value will cause the task to execute with
520 * a fixed interface period.
522 * Example usage:
523 <pre>
524 // Perform an action every 10 ticks.
525 void vTaskFunction( void * pvParameters )
527 portTickType xLastWakeTime;
528 const portTickType xFrequency = 10;
530 // Initialise the xLastWakeTime variable with the current time.
531 xLastWakeTime = xTaskGetTickCount ();
532 for( ;; )
534 // Wait for the next cycle.
535 vTaskDelayUntil( &xLastWakeTime, xFrequency );
537 // Perform action here.
540 </pre>
541 * \defgroup vTaskDelayUntil vTaskDelayUntil
542 * \ingroup TaskCtrl
544 void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement ) PRIVILEGED_FUNCTION;
547 * task. h
548 * <pre>unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask );</pre>
550 * INCLUDE_xTaskPriorityGet must be defined as 1 for this function to be available.
551 * See the configuration section for more information.
553 * Obtain the priority of any task.
555 * @param pxTask Handle of the task to be queried. Passing a NULL
556 * handle results in the priority of the calling task being returned.
558 * @return The priority of pxTask.
560 * Example usage:
561 <pre>
562 void vAFunction( void )
564 xTaskHandle xHandle;
566 // Create a task, storing the handle.
567 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
569 // ...
571 // Use the handle to obtain the priority of the created task.
572 // It was created with tskIDLE_PRIORITY, but may have changed
573 // it itself.
574 if( uxTaskPriorityGet( xHandle ) != tskIDLE_PRIORITY )
576 // The task has changed it's priority.
579 // ...
581 // Is our priority higher than the created task?
582 if( uxTaskPriorityGet( xHandle ) < uxTaskPriorityGet( NULL ) )
584 // Our priority (obtained using NULL handle) is higher.
587 </pre>
588 * \defgroup uxTaskPriorityGet uxTaskPriorityGet
589 * \ingroup TaskCtrl
591 unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask ) PRIVILEGED_FUNCTION;
594 * task. h
595 * <pre>void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority );</pre>
597 * INCLUDE_vTaskPrioritySet must be defined as 1 for this function to be available.
598 * See the configuration section for more information.
600 * Set the priority of any task.
602 * A context switch will occur before the function returns if the priority
603 * being set is higher than the currently executing task.
605 * @param pxTask Handle to the task for which the priority is being set.
606 * Passing a NULL handle results in the priority of the calling task being set.
608 * @param uxNewPriority The priority to which the task will be set.
610 * Example usage:
611 <pre>
612 void vAFunction( void )
614 xTaskHandle xHandle;
616 // Create a task, storing the handle.
617 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
619 // ...
621 // Use the handle to raise the priority of the created task.
622 vTaskPrioritySet( xHandle, tskIDLE_PRIORITY + 1 );
624 // ...
626 // Use a NULL handle to raise our priority to the same value.
627 vTaskPrioritySet( NULL, tskIDLE_PRIORITY + 1 );
629 </pre>
630 * \defgroup vTaskPrioritySet vTaskPrioritySet
631 * \ingroup TaskCtrl
633 void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority ) PRIVILEGED_FUNCTION;
636 * task. h
637 * <pre>void vTaskSuspend( xTaskHandle pxTaskToSuspend );</pre>
639 * INCLUDE_vTaskSuspend must be defined as 1 for this function to be available.
640 * See the configuration section for more information.
642 * Suspend any task. When suspended a task will never get any microcontroller
643 * processing time, no matter what its priority.
645 * Calls to vTaskSuspend are not accumulative -
646 * i.e. calling vTaskSuspend () twice on the same task still only requires one
647 * call to vTaskResume () to ready the suspended task.
649 * @param pxTaskToSuspend Handle to the task being suspended. Passing a NULL
650 * handle will cause the calling task to be suspended.
652 * Example usage:
653 <pre>
654 void vAFunction( void )
656 xTaskHandle xHandle;
658 // Create a task, storing the handle.
659 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
661 // ...
663 // Use the handle to suspend the created task.
664 vTaskSuspend( xHandle );
666 // ...
668 // The created task will not run during this period, unless
669 // another task calls vTaskResume( xHandle ).
671 //...
674 // Suspend ourselves.
675 vTaskSuspend( NULL );
677 // We cannot get here unless another task calls vTaskResume
678 // with our handle as the parameter.
680 </pre>
681 * \defgroup vTaskSuspend vTaskSuspend
682 * \ingroup TaskCtrl
684 void vTaskSuspend( xTaskHandle pxTaskToSuspend ) PRIVILEGED_FUNCTION;
687 * task. h
688 * <pre>void vTaskResume( xTaskHandle pxTaskToResume );</pre>
690 * INCLUDE_vTaskSuspend must be defined as 1 for this function to be available.
691 * See the configuration section for more information.
693 * Resumes a suspended task.
695 * A task that has been suspended by one of more calls to vTaskSuspend ()
696 * will be made available for running again by a single call to
697 * vTaskResume ().
699 * @param pxTaskToResume Handle to the task being readied.
701 * Example usage:
702 <pre>
703 void vAFunction( void )
705 xTaskHandle xHandle;
707 // Create a task, storing the handle.
708 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
710 // ...
712 // Use the handle to suspend the created task.
713 vTaskSuspend( xHandle );
715 // ...
717 // The created task will not run during this period, unless
718 // another task calls vTaskResume( xHandle ).
720 //...
723 // Resume the suspended task ourselves.
724 vTaskResume( xHandle );
726 // The created task will once again get microcontroller processing
727 // time in accordance with it priority within the system.
729 </pre>
730 * \defgroup vTaskResume vTaskResume
731 * \ingroup TaskCtrl
733 void vTaskResume( xTaskHandle pxTaskToResume ) PRIVILEGED_FUNCTION;
736 * task. h
737 * <pre>void xTaskResumeFromISR( xTaskHandle pxTaskToResume );</pre>
739 * INCLUDE_xTaskResumeFromISR must be defined as 1 for this function to be
740 * available. See the configuration section for more information.
742 * An implementation of vTaskResume() that can be called from within an ISR.
744 * A task that has been suspended by one of more calls to vTaskSuspend ()
745 * will be made available for running again by a single call to
746 * xTaskResumeFromISR ().
748 * @param pxTaskToResume Handle to the task being readied.
750 * \defgroup vTaskResumeFromISR vTaskResumeFromISR
751 * \ingroup TaskCtrl
753 portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume ) PRIVILEGED_FUNCTION;
755 /*-----------------------------------------------------------
756 * SCHEDULER CONTROL
757 *----------------------------------------------------------*/
760 * task. h
761 * <pre>void vTaskStartScheduler( void );</pre>
763 * Starts the real time kernel tick processing. After calling the kernel
764 * has control over which tasks are executed and when. This function
765 * does not return until an executing task calls vTaskEndScheduler ().
767 * At least one task should be created via a call to xTaskCreate ()
768 * before calling vTaskStartScheduler (). The idle task is created
769 * automatically when the first application task is created.
771 * See the demo application file main.c for an example of creating
772 * tasks and starting the kernel.
774 * Example usage:
775 <pre>
776 void vAFunction( void )
778 // Create at least one task before starting the kernel.
779 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
781 // Start the real time kernel with preemption.
782 vTaskStartScheduler ();
784 // Will not get here unless a task calls vTaskEndScheduler ()
786 </pre>
788 * \defgroup vTaskStartScheduler vTaskStartScheduler
789 * \ingroup SchedulerControl
791 void vTaskStartScheduler( void ) PRIVILEGED_FUNCTION;
794 * task. h
795 * <pre>void vTaskEndScheduler( void );</pre>
797 * Stops the real time kernel tick. All created tasks will be automatically
798 * deleted and multitasking (either preemptive or cooperative) will
799 * stop. Execution then resumes from the point where vTaskStartScheduler ()
800 * was called, as if vTaskStartScheduler () had just returned.
802 * See the demo application file main. c in the demo/PC directory for an
803 * example that uses vTaskEndScheduler ().
805 * vTaskEndScheduler () requires an exit function to be defined within the
806 * portable layer (see vPortEndScheduler () in port. c for the PC port). This
807 * performs hardware specific operations such as stopping the kernel tick.
809 * vTaskEndScheduler () will cause all of the resources allocated by the
810 * kernel to be freed - but will not free resources allocated by application
811 * tasks.
813 * Example usage:
814 <pre>
815 void vTaskCode( void * pvParameters )
817 for( ;; )
819 // Task code goes here.
821 // At some point we want to end the real time kernel processing
822 // so call ...
823 vTaskEndScheduler ();
827 void vAFunction( void )
829 // Create at least one task before starting the kernel.
830 xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
832 // Start the real time kernel with preemption.
833 vTaskStartScheduler ();
835 // Will only get here when the vTaskCode () task has called
836 // vTaskEndScheduler (). When we get here we are back to single task
837 // execution.
839 </pre>
841 * \defgroup vTaskEndScheduler vTaskEndScheduler
842 * \ingroup SchedulerControl
844 void vTaskEndScheduler( void ) PRIVILEGED_FUNCTION;
847 * task. h
848 * <pre>void vTaskSuspendAll( void );</pre>
850 * Suspends all real time kernel activity while keeping interrupts (including the
851 * kernel tick) enabled.
853 * After calling vTaskSuspendAll () the calling task will continue to execute
854 * without risk of being swapped out until a call to xTaskResumeAll () has been
855 * made.
857 * API functions that have the potential to cause a context switch (for example,
858 * vTaskDelayUntil(), xQueueSend(), etc.) must not be called while the scheduler
859 * is suspended.
861 * Example usage:
862 <pre>
863 void vTask1( void * pvParameters )
865 for( ;; )
867 // Task code goes here.
869 // ...
871 // At some point the task wants to perform a long operation during
872 // which it does not want to get swapped out. It cannot use
873 // taskENTER_CRITICAL ()/taskEXIT_CRITICAL () as the length of the
874 // operation may cause interrupts to be missed - including the
875 // ticks.
877 // Prevent the real time kernel swapping out the task.
878 vTaskSuspendAll ();
880 // Perform the operation here. There is no need to use critical
881 // sections as we have all the microcontroller processing time.
882 // During this time interrupts will still operate and the kernel
883 // tick count will be maintained.
885 // ...
887 // The operation is complete. Restart the kernel.
888 xTaskResumeAll ();
891 </pre>
892 * \defgroup vTaskSuspendAll vTaskSuspendAll
893 * \ingroup SchedulerControl
895 void vTaskSuspendAll( void ) PRIVILEGED_FUNCTION;
898 * task. h
899 * <pre>char xTaskResumeAll( void );</pre>
901 * Resumes real time kernel activity following a call to vTaskSuspendAll ().
902 * After a call to vTaskSuspendAll () the kernel will take control of which
903 * task is executing at any time.
905 * @return If resuming the scheduler caused a context switch then pdTRUE is
906 * returned, otherwise pdFALSE is returned.
908 * Example usage:
909 <pre>
910 void vTask1( void * pvParameters )
912 for( ;; )
914 // Task code goes here.
916 // ...
918 // At some point the task wants to perform a long operation during
919 // which it does not want to get swapped out. It cannot use
920 // taskENTER_CRITICAL ()/taskEXIT_CRITICAL () as the length of the
921 // operation may cause interrupts to be missed - including the
922 // ticks.
924 // Prevent the real time kernel swapping out the task.
925 vTaskSuspendAll ();
927 // Perform the operation here. There is no need to use critical
928 // sections as we have all the microcontroller processing time.
929 // During this time interrupts will still operate and the real
930 // time kernel tick count will be maintained.
932 // ...
934 // The operation is complete. Restart the kernel. We want to force
935 // a context switch - but there is no point if resuming the scheduler
936 // caused a context switch already.
937 if( !xTaskResumeAll () )
939 taskYIELD ();
943 </pre>
944 * \defgroup xTaskResumeAll xTaskResumeAll
945 * \ingroup SchedulerControl
947 signed portBASE_TYPE xTaskResumeAll( void ) PRIVILEGED_FUNCTION;
950 * task. h
951 * <pre>signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask );</pre>
953 * Utility task that simply returns pdTRUE if the task referenced by xTask is
954 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
955 * is in any other state.
958 signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
960 /*-----------------------------------------------------------
961 * TASK UTILITIES
962 *----------------------------------------------------------*/
965 * task. h
966 * <PRE>volatile portTickType xTaskGetTickCount( void );</PRE>
968 * @return The count of ticks since vTaskStartScheduler was called.
970 * \page xTaskGetTickCount xTaskGetTickCount
971 * \ingroup TaskUtils
973 portTickType xTaskGetTickCount( void ) PRIVILEGED_FUNCTION;
976 * task. h
977 * <PRE>unsigned short uxTaskGetNumberOfTasks( void );</PRE>
979 * @return The number of tasks that the real time kernel is currently managing.
980 * This includes all ready, blocked and suspended tasks. A task that
981 * has been deleted but not yet freed by the idle task will also be
982 * included in the count.
984 * \page uxTaskGetNumberOfTasks uxTaskGetNumberOfTasks
985 * \ingroup TaskUtils
987 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void ) PRIVILEGED_FUNCTION;
990 * task. h
991 * <PRE>void vTaskList( char *pcWriteBuffer );</PRE>
993 * configUSE_TRACE_FACILITY must be defined as 1 for this function to be
994 * available. See the configuration section for more information.
996 * NOTE: This function will disable interrupts for its duration. It is
997 * not intended for normal application runtime use but as a debug aid.
999 * Lists all the current tasks, along with their current state and stack
1000 * usage high water mark.
1002 * Tasks are reported as blocked ('B'), ready ('R'), deleted ('D') or
1003 * suspended ('S').
1005 * @param pcWriteBuffer A buffer into which the above mentioned details
1006 * will be written, in ascii form. This buffer is assumed to be large
1007 * enough to contain the generated report. Approximately 40 bytes per
1008 * task should be sufficient.
1010 * \page vTaskList vTaskList
1011 * \ingroup TaskUtils
1013 void vTaskList( signed char *pcWriteBuffer ) PRIVILEGED_FUNCTION;
1016 * task. h
1017 * <PRE>void vTaskGetRunTimeStats( char *pcWriteBuffer );</PRE>
1019 * configGENERATE_RUN_TIME_STATS must be defined as 1 for this function
1020 * to be available. The application must also then provide definitions
1021 * for portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() and
1022 * portGET_RUN_TIME_COUNTER_VALUE to configure a peripheral timer/counter
1023 * and return the timers current count value respectively. The counter
1024 * should be at least 10 times the frequency of the tick count.
1026 * NOTE: This function will disable interrupts for its duration. It is
1027 * not intended for normal application runtime use but as a debug aid.
1029 * Setting configGENERATE_RUN_TIME_STATS to 1 will result in a total
1030 * accumulated execution time being stored for each task. The resolution
1031 * of the accumulated time value depends on the frequency of the timer
1032 * configured by the portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() macro.
1033 * Calling vTaskGetRunTimeStats() writes the total execution time of each
1034 * task into a buffer, both as an absolute count value and as a percentage
1035 * of the total system execution time.
1037 * @param pcWriteBuffer A buffer into which the execution times will be
1038 * written, in ascii form. This buffer is assumed to be large enough to
1039 * contain the generated report. Approximately 40 bytes per task should
1040 * be sufficient.
1042 * \page vTaskGetRunTimeStats vTaskGetRunTimeStats
1043 * \ingroup TaskUtils
1045 void vTaskGetRunTimeStats( signed char *pcWriteBuffer ) PRIVILEGED_FUNCTION;
1048 * task. h
1049 * <PRE>void vTaskStartTrace( char * pcBuffer, unsigned portBASE_TYPE uxBufferSize );</PRE>
1051 * Starts a real time kernel activity trace. The trace logs the identity of
1052 * which task is running when.
1054 * The trace file is stored in binary format. A separate DOS utility called
1055 * convtrce.exe is used to convert this into a tab delimited text file which
1056 * can be viewed and plotted in a spread sheet.
1058 * @param pcBuffer The buffer into which the trace will be written.
1060 * @param ulBufferSize The size of pcBuffer in bytes. The trace will continue
1061 * until either the buffer in full, or ulTaskEndTrace () is called.
1063 * \page vTaskStartTrace vTaskStartTrace
1064 * \ingroup TaskUtils
1066 void vTaskStartTrace( signed char * pcBuffer, unsigned long ulBufferSize ) PRIVILEGED_FUNCTION;
1069 * task. h
1070 * <PRE>unsigned long ulTaskEndTrace( void );</PRE>
1072 * Stops a kernel activity trace. See vTaskStartTrace ().
1074 * @return The number of bytes that have been written into the trace buffer.
1076 * \page usTaskEndTrace usTaskEndTrace
1077 * \ingroup TaskUtils
1079 unsigned long ulTaskEndTrace( void ) PRIVILEGED_FUNCTION;
1082 * task.h
1083 * <PRE>unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask );</PRE>
1085 * INCLUDE_uxTaskGetStackHighWaterMark must be set to 1 in FreeRTOSConfig.h for
1086 * this function to be available.
1088 * Returns the high water mark of the stack associated with xTask. That is,
1089 * the minimum free stack space there has been (in bytes) since the task
1090 * started. The smaller the returned number the closer the task has come
1091 * to overflowing its stack.
1093 * @param xTask Handle of the task associated with the stack to be checked.
1094 * Set xTask to NULL to check the stack of the calling task.
1096 * @return The smallest amount of free stack space there has been (in bytes)
1097 * since the task referenced by xTask was created.
1099 unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
1102 * task.h
1103 * <pre>void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction );</pre>
1105 * Sets pxHookFunction to be the task hook function used by the task xTask.
1106 * Passing xTask as NULL has the effect of setting the calling tasks hook
1107 * function.
1109 void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction ) PRIVILEGED_FUNCTION;
1112 * task.h
1113 * <pre>void xTaskGetApplicationTaskTag( xTaskHandle xTask );</pre>
1115 * Returns the pxHookFunction value assigned to the task xTask.
1117 pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask ) PRIVILEGED_FUNCTION;
1120 * task.h
1121 * <pre>portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction );</pre>
1123 * Calls the hook function associated with xTask. Passing xTask as NULL has
1124 * the effect of calling the Running tasks (the calling task) hook function.
1126 * pvParameter is passed to the hook function for the task to interpret as it
1127 * wants.
1129 portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter ) PRIVILEGED_FUNCTION;
1132 /*-----------------------------------------------------------
1133 * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
1134 *----------------------------------------------------------*/
1137 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
1138 * INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
1139 * AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1141 * Called from the real time kernel tick (either preemptive or cooperative),
1142 * this increments the tick count and checks if any tasks that are blocked
1143 * for a finite period required removing from a blocked list and placing on
1144 * a ready list.
1146 void vTaskIncrementTick( void ) PRIVILEGED_FUNCTION;
1149 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
1150 * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1152 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
1154 * Removes the calling task from the ready list and places it both
1155 * on the list of tasks waiting for a particular event, and the
1156 * list of delayed tasks. The task will be removed from both lists
1157 * and replaced on the ready list should either the event occur (and
1158 * there be no higher priority tasks waiting on the same event) or
1159 * the delay period expires.
1161 * @param pxEventList The list containing tasks that are blocked waiting
1162 * for the event to occur.
1164 * @param xTicksToWait The maximum amount of time that the task should wait
1165 * for the event to occur. This is specified in kernel ticks,the constant
1166 * portTICK_RATE_MS can be used to convert kernel ticks into a real time
1167 * period.
1169 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
1172 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
1173 * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1175 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
1177 * Removes a task from both the specified event list and the list of blocked
1178 * tasks, and places it on a ready queue.
1180 * xTaskRemoveFromEventList () will be called if either an event occurs to
1181 * unblock a task, or the block timeout period expires.
1183 * @return pdTRUE if the task being removed has a higher priority than the task
1184 * making the call, otherwise pdFALSE.
1186 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList ) PRIVILEGED_FUNCTION;
1189 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS AN
1190 * INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1192 * INCLUDE_vTaskCleanUpResources and INCLUDE_vTaskSuspend must be defined as 1
1193 * for this function to be available.
1194 * See the configuration section for more information.
1196 * Empties the ready and delayed queues of task control blocks, freeing the
1197 * memory allocated for the task control block and task stacks as it goes.
1199 void vTaskCleanUpResources( void ) PRIVILEGED_FUNCTION;
1202 * THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
1203 * INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
1204 * AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
1206 * Sets the pointer to the current TCB to the TCB of the highest priority task
1207 * that is ready to run.
1209 void vTaskSwitchContext( void ) PRIVILEGED_FUNCTION;
1212 * Return the handle of the calling task.
1214 xTaskHandle xTaskGetCurrentTaskHandle( void ) PRIVILEGED_FUNCTION;
1217 * Capture the current time status for future reference.
1219 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut ) PRIVILEGED_FUNCTION;
1222 * Compare the time status now with that previously captured to see if the
1223 * timeout has expired.
1225 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait ) PRIVILEGED_FUNCTION;
1228 * Shortcut used by the queue implementation to prevent unnecessary call to
1229 * taskYIELD();
1231 void vTaskMissedYield( void ) PRIVILEGED_FUNCTION;
1234 * Returns the scheduler state as taskSCHEDULER_RUNNING,
1235 * taskSCHEDULER_NOT_STARTED or taskSCHEDULER_SUSPENDED.
1237 portBASE_TYPE xTaskGetSchedulerState( void ) PRIVILEGED_FUNCTION;
1240 * Raises the priority of the mutex holder to that of the calling task should
1241 * the mutex holder have a priority less than the calling task.
1243 void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder ) PRIVILEGED_FUNCTION;
1246 * Set the priority of a task back to its proper priority in the case that it
1247 * inherited a higher priority while it was holding a semaphore.
1249 void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder ) PRIVILEGED_FUNCTION;
1252 * Generic version of the task creation function which is in turn called by the
1253 * xTaskCreate() and xTaskCreateRestricted() macros.
1255 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;
1257 #ifdef __cplusplus
1259 #endif
1260 #endif /* TASK_H */