Added more functionality to the scheduler. It should be able to select the task that should run and soon be able to create tasks. Not fully finished but its not that much left to add
145 lines
5.4 KiB
C
145 lines
5.4 KiB
C
/**************************************************************************
|
|
* NAME: scheduler.h *
|
|
* PURPOSE: Defining the the scheduler to be used in the system to organize*
|
|
* the runtime for the tasks in the system based on priority. *
|
|
* INFORMATION: Many elements and ideas obtained from beta/cleanflight *
|
|
* GLOBAL VARIABLES: *
|
|
* Variable Type Description *
|
|
* -------- ---- ----------- *
|
|
***************************************************************************/
|
|
|
|
#ifndef SCHEDULER_H_
|
|
#define SCHEDULER_H_
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
/* Enum containing all the possible task priorities */
|
|
typedef enum
|
|
{
|
|
IDLE = 0,
|
|
LOW,
|
|
MEDIUM,
|
|
HIGH,
|
|
REALTIME,
|
|
MAX_PRIORITY = 255
|
|
} taskPriority_t;
|
|
|
|
|
|
/* Struct used to contain the information for each task */
|
|
typedef struct
|
|
{
|
|
/* Basic task information */
|
|
const char * taskName; /* Name of the task */
|
|
const char * subTaskName; /*Needed?*/
|
|
bool (*checkEventTriggered)(uint32_t currentDeltaTime); /* Function pointer to event trigger check, if used no standard scheduling */
|
|
void (*taskFunction)(void); /* Pointer to the function that should be called to run the task */
|
|
uint32_t desiredPeriod; /* The period the task wants to run in */
|
|
const uint8_t staticPriority; /* Value used to increment the dynamic priority */
|
|
|
|
/* Scheduling variables */
|
|
uint16_t dynamicPriority; /* Priority increases the longer the task have been idle, increased by staticPriority value */
|
|
uint16_t taskAgeCycle; /* Helps to keep track of the "periods" for the task */
|
|
uint32_t lastExecutedAt; /* last time of invocation */
|
|
uint32_t lastSignaledAt; /* time of invocation event for event-driven tasks */
|
|
|
|
/* Statistic values of the task */
|
|
uint32_t averageExecutionTime; // Moving average over 6 samples, used to calculate guard interval
|
|
uint32_t taskLatestDeltaTime; //
|
|
#ifndef SKIP_TASK_STATISTICS
|
|
uint32_t maxExecutionTime;
|
|
uint32_t totalExecutionTime; // total time consumed by task since boot
|
|
#endif
|
|
|
|
}task_t;
|
|
|
|
|
|
/* Task counter, ToDo: If more tasks are needed add them here first, defines are used to make sure the task is somewhere in the system */
|
|
/* Important: If a new define for a task is added here it MUST be added in the tasks.c */
|
|
typedef enum
|
|
{
|
|
/* All the tasks that will be in the system, some are separated by IfDef */
|
|
TASK_SYSTEM = 0,
|
|
TASK_GYROPID,
|
|
TASK_ACCELEROMETER,
|
|
TASK_ATTITUDE,
|
|
TASK_RX,
|
|
TASK_SERIAL,
|
|
TASK_BATTERY,
|
|
#ifdef BARO
|
|
TASK_BARO,
|
|
#endif
|
|
#ifdef COMPASS
|
|
TASK_COMPASS,
|
|
#endif
|
|
#ifdef GPS
|
|
TASK_GPS,
|
|
#endif
|
|
#ifdef SONAR
|
|
TASK_SONAR,
|
|
#endif
|
|
#if defined(BARO) || defined(SONAR)
|
|
TASK_ALTITUDE,
|
|
#endif
|
|
#if BEEPER
|
|
TASK_BEEPER
|
|
#endif
|
|
|
|
/* Need to be the value directly after the tasks id. Keeps track of the count of the tasks */
|
|
TASK_COUNT,
|
|
|
|
/* Service task IDs */
|
|
TASK_NONE = TASK_COUNT,
|
|
TASK_SELF
|
|
}taskId_t;
|
|
|
|
/* Variables --------------------------------------------------------------------------------- */
|
|
extern task_t SystemTasks[TASK_COUNT]; /* All the tasks that exist in the system */
|
|
extern uint16_t CpuLoad; /* The current load on the cpu Todo */
|
|
extern uint16_t averageSystemLoadPercent; /* The average load on the system Todo */
|
|
|
|
|
|
/* Functions that operate can operate on the tasks ------------------------------------------- */
|
|
|
|
/**************************************************************************
|
|
* BRIEF: Enables or disables a specific task in the system *
|
|
* INFORMATION: Takes the id of the task(task_t) along with a true or *
|
|
* false value. If true the task will be added to the task queue, if false *
|
|
* it will be removed. *
|
|
**************************************************************************/
|
|
void enableTask(taskId_t taskId, bool enabled);
|
|
|
|
/**************************************************************************
|
|
* BRIEF: *
|
|
* INFORMATION: *
|
|
**************************************************************************/
|
|
uint32_t getTaskDeltaTime(taskId_t taskId);
|
|
|
|
/**************************************************************************
|
|
* BRIEF: *
|
|
* INFORMATION: *
|
|
**************************************************************************/
|
|
void rescheduleTask(taskId_t taskId, uint32_t newPeriodMicros);
|
|
|
|
|
|
/* Functions that handle the scheduler ------------------------------------------------------- */
|
|
|
|
/**************************************************************************
|
|
* BRIEF: *
|
|
* INFORMATION: *
|
|
**************************************************************************/
|
|
void scheduler(void);
|
|
|
|
/**************************************************************************
|
|
* BRIEF: *
|
|
* INFORMATION: *
|
|
**************************************************************************/
|
|
void initScheduler(void);
|
|
|
|
|
|
|
|
|
|
#endif /* SCHEDULER_H_ */
|