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 #if defined (USE_GEOZONE) && defined(USE_GPS)
144 /* Count of real tasks */
147 /* Service task IDs */
148 TASK_NONE
= TASK_COUNT
,
154 const char * taskName
;
155 bool (*checkFunc
)(timeUs_t currentTimeUs
, timeDelta_t currentDeltaTimeUs
);
156 void (*taskFunc
)(timeUs_t currentTimeUs
);
157 timeDelta_t desiredPeriod
; // target period of execution
158 const uint8_t staticPriority
; // dynamicPriority grows in steps of this size, shouldn't be zero
161 uint16_t dynamicPriority
; // measurement of how old task was last executed, used to avoid task starvation
162 uint16_t taskAgeCycles
;
163 timeUs_t lastExecutedAt
; // last time of invocation
164 timeUs_t lastSignaledAt
; // time of invocation event for event-driven tasks
165 timeDelta_t taskLatestDeltaTime
;
168 timeUs_t movingSumExecutionTime
; // moving sum over 32 samples
169 timeUs_t maxExecutionTime
;
170 timeUs_t totalExecutionTime
; // total time consumed by task since boot
173 extern cfTask_t cfTasks
[TASK_COUNT
];
174 extern uint16_t averageSystemLoadPercent
;
176 void getCheckFuncInfo(cfCheckFuncInfo_t
*checkFuncInfo
);
177 void getTaskInfo(cfTaskId_e taskId
, cfTaskInfo_t
*taskInfo
);
178 void rescheduleTask(cfTaskId_e taskId
, timeDelta_t newPeriodUs
);
179 void setTaskEnabled(cfTaskId_e taskId
, bool newEnabledState
);
180 timeDelta_t
getTaskDeltaTime(cfTaskId_e taskId
);
181 void schedulerResetTaskStatistics(cfTaskId_e taskId
);
183 void schedulerInit(void);
184 void scheduler(void);
185 void taskSystem(timeUs_t currentTimeUs
);
186 void taskRunRealtimeCallbacks(timeUs_t currentTimeUs
);
188 #define TASK_PERIOD_HZ(hz) (1000000 / (hz))
189 #define TASK_PERIOD_MS(ms) ((ms) * 1000)
190 #define TASK_PERIOD_US(us) (us)
192 #define isSystemOverloaded() (averageSystemLoadPercent >= 100)