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);
56
57void refreshIgnitionSchedule1(unsigned long timeToEnd);
58void disableAllIgnSchedules(void);
59
61
62void startFuelSchedulers(void);
63void stopFuelSchedulers(void);
64
65void beginInjectorPriming(void);
66
69
74 // We use powers of 2 so we can check multiple states with a single bitwise AND
75
77 OFF = 0b00000000U,
79 PENDING = 0b00000001U,
81 RUNNING = 0b00000010U,
83 RUNNING_WITHNEXT = 0b00000100U,
84};
85
86
106struct Schedule {
107 // Deduce the real types of the counter and compare registers.
108 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
109 // store 16-bit counter/compare calculation results.
111 using counter_t = decltype(FUEL1_COUNTER /* <-- Arbitrary choice of macro, assumes all have the same type */);
113 using compare_t = decltype(FUEL1_COMPARE /* <-- Arbitrary choice of macro, assumes all have the same type */);
114
121 constexpr Schedule(counter_t &counter, compare_t &compare)
122 : _counter(counter)
123 , _compare(compare)
124 {
125 }
126
139
142};
143
145static inline bool isRunning(const Schedule &schedule) {
146 // Using flags and bitwise AND (&) to check multiple states is much quicker
147 // than a logical or (||) (one less branch & 30% less instructions)
148 static constexpr uint8_t flags = RUNNING | RUNNING_WITHNEXT;
149 return ((uint8_t)schedule.Status & flags)!=0U;
150}
151
159void setCallbacks(Schedule &schedule, voidVoidCallback pStartCallback, voidVoidCallback pEndCallback);
160
170
171
174struct IgnitionSchedule : public Schedule {
175
176 using Schedule::Schedule;
177
179 volatile unsigned long startTime;
180 volatile bool endScheduleSetByDecoder = false;
181};
182
191{
192 // 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
194}
195
203
209#if IGN_CHANNELS >= 6
211#endif
212#if IGN_CHANNELS >= 7
214#endif
215#if IGN_CHANNELS >= 8
217#endif
218
223struct FuelSchedule : public Schedule {
224
225 using Schedule::Schedule;
226
227};
228
237{
238 // 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
240}
241
249
254#if INJ_CHANNELS >= 5
256#endif
257#if INJ_CHANNELS >= 6
259#endif
260#if INJ_CHANNELS >= 7
262#endif
263#if INJ_CHANNELS >= 8
265#endif
266
267#endif // SCHEDULER_H
#define FUEL1_COUNTER
Definition board_avr2560.h:53
#define COMPARE_TYPE
Definition board_avr2560.h:29
#define FUEL1_COMPARE
Definition board_avr2560.h:71
#define MAX_TIMER_PERIOD
Definition board_avr2560.h:127
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:190
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:236
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:145
FuelSchedule fuelSchedule2
void beginInjectorPriming(void)
Definition scheduler.cpp:348
ScheduleStatus
The current state of a schedule.
Definition scheduler.h:73
@ RUNNING
Definition scheduler.h:81
@ PENDING
Definition scheduler.h:79
@ RUNNING_WITHNEXT
Definition scheduler.h:83
@ OFF
Definition scheduler.h:77
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:223
Definition scheduler.h:174
volatile unsigned long startTime
Definition scheduler.h:179
volatile COMPARE_TYPE endCompare
The counter value of the timer when this will end.
Definition scheduler.h:178
volatile bool endScheduleSetByDecoder
Definition scheduler.h:180
A schedule for a single output channel.
Definition scheduler.h:106
COMPARE_TYPE nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:138
voidVoidCallback pStartCallback
Start Callback function for schedule.
Definition scheduler.h:136
counter_t & _counter
Reference to the counter register. E.g. TCNT3
Definition scheduler.h:140
volatile COMPARE_TYPE duration
Scheduled duration (timer ticks)
Definition scheduler.h:134
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:135
voidVoidCallback pEndCallback
End Callback function for schedule.
Definition scheduler.h:137
constexpr Schedule(counter_t &counter, compare_t &compare)
Construct a new Schedule object.
Definition scheduler.h:121
decltype(FUEL1_COUNTER) counter_t
The type of a timer counter register (this varies between platforms)
Definition scheduler.h:111
compare_t & _compare
**Reference**to the compare register. E.g. OCR3A
Definition scheduler.h:141
decltype(FUEL1_COMPARE) compare_t
The type of a timer compare register (this varies between platforms)
Definition scheduler.h:113