vtx: fix VTX_SETTINGS_POWER_COUNT and add dummy entries to saPowerNames
[inav.git] / src / main / scheduler / scheduler.h
blob176812d8f3f17c6a15c4cd225371d00d60e4c2bc
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 /* Count of real tasks */
141 TASK_COUNT,
143 /* Service task IDs */
144 TASK_NONE = TASK_COUNT,
145 TASK_SELF
146 } cfTaskId_e;
148 typedef struct {
149 /* Configuration */
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
156 /* Scheduling */
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;
163 /* Statistics */
164 timeUs_t movingSumExecutionTime; // moving sum over 32 samples
165 timeUs_t maxExecutionTime;
166 timeUs_t totalExecutionTime; // total time consumed by task since boot
167 } cfTask_t;
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)