NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ninja_sched.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdbool.h>
4 
5 typedef enum {
6  TASK_PRIORITY_IDLE = 0, // Disables dynamic scheduling, task is executed only if no other task is active this cycle
13 
14 typedef struct {
15  const char * taskName;
16  bool isEnabled;
17  uint32_t desiredPeriod;
18  uint8_t staticPriority;
19  uint32_t maxExecutionTime;
22  uint32_t latestDeltaTime;
23 } cfTaskInfo_t;
24 
25 typedef enum {
26  /* Actual tasks */
34 #ifdef GPS
35  TASK_GPS,
36 #endif
38 #ifdef BARO
39  TASK_BARO,
40 #endif
41 #ifdef SONAR
42  TASK_SONAR,
43 #endif
44 #if defined(BARO) || defined(SONAR)
45  TASK_ALTITUDE,
46 #endif
47 #ifdef DISPLAY
48  TASK_DISPLAY,
49 #endif
50 #ifdef TELEMETRY
51  TASK_TELEMETRY,
52 #endif
53 #ifdef LED_STRIP
54  TASK_LEDSTRIP,
55 #endif
57 
58  /* Count of real tasks */
60 
61  /* Service task IDs */
64 } cfTaskId_e;
65 
66 struct ninja_sched;
67 typedef struct cfTask_s {
68  /* Configuration */
69  const char * taskName;
70  bool (*checkFunc)(struct ninja_sched *self, uint32_t currentDeltaTime);
71  void (*taskFunc)(struct ninja_sched *self);
72  uint32_t desiredPeriod; // target period of execution
73  const uint8_t staticPriority; // dynamicPriority grows in steps of this size, shouldn't be zero
74 
75  /* Scheduling */
76  uint16_t dynamicPriority; // measurement of how old task was last executed, used to avoid task starvation
77  uint16_t taskAgeCycles;
78  uint32_t lastExecutedAt; // last time of invocation
79  uint32_t lastSignaledAt; // time of invocation event for event-driven tasks
80 
81  /* Statistics */
82  uint32_t averageExecutionTime; // Moving average over 6 samples, used to calculate guard interval
83  uint32_t taskLatestDeltaTime; //
84  uint32_t maxExecutionTime;
85  uint32_t totalExecutionTime; // total time consumed by task since boot
86 } cfTask_t;
87 
88 struct ninja_sched {
89  // No need for a linked list for the queue, since items are only inserted at startup
90  cfTask_t* taskQueueArray[TASK_COUNT + 1]; // extra item for NULL pointer at end of queue
92 
96 
97  uint32_t currentTime;
99 
102 
103  const struct system_calls_time *time;
104  const struct config *config;
105 };
106 
107 /*
108 void getTaskInfo(cfTaskId_e taskId, cfTaskInfo_t * taskInfo);
109 void rescheduleTask(cfTaskId_e taskId, uint32_t newPeriodMicros);
110 void setTaskEnabled(cfTaskId_e taskId, bool newEnabledState);
111 uint32_t getTaskDeltaTime(cfTaskId_e taskId);
112 */
113 
114 #define LOAD_PERCENTAGE_ONE 100
115 
116 struct ninja;
117 void ninja_sched_init(struct ninja_sched *self, const struct system_calls_time *time, const struct config *config);
118 void ninja_sched_run(struct ninja_sched *self);
119 void ninja_sched_get_task_info(struct ninja_sched *self, cfTaskId_e taskId, cfTaskInfo_t * taskInfo);
120 uint16_t ninja_sched_get_load(struct ninja_sched *self);
int taskQueueSize
Definition: ninja_sched.h:101
Definition: ninja_sched.h:62
void(* taskFunc)(struct ninja_sched *self)
Definition: ninja_sched.h:71
Definition: ninja_sched.h:6
Definition: system_calls.h:136
Definition: config.h:81
Definition: ninja.h:40
uint32_t maxExecutionTime
Definition: ninja_sched.h:19
cfTaskId_e
Definition: ninja_sched.h:25
Definition: ninja_sched.h:37
const char * taskName
Definition: ninja_sched.h:15
Definition: ninja_sched.h:56
uint32_t totalWaitingTasks
Definition: ninja_sched.h:93
const struct config * config
Definition: ninja_sched.h:104
cfTaskPriority_e
Definition: ninja_sched.h:5
void ninja_sched_init(struct ninja_sched *self, const struct system_calls_time *time, const struct config *config)
Definition: ninja_sched.c:174
Definition: ninja_sched.h:28
bool isEnabled
Definition: ninja_sched.h:16
Definition: ninja_sched.h:9
Definition: ninja_sched.h:14
uint32_t realtimeGuardInterval
Definition: ninja_sched.h:95
uint32_t desiredPeriod
Definition: ninja_sched.h:72
void ninja_sched_get_task_info(struct ninja_sched *self, cfTaskId_e taskId, cfTaskInfo_t *taskInfo)
Definition: ninja_sched.c:135
Definition: ninja_sched.h:88
uint32_t averageExecutionTime
Definition: ninja_sched.h:82
Definition: ninja_sched.h:31
void ninja_sched_run(struct ninja_sched *self)
Definition: ninja_sched.c:226
uint16_t taskAgeCycles
Definition: ninja_sched.h:77
uint16_t averageSystemLoadPercent
Definition: ninja_sched.h:98
uint8_t staticPriority
Definition: ninja_sched.h:18
cfTask_t * currentTask
Definition: ninja_sched.h:91
Definition: ninja_sched.h:10
Definition: ninja_sched.h:32
const uint8_t staticPriority
Definition: ninja_sched.h:73
uint32_t maxExecutionTime
Definition: ninja_sched.h:84
Definition: ninja_sched.h:29
uint32_t taskLatestDeltaTime
Definition: ninja_sched.h:83
uint32_t totalWaitingTasksSamples
Definition: ninja_sched.h:94
uint16_t ninja_sched_get_load(struct ninja_sched *self)
Definition: ninja_sched.c:309
uint32_t lastExecutedAt
Definition: ninja_sched.h:78
uint16_t dynamicPriority
Definition: ninja_sched.h:76
uint32_t latestDeltaTime
Definition: ninja_sched.h:22
Definition: ninja_sched.h:67
const char * taskName
Definition: ninja_sched.h:69
uint32_t totalExecutionTime
Definition: ninja_sched.h:20
uint32_t averageExecutionTime
Definition: ninja_sched.h:21
uint32_t desiredPeriod
Definition: ninja_sched.h:17
Definition: ninja_sched.h:30
struct cfTask_s cfTask_t
uint32_t currentTime
Definition: ninja_sched.h:97
bool(* checkFunc)(struct ninja_sched *self, uint32_t currentDeltaTime)
Definition: ninja_sched.h:70
Definition: ninja_sched.h:33
int taskQueuePos
Definition: ninja_sched.h:100
Definition: ninja_sched.h:8
Definition: ninja_sched.h:63
cfTask_t * taskQueueArray[TASK_COUNT+1]
Definition: ninja_sched.h:90
Definition: ninja_sched.h:59
uint32_t totalExecutionTime
Definition: ninja_sched.h:85
Definition: ninja_sched.h:27
Definition: ninja_sched.h:7
const struct system_calls_time * time
Definition: ninja_sched.h:103
uint32_t lastSignaledAt
Definition: ninja_sched.h:79
Definition: ninja_sched.h:11