2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
20 #include "common/time.h"
23 TASK_PRIORITY_IDLE
= 0, // Disables dynamic scheduling, task is executed only if no other task is active this cycle
24 TASK_PRIORITY_LOW
= 1,
25 TASK_PRIORITY_MEDIUM
= 3,
26 TASK_PRIORITY_MEDIUM_HIGH
= 4,
27 TASK_PRIORITY_HIGH
= 5,
28 TASK_PRIORITY_REALTIME
= 18,
29 TASK_PRIORITY_MAX
= 255
33 timeUs_t maxExecutionTime
;
34 timeUs_t totalExecutionTime
;
35 timeUs_t averageExecutionTime
;
39 const char * taskName
;
41 uint8_t staticPriority
;
42 timeDelta_t desiredPeriod
;
43 timeUs_t maxExecutionTime
;
44 timeUs_t totalExecutionTime
;
45 timeUs_t averageExecutionTime
;
46 timeDelta_t latestDeltaTime
;
58 #if defined(BEEPER) || defined(USE_DSHOT)
79 #ifdef USE_RANGEFINDER
91 #if defined(USE_SERVO_SBUS)
109 #ifdef USE_VTX_CONTROL
112 #ifdef USE_PROGRAMMING_FRAMEWORK
113 TASK_PROGRAMMING_FRAMEWORK
,
115 #ifdef USE_RPM_FILTER
119 #if defined(USE_SMARTPORT_MASTER)
120 TASK_SMARTPORT_MASTER
,
125 #ifdef USE_ADAPTIVE_FILTER
126 TASK_ADAPTIVE_FILTER
,
128 #ifdef USE_SERIAL_GIMBAL
132 #ifdef USE_HEADTRACKER
136 #if defined(USE_TELEMETRY) && defined(USE_TELEMETRY_SBUS2)
137 TASK_TELEMETRY_SBUS2
,
140 /* Count of real tasks */
143 /* Service task IDs */
144 TASK_NONE
= TASK_COUNT
,
150 const char * taskName
;
151 bool (*checkFunc
)(timeUs_t currentTimeUs
, timeDelta_t currentDeltaTimeUs
);
152 void (*taskFunc
)(timeUs_t currentTimeUs
);
153 timeDelta_t desiredPeriod
; // target period of execution
154 const uint8_t staticPriority
; // dynamicPriority grows in steps of this size, shouldn't be zero
157 uint16_t dynamicPriority
; // measurement of how old task was last executed, used to avoid task starvation
158 uint16_t taskAgeCycles
;
159 timeUs_t lastExecutedAt
; // last time of invocation
160 timeUs_t lastSignaledAt
; // time of invocation event for event-driven tasks
161 timeDelta_t taskLatestDeltaTime
;
164 timeUs_t movingSumExecutionTime
; // moving sum over 32 samples
165 timeUs_t maxExecutionTime
;
166 timeUs_t totalExecutionTime
; // total time consumed by task since boot
169 extern cfTask_t cfTasks
[TASK_COUNT
];
170 extern uint16_t averageSystemLoadPercent
;
172 void getCheckFuncInfo(cfCheckFuncInfo_t
*checkFuncInfo
);
173 void getTaskInfo(cfTaskId_e taskId
, cfTaskInfo_t
*taskInfo
);
174 void rescheduleTask(cfTaskId_e taskId
, timeDelta_t newPeriodUs
);
175 void setTaskEnabled(cfTaskId_e taskId
, bool newEnabledState
);
176 timeDelta_t
getTaskDeltaTime(cfTaskId_e taskId
);
177 void schedulerResetTaskStatistics(cfTaskId_e taskId
);
179 void schedulerInit(void);
180 void scheduler(void);
181 void taskSystem(timeUs_t currentTimeUs
);
182 void taskRunRealtimeCallbacks(timeUs_t currentTimeUs
);
184 #define TASK_PERIOD_HZ(hz) (1000000 / (hz))
185 #define TASK_PERIOD_MS(ms) ((ms) * 1000)
186 #define TASK_PERIOD_US(us) (us)
188 #define isSystemOverloaded() (averageSystemLoadPercent >= 100)