Speeduino
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1
41#ifndef SCHEDULER_H
42#define SCHEDULER_H
43
44#include <SimplyAtomic.h>
45#include "globals.h"
46#include "crankMaths.h"
47#include "scheduledIO.h"
48
49#define USE_IGN_REFRESH
50#define IGNITION_REFRESH_THRESHOLD 30 //Time in uS that the refresh functions will check to ensure there is enough time before changing the end compare
51
53
55void stopIgnitionSchedulers(void);
57
58void refreshIgnitionSchedule1(unsigned long timeToEnd);
59void disableAllIgnSchedules(void);
60
62
63void startFuelSchedulers(void);
64void stopFuelSchedulers(void);
65
66void beginInjectorPriming(void);
67
70
75 // We use powers of 2 so we can check multiple states with a single bitwise AND
76
78 OFF = 0b00000000U,
80 PENDING = 0b00000001U,
82 RUNNING = 0b00000010U,
84 RUNNING_WITHNEXT = 0b00000100U,
85};
86
87
107struct Schedule {
108 // Deduce the real types of the counter and compare registers.
109 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
110 // store 16-bit counter/compare calculation results.
112 using counter_t = decltype(FUEL1_COUNTER /* <-- Arbitrary choice of macro, assumes all have the same type */);
114 using compare_t = decltype(FUEL1_COMPARE /* <-- Arbitrary choice of macro, assumes all have the same type */);
115
122 constexpr Schedule(counter_t &counter, compare_t &compare)
123 : _counter(counter)
124 , _compare(compare)
125 {
126 }
127
140
143};
144
146static inline bool isRunning(const Schedule &schedule) {
147 // Using flags and bitwise AND (&) to check multiple states is much quicker
148 // than a logical or (||) (one less branch & 30% less instructions)
149 static constexpr uint8_t flags = RUNNING | RUNNING_WITHNEXT;
150 return ((uint8_t)schedule.Status & flags)!=0U;
151}
152
160void setCallbacks(Schedule &schedule, voidVoidCallback pStartCallback, voidVoidCallback pEndCallback);
161
171
172
175struct IgnitionSchedule : public Schedule {
176
177 using Schedule::Schedule;
178
180 volatile unsigned long startTime;
181 volatile bool endScheduleSetByDecoder = false;
182};
183
192{
193 // Only queue up the next schedule if the maximum time between sparks (Based on CRANK_ANGLE_MAX_IGN) is less than the max timer period
195}
196
204
210#if IGN_CHANNELS >= 6
212#endif
213#if IGN_CHANNELS >= 7
215#endif
216#if IGN_CHANNELS >= 8
218#endif
219
224struct FuelSchedule : public Schedule {
225
226 using Schedule::Schedule;
227
228};
229
238{
239 // Only queue up the next schedule if the maximum time between squirts (Based on CRANK_ANGLE_MAX_INJ) is less than the max timer period
241}
242
250
255#if INJ_CHANNELS >= 5
257#endif
258#if INJ_CHANNELS >= 6
260#endif
261#if INJ_CHANNELS >= 7
263#endif
264#if INJ_CHANNELS >= 8
266#endif
267
268#endif // SCHEDULER_H
#define FUEL1_COUNTER
Definition board_avr2560.h:52
#define COMPARE_TYPE
Definition board_avr2560.h:29
#define FUEL1_COMPARE
Definition board_avr2560.h:70
#define MAX_TIMER_PERIOD
Definition board_avr2560.h:126
uint32_t angleToTimeMicroSecPerDegree(uint16_t angle)
int CRANK_ANGLE_MAX_IGN
Definition globals.cpp:102
int CRANK_ANGLE_MAX_INJ
The number of crank degrees that the system track over. Typically 720 divided by the number of squirt...
Definition globals.cpp:103
static TIntegral readSerialIntegralTimeout(void)
Reads an integral type, timing out if necessary.
Definition comms.cpp:173
void moveToNextState(IgnitionSchedule &schedule)
Shared ignition schedule timer ISR implementation. Should be called by the actual ignition timer ISRs...
Definition scheduler.cpp:448
void disableAllFuelSchedules(void)
Definition scheduler.cpp:510
void disableAllIgnSchedules(void)
Definition scheduler.cpp:521
void disableIgnSchedule(uint8_t channel)
Definition scheduler.cpp:488
void disableFuelSchedule(uint8_t channel)
Definition scheduler.cpp:465
void nullCallback(void)
Definition scheduledIO.cpp:114
void(* voidVoidCallback)(void)
Definition scheduledIO.h:186
void startIgnitionSchedulers(void)
Definition scheduler.cpp:178
static void setIgnitionSchedule(IgnitionSchedule &schedule, uint32_t delay, uint16_t duration)
Set the ignition schedule action (charge & fire a coil) to run for a certain duration in the future.
Definition scheduler.h:191
static void setFuelSchedule(FuelSchedule &schedule, uint32_t delay, uint16_t duration)
Set the fuel schedule action (open & close an injector) to run for a certain duration in the future.
Definition scheduler.h:237
void setCallbacks(Schedule &schedule, voidVoidCallback pStartCallback, voidVoidCallback pEndCallback)
Set the schedule action start & end callbacks.
Definition scheduler.cpp:271
void stopIgnitionSchedulers(void)
Definition scheduler.cpp:204
IgnitionSchedule ignitionSchedule5
FuelSchedule fuelSchedule3
void refreshIgnitionSchedule1(unsigned long timeToEnd)
Definition scheduler.cpp:330
void startFuelSchedulers(void)
Definition scheduler.cpp:231
void initialiseFuelSchedulers(void)
Definition scheduler.cpp:84
FuelSchedule fuelSchedule4
IgnitionSchedule ignitionSchedule1
FuelSchedule fuelSchedule1
void stopFuelSchedulers(void)
Definition scheduler.cpp:251
IgnitionSchedule ignitionSchedule4
static bool isRunning(const Schedule &schedule)
Is the schedule action currently running?
Definition scheduler.h:146
FuelSchedule fuelSchedule2
void beginInjectorPriming(void)
Definition scheduler.cpp:348
ScheduleStatus
The current state of a schedule.
Definition scheduler.h:74
@ RUNNING
Definition scheduler.h:82
@ PENDING
Definition scheduler.h:80
@ RUNNING_WITHNEXT
Definition scheduler.h:84
@ OFF
Definition scheduler.h:78
void setSchedule(Schedule &schedule, uint32_t delay, uint16_t duration, bool allowQueuedSchedule)
Set the schedule action to run for a certain duration in the future.
Definition scheduler.cpp:308
void initialiseIgnitionSchedulers(void)
Definition scheduler.cpp:121
IgnitionSchedule ignitionSchedule3
IgnitionSchedule ignitionSchedule2
Definition scheduler.h:224
Definition scheduler.h:175
volatile unsigned long startTime
Definition scheduler.h:180
volatile COMPARE_TYPE endCompare
The counter value of the timer when this will end.
Definition scheduler.h:179
volatile bool endScheduleSetByDecoder
Definition scheduler.h:181
A schedule for a single output channel.
Definition scheduler.h:107
COMPARE_TYPE nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:139
voidVoidCallback pStartCallback
Start Callback function for schedule.
Definition scheduler.h:137
counter_t & _counter
Reference to the counter register. E.g. TCNT3
Definition scheduler.h:141
volatile COMPARE_TYPE duration
Scheduled duration (timer ticks)
Definition scheduler.h:135
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:136
voidVoidCallback pEndCallback
End Callback function for schedule.
Definition scheduler.h:138
constexpr Schedule(counter_t &counter, compare_t &compare)
Construct a new Schedule object.
Definition scheduler.h:122
decltype(FUEL1_COUNTER) counter_t
The type of a timer counter register (this varies between platforms)
Definition scheduler.h:112
compare_t & _compare
**Reference**to the compare register. E.g. OCR3A
Definition scheduler.h:142
decltype(FUEL1_COMPARE) compare_t
The type of a timer compare register (this varies between platforms)
Definition scheduler.h:114