Version 1.0 bump
[inav/snaewe.git] / src / main / scheduler.h
blobd734b095b99c2861a84390196f63e53f68cd801d
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 //#define SCHEDULER_DEBUG
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_HIGH = 5,
27 TASK_PRIORITY_REALTIME = 6,
28 TASK_PRIORITY_MAX = 255
29 } cfTaskPriority_e;
31 typedef struct {
32 const char * taskName;
33 bool isEnabled;
34 uint32_t desiredPeriod;
35 uint8_t staticPriority;
36 uint32_t maxExecutionTime;
37 uint32_t totalExecutionTime;
38 uint32_t averageExecutionTime;
39 } cfTaskInfo_t;
41 typedef enum {
42 /* Actual tasks */
43 TASK_SYSTEM = 0,
44 TASK_GYROPID,
45 TASK_SERIAL,
46 TASK_BEEPER,
47 TASK_BATTERY,
48 TASK_RX,
49 #ifdef GPS
50 TASK_GPS,
51 #endif
52 #ifdef MAG
53 TASK_COMPASS,
54 #endif
55 #ifdef BARO
56 TASK_BARO,
57 #endif
58 #ifdef SONAR
59 TASK_SONAR,
60 #endif
61 #ifdef DISPLAY
62 TASK_DISPLAY,
63 #endif
64 #ifdef TELEMETRY
65 TASK_TELEMETRY,
66 #endif
67 #ifdef LED_STRIP
68 TASK_LEDSTRIP,
69 #endif
71 /* Count of real tasks */
72 TASK_COUNT,
74 /* Service task IDs */
75 TASK_NONE = TASK_COUNT,
76 TASK_SELF
77 } cfTaskId_e;
79 typedef struct {
80 /* Configuration */
81 const char * taskName;
82 bool (*checkFunc)(uint32_t currentDeltaTime);
83 void (*taskFunc)(void);
84 bool isEnabled;
85 uint32_t desiredPeriod; // target period of execution
86 uint8_t staticPriority; // dynamicPriority grows in steps of this size, shouldn't be zero
88 /* Scheduling */
89 uint8_t dynamicPriority; // measurement of how old task was last executed, used to avoid task starvation
90 uint32_t lastExecutedAt; // last time of invocation
91 uint32_t lastSignaledAt; // time of invocation event for event-driven tasks
92 uint16_t taskAgeCycles;
94 /* Statistics */
95 uint32_t averageExecutionTime; // Moving averate over 6 samples, used to calculate guard interval
96 uint32_t taskLatestDeltaTime; //
97 #ifndef SKIP_TASK_STATISTICS
98 uint32_t maxExecutionTime;
99 uint32_t totalExecutionTime; // total time consumed by task since boot
100 #endif
101 } cfTask_t;
103 extern cfTask_t cfTasks[TASK_COUNT];
104 extern uint16_t cpuLoad;
105 extern uint16_t averageSystemLoadPercent;
107 void getTaskInfo(cfTaskId_e taskId, cfTaskInfo_t * taskInfo);
108 void rescheduleTask(cfTaskId_e taskId, uint32_t newPeriodMicros);
109 void setTaskEnabled(cfTaskId_e taskId, bool newEnabledState);
110 uint32_t getTaskDeltaTime(cfTaskId_e taskId);
112 void scheduler(void);
114 #define isSystemOverloaded() (averageSystemLoadPercent >= 100)