Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / src / main / scheduler / scheduler.h
blob5d38a6dba5997d0c4c3226ee0df56d3f98184584
1 /*
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/>.
18 #pragma once
20 #include "common/time.h"
22 typedef enum {
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
30 } cfTaskPriority_e;
32 typedef struct {
33 timeUs_t maxExecutionTime;
34 timeUs_t totalExecutionTime;
35 timeUs_t averageExecutionTime;
36 } cfCheckFuncInfo_t;
38 typedef struct {
39 const char * taskName;
40 bool isEnabled;
41 uint8_t staticPriority;
42 timeDelta_t desiredPeriod;
43 timeUs_t maxExecutionTime;
44 timeUs_t totalExecutionTime;
45 timeUs_t averageExecutionTime;
46 timeDelta_t latestDeltaTime;
47 } cfTaskInfo_t;
49 typedef enum {
50 /* Actual tasks */
51 TASK_SYSTEM = 0,
52 TASK_PID,
53 TASK_GYRO,
54 TASK_RX,
55 TASK_SERIAL,
56 TASK_BATTERY,
57 TASK_TEMPERATURE,
58 #if defined(BEEPER) || defined(USE_DSHOT)
59 TASK_BEEPER,
60 #endif
61 #ifdef USE_LIGHTS
62 TASK_LIGHTS,
63 #endif
64 #ifdef USE_GPS
65 TASK_GPS,
66 #endif
67 #ifdef USE_MAG
68 TASK_COMPASS,
69 #endif
70 #ifdef USE_BARO
71 TASK_BARO,
72 #endif
73 #ifdef USE_ADSB
74 TASK_ADSB,
75 #endif
76 #ifdef USE_PITOT
77 TASK_PITOT,
78 #endif
79 #ifdef USE_RANGEFINDER
80 TASK_RANGEFINDER,
81 #endif
82 #ifdef USE_DASHBOARD
83 TASK_DASHBOARD,
84 #endif
85 #ifdef USE_TELEMETRY
86 TASK_TELEMETRY,
87 #endif
88 #ifdef USE_LED_STRIP
89 TASK_LEDSTRIP,
90 #endif
91 #if defined(USE_SERVO_SBUS)
92 TASK_PWMDRIVER,
93 #endif
94 #ifdef STACK_CHECK
95 TASK_STACK_CHECK,
96 #endif
97 #ifdef USE_OSD
98 TASK_OSD,
99 #endif
100 #ifdef USE_CMS
101 TASK_CMS,
102 #endif
103 #ifdef USE_OPFLOW
104 TASK_OPFLOW,
105 #endif
106 #ifdef USE_RCDEVICE
107 TASK_RCDEVICE,
108 #endif
109 #ifdef USE_VTX_CONTROL
110 TASK_VTXCTRL,
111 #endif
112 #ifdef USE_PROGRAMMING_FRAMEWORK
113 TASK_PROGRAMMING_FRAMEWORK,
114 #endif
115 #ifdef USE_RPM_FILTER
116 TASK_RPM_FILTER,
117 #endif
118 TASK_AUX,
119 #if defined(USE_SMARTPORT_MASTER)
120 TASK_SMARTPORT_MASTER,
121 #endif
122 #ifdef USE_IRLOCK
123 TASK_IRLOCK,
124 #endif
125 #ifdef USE_ADAPTIVE_FILTER
126 TASK_ADAPTIVE_FILTER,
127 #endif
128 #ifdef USE_SERIAL_GIMBAL
129 TASK_GIMBAL,
130 #endif
132 #ifdef USE_HEADTRACKER
133 TASK_HEADTRACKER,
134 #endif
136 #if defined(USE_TELEMETRY) && defined(USE_TELEMETRY_SBUS2)
137 TASK_TELEMETRY_SBUS2,
138 #endif
140 #if defined (USE_GEOZONE) && defined(USE_GPS)
141 TASK_GEOZONE,
142 #endif
144 /* Count of real tasks */
145 TASK_COUNT,
147 /* Service task IDs */
148 TASK_NONE = TASK_COUNT,
149 TASK_SELF
150 } cfTaskId_e;
152 typedef struct {
153 /* Configuration */
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
160 /* Scheduling */
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;
167 /* Statistics */
168 timeUs_t movingSumExecutionTime; // moving sum over 32 samples
169 timeUs_t maxExecutionTime;
170 timeUs_t totalExecutionTime; // total time consumed by task since boot
171 } cfTask_t;
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)