blob: c48db49ab26334f5f9a2263b120cc4f91b879c38 [file] [log] [blame]
Qiufang Dai35c31332020-05-13 15:29:06 +08001/*
2 * FreeRTOS Kernel V10.0.1
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
24 *
25 * 1 tab == 4 spaces!
26 */
27
28/* Standard includes. */
29#include <stdlib.h>
30#include <string.h>
31
32/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
33all the API functions to use the MPU wrappers. That should only be done when
34task.h is included from an application file. */
35#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
36
37/* FreeRTOS includes. */
38#include "FreeRTOS.h"
39#include "task.h"
40#include "timers.h"
41#include "stack_macros.h"
42
43/* Lint e961 and e750 are suppressed as a MISRA exception justified because the
44MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
45header files above, but not in this file, in order to generate the correct
46privileged Vs unprivileged linkage and placement. */
47#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
48
49/* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
50functions but without including stdio.h here. */
51#if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
52 /* At the bottom of this file are two optional functions that can be used
53 to generate human readable text from the raw data generated by the
54 uxTaskGetSystemState() function. Note the formatting functions are provided
55 for convenience only, and are NOT considered part of the kernel. */
56 #include <stdio.h>
57#endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
58
59#if( configUSE_PREEMPTION == 0 )
60 /* If the cooperative scheduler is being used then a yield should not be
61 performed just because a higher priority task has been woken. */
62 #define taskYIELD_IF_USING_PREEMPTION()
63#else
64 #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
65#endif
66
67/* Values that can be assigned to the ucNotifyState member of the TCB. */
68#define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 )
69#define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
70#define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
71
72/*
73 * The value used to fill the stack of a task when the task is created. This
74 * is used purely for checking the high water mark for tasks.
75 */
76#define tskSTACK_FILL_BYTE ( 0xa5U )
77
78/* Sometimes the FreeRTOSConfig.h settings only allow a task to be created using
79dynamically allocated RAM, in which case when any task is deleted it is known
80that both the task's stack and TCB need to be freed. Sometimes the
81FreeRTOSConfig.h settings only allow a task to be created using statically
82allocated RAM, in which case when any task is deleted it is known that neither
83the task's stack or TCB should be freed. Sometimes the FreeRTOSConfig.h
84settings allow a task to be created using either statically or dynamically
85allocated RAM, in which case a member of the TCB is used to record whether the
86stack and/or TCB were allocated statically or dynamically, so when a task is
87deleted the RAM that was allocated dynamically is freed again and no attempt is
88made to free the RAM that was allocated statically.
89tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE is only true if it is possible for a
90task to be created using either statically or dynamically allocated RAM. Note
91that if portUSING_MPU_WRAPPERS is 1 then a protected task can be created with
92a statically allocated stack and a dynamically allocated TCB.
93!!!NOTE!!! If the definition of tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE is
94changed then the definition of StaticTask_t must also be updated. */
95#define tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
96#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
97#define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
98#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
99
100/* If any of the following are set then task stacks are filled with a known
101value so the high water mark can be determined. If none of the following are
102set then don't fill the stack so there is no unnecessary dependency on memset. */
103#if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
104 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
105#else
106 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
107#endif
108
109/*
110 * Macros used by vListTask to indicate which state a task is in.
111 */
112#define tskRUNNING_CHAR ( 'X' )
113#define tskBLOCKED_CHAR ( 'B' )
114#define tskREADY_CHAR ( 'R' )
115#define tskDELETED_CHAR ( 'D' )
116#define tskSUSPENDED_CHAR ( 'S' )
117
118/*
119 * Some kernel aware debuggers require the data the debugger needs access to be
120 * global, rather than file scope.
121 */
122#ifdef portREMOVE_STATIC_QUALIFIER
123 #define static
124#endif
125
126/* The name allocated to the Idle task. This can be overridden by defining
127configIDLE_TASK_NAME in FreeRTOSConfig.h. */
128#ifndef configIDLE_TASK_NAME
129 #define configIDLE_TASK_NAME "IDLE"
130#endif
131
132#if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
133
134 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
135 performed in a generic way that is not optimised to any particular
136 microcontroller architecture. */
137
138 /* uxTopReadyPriority holds the priority of the highest priority ready
139 state task. */
140 #define taskRECORD_READY_PRIORITY( uxPriority ) \
141 { \
142 if( ( uxPriority ) > uxTopReadyPriority ) \
143 { \
144 uxTopReadyPriority = ( uxPriority ); \
145 } \
146 } /* taskRECORD_READY_PRIORITY */
147
148 /*-----------------------------------------------------------*/
149
150 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
151 { \
152 UBaseType_t uxTopPriority = uxTopReadyPriority; \
153 \
154 /* Find the highest priority queue that contains ready tasks. */ \
155 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
156 { \
157 configASSERT( uxTopPriority ); \
158 --uxTopPriority; \
159 } \
160 \
161 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
162 the same priority get an equal share of the processor time. */ \
163 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
164 uxTopReadyPriority = uxTopPriority; \
165 } /* taskSELECT_HIGHEST_PRIORITY_TASK */
166
167 /*-----------------------------------------------------------*/
168
169 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
170 they are only required when a port optimised method of task selection is
171 being used. */
172 #define taskRESET_READY_PRIORITY( uxPriority )
173 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
174
175#else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
176
177 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
178 performed in a way that is tailored to the particular microcontroller
179 architecture being used. */
180
181 /* A port optimised version is provided. Call the port defined macros. */
182 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
183
184 /*-----------------------------------------------------------*/
185
186 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
187 { \
188 UBaseType_t uxTopPriority; \
189 \
190 /* Find the highest priority list that contains ready tasks. */ \
191 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
192 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
193 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
194 } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
195
196 /*-----------------------------------------------------------*/
197
198 /* A port optimised version is provided, call it only if the TCB being reset
199 is being referenced from a ready list. If it is referenced from a delayed
200 or suspended list then it won't be in a ready list. */
201 #define taskRESET_READY_PRIORITY( uxPriority ) \
202 { \
203 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
204 { \
205 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
206 } \
207 }
208
209#endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
210
211/*-----------------------------------------------------------*/
212
213/* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
214count overflows. */
215#define taskSWITCH_DELAYED_LISTS() \
216{ \
217 List_t *pxTemp; \
218 \
219 /* The delayed tasks list should be empty when the lists are switched. */ \
220 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
221 \
222 pxTemp = pxDelayedTaskList; \
223 pxDelayedTaskList = pxOverflowDelayedTaskList; \
224 pxOverflowDelayedTaskList = pxTemp; \
225 xNumOfOverflows++; \
226 prvResetNextTaskUnblockTime(); \
227}
228
229/*-----------------------------------------------------------*/
230
231/*
232 * Place the task represented by pxTCB into the appropriate ready list for
233 * the task. It is inserted at the end of the list.
234 */
235#define prvAddTaskToReadyList( pxTCB ) \
236 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
237 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
238 vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
239 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
240/*-----------------------------------------------------------*/
241
242/*
243 * Several functions take an TaskHandle_t parameter that can optionally be NULL,
244 * where NULL is used to indicate that the handle of the currently executing
245 * task should be used in place of the parameter. This macro simply checks to
246 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
247 */
248#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( TCB_t * ) pxCurrentTCB : ( TCB_t * ) ( pxHandle ) )
249
250/* The item value of the event list item is normally used to hold the priority
251of the task to which it belongs (coded to allow it to be held in reverse
252priority order). However, it is occasionally borrowed for other purposes. It
253is important its value is not updated due to a task priority change while it is
254being used for another purpose. The following bit definition is used to inform
255the scheduler that the value should not be changed - in which case it is the
256responsibility of whichever module is using the value to ensure it gets set back
257to its original value when it is released. */
258#if( configUSE_16_BIT_TICKS == 1 )
259 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
260#else
261 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
262#endif
263
264/*
265 * Task control block. A task control block (TCB) is allocated for each task,
266 * and stores task state information, including a pointer to the task's context
267 * (the task's run time environment, including register values)
268 */
269typedef struct tskTaskControlBlock
270{
271 volatile StackType_t *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
272
273 #if ( portUSING_MPU_WRAPPERS == 1 )
274 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
275 #endif
276
277 ListItem_t xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
278 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
279 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
280 StackType_t *pxStack; /*< Points to the start of the stack. */
Jianxiong Pan27d913f2020-08-03 15:22:00 +0800281 StackType_t uStackDepth;
Qiufang Dai35c31332020-05-13 15:29:06 +0800282 char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
283
284 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
285 StackType_t *pxEndOfStack; /*< Points to the highest valid address for the stack. */
286 #endif
287
288 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
289 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
290 #endif
291
292 #if ( configUSE_TRACE_FACILITY == 1 )
293 UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
294 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
295 #endif
296
297 #if ( configUSE_MUTEXES == 1 )
298 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
299 UBaseType_t uxMutexesHeld;
300 #endif
301
302 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
303 TaskHookFunction_t pxTaskTag;
304 #endif
305
306 #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
307 void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
308 #endif
309
310 #if( configGENERATE_RUN_TIME_STATS == 1 )
311 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
312 #endif
313
314 #if ( configUSE_NEWLIB_REENTRANT == 1 )
315 /* Allocate a Newlib reent structure that is specific to this task.
316 Note Newlib support has been included by popular demand, but is not
317 used by the FreeRTOS maintainers themselves. FreeRTOS is not
318 responsible for resulting newlib operation. User must be familiar with
319 newlib and must provide system-wide implementations of the necessary
320 stubs. Be warned that (at the time of writing) the current newlib design
321 implements a system-wide malloc() that must be provided with locks. */
322 struct _reent xNewLib_reent;
323 #endif
324
325 #if( configUSE_TASK_NOTIFICATIONS == 1 )
326 volatile uint32_t ulNotifiedValue;
327 volatile uint8_t ucNotifyState;
328 #endif
329
330 /* See the comments above the definition of
331 tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
332 #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 Macro has been consolidated for readability reasons. */
333 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
334 #endif
335
336 #if( INCLUDE_xTaskAbortDelay == 1 )
337 uint8_t ucDelayAborted;
338 #endif
339
340} tskTCB;
341
342/* The old tskTCB name is maintained above then typedefed to the new TCB_t name
343below to enable the use of older kernel aware debuggers. */
344typedef tskTCB TCB_t;
345
346/*lint -save -e956 A manual analysis and inspection has been used to determine
347which static variables must be declared volatile. */
348
349PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
350
351/* Lists for ready and blocked tasks. --------------------*/
352PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ];/*< Prioritised ready tasks. */
353PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
354PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
355PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
356PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
357PRIVILEGED_DATA static List_t xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
358
359#if( INCLUDE_vTaskDelete == 1 )
360
361 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
362 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
363
364#endif
365
366#if ( INCLUDE_vTaskSuspend == 1 )
367
368 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
369
370#endif
371
372/* Other file private variables. --------------------------------*/
373PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
374PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
375PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
376PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
377PRIVILEGED_DATA static volatile UBaseType_t uxPendedTicks = ( UBaseType_t ) 0U;
378PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdFALSE;
379PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
380PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
381PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
382PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle = NULL; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
383
384/* Context switches are held pending while the scheduler is suspended. Also,
385interrupts must not manipulate the xStateListItem of a TCB, or any of the
386lists the xStateListItem can be referenced from, if the scheduler is suspended.
387If an interrupt needs to unblock a task while the scheduler is suspended then it
388moves the task's event list item into the xPendingReadyList, ready for the
389kernel to move the task from the pending ready list into the real ready list
390when the scheduler is unsuspended. The pending ready list itself can only be
391accessed from a critical section. */
392PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
393
394#if ( configGENERATE_RUN_TIME_STATS == 1 )
395
396 PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
397 PRIVILEGED_DATA static uint32_t ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
398
399#endif
400
401/*lint -restore */
402
403/*-----------------------------------------------------------*/
404
405/* Callback function prototypes. --------------------------*/
406#if( configCHECK_FOR_STACK_OVERFLOW > 0 )
407
408 extern void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName );
409
410#endif
411
412#if( configUSE_TICK_HOOK > 0 )
413
414 extern void vApplicationTickHook( void );
415
416#endif
417
418#if( configSUPPORT_STATIC_ALLOCATION == 1 )
419
420 extern void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
421
422#endif
423
424/* File private functions. --------------------------------*/
425
426/**
427 * Utility task that simply returns pdTRUE if the task referenced by xTask is
428 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
429 * is in any other state.
430 */
431#if ( INCLUDE_vTaskSuspend == 1 )
432
433 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
434
435#endif /* INCLUDE_vTaskSuspend */
436
437/*
438 * Utility to ready all the lists used by the scheduler. This is called
439 * automatically upon the creation of the first task.
440 */
441static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
442
443/*
444 * The idle task, which as all tasks is implemented as a never ending loop.
445 * The idle task is automatically created and added to the ready lists upon
446 * creation of the first user task.
447 *
448 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
449 * language extensions. The equivalent prototype for this function is:
450 *
451 * void prvIdleTask( void *pvParameters );
452 *
453 */
454static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
455
456/*
457 * Utility to free all memory allocated by the scheduler to hold a TCB,
458 * including the stack pointed to by the TCB.
459 *
460 * This does not free memory allocated by the task itself (i.e. memory
461 * allocated by calls to pvPortMalloc from within the tasks application code).
462 */
463#if ( INCLUDE_vTaskDelete == 1 )
464
465 static void prvDeleteTCB( TCB_t *pxTCB ) PRIVILEGED_FUNCTION;
466
467#endif
468
469/*
470 * Used only by the idle task. This checks to see if anything has been placed
471 * in the list of tasks waiting to be deleted. If so the task is cleaned up
472 * and its TCB deleted.
473 */
474static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
475
476/*
477 * The currently executing task is entering the Blocked state. Add the task to
478 * either the current or the overflow delayed task list.
479 */
480static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
481
482/*
483 * Fills an TaskStatus_t structure with information on each task that is
484 * referenced from the pxList list (which may be a ready list, a delayed list,
485 * a suspended list, etc.).
486 *
487 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
488 * NORMAL APPLICATION CODE.
489 */
490#if ( configUSE_TRACE_FACILITY == 1 )
491
492 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState ) PRIVILEGED_FUNCTION;
493
494#endif
495
496/*
497 * Searches pxList for a task with name pcNameToQuery - returning a handle to
498 * the task if it is found, or NULL if the task is not found.
499 */
500#if ( INCLUDE_xTaskGetHandle == 1 )
501
502 static TCB_t *prvSearchForNameWithinSingleList( List_t *pxList, const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
503
504#endif
505
506/*
507 * When a task is created, the stack of the task is filled with a known value.
508 * This function determines the 'high water mark' of the task stack by
509 * determining how much of the stack remains at the original preset value.
510 */
511#if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
512
513 static uint16_t prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
514
515#endif
516
517/*
518 * Return the amount of time, in ticks, that will pass before the kernel will
519 * next move a task from the Blocked state to the Running state.
520 *
521 * This conditional compilation should use inequality to 0, not equality to 1.
522 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
523 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
524 * set to a value other than 1.
525 */
526#if ( configUSE_TICKLESS_IDLE != 0 )
527
528 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
529
530#endif
531
532/*
533 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
534 * will exit the Blocked state.
535 */
536static void prvResetNextTaskUnblockTime( void );
537
538#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
539
540 /*
541 * Helper function used to pad task names with spaces when printing out
542 * human readable tables of task information.
543 */
544 static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName ) PRIVILEGED_FUNCTION;
545
546#endif
547
548/*
549 * Called after a Task_t structure has been allocated either statically or
550 * dynamically to fill in the structure's members.
551 */
552static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
553 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
554 const uint32_t ulStackDepth,
555 void * const pvParameters,
556 UBaseType_t uxPriority,
557 TaskHandle_t * const pxCreatedTask,
558 TCB_t *pxNewTCB,
559 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
560
561/*
562 * Called after a new task has been created and initialised to place the task
563 * under the control of the scheduler.
564 */
565static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB ) PRIVILEGED_FUNCTION;
566
567/*
568 * freertos_tasks_c_additions_init() should only be called if the user definable
569 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
570 * called by the function.
571 */
572#ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
573
574 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
575
576#endif
577
578/*-----------------------------------------------------------*/
579
580#if( configSUPPORT_STATIC_ALLOCATION == 1 )
581
582 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
583 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
584 const uint32_t ulStackDepth,
585 void * const pvParameters,
586 UBaseType_t uxPriority,
587 StackType_t * const puxStackBuffer,
588 StaticTask_t * const pxTaskBuffer )
589 {
590 TCB_t *pxNewTCB;
591 TaskHandle_t xReturn;
592
593 configASSERT( puxStackBuffer != NULL );
594 configASSERT( pxTaskBuffer != NULL );
595
596 #if( configASSERT_DEFINED == 1 )
597 {
598 /* Sanity check that the size of the structure used to declare a
599 variable of type StaticTask_t equals the size of the real task
600 structure. */
601 volatile size_t xSize = sizeof( StaticTask_t );
602 configASSERT( xSize == sizeof( TCB_t ) );
603 }
604 #endif /* configASSERT_DEFINED */
605
606
607 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
608 {
609 /* The memory used for the task's TCB and stack are passed into this
610 function - use them. */
611 pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
612 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
613
614 #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 Macro has been consolidated for readability reasons. */
615 {
616 /* Tasks can be created statically or dynamically, so note this
617 task was created statically in case the task is later deleted. */
618 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
619 }
620 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
621
622 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
623 prvAddNewTaskToReadyList( pxNewTCB );
624 }
625 else
626 {
627 xReturn = NULL;
628 }
629
630 return xReturn;
631 }
632
633#endif /* SUPPORT_STATIC_ALLOCATION */
634/*-----------------------------------------------------------*/
635
636#if( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
637
638 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask )
639 {
640 TCB_t *pxNewTCB;
641 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
642
643 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
644 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
645
646 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
647 {
648 /* Allocate space for the TCB. Where the memory comes from depends
649 on the implementation of the port malloc function and whether or
650 not static allocation is being used. */
651 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
652
653 /* Store the stack location in the TCB. */
654 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
655
656 #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
657 {
658 /* Tasks can be created statically or dynamically, so note this
659 task was created statically in case the task is later deleted. */
660 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
661 }
662 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
663
664 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
665 pxTaskDefinition->pcName,
666 ( uint32_t ) pxTaskDefinition->usStackDepth,
667 pxTaskDefinition->pvParameters,
668 pxTaskDefinition->uxPriority,
669 pxCreatedTask, pxNewTCB,
670 pxTaskDefinition->xRegions );
671
672 prvAddNewTaskToReadyList( pxNewTCB );
673 xReturn = pdPASS;
674 }
675
676 return xReturn;
677 }
678
679#endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
680/*-----------------------------------------------------------*/
681
682#if( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
683
684 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask )
685 {
686 TCB_t *pxNewTCB;
687 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
688
689 configASSERT( pxTaskDefinition->puxStackBuffer );
690
691 if( pxTaskDefinition->puxStackBuffer != NULL )
692 {
693 /* Allocate space for the TCB. Where the memory comes from depends
694 on the implementation of the port malloc function and whether or
695 not static allocation is being used. */
696 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
697
698 if( pxNewTCB != NULL )
699 {
700 /* Store the stack location in the TCB. */
701 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
702
703 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
704 {
705 /* Tasks can be created statically or dynamically, so note
706 this task had a statically allocated stack in case it is
707 later deleted. The TCB was allocated dynamically. */
708 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
709 }
710 #endif
711
712 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
713 pxTaskDefinition->pcName,
714 ( uint32_t ) pxTaskDefinition->usStackDepth,
715 pxTaskDefinition->pvParameters,
716 pxTaskDefinition->uxPriority,
717 pxCreatedTask, pxNewTCB,
718 pxTaskDefinition->xRegions );
719
720 prvAddNewTaskToReadyList( pxNewTCB );
721 xReturn = pdPASS;
722 }
723 }
724
725 return xReturn;
726 }
727
728#endif /* portUSING_MPU_WRAPPERS */
729/*-----------------------------------------------------------*/
730
731#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
732
733 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
734 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
735 const configSTACK_DEPTH_TYPE usStackDepth,
736 void * const pvParameters,
737 UBaseType_t uxPriority,
738 TaskHandle_t * const pxCreatedTask )
739 {
740 TCB_t *pxNewTCB;
741 BaseType_t xReturn;
742
743 /* If the stack grows down then allocate the stack then the TCB so the stack
744 does not grow into the TCB. Likewise if the stack grows up then allocate
745 the TCB then the stack. */
746 #if( portSTACK_GROWTH > 0 )
747 {
748 /* Allocate space for the TCB. Where the memory comes from depends on
749 the implementation of the port malloc function and whether or not static
750 allocation is being used. */
751 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
752
753 if( pxNewTCB != NULL )
754 {
755 /* Allocate space for the stack used by the task being created.
756 The base of the stack memory stored in the TCB so the task can
757 be deleted later if required. */
758 pxNewTCB->pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
759
760 if( pxNewTCB->pxStack == NULL )
761 {
762 /* Could not allocate the stack. Delete the allocated TCB. */
763 vPortFree( pxNewTCB );
764 pxNewTCB = NULL;
765 }
766 }
767 }
768 #else /* portSTACK_GROWTH */
769 {
770 StackType_t *pxStack;
771
772 /* Allocate space for the stack used by the task being created. */
773 pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
774
775 if( pxStack != NULL )
776 {
777 /* Allocate space for the TCB. */
778 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e961 MISRA exception as the casts are only redundant for some paths. */
779
780 if( pxNewTCB != NULL )
781 {
782 /* Store the stack location in the TCB. */
783 pxNewTCB->pxStack = pxStack;
784 }
785 else
786 {
787 /* The stack cannot be used as the TCB was not created. Free
788 it again. */
789 vPortFree( pxStack );
790 }
791 }
792 else
793 {
794 pxNewTCB = NULL;
795 }
796 }
797 #endif /* portSTACK_GROWTH */
798
799 if( pxNewTCB != NULL )
800 {
801 #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 Macro has been consolidated for readability reasons. */
802 {
803 /* Tasks can be created statically or dynamically, so note this
804 task was created dynamically in case it is later deleted. */
805 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
806 }
807 #endif /* configSUPPORT_STATIC_ALLOCATION */
808
809 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
810 prvAddNewTaskToReadyList( pxNewTCB );
811 xReturn = pdPASS;
812 }
813 else
814 {
815 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
816 }
817
818 return xReturn;
819 }
820
821#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
822/*-----------------------------------------------------------*/
823
824static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
825 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
826 const uint32_t ulStackDepth,
827 void * const pvParameters,
828 UBaseType_t uxPriority,
829 TaskHandle_t * const pxCreatedTask,
830 TCB_t *pxNewTCB,
831 const MemoryRegion_t * const xRegions )
832{
833StackType_t *pxTopOfStack;
834UBaseType_t x;
835
836 #if( portUSING_MPU_WRAPPERS == 1 )
837 /* Should the task be created in privileged mode? */
838 BaseType_t xRunPrivileged;
839 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
840 {
841 xRunPrivileged = pdTRUE;
842 }
843 else
844 {
845 xRunPrivileged = pdFALSE;
846 }
847 uxPriority &= ~portPRIVILEGE_BIT;
848 #endif /* portUSING_MPU_WRAPPERS == 1 */
849
Jianxiong Pan27d913f2020-08-03 15:22:00 +0800850 pxNewTCB->uStackDepth = ulStackDepth;
Qiufang Dai35c31332020-05-13 15:29:06 +0800851 /* Avoid dependency on memset() if it is not required. */
852 #if( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
853 {
854 /* Fill the stack with a known value to assist debugging. */
855 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
856 }
857 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
858
859 /* Calculate the top of stack address. This depends on whether the stack
860 grows from high memory to low (as per the 80x86) or vice versa.
861 portSTACK_GROWTH is used to make the result positive or negative as required
862 by the port. */
863 #if( portSTACK_GROWTH < 0 )
864 {
865 pxTopOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
866 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. */
867
868 /* Check the alignment of the calculated top of stack is correct. */
869 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
870
871 #if( configRECORD_STACK_HIGH_ADDRESS == 1 )
872 {
873 /* Also record the stack's high address, which may assist
874 debugging. */
875 pxNewTCB->pxEndOfStack = pxTopOfStack;
876 }
877 #endif /* configRECORD_STACK_HIGH_ADDRESS */
878 }
879 #else /* portSTACK_GROWTH */
880 {
881 pxTopOfStack = pxNewTCB->pxStack;
882
883 /* Check the alignment of the stack buffer is correct. */
884 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
885
886 /* The other extreme of the stack space is required if stack checking is
887 performed. */
888 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
889 }
890 #endif /* portSTACK_GROWTH */
891
892 /* Store the task name in the TCB. */
893 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
894 {
895 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
896
897 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
898 configMAX_TASK_NAME_LEN characters just in case the memory after the
899 string is not accessible (extremely unlikely). */
900 if( pcName[ x ] == 0x00 )
901 {
902 break;
903 }
904 else
905 {
906 mtCOVERAGE_TEST_MARKER();
907 }
908 }
909
910 /* Ensure the name string is terminated in the case that the string length
911 was greater or equal to configMAX_TASK_NAME_LEN. */
912 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
913
914 /* This is used as an array index so must ensure it's not too large. First
915 remove the privilege bit if one is present. */
916 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
917 {
918 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
919 }
920 else
921 {
922 mtCOVERAGE_TEST_MARKER();
923 }
924
925 pxNewTCB->uxPriority = uxPriority;
926 #if ( configUSE_MUTEXES == 1 )
927 {
928 pxNewTCB->uxBasePriority = uxPriority;
929 pxNewTCB->uxMutexesHeld = 0;
930 }
931 #endif /* configUSE_MUTEXES */
932
933 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
934 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
935
936 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
937 back to the containing TCB from a generic item in a list. */
938 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
939
940 /* Event lists are always in priority order. */
941 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
942 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
943
944 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
945 {
946 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
947 }
948 #endif /* portCRITICAL_NESTING_IN_TCB */
949
950 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
951 {
952 pxNewTCB->pxTaskTag = NULL;
953 }
954 #endif /* configUSE_APPLICATION_TASK_TAG */
955
956 #if ( configGENERATE_RUN_TIME_STATS == 1 )
957 {
958 pxNewTCB->ulRunTimeCounter = 0UL;
959 }
960 #endif /* configGENERATE_RUN_TIME_STATS */
961
962 #if ( portUSING_MPU_WRAPPERS == 1 )
963 {
964 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
965 }
966 #else
967 {
968 /* Avoid compiler warning about unreferenced parameter. */
969 ( void ) xRegions;
970 }
971 #endif
972
973 #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
974 {
975 for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )
976 {
977 pxNewTCB->pvThreadLocalStoragePointers[ x ] = NULL;
978 }
979 }
980 #endif
981
982 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
983 {
984 pxNewTCB->ulNotifiedValue = 0;
985 pxNewTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;
986 }
987 #endif
988
989 #if ( configUSE_NEWLIB_REENTRANT == 1 )
990 {
991 /* Initialise this task's Newlib reent structure. */
992 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
993 }
994 #endif
995
996 #if( INCLUDE_xTaskAbortDelay == 1 )
997 {
998 pxNewTCB->ucDelayAborted = pdFALSE;
999 }
1000 #endif
1001
1002 /* Initialize the TCB stack to look as if the task was already running,
1003 but had been interrupted by the scheduler. The return address is set
1004 to the start of the task function. Once the stack has been initialised
1005 the top of stack variable is updated. */
1006 #if( portUSING_MPU_WRAPPERS == 1 )
1007 {
1008 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1009 }
1010 #else /* portUSING_MPU_WRAPPERS */
1011 {
1012 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1013 }
1014 #endif /* portUSING_MPU_WRAPPERS */
1015
1016 if( ( void * ) pxCreatedTask != NULL )
1017 {
1018 /* Pass the handle out in an anonymous way. The handle can be used to
1019 change the created task's priority, delete the created task, etc.*/
1020 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1021 }
1022 else
1023 {
1024 mtCOVERAGE_TEST_MARKER();
1025 }
1026}
1027/*-----------------------------------------------------------*/
1028
1029static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB )
1030{
1031 /* Ensure interrupts don't access the task lists while the lists are being
1032 updated. */
1033 taskENTER_CRITICAL();
1034 {
1035 uxCurrentNumberOfTasks++;
1036 if( pxCurrentTCB == NULL )
1037 {
1038 /* There are no other tasks, or all the other tasks are in
1039 the suspended state - make this the current task. */
1040 pxCurrentTCB = pxNewTCB;
1041
1042 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1043 {
1044 /* This is the first task to be created so do the preliminary
1045 initialisation required. We will not recover if this call
1046 fails, but we will report the failure. */
1047 prvInitialiseTaskLists();
1048 }
1049 else
1050 {
1051 mtCOVERAGE_TEST_MARKER();
1052 }
1053 }
1054 else
1055 {
1056 /* If the scheduler is not already running, make this task the
1057 current task if it is the highest priority task to be created
1058 so far. */
1059 if( xSchedulerRunning == pdFALSE )
1060 {
1061 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1062 {
1063 pxCurrentTCB = pxNewTCB;
1064 }
1065 else
1066 {
1067 mtCOVERAGE_TEST_MARKER();
1068 }
1069 }
1070 else
1071 {
1072 mtCOVERAGE_TEST_MARKER();
1073 }
1074 }
1075
1076 uxTaskNumber++;
1077
1078 #if ( configUSE_TRACE_FACILITY == 1 )
1079 {
1080 /* Add a counter into the TCB for tracing only. */
1081 pxNewTCB->uxTCBNumber = uxTaskNumber;
1082 }
1083 #endif /* configUSE_TRACE_FACILITY */
1084 traceTASK_CREATE( pxNewTCB );
1085
1086 prvAddTaskToReadyList( pxNewTCB );
1087
1088 portSETUP_TCB( pxNewTCB );
1089 }
1090 taskEXIT_CRITICAL();
1091
1092 if( xSchedulerRunning != pdFALSE )
1093 {
1094 /* If the created task is of a higher priority than the current task
1095 then it should run now. */
1096 if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1097 {
1098 taskYIELD_IF_USING_PREEMPTION();
1099 }
1100 else
1101 {
1102 mtCOVERAGE_TEST_MARKER();
1103 }
1104 }
1105 else
1106 {
1107 mtCOVERAGE_TEST_MARKER();
1108 }
1109}
1110/*-----------------------------------------------------------*/
1111
1112#if ( INCLUDE_vTaskDelete == 1 )
1113
1114 void vTaskDelete( TaskHandle_t xTaskToDelete )
1115 {
1116 TCB_t *pxTCB;
1117
1118 taskENTER_CRITICAL();
1119 {
1120 /* If null is passed in here then it is the calling task that is
1121 being deleted. */
1122 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1123
1124 /* Remove task from the ready list. */
1125 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1126 {
1127 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1128 }
1129 else
1130 {
1131 mtCOVERAGE_TEST_MARKER();
1132 }
1133
1134 /* Is the task waiting on an event also? */
1135 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1136 {
1137 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1138 }
1139 else
1140 {
1141 mtCOVERAGE_TEST_MARKER();
1142 }
1143
1144 /* Increment the uxTaskNumber also so kernel aware debuggers can
1145 detect that the task lists need re-generating. This is done before
1146 portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1147 not return. */
1148 uxTaskNumber++;
1149
1150 if( pxTCB == pxCurrentTCB )
1151 {
1152 /* A task is deleting itself. This cannot complete within the
1153 task itself, as a context switch to another task is required.
1154 Place the task in the termination list. The idle task will
1155 check the termination list and free up any memory allocated by
1156 the scheduler for the TCB and stack of the deleted task. */
1157 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1158
1159 /* Increment the ucTasksDeleted variable so the idle task knows
1160 there is a task that has been deleted and that it should therefore
1161 check the xTasksWaitingTermination list. */
1162 ++uxDeletedTasksWaitingCleanUp;
1163
1164 /* The pre-delete hook is primarily for the Windows simulator,
1165 in which Windows specific clean up operations are performed,
1166 after which it is not possible to yield away from this task -
1167 hence xYieldPending is used to latch that a context switch is
1168 required. */
1169 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
1170 }
1171 else
1172 {
1173 --uxCurrentNumberOfTasks;
1174 prvDeleteTCB( pxTCB );
1175
1176 /* Reset the next expected unblock time in case it referred to
1177 the task that has just been deleted. */
1178 prvResetNextTaskUnblockTime();
1179 }
1180
1181 traceTASK_DELETE( pxTCB );
1182 }
1183 taskEXIT_CRITICAL();
1184
1185 /* Force a reschedule if it is the currently running task that has just
1186 been deleted. */
1187 if( xSchedulerRunning != pdFALSE )
1188 {
1189 if( pxTCB == pxCurrentTCB )
1190 {
1191 configASSERT( uxSchedulerSuspended == 0 );
1192 portYIELD_WITHIN_API();
1193 }
1194 else
1195 {
1196 mtCOVERAGE_TEST_MARKER();
1197 }
1198 }
1199 }
1200
1201#endif /* INCLUDE_vTaskDelete */
1202/*-----------------------------------------------------------*/
1203
1204#if ( INCLUDE_vTaskDelayUntil == 1 )
1205
1206 void vTaskDelayUntil( TickType_t * const pxPreviousWakeTime, const TickType_t xTimeIncrement )
1207 {
1208 TickType_t xTimeToWake;
1209 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1210
1211 configASSERT( pxPreviousWakeTime );
1212 configASSERT( ( xTimeIncrement > 0U ) );
1213 configASSERT( uxSchedulerSuspended == 0 );
1214
1215 vTaskSuspendAll();
1216 {
1217 /* Minor optimisation. The tick count cannot change in this
1218 block. */
1219 const TickType_t xConstTickCount = xTickCount;
1220
1221 /* Generate the tick time at which the task wants to wake. */
1222 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1223
1224 if( xConstTickCount < *pxPreviousWakeTime )
1225 {
1226 /* The tick count has overflowed since this function was
1227 lasted called. In this case the only time we should ever
1228 actually delay is if the wake time has also overflowed,
1229 and the wake time is greater than the tick time. When this
1230 is the case it is as if neither time had overflowed. */
1231 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1232 {
1233 xShouldDelay = pdTRUE;
1234 }
1235 else
1236 {
1237 mtCOVERAGE_TEST_MARKER();
1238 }
1239 }
1240 else
1241 {
1242 /* The tick time has not overflowed. In this case we will
1243 delay if either the wake time has overflowed, and/or the
1244 tick time is less than the wake time. */
1245 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1246 {
1247 xShouldDelay = pdTRUE;
1248 }
1249 else
1250 {
1251 mtCOVERAGE_TEST_MARKER();
1252 }
1253 }
1254
1255 /* Update the wake time ready for the next call. */
1256 *pxPreviousWakeTime = xTimeToWake;
1257
1258 if( xShouldDelay != pdFALSE )
1259 {
1260 traceTASK_DELAY_UNTIL( xTimeToWake );
1261
1262 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1263 the time to wake, so subtract the current tick count. */
1264 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1265 }
1266 else
1267 {
1268 mtCOVERAGE_TEST_MARKER();
1269 }
1270 }
1271 xAlreadyYielded = xTaskResumeAll();
1272
1273 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1274 have put ourselves to sleep. */
1275 if( xAlreadyYielded == pdFALSE )
1276 {
1277 portYIELD_WITHIN_API();
1278 }
1279 else
1280 {
1281 mtCOVERAGE_TEST_MARKER();
1282 }
1283 }
1284
1285#endif /* INCLUDE_vTaskDelayUntil */
1286/*-----------------------------------------------------------*/
1287
1288#if ( INCLUDE_vTaskDelay == 1 )
1289
1290 void vTaskDelay( const TickType_t xTicksToDelay )
1291 {
1292 BaseType_t xAlreadyYielded = pdFALSE;
1293
1294 /* A delay time of zero just forces a reschedule. */
1295 if( xTicksToDelay > ( TickType_t ) 0U )
1296 {
1297 configASSERT( uxSchedulerSuspended == 0 );
1298 vTaskSuspendAll();
1299 {
1300 traceTASK_DELAY();
1301
1302 /* A task that is removed from the event list while the
1303 scheduler is suspended will not get placed in the ready
1304 list or removed from the blocked list until the scheduler
1305 is resumed.
1306
1307 This task cannot be in an event list as it is the currently
1308 executing task. */
1309 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1310 }
1311 xAlreadyYielded = xTaskResumeAll();
1312 }
1313 else
1314 {
1315 mtCOVERAGE_TEST_MARKER();
1316 }
1317
1318 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1319 have put ourselves to sleep. */
1320 if( xAlreadyYielded == pdFALSE )
1321 {
1322 portYIELD_WITHIN_API();
1323 }
1324 else
1325 {
1326 mtCOVERAGE_TEST_MARKER();
1327 }
1328 }
1329
1330#endif /* INCLUDE_vTaskDelay */
1331/*-----------------------------------------------------------*/
1332
1333#if( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) )
1334
1335 eTaskState eTaskGetState( TaskHandle_t xTask )
1336 {
1337 eTaskState eReturn;
1338 List_t *pxStateList;
1339 const TCB_t * const pxTCB = ( TCB_t * ) xTask;
1340
1341 configASSERT( pxTCB );
1342
1343 if( pxTCB == pxCurrentTCB )
1344 {
1345 /* The task calling this function is querying its own state. */
1346 eReturn = eRunning;
1347 }
1348 else
1349 {
1350 taskENTER_CRITICAL();
1351 {
1352 pxStateList = ( List_t * ) listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1353 }
1354 taskEXIT_CRITICAL();
1355
1356 if( ( pxStateList == pxDelayedTaskList ) || ( pxStateList == pxOverflowDelayedTaskList ) )
1357 {
1358 /* The task being queried is referenced from one of the Blocked
1359 lists. */
1360 eReturn = eBlocked;
1361 }
1362
1363 #if ( INCLUDE_vTaskSuspend == 1 )
1364 else if( pxStateList == &xSuspendedTaskList )
1365 {
1366 /* The task being queried is referenced from the suspended
1367 list. Is it genuinely suspended or is it block
1368 indefinitely? */
1369 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1370 {
1371 eReturn = eSuspended;
1372 }
1373 else
1374 {
1375 eReturn = eBlocked;
1376 }
1377 }
1378 #endif
1379
1380 #if ( INCLUDE_vTaskDelete == 1 )
1381 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1382 {
1383 /* The task being queried is referenced from the deleted
1384 tasks list, or it is not referenced from any lists at
1385 all. */
1386 eReturn = eDeleted;
1387 }
1388 #endif
1389
1390 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1391 {
1392 /* If the task is not in any other state, it must be in the
1393 Ready (including pending ready) state. */
1394 eReturn = eReady;
1395 }
1396 }
1397
1398 return eReturn;
1399 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1400
1401#endif /* INCLUDE_eTaskGetState */
1402/*-----------------------------------------------------------*/
1403
1404#if ( INCLUDE_uxTaskPriorityGet == 1 )
1405
1406 UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask )
1407 {
1408 TCB_t *pxTCB;
1409 UBaseType_t uxReturn;
1410
1411 taskENTER_CRITICAL();
1412 {
1413 /* If null is passed in here then it is the priority of the that
1414 called uxTaskPriorityGet() that is being queried. */
1415 pxTCB = prvGetTCBFromHandle( xTask );
1416 uxReturn = pxTCB->uxPriority;
1417 }
1418 taskEXIT_CRITICAL();
1419
1420 return uxReturn;
1421 }
1422
1423#endif /* INCLUDE_uxTaskPriorityGet */
1424/*-----------------------------------------------------------*/
1425
1426#if ( INCLUDE_uxTaskPriorityGet == 1 )
1427
1428 UBaseType_t uxTaskPriorityGetFromISR( TaskHandle_t xTask )
1429 {
1430 TCB_t *pxTCB;
1431 UBaseType_t uxReturn, uxSavedInterruptState;
1432
1433 /* RTOS ports that support interrupt nesting have the concept of a
1434 maximum system call (or maximum API call) interrupt priority.
1435 Interrupts that are above the maximum system call priority are keep
1436 permanently enabled, even when the RTOS kernel is in a critical section,
1437 but cannot make any calls to FreeRTOS API functions. If configASSERT()
1438 is defined in FreeRTOSConfig.h then
1439 portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1440 failure if a FreeRTOS API function is called from an interrupt that has
1441 been assigned a priority above the configured maximum system call
1442 priority. Only FreeRTOS functions that end in FromISR can be called
1443 from interrupts that have been assigned a priority at or (logically)
1444 below the maximum system call interrupt priority. FreeRTOS maintains a
1445 separate interrupt safe API to ensure interrupt entry is as fast and as
1446 simple as possible. More information (albeit Cortex-M specific) is
1447 provided on the following link:
1448 http://www.freertos.org/RTOS-Cortex-M3-M4.html */
1449 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1450
1451 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
1452 {
1453 /* If null is passed in here then it is the priority of the calling
1454 task that is being queried. */
1455 pxTCB = prvGetTCBFromHandle( xTask );
1456 uxReturn = pxTCB->uxPriority;
1457 }
1458 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
1459
1460 return uxReturn;
1461 }
1462
1463#endif /* INCLUDE_uxTaskPriorityGet */
1464/*-----------------------------------------------------------*/
1465
1466#if ( INCLUDE_vTaskPrioritySet == 1 )
1467
1468 void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority )
1469 {
1470 TCB_t *pxTCB;
1471 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
1472 BaseType_t xYieldRequired = pdFALSE;
1473
1474 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
1475
1476 /* Ensure the new priority is valid. */
1477 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1478 {
1479 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1480 }
1481 else
1482 {
1483 mtCOVERAGE_TEST_MARKER();
1484 }
1485
1486 taskENTER_CRITICAL();
1487 {
1488 /* If null is passed in here then it is the priority of the calling
1489 task that is being changed. */
1490 pxTCB = prvGetTCBFromHandle( xTask );
1491
1492 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
1493
1494 #if ( configUSE_MUTEXES == 1 )
1495 {
1496 uxCurrentBasePriority = pxTCB->uxBasePriority;
1497 }
1498 #else
1499 {
1500 uxCurrentBasePriority = pxTCB->uxPriority;
1501 }
1502 #endif
1503
1504 if( uxCurrentBasePriority != uxNewPriority )
1505 {
1506 /* The priority change may have readied a task of higher
1507 priority than the calling task. */
1508 if( uxNewPriority > uxCurrentBasePriority )
1509 {
1510 if( pxTCB != pxCurrentTCB )
1511 {
1512 /* The priority of a task other than the currently
1513 running task is being raised. Is the priority being
1514 raised above that of the running task? */
1515 if( uxNewPriority >= pxCurrentTCB->uxPriority )
1516 {
1517 xYieldRequired = pdTRUE;
1518 }
1519 else
1520 {
1521 mtCOVERAGE_TEST_MARKER();
1522 }
1523 }
1524 else
1525 {
1526 /* The priority of the running task is being raised,
1527 but the running task must already be the highest
1528 priority task able to run so no yield is required. */
1529 }
1530 }
1531 else if( pxTCB == pxCurrentTCB )
1532 {
1533 /* Setting the priority of the running task down means
1534 there may now be another task of higher priority that
1535 is ready to execute. */
1536 xYieldRequired = pdTRUE;
1537 }
1538 else
1539 {
1540 /* Setting the priority of any other task down does not
1541 require a yield as the running task must be above the
1542 new priority of the task being modified. */
1543 }
1544
1545 /* Remember the ready list the task might be referenced from
1546 before its uxPriority member is changed so the
1547 taskRESET_READY_PRIORITY() macro can function correctly. */
1548 uxPriorityUsedOnEntry = pxTCB->uxPriority;
1549
1550 #if ( configUSE_MUTEXES == 1 )
1551 {
1552 /* Only change the priority being used if the task is not
1553 currently using an inherited priority. */
1554 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
1555 {
1556 pxTCB->uxPriority = uxNewPriority;
1557 }
1558 else
1559 {
1560 mtCOVERAGE_TEST_MARKER();
1561 }
1562
1563 /* The base priority gets set whatever. */
1564 pxTCB->uxBasePriority = uxNewPriority;
1565 }
1566 #else
1567 {
1568 pxTCB->uxPriority = uxNewPriority;
1569 }
1570 #endif
1571
1572 /* Only reset the event list item value if the value is not
1573 being used for anything else. */
1574 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
1575 {
1576 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1577 }
1578 else
1579 {
1580 mtCOVERAGE_TEST_MARKER();
1581 }
1582
1583 /* If the task is in the blocked or suspended list we need do
1584 nothing more than change its priority variable. However, if
1585 the task is in a ready list it needs to be removed and placed
1586 in the list appropriate to its new priority. */
1587 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
1588 {
1589 /* The task is currently in its ready list - remove before
1590 adding it to it's new ready list. As we are in a critical
1591 section we can do this even if the scheduler is suspended. */
1592 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1593 {
1594 /* It is known that the task is in its ready list so
1595 there is no need to check again and the port level
1596 reset macro can be called directly. */
1597 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
1598 }
1599 else
1600 {
1601 mtCOVERAGE_TEST_MARKER();
1602 }
1603 prvAddTaskToReadyList( pxTCB );
1604 }
1605 else
1606 {
1607 mtCOVERAGE_TEST_MARKER();
1608 }
1609
1610 if( xYieldRequired != pdFALSE )
1611 {
1612 taskYIELD_IF_USING_PREEMPTION();
1613 }
1614 else
1615 {
1616 mtCOVERAGE_TEST_MARKER();
1617 }
1618
1619 /* Remove compiler warning about unused variables when the port
1620 optimised task selection is not being used. */
1621 ( void ) uxPriorityUsedOnEntry;
1622 }
1623 }
1624 taskEXIT_CRITICAL();
1625 }
1626
1627#endif /* INCLUDE_vTaskPrioritySet */
1628/*-----------------------------------------------------------*/
1629
1630#if ( INCLUDE_vTaskSuspend == 1 )
1631
1632 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
1633 {
1634 TCB_t *pxTCB;
1635
1636 taskENTER_CRITICAL();
1637 {
1638 /* If null is passed in here then it is the running task that is
1639 being suspended. */
1640 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
1641
1642 traceTASK_SUSPEND( pxTCB );
1643
1644 /* Remove task from the ready/delayed list and place in the
1645 suspended list. */
1646 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1647 {
1648 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1649 }
1650 else
1651 {
1652 mtCOVERAGE_TEST_MARKER();
1653 }
1654
1655 /* Is the task waiting on an event also? */
1656 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1657 {
1658 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1659 }
1660 else
1661 {
1662 mtCOVERAGE_TEST_MARKER();
1663 }
1664
1665 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
1666
1667 #if( configUSE_TASK_NOTIFICATIONS == 1 )
1668 {
1669 if( pxTCB->ucNotifyState == taskWAITING_NOTIFICATION )
1670 {
1671 /* The task was blocked to wait for a notification, but is
1672 now suspended, so no notification was received. */
1673 pxTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;
1674 }
1675 }
1676 #endif
1677 }
1678 taskEXIT_CRITICAL();
1679
1680 if( xSchedulerRunning != pdFALSE )
1681 {
1682 /* Reset the next expected unblock time in case it referred to the
1683 task that is now in the Suspended state. */
1684 taskENTER_CRITICAL();
1685 {
1686 prvResetNextTaskUnblockTime();
1687 }
1688 taskEXIT_CRITICAL();
1689 }
1690 else
1691 {
1692 mtCOVERAGE_TEST_MARKER();
1693 }
1694
1695 if( pxTCB == pxCurrentTCB )
1696 {
1697 if( xSchedulerRunning != pdFALSE )
1698 {
1699 /* The current task has just been suspended. */
1700 configASSERT( uxSchedulerSuspended == 0 );
1701 portYIELD_WITHIN_API();
1702 }
1703 else
1704 {
1705 /* The scheduler is not running, but the task that was pointed
1706 to by pxCurrentTCB has just been suspended and pxCurrentTCB
1707 must be adjusted to point to a different task. */
1708 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )
1709 {
1710 /* No other tasks are ready, so set pxCurrentTCB back to
1711 NULL so when the next task is created pxCurrentTCB will
1712 be set to point to it no matter what its relative priority
1713 is. */
1714 pxCurrentTCB = NULL;
1715 }
1716 else
1717 {
1718 vTaskSwitchContext();
1719 }
1720 }
1721 }
1722 else
1723 {
1724 mtCOVERAGE_TEST_MARKER();
1725 }
1726 }
1727
1728#endif /* INCLUDE_vTaskSuspend */
1729/*-----------------------------------------------------------*/
1730
1731#if ( INCLUDE_vTaskSuspend == 1 )
1732
1733 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
1734 {
1735 BaseType_t xReturn = pdFALSE;
1736 const TCB_t * const pxTCB = ( TCB_t * ) xTask;
1737
1738 /* Accesses xPendingReadyList so must be called from a critical
1739 section. */
1740
1741 /* It does not make sense to check if the calling task is suspended. */
1742 configASSERT( xTask );
1743
1744 /* Is the task being resumed actually in the suspended list? */
1745 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
1746 {
1747 /* Has the task already been resumed from within an ISR? */
1748 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
1749 {
1750 /* Is it in the suspended list because it is in the Suspended
1751 state, or because is is blocked with no timeout? */
1752 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
1753 {
1754 xReturn = pdTRUE;
1755 }
1756 else
1757 {
1758 mtCOVERAGE_TEST_MARKER();
1759 }
1760 }
1761 else
1762 {
1763 mtCOVERAGE_TEST_MARKER();
1764 }
1765 }
1766 else
1767 {
1768 mtCOVERAGE_TEST_MARKER();
1769 }
1770
1771 return xReturn;
1772 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1773
1774#endif /* INCLUDE_vTaskSuspend */
1775/*-----------------------------------------------------------*/
1776
1777#if ( INCLUDE_vTaskSuspend == 1 )
1778
1779 void vTaskResume( TaskHandle_t xTaskToResume )
1780 {
1781 TCB_t * const pxTCB = ( TCB_t * ) xTaskToResume;
1782
1783 /* It does not make sense to resume the calling task. */
1784 configASSERT( xTaskToResume );
1785
1786 /* The parameter cannot be NULL as it is impossible to resume the
1787 currently executing task. */
1788 if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )
1789 {
1790 taskENTER_CRITICAL();
1791 {
1792 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1793 {
1794 traceTASK_RESUME( pxTCB );
1795
1796 /* The ready list can be accessed even if the scheduler is
1797 suspended because this is inside a critical section. */
1798 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1799 prvAddTaskToReadyList( pxTCB );
1800
1801 /* A higher priority task may have just been resumed. */
1802 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1803 {
1804 /* This yield may not cause the task just resumed to run,
1805 but will leave the lists in the correct state for the
1806 next yield. */
1807 taskYIELD_IF_USING_PREEMPTION();
1808 }
1809 else
1810 {
1811 mtCOVERAGE_TEST_MARKER();
1812 }
1813 }
1814 else
1815 {
1816 mtCOVERAGE_TEST_MARKER();
1817 }
1818 }
1819 taskEXIT_CRITICAL();
1820 }
1821 else
1822 {
1823 mtCOVERAGE_TEST_MARKER();
1824 }
1825 }
1826
1827#endif /* INCLUDE_vTaskSuspend */
1828
1829/*-----------------------------------------------------------*/
1830
1831#if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
1832
1833 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
1834 {
1835 BaseType_t xYieldRequired = pdFALSE;
1836 TCB_t * const pxTCB = ( TCB_t * ) xTaskToResume;
1837 UBaseType_t uxSavedInterruptStatus;
1838
1839 configASSERT( xTaskToResume );
1840
1841 /* RTOS ports that support interrupt nesting have the concept of a
1842 maximum system call (or maximum API call) interrupt priority.
1843 Interrupts that are above the maximum system call priority are keep
1844 permanently enabled, even when the RTOS kernel is in a critical section,
1845 but cannot make any calls to FreeRTOS API functions. If configASSERT()
1846 is defined in FreeRTOSConfig.h then
1847 portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1848 failure if a FreeRTOS API function is called from an interrupt that has
1849 been assigned a priority above the configured maximum system call
1850 priority. Only FreeRTOS functions that end in FromISR can be called
1851 from interrupts that have been assigned a priority at or (logically)
1852 below the maximum system call interrupt priority. FreeRTOS maintains a
1853 separate interrupt safe API to ensure interrupt entry is as fast and as
1854 simple as possible. More information (albeit Cortex-M specific) is
1855 provided on the following link:
1856 http://www.freertos.org/RTOS-Cortex-M3-M4.html */
1857 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1858
1859 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1860 {
1861 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1862 {
1863 traceTASK_RESUME_FROM_ISR( pxTCB );
1864
1865 /* Check the ready lists can be accessed. */
1866 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
1867 {
1868 /* Ready lists can be accessed so move the task from the
1869 suspended list to the ready list directly. */
1870 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1871 {
1872 xYieldRequired = pdTRUE;
1873 }
1874 else
1875 {
1876 mtCOVERAGE_TEST_MARKER();
1877 }
1878
1879 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1880 prvAddTaskToReadyList( pxTCB );
1881 }
1882 else
1883 {
1884 /* The delayed or ready lists cannot be accessed so the task
1885 is held in the pending ready list until the scheduler is
1886 unsuspended. */
1887 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
1888 }
1889 }
1890 else
1891 {
1892 mtCOVERAGE_TEST_MARKER();
1893 }
1894 }
1895 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1896
1897 return xYieldRequired;
1898 }
1899
1900#endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
1901/*-----------------------------------------------------------*/
1902
1903void vTaskStartScheduler( void )
1904{
1905BaseType_t xReturn;
1906
1907 /* Add the idle task at the lowest priority. */
1908 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
1909 {
1910 StaticTask_t *pxIdleTaskTCBBuffer = NULL;
1911 StackType_t *pxIdleTaskStackBuffer = NULL;
1912 uint32_t ulIdleTaskStackSize;
1913
1914 /* The Idle task is created using user provided RAM - obtain the
1915 address of the RAM then create the idle task. */
1916 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
1917 xIdleTaskHandle = xTaskCreateStatic( prvIdleTask,
1918 configIDLE_TASK_NAME,
1919 ulIdleTaskStackSize,
1920 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
1921 ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ),
1922 pxIdleTaskStackBuffer,
1923 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
1924
1925 if( xIdleTaskHandle != NULL )
1926 {
1927 xReturn = pdPASS;
1928 }
1929 else
1930 {
1931 xReturn = pdFAIL;
1932 }
1933 }
1934 #else
1935 {
1936 /* The Idle task is being created using dynamically allocated RAM. */
1937 xReturn = xTaskCreate( prvIdleTask,
1938 configIDLE_TASK_NAME,
1939 configMINIMAL_STACK_SIZE,
1940 ( void * ) NULL,
1941 ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ),
1942 &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
1943 }
1944 #endif /* configSUPPORT_STATIC_ALLOCATION */
1945
1946 #if ( configUSE_TIMERS == 1 )
1947 {
1948 if( xReturn == pdPASS )
1949 {
1950 xReturn = xTimerCreateTimerTask();
1951 }
1952 else
1953 {
1954 mtCOVERAGE_TEST_MARKER();
1955 }
1956 }
1957 #endif /* configUSE_TIMERS */
1958
1959 if( xReturn == pdPASS )
1960 {
1961 /* freertos_tasks_c_additions_init() should only be called if the user
1962 definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
1963 the only macro called by the function. */
1964 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
1965 {
1966 freertos_tasks_c_additions_init();
1967 }
1968 #endif
1969
1970 /* Interrupts are turned off here, to ensure a tick does not occur
1971 before or during the call to xPortStartScheduler(). The stacks of
1972 the created tasks contain a status word with interrupts switched on
1973 so interrupts will automatically get re-enabled when the first task
1974 starts to run. */
1975 portDISABLE_INTERRUPTS();
1976
1977 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1978 {
1979 /* Switch Newlib's _impure_ptr variable to point to the _reent
1980 structure specific to the task that will run first. */
1981 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
1982 }
1983 #endif /* configUSE_NEWLIB_REENTRANT */
1984
1985 xNextTaskUnblockTime = portMAX_DELAY;
1986 xSchedulerRunning = pdTRUE;
1987 xTickCount = ( TickType_t ) 0U;
1988
1989 /* If configGENERATE_RUN_TIME_STATS is defined then the following
1990 macro must be defined to configure the timer/counter used to generate
1991 the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
1992 is set to 0 and the following line fails to build then ensure you do not
1993 have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
1994 FreeRTOSConfig.h file. */
1995 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
1996
1997 /* Setting up the timer tick is hardware specific and thus in the
1998 portable interface. */
1999 if( xPortStartScheduler() != pdFALSE )
2000 {
2001 /* Should not reach here as if the scheduler is running the
2002 function will not return. */
2003 }
2004 else
2005 {
2006 /* Should only reach here if a task calls xTaskEndScheduler(). */
2007 }
2008 }
2009 else
2010 {
2011 /* This line will only be reached if the kernel could not be started,
2012 because there was not enough FreeRTOS heap to create the idle task
2013 or the timer task. */
2014 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2015 }
2016
2017 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2018 meaning xIdleTaskHandle is not used anywhere else. */
2019 ( void ) xIdleTaskHandle;
2020}
2021/*-----------------------------------------------------------*/
2022
2023void vTaskEndScheduler( void )
2024{
2025 /* Stop the scheduler interrupts and call the portable scheduler end
2026 routine so the original ISRs can be restored if necessary. The port
2027 layer must ensure interrupts enable bit is left in the correct state. */
2028 portDISABLE_INTERRUPTS();
2029 xSchedulerRunning = pdFALSE;
2030 vPortEndScheduler();
2031}
2032/*----------------------------------------------------------*/
2033
2034void vTaskSuspendAll( void )
2035{
2036 /* A critical section is not required as the variable is of type
2037 BaseType_t. Please read Richard Barry's reply in the following link to a
2038 post in the FreeRTOS support forum before reporting this as a bug! -
2039 http://goo.gl/wu4acr */
2040 ++uxSchedulerSuspended;
2041}
2042/*----------------------------------------------------------*/
2043
2044#if ( configUSE_TICKLESS_IDLE != 0 )
2045
2046 static TickType_t prvGetExpectedIdleTime( void )
2047 {
2048 TickType_t xReturn;
2049 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2050
2051 /* uxHigherPriorityReadyTasks takes care of the case where
2052 configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2053 task that are in the Ready state, even though the idle task is
2054 running. */
2055 #if( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2056 {
2057 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2058 {
2059 uxHigherPriorityReadyTasks = pdTRUE;
2060 }
2061 }
2062 #else
2063 {
2064 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2065
2066 /* When port optimised task selection is used the uxTopReadyPriority
2067 variable is used as a bit map. If bits other than the least
2068 significant bit are set then there are tasks that have a priority
2069 above the idle priority that are in the Ready state. This takes
2070 care of the case where the co-operative scheduler is in use. */
2071 if( uxTopReadyPriority > uxLeastSignificantBit )
2072 {
2073 uxHigherPriorityReadyTasks = pdTRUE;
2074 }
2075 }
2076 #endif
2077
2078 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2079 {
2080 xReturn = 0;
2081 }
2082 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2083 {
2084 /* There are other idle priority tasks in the ready state. If
2085 time slicing is used then the very next tick interrupt must be
2086 processed. */
2087 xReturn = 0;
2088 }
2089 else if( uxHigherPriorityReadyTasks != pdFALSE )
2090 {
2091 /* There are tasks in the Ready state that have a priority above the
2092 idle priority. This path can only be reached if
2093 configUSE_PREEMPTION is 0. */
2094 xReturn = 0;
2095 }
2096 else
2097 {
2098 xReturn = xNextTaskUnblockTime - xTickCount;
2099 }
2100
2101 return xReturn;
2102 }
2103
2104#endif /* configUSE_TICKLESS_IDLE */
2105/*----------------------------------------------------------*/
2106
2107BaseType_t xTaskResumeAll( void )
2108{
2109TCB_t *pxTCB = NULL;
2110BaseType_t xAlreadyYielded = pdFALSE;
2111
2112 /* If uxSchedulerSuspended is zero then this function does not match a
2113 previous call to vTaskSuspendAll(). */
2114 configASSERT( uxSchedulerSuspended );
2115
2116 /* It is possible that an ISR caused a task to be removed from an event
2117 list while the scheduler was suspended. If this was the case then the
2118 removed task will have been added to the xPendingReadyList. Once the
2119 scheduler has been resumed it is safe to move all the pending ready
2120 tasks from this list into their appropriate ready list. */
2121 taskENTER_CRITICAL();
2122 {
2123 --uxSchedulerSuspended;
2124
2125 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2126 {
2127 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2128 {
2129 /* Move any readied tasks from the pending list into the
2130 appropriate ready list. */
2131 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2132 {
2133 pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
2134 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2135 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2136 prvAddTaskToReadyList( pxTCB );
2137
2138 /* If the moved task has a priority higher than the current
2139 task then a yield must be performed. */
2140 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
2141 {
2142 xYieldPending = pdTRUE;
2143 }
2144 else
2145 {
2146 mtCOVERAGE_TEST_MARKER();
2147 }
2148 }
2149
2150 if( pxTCB != NULL )
2151 {
2152 /* A task was unblocked while the scheduler was suspended,
2153 which may have prevented the next unblock time from being
2154 re-calculated, in which case re-calculate it now. Mainly
2155 important for low power tickless implementations, where
2156 this can prevent an unnecessary exit from low power
2157 state. */
2158 prvResetNextTaskUnblockTime();
2159 }
2160
2161 /* If any ticks occurred while the scheduler was suspended then
2162 they should be processed now. This ensures the tick count does
2163 not slip, and that any delayed tasks are resumed at the correct
2164 time. */
2165 {
2166 UBaseType_t uxPendedCounts = uxPendedTicks; /* Non-volatile copy. */
2167
2168 if( uxPendedCounts > ( UBaseType_t ) 0U )
2169 {
2170 do
2171 {
2172 if( xTaskIncrementTick() != pdFALSE )
2173 {
2174 xYieldPending = pdTRUE;
2175 }
2176 else
2177 {
2178 mtCOVERAGE_TEST_MARKER();
2179 }
2180 --uxPendedCounts;
2181 } while( uxPendedCounts > ( UBaseType_t ) 0U );
2182
2183 uxPendedTicks = 0;
2184 }
2185 else
2186 {
2187 mtCOVERAGE_TEST_MARKER();
2188 }
2189 }
2190
2191 if( xYieldPending != pdFALSE )
2192 {
2193 #if( configUSE_PREEMPTION != 0 )
2194 {
2195 xAlreadyYielded = pdTRUE;
2196 }
2197 #endif
2198 taskYIELD_IF_USING_PREEMPTION();
2199 }
2200 else
2201 {
2202 mtCOVERAGE_TEST_MARKER();
2203 }
2204 }
2205 }
2206 else
2207 {
2208 mtCOVERAGE_TEST_MARKER();
2209 }
2210 }
2211 taskEXIT_CRITICAL();
2212
2213 return xAlreadyYielded;
2214}
2215/*-----------------------------------------------------------*/
2216
2217TickType_t xTaskGetTickCount( void )
2218{
2219TickType_t xTicks;
2220
2221 /* Critical section required if running on a 16 bit processor. */
2222 portTICK_TYPE_ENTER_CRITICAL();
2223 {
2224 xTicks = xTickCount;
2225 }
2226 portTICK_TYPE_EXIT_CRITICAL();
2227
2228 return xTicks;
2229}
2230/*-----------------------------------------------------------*/
2231
2232TickType_t xTaskGetTickCountFromISR( void )
2233{
2234TickType_t xReturn;
2235UBaseType_t uxSavedInterruptStatus;
2236
2237 /* RTOS ports that support interrupt nesting have the concept of a maximum
2238 system call (or maximum API call) interrupt priority. Interrupts that are
2239 above the maximum system call priority are kept permanently enabled, even
2240 when the RTOS kernel is in a critical section, but cannot make any calls to
2241 FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2242 then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2243 failure if a FreeRTOS API function is called from an interrupt that has been
2244 assigned a priority above the configured maximum system call priority.
2245 Only FreeRTOS functions that end in FromISR can be called from interrupts
2246 that have been assigned a priority at or (logically) below the maximum
2247 system call interrupt priority. FreeRTOS maintains a separate interrupt
2248 safe API to ensure interrupt entry is as fast and as simple as possible.
2249 More information (albeit Cortex-M specific) is provided on the following
2250 link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */
2251 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2252
2253 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
2254 {
2255 xReturn = xTickCount;
2256 }
2257 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2258
2259 return xReturn;
2260}
2261/*-----------------------------------------------------------*/
2262
2263UBaseType_t uxTaskGetNumberOfTasks( void )
2264{
2265 /* A critical section is not required because the variables are of type
2266 BaseType_t. */
2267 return uxCurrentNumberOfTasks;
2268}
2269/*-----------------------------------------------------------*/
2270
2271char *pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2272{
2273TCB_t *pxTCB;
2274
2275 /* If null is passed in here then the name of the calling task is being
2276 queried. */
2277 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
2278 configASSERT( pxTCB );
2279 return &( pxTCB->pcTaskName[ 0 ] );
2280}
2281/*-----------------------------------------------------------*/
2282
2283#if ( INCLUDE_xTaskGetHandle == 1 )
2284
2285 static TCB_t *prvSearchForNameWithinSingleList( List_t *pxList, const char pcNameToQuery[] )
2286 {
2287 TCB_t *pxNextTCB, *pxFirstTCB, *pxReturn = NULL;
2288 UBaseType_t x;
2289 char cNextChar;
2290
2291 /* This function is called with the scheduler suspended. */
2292
2293 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
2294 {
2295 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
2296
2297 do
2298 {
2299 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
2300
2301 /* Check each character in the name looking for a match or
2302 mismatch. */
2303 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2304 {
2305 cNextChar = pxNextTCB->pcTaskName[ x ];
2306
2307 if( cNextChar != pcNameToQuery[ x ] )
2308 {
2309 /* Characters didn't match. */
2310 break;
2311 }
2312 else if( cNextChar == 0x00 )
2313 {
2314 /* Both strings terminated, a match must have been
2315 found. */
2316 pxReturn = pxNextTCB;
2317 break;
2318 }
2319 else
2320 {
2321 mtCOVERAGE_TEST_MARKER();
2322 }
2323 }
2324
2325 if( pxReturn != NULL )
2326 {
2327 /* The handle has been found. */
2328 break;
2329 }
2330
2331 } while( pxNextTCB != pxFirstTCB );
2332 }
2333 else
2334 {
2335 mtCOVERAGE_TEST_MARKER();
2336 }
2337
2338 return pxReturn;
2339 }
2340
2341#endif /* INCLUDE_xTaskGetHandle */
2342/*-----------------------------------------------------------*/
2343
2344#if ( INCLUDE_xTaskGetHandle == 1 )
2345
2346 TaskHandle_t xTaskGetHandle( const char *pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2347 {
2348 UBaseType_t uxQueue = configMAX_PRIORITIES;
2349 TCB_t* pxTCB;
2350
2351 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
2352 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
2353
2354 vTaskSuspendAll();
2355 {
2356 /* Search the ready lists. */
2357 do
2358 {
2359 uxQueue--;
2360 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
2361
2362 if( pxTCB != NULL )
2363 {
2364 /* Found the handle. */
2365 break;
2366 }
2367
2368 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2369
2370 /* Search the delayed lists. */
2371 if( pxTCB == NULL )
2372 {
2373 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
2374 }
2375
2376 if( pxTCB == NULL )
2377 {
2378 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
2379 }
2380
2381 #if ( INCLUDE_vTaskSuspend == 1 )
2382 {
2383 if( pxTCB == NULL )
2384 {
2385 /* Search the suspended list. */
2386 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
2387 }
2388 }
2389 #endif
2390
2391 #if( INCLUDE_vTaskDelete == 1 )
2392 {
2393 if( pxTCB == NULL )
2394 {
2395 /* Search the deleted list. */
2396 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
2397 }
2398 }
2399 #endif
2400 }
2401 ( void ) xTaskResumeAll();
2402
2403 return ( TaskHandle_t ) pxTCB;
2404 }
2405
2406#endif /* INCLUDE_xTaskGetHandle */
2407/*-----------------------------------------------------------*/
2408
2409#if ( configUSE_TRACE_FACILITY == 1 )
2410
2411 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray, const UBaseType_t uxArraySize, uint32_t * const pulTotalRunTime )
2412 {
2413 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
2414
2415 vTaskSuspendAll();
2416 {
2417 /* Is there a space in the array for each task in the system? */
2418 if( uxArraySize >= uxCurrentNumberOfTasks )
2419 {
2420 /* Fill in an TaskStatus_t structure with information on each
2421 task in the Ready state. */
2422 do
2423 {
2424 uxQueue--;
2425 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
2426
2427 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2428
2429 /* Fill in an TaskStatus_t structure with information on each
2430 task in the Blocked state. */
2431 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
2432 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
2433
2434 #if( INCLUDE_vTaskDelete == 1 )
2435 {
2436 /* Fill in an TaskStatus_t structure with information on
2437 each task that has been deleted but not yet cleaned up. */
2438 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
2439 }
2440 #endif
2441
2442 #if ( INCLUDE_vTaskSuspend == 1 )
2443 {
2444 /* Fill in an TaskStatus_t structure with information on
2445 each task in the Suspended state. */
2446 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
2447 }
2448 #endif
2449
2450 #if ( configGENERATE_RUN_TIME_STATS == 1)
2451 {
2452 if( pulTotalRunTime != NULL )
2453 {
2454 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2455 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
2456 #else
2457 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2458 #endif
2459 }
2460 }
2461 #else
2462 {
2463 if( pulTotalRunTime != NULL )
2464 {
2465 *pulTotalRunTime = 0;
2466 }
2467 }
2468 #endif
2469 }
2470 else
2471 {
2472 mtCOVERAGE_TEST_MARKER();
2473 }
2474 }
2475 ( void ) xTaskResumeAll();
2476
2477 return uxTask;
2478 }
2479
2480#endif /* configUSE_TRACE_FACILITY */
2481/*----------------------------------------------------------*/
2482
2483#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2484
2485 TaskHandle_t xTaskGetIdleTaskHandle( void )
2486 {
2487 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
2488 started, then xIdleTaskHandle will be NULL. */
2489 configASSERT( ( xIdleTaskHandle != NULL ) );
2490 return xIdleTaskHandle;
2491 }
2492
2493#endif /* INCLUDE_xTaskGetIdleTaskHandle */
2494/*----------------------------------------------------------*/
2495
2496/* This conditional compilation should use inequality to 0, not equality to 1.
2497This is to ensure vTaskStepTick() is available when user defined low power mode
2498implementations require configUSE_TICKLESS_IDLE to be set to a value other than
24991. */
2500#if ( configUSE_TICKLESS_IDLE != 0 )
2501
2502 void vTaskStepTick( const TickType_t xTicksToJump )
2503 {
2504 /* Correct the tick count value after a period during which the tick
2505 was suppressed. Note this does *not* call the tick hook function for
2506 each stepped tick. */
2507 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
2508 xTickCount += xTicksToJump;
2509 traceINCREASE_TICK_COUNT( xTicksToJump );
2510 }
2511
2512#endif /* configUSE_TICKLESS_IDLE */
2513/*----------------------------------------------------------*/
2514
2515#if ( INCLUDE_xTaskAbortDelay == 1 )
2516
2517 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
2518 {
2519 TCB_t *pxTCB = ( TCB_t * ) xTask;
2520 BaseType_t xReturn;
2521
2522 configASSERT( pxTCB );
2523
2524 vTaskSuspendAll();
2525 {
2526 /* A task can only be prematurely removed from the Blocked state if
2527 it is actually in the Blocked state. */
2528 if( eTaskGetState( xTask ) == eBlocked )
2529 {
2530 xReturn = pdPASS;
2531
2532 /* Remove the reference to the task from the blocked list. An
2533 interrupt won't touch the xStateListItem because the
2534 scheduler is suspended. */
2535 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2536
2537 /* Is the task waiting on an event also? If so remove it from
2538 the event list too. Interrupts can touch the event list item,
2539 even though the scheduler is suspended, so a critical section
2540 is used. */
2541 taskENTER_CRITICAL();
2542 {
2543 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2544 {
2545 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2546 pxTCB->ucDelayAborted = pdTRUE;
2547 }
2548 else
2549 {
2550 mtCOVERAGE_TEST_MARKER();
2551 }
2552 }
2553 taskEXIT_CRITICAL();
2554
2555 /* Place the unblocked task into the appropriate ready list. */
2556 prvAddTaskToReadyList( pxTCB );
2557
2558 /* A task being unblocked cannot cause an immediate context
2559 switch if preemption is turned off. */
2560 #if ( configUSE_PREEMPTION == 1 )
2561 {
2562 /* Preemption is on, but a context switch should only be
2563 performed if the unblocked task has a priority that is
2564 equal to or higher than the currently executing task. */
2565 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2566 {
2567 /* Pend the yield to be performed when the scheduler
2568 is unsuspended. */
2569 xYieldPending = pdTRUE;
2570 }
2571 else
2572 {
2573 mtCOVERAGE_TEST_MARKER();
2574 }
2575 }
2576 #endif /* configUSE_PREEMPTION */
2577 }
2578 else
2579 {
2580 xReturn = pdFAIL;
2581 }
2582 }
2583 ( void ) xTaskResumeAll();
2584
2585 return xReturn;
2586 }
2587
2588#endif /* INCLUDE_xTaskAbortDelay */
2589/*----------------------------------------------------------*/
2590
2591BaseType_t xTaskIncrementTick( void )
2592{
2593TCB_t * pxTCB;
2594TickType_t xItemValue;
2595BaseType_t xSwitchRequired = pdFALSE;
2596
2597 /* Called by the portable layer each time a tick interrupt occurs.
2598 Increments the tick then checks to see if the new tick value will cause any
2599 tasks to be unblocked. */
2600 traceTASK_INCREMENT_TICK( xTickCount );
2601 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2602 {
2603 /* Minor optimisation. The tick count cannot change in this
2604 block. */
2605 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
2606
2607 /* Increment the RTOS tick, switching the delayed and overflowed
2608 delayed lists if it wraps to 0. */
2609 xTickCount = xConstTickCount;
2610
2611 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
2612 {
2613 taskSWITCH_DELAYED_LISTS();
2614 }
2615 else
2616 {
2617 mtCOVERAGE_TEST_MARKER();
2618 }
2619
2620 /* See if this tick has made a timeout expire. Tasks are stored in
2621 the queue in the order of their wake time - meaning once one task
2622 has been found whose block time has not expired there is no need to
2623 look any further down the list. */
2624 if( xConstTickCount >= xNextTaskUnblockTime )
2625 {
2626 for( ;; )
2627 {
2628 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
2629 {
2630 /* The delayed list is empty. Set xNextTaskUnblockTime
2631 to the maximum possible value so it is extremely
2632 unlikely that the
2633 if( xTickCount >= xNextTaskUnblockTime ) test will pass
2634 next time through. */
2635 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2636 break;
2637 }
2638 else
2639 {
2640 /* The delayed list is not empty, get the value of the
2641 item at the head of the delayed list. This is the time
2642 at which the task at the head of the delayed list must
2643 be removed from the Blocked state. */
2644 pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
2645 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
2646
2647 if( xConstTickCount < xItemValue )
2648 {
2649 /* It is not time to unblock this item yet, but the
2650 item value is the time at which the task at the head
2651 of the blocked list must be removed from the Blocked
2652 state - so record the item value in
2653 xNextTaskUnblockTime. */
2654 xNextTaskUnblockTime = xItemValue;
2655 break;
2656 }
2657 else
2658 {
2659 mtCOVERAGE_TEST_MARKER();
2660 }
2661
2662 /* It is time to remove the item from the Blocked state. */
2663 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2664
2665 /* Is the task waiting on an event also? If so remove
2666 it from the event list. */
2667 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2668 {
2669 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2670 }
2671 else
2672 {
2673 mtCOVERAGE_TEST_MARKER();
2674 }
2675
2676 /* Place the unblocked task into the appropriate ready
2677 list. */
2678 prvAddTaskToReadyList( pxTCB );
2679
2680 /* A task being unblocked cannot cause an immediate
2681 context switch if preemption is turned off. */
2682 #if ( configUSE_PREEMPTION == 1 )
2683 {
2684 /* Preemption is on, but a context switch should
2685 only be performed if the unblocked task has a
2686 priority that is equal to or higher than the
2687 currently executing task. */
2688 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
2689 {
2690 xSwitchRequired = pdTRUE;
2691 }
2692 else
2693 {
2694 mtCOVERAGE_TEST_MARKER();
2695 }
2696 }
2697 #endif /* configUSE_PREEMPTION */
2698 }
2699 }
2700 }
2701
2702 /* Tasks of equal priority to the currently running task will share
2703 processing time (time slice) if preemption is on, and the application
2704 writer has not explicitly turned time slicing off. */
2705 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
2706 {
2707 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
2708 {
2709 xSwitchRequired = pdTRUE;
2710 }
2711 else
2712 {
2713 mtCOVERAGE_TEST_MARKER();
2714 }
2715 }
2716 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
2717
2718 #if ( configUSE_TICK_HOOK == 1 )
2719 {
2720 /* Guard against the tick hook being called when the pended tick
2721 count is being unwound (when the scheduler is being unlocked). */
2722 if( uxPendedTicks == ( UBaseType_t ) 0U )
2723 {
2724 vApplicationTickHook();
2725 }
2726 else
2727 {
2728 mtCOVERAGE_TEST_MARKER();
2729 }
2730 }
2731 #endif /* configUSE_TICK_HOOK */
2732 }
2733 else
2734 {
2735 ++uxPendedTicks;
2736
2737 /* The tick hook gets called at regular intervals, even if the
2738 scheduler is locked. */
2739 #if ( configUSE_TICK_HOOK == 1 )
2740 {
2741 vApplicationTickHook();
2742 }
2743 #endif
2744 }
2745
2746 #if ( configUSE_PREEMPTION == 1 )
2747 {
2748 if( xYieldPending != pdFALSE )
2749 {
2750 xSwitchRequired = pdTRUE;
2751 }
2752 else
2753 {
2754 mtCOVERAGE_TEST_MARKER();
2755 }
2756 }
2757 #endif /* configUSE_PREEMPTION */
2758
2759 return xSwitchRequired;
2760}
2761/*-----------------------------------------------------------*/
2762
2763#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2764
2765 void vTaskSetApplicationTaskTag( TaskHandle_t xTask, TaskHookFunction_t pxHookFunction )
2766 {
2767 TCB_t *xTCB;
2768
2769 /* If xTask is NULL then it is the task hook of the calling task that is
2770 getting set. */
2771 if( xTask == NULL )
2772 {
2773 xTCB = ( TCB_t * ) pxCurrentTCB;
2774 }
2775 else
2776 {
2777 xTCB = ( TCB_t * ) xTask;
2778 }
2779
2780 /* Save the hook function in the TCB. A critical section is required as
2781 the value can be accessed from an interrupt. */
2782 taskENTER_CRITICAL();
2783 xTCB->pxTaskTag = pxHookFunction;
2784 taskEXIT_CRITICAL();
2785 }
2786
2787#endif /* configUSE_APPLICATION_TASK_TAG */
2788/*-----------------------------------------------------------*/
2789
2790#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2791
2792 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
2793 {
2794 TCB_t *xTCB;
2795 TaskHookFunction_t xReturn;
2796
2797 /* If xTask is NULL then we are setting our own task hook. */
2798 if( xTask == NULL )
2799 {
2800 xTCB = ( TCB_t * ) pxCurrentTCB;
2801 }
2802 else
2803 {
2804 xTCB = ( TCB_t * ) xTask;
2805 }
2806
2807 /* Save the hook function in the TCB. A critical section is required as
2808 the value can be accessed from an interrupt. */
2809 taskENTER_CRITICAL();
2810 {
2811 xReturn = xTCB->pxTaskTag;
2812 }
2813 taskEXIT_CRITICAL();
2814
2815 return xReturn;
2816 }
2817
2818#endif /* configUSE_APPLICATION_TASK_TAG */
2819/*-----------------------------------------------------------*/
2820
2821#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2822
2823 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask, void *pvParameter )
2824 {
2825 TCB_t *xTCB;
2826 BaseType_t xReturn;
2827
2828 /* If xTask is NULL then we are calling our own task hook. */
2829 if( xTask == NULL )
2830 {
2831 xTCB = ( TCB_t * ) pxCurrentTCB;
2832 }
2833 else
2834 {
2835 xTCB = ( TCB_t * ) xTask;
2836 }
2837
2838 if( xTCB->pxTaskTag != NULL )
2839 {
2840 xReturn = xTCB->pxTaskTag( pvParameter );
2841 }
2842 else
2843 {
2844 xReturn = pdFAIL;
2845 }
2846
2847 return xReturn;
2848 }
2849
2850#endif /* configUSE_APPLICATION_TASK_TAG */
2851/*-----------------------------------------------------------*/
2852
2853void vTaskSwitchContext( void )
2854{
2855 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
2856 {
2857 /* The scheduler is currently suspended - do not allow a context
2858 switch. */
2859 xYieldPending = pdTRUE;
2860 }
2861 else
2862 {
2863 xYieldPending = pdFALSE;
2864 traceTASK_SWITCHED_OUT();
2865
2866 #if ( configGENERATE_RUN_TIME_STATS == 1 )
2867 {
2868 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2869 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
2870 #else
2871 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2872 #endif
2873
2874 /* Add the amount of time the task has been running to the
2875 accumulated time so far. The time the task started running was
2876 stored in ulTaskSwitchedInTime. Note that there is no overflow
2877 protection here so count values are only valid until the timer
2878 overflows. The guard against negative values is to protect
2879 against suspect run time stat counter implementations - which
2880 are provided by the application, not the kernel. */
2881 if( ulTotalRunTime > ulTaskSwitchedInTime )
2882 {
2883 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
2884 }
2885 else
2886 {
2887 mtCOVERAGE_TEST_MARKER();
2888 }
2889 ulTaskSwitchedInTime = ulTotalRunTime;
2890 }
2891 #endif /* configGENERATE_RUN_TIME_STATS */
2892
2893 /* Check for stack overflow, if configured. */
2894 taskCHECK_FOR_STACK_OVERFLOW();
2895
2896 /* Select a new task to run using either the generic C or port
2897 optimised asm code. */
2898 taskSELECT_HIGHEST_PRIORITY_TASK();
2899 traceTASK_SWITCHED_IN();
2900
2901 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2902 {
2903 /* Switch Newlib's _impure_ptr variable to point to the _reent
2904 structure specific to this task. */
2905 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2906 }
2907 #endif /* configUSE_NEWLIB_REENTRANT */
2908 }
2909}
2910/*-----------------------------------------------------------*/
2911
2912void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait )
2913{
2914 configASSERT( pxEventList );
2915
2916 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
2917 SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
2918
2919 /* Place the event list item of the TCB in the appropriate event list.
2920 This is placed in the list in priority order so the highest priority task
2921 is the first to be woken by the event. The queue that contains the event
2922 list is locked, preventing simultaneous access from interrupts. */
2923 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
2924
2925 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
2926}
2927/*-----------------------------------------------------------*/
2928
2929void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait )
2930{
2931 configASSERT( pxEventList );
2932
2933 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
2934 the event groups implementation. */
2935 configASSERT( uxSchedulerSuspended != 0 );
2936
2937 /* Store the item value in the event list item. It is safe to access the
2938 event list item here as interrupts won't access the event list item of a
2939 task that is not in the Blocked state. */
2940 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
2941
2942 /* Place the event list item of the TCB at the end of the appropriate event
2943 list. It is safe to access the event list here because it is part of an
2944 event group implementation - and interrupts don't access event groups
2945 directly (instead they access them indirectly by pending function calls to
2946 the task level). */
2947 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
2948
2949 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
2950}
2951/*-----------------------------------------------------------*/
2952
2953#if( configUSE_TIMERS == 1 )
2954
2955 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList, TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely )
2956 {
2957 configASSERT( pxEventList );
2958
2959 /* This function should not be called by application code hence the
2960 'Restricted' in its name. It is not part of the public API. It is
2961 designed for use by kernel code, and has special calling requirements -
2962 it should be called with the scheduler suspended. */
2963
2964
2965 /* Place the event list item of the TCB in the appropriate event list.
2966 In this case it is assume that this is the only task that is going to
2967 be waiting on this event list, so the faster vListInsertEnd() function
2968 can be used in place of vListInsert. */
2969 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
2970
2971 /* If the task should block indefinitely then set the block time to a
2972 value that will be recognised as an indefinite delay inside the
2973 prvAddCurrentTaskToDelayedList() function. */
2974 if( xWaitIndefinitely != pdFALSE )
2975 {
2976 xTicksToWait = portMAX_DELAY;
2977 }
2978
2979 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
2980 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
2981 }
2982
2983#endif /* configUSE_TIMERS */
2984/*-----------------------------------------------------------*/
2985
2986BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
2987{
2988TCB_t *pxUnblockedTCB;
2989BaseType_t xReturn;
2990
2991 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
2992 called from a critical section within an ISR. */
2993
2994 /* The event list is sorted in priority order, so the first in the list can
2995 be removed as it is known to be the highest priority. Remove the TCB from
2996 the delayed list, and add it to the ready list.
2997
2998 If an event is for a queue that is locked then this function will never
2999 get called - the lock count on the queue will get modified instead. This
3000 means exclusive access to the event list is guaranteed here.
3001
3002 This function assumes that a check has already been made to ensure that
3003 pxEventList is not empty. */
3004 pxUnblockedTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
3005 configASSERT( pxUnblockedTCB );
3006 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
3007
3008 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3009 {
3010 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
3011 prvAddTaskToReadyList( pxUnblockedTCB );
3012 }
3013 else
3014 {
3015 /* The delayed and ready lists cannot be accessed, so hold this task
3016 pending until the scheduler is resumed. */
3017 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
3018 }
3019
3020 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3021 {
3022 /* Return true if the task removed from the event list has a higher
3023 priority than the calling task. This allows the calling task to know if
3024 it should force a context switch now. */
3025 xReturn = pdTRUE;
3026
3027 /* Mark that a yield is pending in case the user is not using the
3028 "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3029 xYieldPending = pdTRUE;
3030 }
3031 else
3032 {
3033 xReturn = pdFALSE;
3034 }
3035
3036 #if( configUSE_TICKLESS_IDLE != 0 )
3037 {
3038 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3039 might be set to the blocked task's time out time. If the task is
3040 unblocked for a reason other than a timeout xNextTaskUnblockTime is
3041 normally left unchanged, because it is automatically reset to a new
3042 value when the tick count equals xNextTaskUnblockTime. However if
3043 tickless idling is used it might be more important to enter sleep mode
3044 at the earliest possible time - so reset xNextTaskUnblockTime here to
3045 ensure it is updated at the earliest possible time. */
3046 prvResetNextTaskUnblockTime();
3047 }
3048 #endif
3049
3050 return xReturn;
3051}
3052/*-----------------------------------------------------------*/
3053
3054void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, const TickType_t xItemValue )
3055{
3056TCB_t *pxUnblockedTCB;
3057
3058 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3059 the event flags implementation. */
3060 configASSERT( uxSchedulerSuspended != pdFALSE );
3061
3062 /* Store the new item value in the event list. */
3063 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3064
3065 /* Remove the event list form the event flag. Interrupts do not access
3066 event flags. */
3067 pxUnblockedTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxEventListItem );
3068 configASSERT( pxUnblockedTCB );
3069 ( void ) uxListRemove( pxEventListItem );
3070
3071 /* Remove the task from the delayed list and add it to the ready list. The
3072 scheduler is suspended so interrupts will not be accessing the ready
3073 lists. */
3074 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
3075 prvAddTaskToReadyList( pxUnblockedTCB );
3076
3077 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3078 {
3079 /* The unblocked task has a priority above that of the calling task, so
3080 a context switch is required. This function is called with the
3081 scheduler suspended so xYieldPending is set so the context switch
3082 occurs immediately that the scheduler is resumed (unsuspended). */
3083 xYieldPending = pdTRUE;
3084 }
3085}
3086/*-----------------------------------------------------------*/
3087
3088void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
3089{
3090 configASSERT( pxTimeOut );
3091 taskENTER_CRITICAL();
3092 {
3093 pxTimeOut->xOverflowCount = xNumOfOverflows;
3094 pxTimeOut->xTimeOnEntering = xTickCount;
3095 }
3096 taskEXIT_CRITICAL();
3097}
3098/*-----------------------------------------------------------*/
3099
3100void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
3101{
3102 /* For internal use only as it does not use a critical section. */
3103 pxTimeOut->xOverflowCount = xNumOfOverflows;
3104 pxTimeOut->xTimeOnEntering = xTickCount;
3105}
3106/*-----------------------------------------------------------*/
3107
3108BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, TickType_t * const pxTicksToWait )
3109{
3110BaseType_t xReturn;
3111
3112 configASSERT( pxTimeOut );
3113 configASSERT( pxTicksToWait );
3114
3115 taskENTER_CRITICAL();
3116 {
3117 /* Minor optimisation. The tick count cannot change in this block. */
3118 const TickType_t xConstTickCount = xTickCount;
3119 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
3120
3121 #if( INCLUDE_xTaskAbortDelay == 1 )
3122 if( pxCurrentTCB->ucDelayAborted != pdFALSE )
3123 {
3124 /* The delay was aborted, which is not the same as a time out,
3125 but has the same result. */
3126 pxCurrentTCB->ucDelayAborted = pdFALSE;
3127 xReturn = pdTRUE;
3128 }
3129 else
3130 #endif
3131
3132 #if ( INCLUDE_vTaskSuspend == 1 )
3133 if( *pxTicksToWait == portMAX_DELAY )
3134 {
3135 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
3136 specified is the maximum block time then the task should block
3137 indefinitely, and therefore never time out. */
3138 xReturn = pdFALSE;
3139 }
3140 else
3141 #endif
3142
3143 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
3144 {
3145 /* The tick count is greater than the time at which
3146 vTaskSetTimeout() was called, but has also overflowed since
3147 vTaskSetTimeOut() was called. It must have wrapped all the way
3148 around and gone past again. This passed since vTaskSetTimeout()
3149 was called. */
3150 xReturn = pdTRUE;
3151 }
3152 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
3153 {
3154 /* Not a genuine timeout. Adjust parameters for time remaining. */
3155 *pxTicksToWait -= xElapsedTime;
3156 vTaskInternalSetTimeOutState( pxTimeOut );
3157 xReturn = pdFALSE;
3158 }
3159 else
3160 {
3161 *pxTicksToWait = 0;
3162 xReturn = pdTRUE;
3163 }
3164 }
3165 taskEXIT_CRITICAL();
3166
3167 return xReturn;
3168}
3169/*-----------------------------------------------------------*/
3170
3171void vTaskMissedYield( void )
3172{
3173 xYieldPending = pdTRUE;
3174}
3175/*-----------------------------------------------------------*/
3176
3177#if ( configUSE_TRACE_FACILITY == 1 )
3178
3179 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
3180 {
3181 UBaseType_t uxReturn;
3182 TCB_t *pxTCB;
3183
3184 if( xTask != NULL )
3185 {
3186 pxTCB = ( TCB_t * ) xTask;
3187 uxReturn = pxTCB->uxTaskNumber;
3188 }
3189 else
3190 {
3191 uxReturn = 0U;
3192 }
3193
3194 return uxReturn;
3195 }
3196
3197#endif /* configUSE_TRACE_FACILITY */
3198/*-----------------------------------------------------------*/
3199
3200#if ( configUSE_TRACE_FACILITY == 1 )
3201
3202 void vTaskSetTaskNumber( TaskHandle_t xTask, const UBaseType_t uxHandle )
3203 {
3204 TCB_t *pxTCB;
3205
3206 if( xTask != NULL )
3207 {
3208 pxTCB = ( TCB_t * ) xTask;
3209 pxTCB->uxTaskNumber = uxHandle;
3210 }
3211 }
3212
3213#endif /* configUSE_TRACE_FACILITY */
3214
3215/*
3216 * -----------------------------------------------------------
3217 * The Idle task.
3218 * ----------------------------------------------------------
3219 *
3220 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
3221 * language extensions. The equivalent prototype for this function is:
3222 *
3223 * void prvIdleTask( void *pvParameters );
3224 *
3225 */
3226static portTASK_FUNCTION( prvIdleTask, pvParameters )
3227{
3228 /* Stop warnings. */
3229 ( void ) pvParameters;
3230
3231 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
3232 SCHEDULER IS STARTED. **/
3233
3234 /* In case a task that has a secure context deletes itself, in which case
3235 the idle task is responsible for deleting the task's secure context, if
3236 any. */
3237 portTASK_CALLS_SECURE_FUNCTIONS();
3238
3239 for( ;; )
3240 {
3241 /* See if any tasks have deleted themselves - if so then the idle task
3242 is responsible for freeing the deleted task's TCB and stack. */
3243 prvCheckTasksWaitingTermination();
3244
3245 #if ( configUSE_PREEMPTION == 0 )
3246 {
3247 /* If we are not using preemption we keep forcing a task switch to
3248 see if any other task has become available. If we are using
3249 preemption we don't need to do this as any task becoming available
3250 will automatically get the processor anyway. */
3251 taskYIELD();
3252 }
3253 #endif /* configUSE_PREEMPTION */
3254
3255 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
3256 {
3257 /* When using preemption tasks of equal priority will be
3258 timesliced. If a task that is sharing the idle priority is ready
3259 to run then the idle task should yield before the end of the
3260 timeslice.
3261
3262 A critical region is not required here as we are just reading from
3263 the list, and an occasional incorrect value will not matter. If
3264 the ready list at the idle priority contains more than one task
3265 then a task other than the idle task is ready to execute. */
3266 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
3267 {
3268 taskYIELD();
3269 }
3270 else
3271 {
3272 mtCOVERAGE_TEST_MARKER();
3273 }
3274 }
3275 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
3276
3277 #if ( configUSE_IDLE_HOOK == 1 )
3278 {
3279 extern void vApplicationIdleHook( void );
3280
3281 /* Call the user defined function from within the idle task. This
3282 allows the application designer to add background functionality
3283 without the overhead of a separate task.
3284 NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
3285 CALL A FUNCTION THAT MIGHT BLOCK. */
3286 vApplicationIdleHook();
3287 }
3288 #endif /* configUSE_IDLE_HOOK */
3289
3290 /* This conditional compilation should use inequality to 0, not equality
3291 to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
3292 user defined low power mode implementations require
3293 configUSE_TICKLESS_IDLE to be set to a value other than 1. */
3294 #if ( configUSE_TICKLESS_IDLE != 0 )
3295 {
3296 TickType_t xExpectedIdleTime;
3297
3298 /* It is not desirable to suspend then resume the scheduler on
3299 each iteration of the idle task. Therefore, a preliminary
3300 test of the expected idle time is performed without the
3301 scheduler suspended. The result here is not necessarily
3302 valid. */
3303 xExpectedIdleTime = prvGetExpectedIdleTime();
3304
3305 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3306 {
3307 vTaskSuspendAll();
3308 {
3309 /* Now the scheduler is suspended, the expected idle
3310 time can be sampled again, and this time its value can
3311 be used. */
3312 configASSERT( xNextTaskUnblockTime >= xTickCount );
3313 xExpectedIdleTime = prvGetExpectedIdleTime();
3314
3315 /* Define the following macro to set xExpectedIdleTime to 0
3316 if the application does not want
3317 portSUPPRESS_TICKS_AND_SLEEP() to be called. */
3318 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
3319
3320 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3321 {
3322 traceLOW_POWER_IDLE_BEGIN();
3323 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
3324 traceLOW_POWER_IDLE_END();
3325 }
3326 else
3327 {
3328 mtCOVERAGE_TEST_MARKER();
3329 }
3330 }
3331 ( void ) xTaskResumeAll();
3332 }
3333 else
3334 {
3335 mtCOVERAGE_TEST_MARKER();
3336 }
3337 }
3338 #endif /* configUSE_TICKLESS_IDLE */
3339 }
3340}
3341/*-----------------------------------------------------------*/
3342
3343#if( configUSE_TICKLESS_IDLE != 0 )
3344
3345 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
3346 {
3347 /* The idle task exists in addition to the application tasks. */
3348 const UBaseType_t uxNonApplicationTasks = 1;
3349 eSleepModeStatus eReturn = eStandardSleep;
3350
3351 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
3352 {
3353 /* A task was made ready while the scheduler was suspended. */
3354 eReturn = eAbortSleep;
3355 }
3356 else if( xYieldPending != pdFALSE )
3357 {
3358 /* A yield was pended while the scheduler was suspended. */
3359 eReturn = eAbortSleep;
3360 }
3361 else
3362 {
3363 /* If all the tasks are in the suspended list (which might mean they
3364 have an infinite block time rather than actually being suspended)
3365 then it is safe to turn all clocks off and just wait for external
3366 interrupts. */
3367 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
3368 {
3369 eReturn = eNoTasksWaitingTimeout;
3370 }
3371 else
3372 {
3373 mtCOVERAGE_TEST_MARKER();
3374 }
3375 }
3376
3377 return eReturn;
3378 }
3379
3380#endif /* configUSE_TICKLESS_IDLE */
3381/*-----------------------------------------------------------*/
3382
3383#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3384
3385 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue )
3386 {
3387 TCB_t *pxTCB;
3388
3389 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
3390 {
3391 pxTCB = prvGetTCBFromHandle( xTaskToSet );
3392 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3393 }
3394 }
3395
3396#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3397/*-----------------------------------------------------------*/
3398
3399#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3400
3401 void *pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery, BaseType_t xIndex )
3402 {
3403 void *pvReturn = NULL;
3404 TCB_t *pxTCB;
3405
3406 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
3407 {
3408 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3409 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
3410 }
3411 else
3412 {
3413 pvReturn = NULL;
3414 }
3415
3416 return pvReturn;
3417 }
3418
3419#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3420/*-----------------------------------------------------------*/
3421
3422#if ( portUSING_MPU_WRAPPERS == 1 )
3423
3424 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify, const MemoryRegion_t * const xRegions )
3425 {
3426 TCB_t *pxTCB;
3427
3428 /* If null is passed in here then we are modifying the MPU settings of
3429 the calling task. */
3430 pxTCB = prvGetTCBFromHandle( xTaskToModify );
3431
3432 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
3433 }
3434
3435#endif /* portUSING_MPU_WRAPPERS */
3436/*-----------------------------------------------------------*/
3437
3438static void prvInitialiseTaskLists( void )
3439{
3440UBaseType_t uxPriority;
3441
3442 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
3443 {
3444 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
3445 }
3446
3447 vListInitialise( &xDelayedTaskList1 );
3448 vListInitialise( &xDelayedTaskList2 );
3449 vListInitialise( &xPendingReadyList );
3450
3451 #if ( INCLUDE_vTaskDelete == 1 )
3452 {
3453 vListInitialise( &xTasksWaitingTermination );
3454 }
3455 #endif /* INCLUDE_vTaskDelete */
3456
3457 #if ( INCLUDE_vTaskSuspend == 1 )
3458 {
3459 vListInitialise( &xSuspendedTaskList );
3460 }
3461 #endif /* INCLUDE_vTaskSuspend */
3462
3463 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
3464 using list2. */
3465 pxDelayedTaskList = &xDelayedTaskList1;
3466 pxOverflowDelayedTaskList = &xDelayedTaskList2;
3467}
3468/*-----------------------------------------------------------*/
3469
3470static void prvCheckTasksWaitingTermination( void )
3471{
3472
3473 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
3474
3475 #if ( INCLUDE_vTaskDelete == 1 )
3476 {
3477 TCB_t *pxTCB;
3478
3479 /* uxDeletedTasksWaitingCleanUp is used to prevent vTaskSuspendAll()
3480 being called too often in the idle task. */
3481 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
3482 {
3483 taskENTER_CRITICAL();
3484 {
3485 pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
3486 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3487 --uxCurrentNumberOfTasks;
3488 --uxDeletedTasksWaitingCleanUp;
3489 }
3490 taskEXIT_CRITICAL();
3491
3492 prvDeleteTCB( pxTCB );
3493 }
3494 }
3495 #endif /* INCLUDE_vTaskDelete */
3496}
3497/*-----------------------------------------------------------*/
3498
3499#if( configUSE_TRACE_FACILITY == 1 )
3500
3501 void vTaskGetInfo( TaskHandle_t xTask, TaskStatus_t *pxTaskStatus, BaseType_t xGetFreeStackSpace, eTaskState eState )
3502 {
3503 TCB_t *pxTCB;
3504
3505 /* xTask is NULL then get the state of the calling task. */
3506 pxTCB = prvGetTCBFromHandle( xTask );
3507
3508 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
3509 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName [ 0 ] );
3510 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
3511 pxTaskStatus->pxStackBase = pxTCB->pxStack;
3512 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
3513
3514 #if ( configUSE_MUTEXES == 1 )
3515 {
3516 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
3517 }
3518 #else
3519 {
3520 pxTaskStatus->uxBasePriority = 0;
3521 }
3522 #endif
3523
3524 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3525 {
3526 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
3527 }
3528 #else
3529 {
3530 pxTaskStatus->ulRunTimeCounter = 0;
3531 }
3532 #endif
3533
3534 /* Obtaining the task state is a little fiddly, so is only done if the
3535 value of eState passed into this function is eInvalid - otherwise the
3536 state is just set to whatever is passed in. */
3537 if( eState != eInvalid )
3538 {
3539 if( pxTCB == pxCurrentTCB )
3540 {
3541 pxTaskStatus->eCurrentState = eRunning;
3542 }
3543 else
3544 {
3545 pxTaskStatus->eCurrentState = eState;
3546
3547 #if ( INCLUDE_vTaskSuspend == 1 )
3548 {
3549 /* If the task is in the suspended list then there is a
3550 chance it is actually just blocked indefinitely - so really
3551 it should be reported as being in the Blocked state. */
3552 if( eState == eSuspended )
3553 {
3554 vTaskSuspendAll();
3555 {
3556 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3557 {
3558 pxTaskStatus->eCurrentState = eBlocked;
3559 }
3560 }
3561 ( void ) xTaskResumeAll();
3562 }
3563 }
3564 #endif /* INCLUDE_vTaskSuspend */
3565 }
3566 }
3567 else
3568 {
3569 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
3570 }
3571
3572 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
3573 parameter is provided to allow it to be skipped. */
3574 if( xGetFreeStackSpace != pdFALSE )
3575 {
3576 #if ( portSTACK_GROWTH > 0 )
3577 {
3578 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
3579 }
3580 #else
3581 {
3582 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
3583 }
3584 #endif
3585 }
3586 else
3587 {
3588 pxTaskStatus->usStackHighWaterMark = 0;
3589 }
3590 }
3591
3592#endif /* configUSE_TRACE_FACILITY */
3593/*-----------------------------------------------------------*/
3594
3595#if ( configUSE_TRACE_FACILITY == 1 )
3596
3597 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState )
3598 {
3599 configLIST_VOLATILE TCB_t *pxNextTCB, *pxFirstTCB;
3600 UBaseType_t uxTask = 0;
3601
3602 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3603 {
3604 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
3605
3606 /* Populate an TaskStatus_t structure within the
3607 pxTaskStatusArray array for each task that is referenced from
3608 pxList. See the definition of TaskStatus_t in task.h for the
3609 meaning of each TaskStatus_t structure member. */
3610 do
3611 {
3612 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
3613 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
3614 uxTask++;
3615 } while( pxNextTCB != pxFirstTCB );
3616 }
3617 else
3618 {
3619 mtCOVERAGE_TEST_MARKER();
3620 }
3621
3622 return uxTask;
3623 }
3624
3625#endif /* configUSE_TRACE_FACILITY */
3626/*-----------------------------------------------------------*/
3627
3628#if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
3629
3630 static uint16_t prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
3631 {
3632 uint32_t ulCount = 0U;
3633
3634 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
3635 {
3636 pucStackByte -= portSTACK_GROWTH;
3637 ulCount++;
3638 }
3639
3640 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
3641
3642 return ( uint16_t ) ulCount;
3643 }
3644
3645#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) */
3646/*-----------------------------------------------------------*/
3647
3648#if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
3649
3650 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
3651 {
3652 TCB_t *pxTCB;
3653 uint8_t *pucEndOfStack;
3654 UBaseType_t uxReturn;
3655
3656 pxTCB = prvGetTCBFromHandle( xTask );
3657
3658 #if portSTACK_GROWTH < 0
3659 {
3660 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3661 }
3662 #else
3663 {
3664 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3665 }
3666 #endif
3667
3668 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
3669
3670 return uxReturn;
3671 }
3672
3673#endif /* INCLUDE_uxTaskGetStackHighWaterMark */
3674/*-----------------------------------------------------------*/
3675
3676#if ( INCLUDE_vTaskDelete == 1 )
3677
3678 static void prvDeleteTCB( TCB_t *pxTCB )
3679 {
3680 /* This call is required specifically for the TriCore port. It must be
3681 above the vPortFree() calls. The call is also used by ports/demos that
3682 want to allocate and clean RAM statically. */
3683 portCLEAN_UP_TCB( pxTCB );
3684
3685 /* Free up the memory allocated by the scheduler for the task. It is up
3686 to the task to free any memory allocated at the application level. */
3687 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3688 {
3689 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
3690 }
3691 #endif /* configUSE_NEWLIB_REENTRANT */
3692
3693 #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
3694 {
3695 /* The task can only have been allocated dynamically - free both
3696 the stack and TCB. */
3697 vPortFree( pxTCB->pxStack );
3698 vPortFree( pxTCB );
3699 }
3700 #elif( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 Macro has been consolidated for readability reasons. */
3701 {
3702 /* The task could have been allocated statically or dynamically, so
3703 check what was statically allocated before trying to free the
3704 memory. */
3705 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
3706 {
3707 /* Both the stack and TCB were allocated dynamically, so both
3708 must be freed. */
3709 vPortFree( pxTCB->pxStack );
3710 vPortFree( pxTCB );
3711 }
3712 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3713 {
3714 /* Only the stack was statically allocated, so the TCB is the
3715 only memory that must be freed. */
3716 vPortFree( pxTCB );
3717 }
3718 else
3719 {
3720 /* Neither the stack nor the TCB were allocated dynamically, so
3721 nothing needs to be freed. */
3722 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
3723 mtCOVERAGE_TEST_MARKER();
3724 }
3725 }
3726 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
3727 }
3728
3729#endif /* INCLUDE_vTaskDelete */
3730/*-----------------------------------------------------------*/
3731
3732static void prvResetNextTaskUnblockTime( void )
3733{
3734TCB_t *pxTCB;
3735
3736 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3737 {
3738 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
3739 the maximum possible value so it is extremely unlikely that the
3740 if( xTickCount >= xNextTaskUnblockTime ) test will pass until
3741 there is an item in the delayed list. */
3742 xNextTaskUnblockTime = portMAX_DELAY;
3743 }
3744 else
3745 {
3746 /* The new current delayed list is not empty, get the value of
3747 the item at the head of the delayed list. This is the time at
3748 which the task at the head of the delayed list should be removed
3749 from the Blocked state. */
3750 ( pxTCB ) = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
3751 xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( ( pxTCB )->xStateListItem ) );
3752 }
3753}
3754/*-----------------------------------------------------------*/
3755
3756#if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
3757
3758 TaskHandle_t xTaskGetCurrentTaskHandle( void )
3759 {
3760 TaskHandle_t xReturn;
3761
3762 /* A critical section is not required as this is not called from
3763 an interrupt and the current TCB will always be the same for any
3764 individual execution thread. */
3765 xReturn = pxCurrentTCB;
3766
3767 return xReturn;
3768 }
3769
3770#endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
3771/*-----------------------------------------------------------*/
3772
3773#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
3774
3775 BaseType_t xTaskGetSchedulerState( void )
3776 {
3777 BaseType_t xReturn;
3778
3779 if( xSchedulerRunning == pdFALSE )
3780 {
3781 xReturn = taskSCHEDULER_NOT_STARTED;
3782 }
3783 else
3784 {
3785 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3786 {
3787 xReturn = taskSCHEDULER_RUNNING;
3788 }
3789 else
3790 {
3791 xReturn = taskSCHEDULER_SUSPENDED;
3792 }
3793 }
3794
3795 return xReturn;
3796 }
3797
3798#endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
3799/*-----------------------------------------------------------*/
3800
3801#if ( configUSE_MUTEXES == 1 )
3802
3803 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
3804 {
3805 TCB_t * const pxMutexHolderTCB = ( TCB_t * ) pxMutexHolder;
3806 BaseType_t xReturn = pdFALSE;
3807
3808 /* If the mutex was given back by an interrupt while the queue was
3809 locked then the mutex holder might now be NULL. _RB_ Is this still
3810 needed as interrupts can no longer use mutexes? */
3811 if( pxMutexHolder != NULL )
3812 {
3813 /* If the holder of the mutex has a priority below the priority of
3814 the task attempting to obtain the mutex then it will temporarily
3815 inherit the priority of the task attempting to obtain the mutex. */
3816 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
3817 {
3818 /* Adjust the mutex holder state to account for its new
3819 priority. Only reset the event list item value if the value is
3820 not being used for anything else. */
3821 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
3822 {
3823 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3824 }
3825 else
3826 {
3827 mtCOVERAGE_TEST_MARKER();
3828 }
3829
3830 /* If the task being modified is in the ready state it will need
3831 to be moved into a new list. */
3832 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
3833 {
3834 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3835 {
3836 taskRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority );
3837 }
3838 else
3839 {
3840 mtCOVERAGE_TEST_MARKER();
3841 }
3842
3843 /* Inherit the priority before being moved into the new list. */
3844 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
3845 prvAddTaskToReadyList( pxMutexHolderTCB );
3846 }
3847 else
3848 {
3849 /* Just inherit the priority. */
3850 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
3851 }
3852
3853 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
3854
3855 /* Inheritance occurred. */
3856 xReturn = pdTRUE;
3857 }
3858 else
3859 {
3860 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
3861 {
3862 /* The base priority of the mutex holder is lower than the
3863 priority of the task attempting to take the mutex, but the
3864 current priority of the mutex holder is not lower than the
3865 priority of the task attempting to take the mutex.
3866 Therefore the mutex holder must have already inherited a
3867 priority, but inheritance would have occurred if that had
3868 not been the case. */
3869 xReturn = pdTRUE;
3870 }
3871 else
3872 {
3873 mtCOVERAGE_TEST_MARKER();
3874 }
3875 }
3876 }
3877 else
3878 {
3879 mtCOVERAGE_TEST_MARKER();
3880 }
3881
3882 return xReturn;
3883 }
3884
3885#endif /* configUSE_MUTEXES */
3886/*-----------------------------------------------------------*/
3887
3888#if ( configUSE_MUTEXES == 1 )
3889
3890 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
3891 {
3892 TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;
3893 BaseType_t xReturn = pdFALSE;
3894
3895 if( pxMutexHolder != NULL )
3896 {
3897 /* A task can only have an inherited priority if it holds the mutex.
3898 If the mutex is held by a task then it cannot be given from an
3899 interrupt, and if a mutex is given by the holding task then it must
3900 be the running state task. */
3901 configASSERT( pxTCB == pxCurrentTCB );
3902 configASSERT( pxTCB->uxMutexesHeld );
3903 ( pxTCB->uxMutexesHeld )--;
3904
3905 /* Has the holder of the mutex inherited the priority of another
3906 task? */
3907 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
3908 {
3909 /* Only disinherit if no other mutexes are held. */
3910 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
3911 {
3912 /* A task can only have an inherited priority if it holds
3913 the mutex. If the mutex is held by a task then it cannot be
3914 given from an interrupt, and if a mutex is given by the
3915 holding task then it must be the running state task. Remove
3916 the holding task from the ready list. */
3917 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3918 {
3919 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3920 }
3921 else
3922 {
3923 mtCOVERAGE_TEST_MARKER();
3924 }
3925
3926 /* Disinherit the priority before adding the task into the
3927 new ready list. */
3928 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
3929 pxTCB->uxPriority = pxTCB->uxBasePriority;
3930
3931 /* Reset the event list item value. It cannot be in use for
3932 any other purpose if this task is running, and it must be
3933 running to give back the mutex. */
3934 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3935 prvAddTaskToReadyList( pxTCB );
3936
3937 /* Return true to indicate that a context switch is required.
3938 This is only actually required in the corner case whereby
3939 multiple mutexes were held and the mutexes were given back
3940 in an order different to that in which they were taken.
3941 If a context switch did not occur when the first mutex was
3942 returned, even if a task was waiting on it, then a context
3943 switch should occur when the last mutex is returned whether
3944 a task is waiting on it or not. */
3945 xReturn = pdTRUE;
3946 }
3947 else
3948 {
3949 mtCOVERAGE_TEST_MARKER();
3950 }
3951 }
3952 else
3953 {
3954 mtCOVERAGE_TEST_MARKER();
3955 }
3956 }
3957 else
3958 {
3959 mtCOVERAGE_TEST_MARKER();
3960 }
3961
3962 return xReturn;
3963 }
3964
3965#endif /* configUSE_MUTEXES */
3966/*-----------------------------------------------------------*/
3967
3968#if ( configUSE_MUTEXES == 1 )
3969
3970 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder, UBaseType_t uxHighestPriorityWaitingTask )
3971 {
3972 TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;
3973 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
3974 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
3975
3976 if( pxMutexHolder != NULL )
3977 {
3978 /* If pxMutexHolder is not NULL then the holder must hold at least
3979 one mutex. */
3980 configASSERT( pxTCB->uxMutexesHeld );
3981
3982 /* Determine the priority to which the priority of the task that
3983 holds the mutex should be set. This will be the greater of the
3984 holding task's base priority and the priority of the highest
3985 priority task that is waiting to obtain the mutex. */
3986 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
3987 {
3988 uxPriorityToUse = uxHighestPriorityWaitingTask;
3989 }
3990 else
3991 {
3992 uxPriorityToUse = pxTCB->uxBasePriority;
3993 }
3994
3995 /* Does the priority need to change? */
3996 if( pxTCB->uxPriority != uxPriorityToUse )
3997 {
3998 /* Only disinherit if no other mutexes are held. This is a
3999 simplification in the priority inheritance implementation. If
4000 the task that holds the mutex is also holding other mutexes then
4001 the other mutexes may have caused the priority inheritance. */
4002 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
4003 {
4004 /* If a task has timed out because it already holds the
4005 mutex it was trying to obtain then it cannot of inherited
4006 its own priority. */
4007 configASSERT( pxTCB != pxCurrentTCB );
4008
4009 /* Disinherit the priority, remembering the previous
4010 priority to facilitate determining the subject task's
4011 state. */
4012 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4013 uxPriorityUsedOnEntry = pxTCB->uxPriority;
4014 pxTCB->uxPriority = uxPriorityToUse;
4015
4016 /* Only reset the event list item value if the value is not
4017 being used for anything else. */
4018 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4019 {
4020 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4021 }
4022 else
4023 {
4024 mtCOVERAGE_TEST_MARKER();
4025 }
4026
4027 /* If the running task is not the task that holds the mutex
4028 then the task that holds the mutex could be in either the
4029 Ready, Blocked or Suspended states. Only remove the task
4030 from its current state list if it is in the Ready state as
4031 the task's priority is going to change and there is one
4032 Ready list per priority. */
4033 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
4034 {
4035 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4036 {
4037 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
4038 }
4039 else
4040 {
4041 mtCOVERAGE_TEST_MARKER();
4042 }
4043
4044 prvAddTaskToReadyList( pxTCB );
4045 }
4046 else
4047 {
4048 mtCOVERAGE_TEST_MARKER();
4049 }
4050 }
4051 else
4052 {
4053 mtCOVERAGE_TEST_MARKER();
4054 }
4055 }
4056 else
4057 {
4058 mtCOVERAGE_TEST_MARKER();
4059 }
4060 }
4061 else
4062 {
4063 mtCOVERAGE_TEST_MARKER();
4064 }
4065 }
4066
4067#endif /* configUSE_MUTEXES */
4068/*-----------------------------------------------------------*/
4069
4070#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4071
4072 void vTaskEnterCritical( void )
4073 {
4074 portDISABLE_INTERRUPTS();
4075
4076 if( xSchedulerRunning != pdFALSE )
4077 {
4078 ( pxCurrentTCB->uxCriticalNesting )++;
4079
4080 /* This is not the interrupt safe version of the enter critical
4081 function so assert() if it is being called from an interrupt
4082 context. Only API functions that end in "FromISR" can be used in an
4083 interrupt. Only assert if the critical nesting count is 1 to
4084 protect against recursive calls if the assert function also uses a
4085 critical section. */
4086 if( pxCurrentTCB->uxCriticalNesting == 1 )
4087 {
4088 portASSERT_IF_IN_ISR();
4089 }
4090 }
4091 else
4092 {
4093 mtCOVERAGE_TEST_MARKER();
4094 }
4095 }
4096
4097#endif /* portCRITICAL_NESTING_IN_TCB */
4098/*-----------------------------------------------------------*/
4099
4100#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4101
4102 void vTaskExitCritical( void )
4103 {
4104 if( xSchedulerRunning != pdFALSE )
4105 {
4106 if( pxCurrentTCB->uxCriticalNesting > 0U )
4107 {
4108 ( pxCurrentTCB->uxCriticalNesting )--;
4109
4110 if( pxCurrentTCB->uxCriticalNesting == 0U )
4111 {
4112 portENABLE_INTERRUPTS();
4113 }
4114 else
4115 {
4116 mtCOVERAGE_TEST_MARKER();
4117 }
4118 }
4119 else
4120 {
4121 mtCOVERAGE_TEST_MARKER();
4122 }
4123 }
4124 else
4125 {
4126 mtCOVERAGE_TEST_MARKER();
4127 }
4128 }
4129
4130#endif /* portCRITICAL_NESTING_IN_TCB */
4131/*-----------------------------------------------------------*/
4132
4133#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4134
4135 static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName )
4136 {
4137 size_t x;
4138
4139 /* Start by copying the entire string. */
4140 strcpy( pcBuffer, pcTaskName );
4141
4142 /* Pad the end of the string with spaces to ensure columns line up when
4143 printed out. */
4144 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
4145 {
4146 pcBuffer[ x ] = ' ';
4147 }
4148
4149 /* Terminate. */
4150 pcBuffer[ x ] = 0x00;
4151
4152 /* Return the new end of string. */
4153 return &( pcBuffer[ x ] );
4154 }
4155
4156#endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
4157/*-----------------------------------------------------------*/
4158
4159#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
4160
4161 void vTaskList( char * pcWriteBuffer )
4162 {
4163 TaskStatus_t *pxTaskStatusArray;
4164 volatile UBaseType_t uxArraySize, x;
4165 char cStatus;
4166
4167 /*
4168 * PLEASE NOTE:
4169 *
4170 * This function is provided for convenience only, and is used by many
4171 * of the demo applications. Do not consider it to be part of the
4172 * scheduler.
4173 *
4174 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
4175 * uxTaskGetSystemState() output into a human readable table that
4176 * displays task names, states and stack usage.
4177 *
4178 * vTaskList() has a dependency on the sprintf() C library function that
4179 * might bloat the code size, use a lot of stack, and provide different
4180 * results on different platforms. An alternative, tiny, third party,
4181 * and limited functionality implementation of sprintf() is provided in
4182 * many of the FreeRTOS/Demo sub-directories in a file called
4183 * printf-stdarg.c (note printf-stdarg.c does not provide a full
4184 * snprintf() implementation!).
4185 *
4186 * It is recommended that production systems call uxTaskGetSystemState()
4187 * directly to get access to raw stats data, rather than indirectly
4188 * through a call to vTaskList().
4189 */
4190
4191
4192 /* Make sure the write buffer does not contain a string. */
4193 *pcWriteBuffer = 0x00;
4194
4195 /* Take a snapshot of the number of tasks in case it changes while this
4196 function is executing. */
4197 uxArraySize = uxCurrentNumberOfTasks;
4198
4199 /* Allocate an array index for each task. NOTE! if
4200 configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4201 equate to NULL. */
4202 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
4203
4204 if( pxTaskStatusArray != NULL )
4205 {
4206 /* Generate the (binary) data. */
4207 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
4208
4209 /* Create a human readable table from the binary data. */
4210 for( x = 0; x < uxArraySize; x++ )
4211 {
4212 switch( pxTaskStatusArray[ x ].eCurrentState )
4213 {
4214 case eRunning: cStatus = tskRUNNING_CHAR;
4215 break;
4216
4217 case eReady: cStatus = tskREADY_CHAR;
4218 break;
4219
4220 case eBlocked: cStatus = tskBLOCKED_CHAR;
4221 break;
4222
4223 case eSuspended: cStatus = tskSUSPENDED_CHAR;
4224 break;
4225
4226 case eDeleted: cStatus = tskDELETED_CHAR;
4227 break;
4228
4229 default: /* Should not get here, but it is included
4230 to prevent static checking errors. */
4231 cStatus = 0x00;
4232 break;
4233 }
4234
4235 /* Write the task name to the string, padding with spaces so it
4236 can be printed in tabular form more easily. */
4237 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4238
4239 /* Write the rest of the string. */
4240 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
4241 pcWriteBuffer += strlen( pcWriteBuffer );
4242 }
4243
4244 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4245 is 0 then vPortFree() will be #defined to nothing. */
4246 vPortFree( pxTaskStatusArray );
4247 }
4248 else
4249 {
4250 mtCOVERAGE_TEST_MARKER();
4251 }
4252 }
4253
4254#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
4255/*----------------------------------------------------------*/
4256
4257#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
4258
4259 void vTaskGetRunTimeStats( char *pcWriteBuffer )
4260 {
4261 TaskStatus_t *pxTaskStatusArray;
4262 volatile UBaseType_t uxArraySize, x;
4263 uint32_t ulTotalTime, ulStatsAsPercentage;
4264
4265 #if( configUSE_TRACE_FACILITY != 1 )
4266 {
4267 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
4268 }
4269 #endif
4270
4271 /*
4272 * PLEASE NOTE:
4273 *
4274 * This function is provided for convenience only, and is used by many
4275 * of the demo applications. Do not consider it to be part of the
4276 * scheduler.
4277 *
4278 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
4279 * of the uxTaskGetSystemState() output into a human readable table that
4280 * displays the amount of time each task has spent in the Running state
4281 * in both absolute and percentage terms.
4282 *
4283 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
4284 * function that might bloat the code size, use a lot of stack, and
4285 * provide different results on different platforms. An alternative,
4286 * tiny, third party, and limited functionality implementation of
4287 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
4288 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
4289 * a full snprintf() implementation!).
4290 *
4291 * It is recommended that production systems call uxTaskGetSystemState()
4292 * directly to get access to raw stats data, rather than indirectly
4293 * through a call to vTaskGetRunTimeStats().
4294 */
4295
4296 /* Make sure the write buffer does not contain a string. */
4297 *pcWriteBuffer = 0x00;
4298
4299 /* Take a snapshot of the number of tasks in case it changes while this
4300 function is executing. */
4301 uxArraySize = uxCurrentNumberOfTasks;
4302
4303 /* Allocate an array index for each task. NOTE! If
4304 configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4305 equate to NULL. */
4306 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
4307
4308 if( pxTaskStatusArray != NULL )
4309 {
4310 /* Generate the (binary) data. */
4311 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
4312
4313 /* For percentage calculations. */
4314 ulTotalTime /= 100UL;
4315
4316 /* Avoid divide by zero errors. */
4317 if( ulTotalTime > 0 )
4318 {
4319 /* Create a human readable table from the binary data. */
4320 for( x = 0; x < uxArraySize; x++ )
4321 {
4322 /* What percentage of the total run time has the task used?
4323 This will always be rounded down to the nearest integer.
4324 ulTotalRunTimeDiv100 has already been divided by 100. */
4325 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
4326
4327 /* Write the task name to the string, padding with
4328 spaces so it can be printed in tabular form more
4329 easily. */
4330 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4331
4332 if( ulStatsAsPercentage > 0UL )
4333 {
4334 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4335 {
4336 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
4337 }
4338 #else
4339 {
4340 /* sizeof( int ) == sizeof( long ) so a smaller
4341 printf() library can be used. */
4342 sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
4343 }
4344 #endif
4345 }
4346 else
4347 {
4348 /* If the percentage is zero here then the task has
4349 consumed less than 1% of the total run time. */
4350 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4351 {
4352 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
4353 }
4354 #else
4355 {
4356 /* sizeof( int ) == sizeof( long ) so a smaller
4357 printf() library can be used. */
4358 sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
4359 }
4360 #endif
4361 }
4362
4363 pcWriteBuffer += strlen( pcWriteBuffer );
4364 }
4365 }
4366 else
4367 {
4368 mtCOVERAGE_TEST_MARKER();
4369 }
4370
4371 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4372 is 0 then vPortFree() will be #defined to nothing. */
4373 vPortFree( pxTaskStatusArray );
4374 }
4375 else
4376 {
4377 mtCOVERAGE_TEST_MARKER();
4378 }
4379 }
4380
4381#endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
4382/*-----------------------------------------------------------*/
4383
4384TickType_t uxTaskResetEventItemValue( void )
4385{
4386TickType_t uxReturn;
4387
4388 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
4389
4390 /* Reset the event list item to its normal value - so it can be used with
4391 queues and semaphores. */
4392 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4393
4394 return uxReturn;
4395}
4396/*-----------------------------------------------------------*/
4397
4398#if ( configUSE_MUTEXES == 1 )
4399
4400 void *pvTaskIncrementMutexHeldCount( void )
4401 {
4402 /* If xSemaphoreCreateMutex() is called before any tasks have been created
4403 then pxCurrentTCB will be NULL. */
4404 if( pxCurrentTCB != NULL )
4405 {
4406 ( pxCurrentTCB->uxMutexesHeld )++;
4407 }
4408
4409 return pxCurrentTCB;
4410 }
4411
4412#endif /* configUSE_MUTEXES */
4413/*-----------------------------------------------------------*/
4414
4415#if( configUSE_TASK_NOTIFICATIONS == 1 )
4416
4417 uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait )
4418 {
4419 uint32_t ulReturn;
4420
4421 taskENTER_CRITICAL();
4422 {
4423 /* Only block if the notification count is not already non-zero. */
4424 if( pxCurrentTCB->ulNotifiedValue == 0UL )
4425 {
4426 /* Mark this task as waiting for a notification. */
4427 pxCurrentTCB->ucNotifyState = taskWAITING_NOTIFICATION;
4428
4429 if( xTicksToWait > ( TickType_t ) 0 )
4430 {
4431 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4432 traceTASK_NOTIFY_TAKE_BLOCK();
4433
4434 /* All ports are written to allow a yield in a critical
4435 section (some will yield immediately, others wait until the
4436 critical section exits) - but it is not something that
4437 application code should ever do. */
4438 portYIELD_WITHIN_API();
4439 }
4440 else
4441 {
4442 mtCOVERAGE_TEST_MARKER();
4443 }
4444 }
4445 else
4446 {
4447 mtCOVERAGE_TEST_MARKER();
4448 }
4449 }
4450 taskEXIT_CRITICAL();
4451
4452 taskENTER_CRITICAL();
4453 {
4454 traceTASK_NOTIFY_TAKE();
4455 ulReturn = pxCurrentTCB->ulNotifiedValue;
4456
4457 if( ulReturn != 0UL )
4458 {
4459 if( xClearCountOnExit != pdFALSE )
4460 {
4461 pxCurrentTCB->ulNotifiedValue = 0UL;
4462 }
4463 else
4464 {
4465 pxCurrentTCB->ulNotifiedValue = ulReturn - ( uint32_t ) 1;
4466 }
4467 }
4468 else
4469 {
4470 mtCOVERAGE_TEST_MARKER();
4471 }
4472
4473 pxCurrentTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;
4474 }
4475 taskEXIT_CRITICAL();
4476
4477 return ulReturn;
4478 }
4479
4480#endif /* configUSE_TASK_NOTIFICATIONS */
4481/*-----------------------------------------------------------*/
4482
4483#if( configUSE_TASK_NOTIFICATIONS == 1 )
4484
4485 BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait )
4486 {
4487 BaseType_t xReturn;
4488
4489 taskENTER_CRITICAL();
4490 {
4491 /* Only block if a notification is not already pending. */
4492 if( pxCurrentTCB->ucNotifyState != taskNOTIFICATION_RECEIVED )
4493 {
4494 /* Clear bits in the task's notification value as bits may get
4495 set by the notifying task or interrupt. This can be used to
4496 clear the value to zero. */
4497 pxCurrentTCB->ulNotifiedValue &= ~ulBitsToClearOnEntry;
4498
4499 /* Mark this task as waiting for a notification. */
4500 pxCurrentTCB->ucNotifyState = taskWAITING_NOTIFICATION;
4501
4502 if( xTicksToWait > ( TickType_t ) 0 )
4503 {
4504 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4505 traceTASK_NOTIFY_WAIT_BLOCK();
4506
4507 /* All ports are written to allow a yield in a critical
4508 section (some will yield immediately, others wait until the
4509 critical section exits) - but it is not something that
4510 application code should ever do. */
4511 portYIELD_WITHIN_API();
4512 }
4513 else
4514 {
4515 mtCOVERAGE_TEST_MARKER();
4516 }
4517 }
4518 else
4519 {
4520 mtCOVERAGE_TEST_MARKER();
4521 }
4522 }
4523 taskEXIT_CRITICAL();
4524
4525 taskENTER_CRITICAL();
4526 {
4527 traceTASK_NOTIFY_WAIT();
4528
4529 if( pulNotificationValue != NULL )
4530 {
4531 /* Output the current notification value, which may or may not
4532 have changed. */
4533 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue;
4534 }
4535
4536 /* If ucNotifyValue is set then either the task never entered the
4537 blocked state (because a notification was already pending) or the
4538 task unblocked because of a notification. Otherwise the task
4539 unblocked because of a timeout. */
4540 if( pxCurrentTCB->ucNotifyState != taskNOTIFICATION_RECEIVED )
4541 {
4542 /* A notification was not received. */
4543 xReturn = pdFALSE;
4544 }
4545 else
4546 {
4547 /* A notification was already pending or a notification was
4548 received while the task was waiting. */
4549 pxCurrentTCB->ulNotifiedValue &= ~ulBitsToClearOnExit;
4550 xReturn = pdTRUE;
4551 }
4552
4553 pxCurrentTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;
4554 }
4555 taskEXIT_CRITICAL();
4556
4557 return xReturn;
4558 }
4559
4560#endif /* configUSE_TASK_NOTIFICATIONS */
4561/*-----------------------------------------------------------*/
4562
4563#if( configUSE_TASK_NOTIFICATIONS == 1 )
4564
4565 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotificationValue )
4566 {
4567 TCB_t * pxTCB;
4568 BaseType_t xReturn = pdPASS;
4569 uint8_t ucOriginalNotifyState;
4570
4571 configASSERT( xTaskToNotify );
4572 pxTCB = ( TCB_t * ) xTaskToNotify;
4573
4574 taskENTER_CRITICAL();
4575 {
4576 if( pulPreviousNotificationValue != NULL )
4577 {
4578 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue;
4579 }
4580
4581 ucOriginalNotifyState = pxTCB->ucNotifyState;
4582
4583 pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;
4584
4585 switch( eAction )
4586 {
4587 case eSetBits :
4588 pxTCB->ulNotifiedValue |= ulValue;
4589 break;
4590
4591 case eIncrement :
4592 ( pxTCB->ulNotifiedValue )++;
4593 break;
4594
4595 case eSetValueWithOverwrite :
4596 pxTCB->ulNotifiedValue = ulValue;
4597 break;
4598
4599 case eSetValueWithoutOverwrite :
4600 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
4601 {
4602 pxTCB->ulNotifiedValue = ulValue;
4603 }
4604 else
4605 {
4606 /* The value could not be written to the task. */
4607 xReturn = pdFAIL;
4608 }
4609 break;
4610
4611 case eNoAction:
4612 /* The task is being notified without its notify value being
4613 updated. */
4614 break;
4615 }
4616
4617 traceTASK_NOTIFY();
4618
4619 /* If the task is in the blocked state specifically to wait for a
4620 notification then unblock it now. */
4621 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4622 {
4623 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4624 prvAddTaskToReadyList( pxTCB );
4625
4626 /* The task should not have been on an event list. */
4627 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4628
4629 #if( configUSE_TICKLESS_IDLE != 0 )
4630 {
4631 /* If a task is blocked waiting for a notification then
4632 xNextTaskUnblockTime might be set to the blocked task's time
4633 out time. If the task is unblocked for a reason other than
4634 a timeout xNextTaskUnblockTime is normally left unchanged,
4635 because it will automatically get reset to a new value when
4636 the tick count equals xNextTaskUnblockTime. However if
4637 tickless idling is used it might be more important to enter
4638 sleep mode at the earliest possible time - so reset
4639 xNextTaskUnblockTime here to ensure it is updated at the
4640 earliest possible time. */
4641 prvResetNextTaskUnblockTime();
4642 }
4643 #endif
4644
4645 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4646 {
4647 /* The notified task has a priority above the currently
4648 executing task so a yield is required. */
4649 taskYIELD_IF_USING_PREEMPTION();
4650 }
4651 else
4652 {
4653 mtCOVERAGE_TEST_MARKER();
4654 }
4655 }
4656 else
4657 {
4658 mtCOVERAGE_TEST_MARKER();
4659 }
4660 }
4661 taskEXIT_CRITICAL();
4662
4663 return xReturn;
4664 }
4665
4666#endif /* configUSE_TASK_NOTIFICATIONS */
4667/*-----------------------------------------------------------*/
4668
4669#if( configUSE_TASK_NOTIFICATIONS == 1 )
4670
4671 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotificationValue, BaseType_t *pxHigherPriorityTaskWoken )
4672 {
4673 TCB_t * pxTCB;
4674 uint8_t ucOriginalNotifyState;
4675 BaseType_t xReturn = pdPASS;
4676 UBaseType_t uxSavedInterruptStatus;
4677
4678 configASSERT( xTaskToNotify );
4679
4680 /* RTOS ports that support interrupt nesting have the concept of a
4681 maximum system call (or maximum API call) interrupt priority.
4682 Interrupts that are above the maximum system call priority are keep
4683 permanently enabled, even when the RTOS kernel is in a critical section,
4684 but cannot make any calls to FreeRTOS API functions. If configASSERT()
4685 is defined in FreeRTOSConfig.h then
4686 portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4687 failure if a FreeRTOS API function is called from an interrupt that has
4688 been assigned a priority above the configured maximum system call
4689 priority. Only FreeRTOS functions that end in FromISR can be called
4690 from interrupts that have been assigned a priority at or (logically)
4691 below the maximum system call interrupt priority. FreeRTOS maintains a
4692 separate interrupt safe API to ensure interrupt entry is as fast and as
4693 simple as possible. More information (albeit Cortex-M specific) is
4694 provided on the following link:
4695 http://www.freertos.org/RTOS-Cortex-M3-M4.html */
4696 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4697
4698 pxTCB = ( TCB_t * ) xTaskToNotify;
4699
4700 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
4701 {
4702 if( pulPreviousNotificationValue != NULL )
4703 {
4704 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue;
4705 }
4706
4707 ucOriginalNotifyState = pxTCB->ucNotifyState;
4708 pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;
4709
4710 switch( eAction )
4711 {
4712 case eSetBits :
4713 pxTCB->ulNotifiedValue |= ulValue;
4714 break;
4715
4716 case eIncrement :
4717 ( pxTCB->ulNotifiedValue )++;
4718 break;
4719
4720 case eSetValueWithOverwrite :
4721 pxTCB->ulNotifiedValue = ulValue;
4722 break;
4723
4724 case eSetValueWithoutOverwrite :
4725 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
4726 {
4727 pxTCB->ulNotifiedValue = ulValue;
4728 }
4729 else
4730 {
4731 /* The value could not be written to the task. */
4732 xReturn = pdFAIL;
4733 }
4734 break;
4735
4736 case eNoAction :
4737 /* The task is being notified without its notify value being
4738 updated. */
4739 break;
4740 }
4741
4742 traceTASK_NOTIFY_FROM_ISR();
4743
4744 /* If the task is in the blocked state specifically to wait for a
4745 notification then unblock it now. */
4746 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4747 {
4748 /* The task should not have been on an event list. */
4749 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4750
4751 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4752 {
4753 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4754 prvAddTaskToReadyList( pxTCB );
4755 }
4756 else
4757 {
4758 /* The delayed and ready lists cannot be accessed, so hold
4759 this task pending until the scheduler is resumed. */
4760 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
4761 }
4762
4763 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4764 {
4765 /* The notified task has a priority above the currently
4766 executing task so a yield is required. */
4767 if( pxHigherPriorityTaskWoken != NULL )
4768 {
4769 *pxHigherPriorityTaskWoken = pdTRUE;
4770 }
4771 else
4772 {
4773 /* Mark that a yield is pending in case the user is not
4774 using the "xHigherPriorityTaskWoken" parameter to an ISR
4775 safe FreeRTOS function. */
4776 xYieldPending = pdTRUE;
4777 }
4778 }
4779 else
4780 {
4781 mtCOVERAGE_TEST_MARKER();
4782 }
4783 }
4784 }
4785 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4786
4787 return xReturn;
4788 }
4789
4790#endif /* configUSE_TASK_NOTIFICATIONS */
4791/*-----------------------------------------------------------*/
4792
4793#if( configUSE_TASK_NOTIFICATIONS == 1 )
4794
4795 void vTaskNotifyGiveFromISR( TaskHandle_t xTaskToNotify, BaseType_t *pxHigherPriorityTaskWoken )
4796 {
4797 TCB_t * pxTCB;
4798 uint8_t ucOriginalNotifyState;
4799 UBaseType_t uxSavedInterruptStatus;
4800
4801 configASSERT( xTaskToNotify );
4802
4803 /* RTOS ports that support interrupt nesting have the concept of a
4804 maximum system call (or maximum API call) interrupt priority.
4805 Interrupts that are above the maximum system call priority are keep
4806 permanently enabled, even when the RTOS kernel is in a critical section,
4807 but cannot make any calls to FreeRTOS API functions. If configASSERT()
4808 is defined in FreeRTOSConfig.h then
4809 portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4810 failure if a FreeRTOS API function is called from an interrupt that has
4811 been assigned a priority above the configured maximum system call
4812 priority. Only FreeRTOS functions that end in FromISR can be called
4813 from interrupts that have been assigned a priority at or (logically)
4814 below the maximum system call interrupt priority. FreeRTOS maintains a
4815 separate interrupt safe API to ensure interrupt entry is as fast and as
4816 simple as possible. More information (albeit Cortex-M specific) is
4817 provided on the following link:
4818 http://www.freertos.org/RTOS-Cortex-M3-M4.html */
4819 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4820
4821 pxTCB = ( TCB_t * ) xTaskToNotify;
4822
4823 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
4824 {
4825 ucOriginalNotifyState = pxTCB->ucNotifyState;
4826 pxTCB->ucNotifyState = taskNOTIFICATION_RECEIVED;
4827
4828 /* 'Giving' is equivalent to incrementing a count in a counting
4829 semaphore. */
4830 ( pxTCB->ulNotifiedValue )++;
4831
4832 traceTASK_NOTIFY_GIVE_FROM_ISR();
4833
4834 /* If the task is in the blocked state specifically to wait for a
4835 notification then unblock it now. */
4836 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4837 {
4838 /* The task should not have been on an event list. */
4839 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4840
4841 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4842 {
4843 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4844 prvAddTaskToReadyList( pxTCB );
4845 }
4846 else
4847 {
4848 /* The delayed and ready lists cannot be accessed, so hold
4849 this task pending until the scheduler is resumed. */
4850 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
4851 }
4852
4853 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4854 {
4855 /* The notified task has a priority above the currently
4856 executing task so a yield is required. */
4857 if( pxHigherPriorityTaskWoken != NULL )
4858 {
4859 *pxHigherPriorityTaskWoken = pdTRUE;
4860 }
4861 else
4862 {
4863 /* Mark that a yield is pending in case the user is not
4864 using the "xHigherPriorityTaskWoken" parameter in an ISR
4865 safe FreeRTOS function. */
4866 xYieldPending = pdTRUE;
4867 }
4868 }
4869 else
4870 {
4871 mtCOVERAGE_TEST_MARKER();
4872 }
4873 }
4874 }
4875 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4876 }
4877
4878#endif /* configUSE_TASK_NOTIFICATIONS */
4879
4880/*-----------------------------------------------------------*/
4881
4882#if( configUSE_TASK_NOTIFICATIONS == 1 )
4883
4884 BaseType_t xTaskNotifyStateClear( TaskHandle_t xTask )
4885 {
4886 TCB_t *pxTCB;
4887 BaseType_t xReturn;
4888
4889 /* If null is passed in here then it is the calling task that is having
4890 its notification state cleared. */
4891 pxTCB = prvGetTCBFromHandle( xTask );
4892
4893 taskENTER_CRITICAL();
4894 {
4895 if( pxTCB->ucNotifyState == taskNOTIFICATION_RECEIVED )
4896 {
4897 pxTCB->ucNotifyState = taskNOT_WAITING_NOTIFICATION;
4898 xReturn = pdPASS;
4899 }
4900 else
4901 {
4902 xReturn = pdFAIL;
4903 }
4904 }
4905 taskEXIT_CRITICAL();
4906
4907 return xReturn;
4908 }
4909
4910#endif /* configUSE_TASK_NOTIFICATIONS */
4911/*-----------------------------------------------------------*/
4912
4913
4914static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const BaseType_t xCanBlockIndefinitely )
4915{
4916TickType_t xTimeToWake;
4917const TickType_t xConstTickCount = xTickCount;
4918
4919 #if( INCLUDE_xTaskAbortDelay == 1 )
4920 {
4921 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
4922 reset to pdFALSE so it can be detected as having been set to pdTRUE
4923 when the task leaves the Blocked state. */
4924 pxCurrentTCB->ucDelayAborted = pdFALSE;
4925 }
4926 #endif
4927
4928 /* Remove the task from the ready list before adding it to the blocked list
4929 as the same list item is used for both lists. */
4930 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4931 {
4932 /* The current task must be in a ready list, so there is no need to
4933 check, and the port reset macro can be called directly. */
4934 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
4935 }
4936 else
4937 {
4938 mtCOVERAGE_TEST_MARKER();
4939 }
4940
4941 #if ( INCLUDE_vTaskSuspend == 1 )
4942 {
4943 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
4944 {
4945 /* Add the task to the suspended task list instead of a delayed task
4946 list to ensure it is not woken by a timing event. It will block
4947 indefinitely. */
4948 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
4949 }
4950 else
4951 {
4952 /* Calculate the time at which the task should be woken if the event
4953 does not occur. This may overflow but this doesn't matter, the
4954 kernel will manage it correctly. */
4955 xTimeToWake = xConstTickCount + xTicksToWait;
4956
4957 /* The list item will be inserted in wake time order. */
4958 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
4959
4960 if( xTimeToWake < xConstTickCount )
4961 {
4962 /* Wake time has overflowed. Place this item in the overflow
4963 list. */
4964 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
4965 }
4966 else
4967 {
4968 /* The wake time has not overflowed, so the current block list
4969 is used. */
4970 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
4971
4972 /* If the task entering the blocked state was placed at the
4973 head of the list of blocked tasks then xNextTaskUnblockTime
4974 needs to be updated too. */
4975 if( xTimeToWake < xNextTaskUnblockTime )
4976 {
4977 xNextTaskUnblockTime = xTimeToWake;
4978 }
4979 else
4980 {
4981 mtCOVERAGE_TEST_MARKER();
4982 }
4983 }
4984 }
4985 }
4986 #else /* INCLUDE_vTaskSuspend */
4987 {
4988 /* Calculate the time at which the task should be woken if the event
4989 does not occur. This may overflow but this doesn't matter, the kernel
4990 will manage it correctly. */
4991 xTimeToWake = xConstTickCount + xTicksToWait;
4992
4993 /* The list item will be inserted in wake time order. */
4994 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
4995
4996 if( xTimeToWake < xConstTickCount )
4997 {
4998 /* Wake time has overflowed. Place this item in the overflow list. */
4999 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5000 }
5001 else
5002 {
5003 /* The wake time has not overflowed, so the current block list is used. */
5004 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5005
5006 /* If the task entering the blocked state was placed at the head of the
5007 list of blocked tasks then xNextTaskUnblockTime needs to be updated
5008 too. */
5009 if( xTimeToWake < xNextTaskUnblockTime )
5010 {
5011 xNextTaskUnblockTime = xTimeToWake;
5012 }
5013 else
5014 {
5015 mtCOVERAGE_TEST_MARKER();
5016 }
5017 }
5018
5019 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
5020 ( void ) xCanBlockIndefinitely;
5021 }
5022 #endif /* INCLUDE_vTaskSuspend */
5023}
5024
Jianxiong Pan27d913f2020-08-03 15:22:00 +08005025void vTaskDumpStack(TaskHandle_t xTask)
5026{
5027 TCB_t *pxTCB;
5028 StackType_t *p;
5029 int i;
5030
5031 pxTCB = prvGetTCBFromHandle( xTask );
5032 if (!pxTCB)
5033 return;
5034 p = pxTCB->pxStack+pxTCB->uStackDepth-1;
5035 printf("Dump Stack:\n");
5036 while (p >= pxTCB->pxStack) {
5037 printf("%08p:",(unsigned long)p);
5038 for (i = 0; i < 8 && p >= pxTCB->pxStack; i++)
5039 printf(" %08x",*p--);
5040 printf("\n");
5041 }
5042}
5043
Qiufang Dai35c31332020-05-13 15:29:06 +08005044/* Code below here allows additional code to be inserted into this source file,
5045especially where access to file scope functions and data is needed (for example
5046when performing module tests). */
5047
5048#ifdef FREERTOS_MODULE_TEST
5049 #include "tasks_test_access_functions.h"
5050#endif
5051
5052
5053#if( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
5054
5055 #include "freertos_tasks_c_additions.h"
5056
5057 static void freertos_tasks_c_additions_init( void )
5058 {
5059 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
5060 FREERTOS_TASKS_C_ADDITIONS_INIT();
5061 #endif
5062 }
5063
5064#endif
5065
5066