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 "globals.h"
45#include "crankMaths.h"
46#include "scheduledIO.h"
47
48#define USE_IGN_REFRESH
49#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
50
52
54void stopIgnitionSchedulers(void);
55void refreshIgnitionSchedule1(unsigned long timeToEnd);
56
58
59void startFuelSchedulers(void);
60void stopFuelSchedulers(void);
61
62void beginInjectorPriming(void);
63
68 // We use powers of 2 so we can check multiple states with a single bitwise AND
69
71 OFF = 0b00000000U,
73 PENDING = 0b00000001U,
75 RUNNING = 0b00000010U,
77 RUNNING_WITHNEXT = 0b00000100U,
78};
79
80
100struct Schedule {
101 // Deduce the real types of the counter and compare registers.
102 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
103 // store 16-bit counter/compare calculation results.
105 using counter_t = decltype(FUEL1_COUNTER /* <-- Arbitrary choice of macro, assumes all have the same type */);
107 using compare_t = decltype(FUEL1_COMPARE /* <-- Arbitrary choice of macro, assumes all have the same type */);
108
117 , _compare(compare)
118 {
119 }
120
133
136};
137
139static inline bool isRunning(const Schedule &schedule) {
140 // Using flags and bitwise AND (&) to check multiple states is much quicker
141 // than a logical or (||) (one less branch & 30% less instructions)
142 static constexpr uint8_t flags = RUNNING | RUNNING_WITHNEXT;
143 return ((uint8_t)schedule.Status & flags)!=0U;
144}
145
153void setCallbacks(Schedule &schedule, voidVoidCallback pStartCallback, voidVoidCallback pEndCallback);
154
164
167struct IgnitionSchedule : public Schedule {
168
169 using Schedule::Schedule;
170
172 volatile unsigned long startTime;
173 volatile bool endScheduleSetByDecoder = false;
174};
175
184{
185 // 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
187}
188
196
202#if IGN_CHANNELS >= 6
204#endif
205#if IGN_CHANNELS >= 7
207#endif
208#if IGN_CHANNELS >= 8
210#endif
211
216struct FuelSchedule : public Schedule {
217
218 using Schedule::Schedule;
219
220};
221
230{
231 // 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
233}
234
242
247#if INJ_CHANNELS >= 5
249#endif
250#if INJ_CHANNELS >= 6
252#endif
253#if INJ_CHANNELS >= 7
255#endif
256#if INJ_CHANNELS >= 8
258#endif
259
260#endif // SCHEDULER_H
#define FUEL1_COUNTER
Definition board_avr2560.h:79
uint16_t COMPARE_TYPE
The timer overflow type.
Definition board_avr2560.h:33
#define FUEL1_COMPARE
Definition board_avr2560.h:97
constexpr uint32_t MAX_TIMER_PERIOD
The longest period of time (in uS) that the timer can permit.
Definition board_definition.h:91
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:175
void moveToNextState(IgnitionSchedule &schedule)
Shared ignition schedule timer ISR implementation. Should be called by the actual ignition timer ISRs...
Definition scheduler.cpp:444
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:183
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:229
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:326
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:139
FuelSchedule fuelSchedule2
void beginInjectorPriming(void)
Definition scheduler.cpp:344
ScheduleStatus
The current state of a schedule.
Definition scheduler.h:67
@ RUNNING
Definition scheduler.h:75
@ PENDING
Definition scheduler.h:73
@ RUNNING_WITHNEXT
Definition scheduler.h:77
@ OFF
Definition scheduler.h:71
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:304
void initialiseIgnitionSchedulers(void)
Definition scheduler.cpp:121
IgnitionSchedule ignitionSchedule3
IgnitionSchedule ignitionSchedule2
Definition scheduler.h:216
Definition scheduler.h:167
volatile unsigned long startTime
Definition scheduler.h:172
volatile COMPARE_TYPE endCompare
The counter value of the timer when this will end.
Definition scheduler.h:171
volatile bool endScheduleSetByDecoder
Definition scheduler.h:173
A schedule for a single output channel.
Definition scheduler.h:100
COMPARE_TYPE nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:132
voidVoidCallback pStartCallback
Start Callback function for schedule.
Definition scheduler.h:130
counter_t & _counter
Reference to the counter register. E.g. TCNT3
Definition scheduler.h:134
volatile COMPARE_TYPE duration
Scheduled duration (timer ticks)
Definition scheduler.h:128
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:129
voidVoidCallback pEndCallback
End Callback function for schedule.
Definition scheduler.h:131
constexpr Schedule(counter_t &counter, compare_t &compare)
Construct a new Schedule object.
Definition scheduler.h:115
decltype(FUEL1_COUNTER) counter_t
The type of a timer counter register (this varies between platforms)
Definition scheduler.h:105
compare_t & _compare
**Reference**to the compare register. E.g. OCR3A
Definition scheduler.h:135
decltype(FUEL1_COMPARE) compare_t
The type of a timer compare register (this varies between platforms)
Definition scheduler.h:107