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
49
51void startFuelSchedulers(void);
52
54void stopFuelSchedulers(void);
55
57void beginInjectorPriming(void);
58
63 // We use powers of 2 so we can check multiple states with a single bitwise AND
64
66 OFF = 0b00000000U,
68 PENDING = 0b00000001U,
70 RUNNING = 0b00000010U,
72 RUNNING_WITHNEXT = 0b00000100U,
73};
74
76void nullCallback(void);
77
98struct Schedule {
99 // Deduce the real types of the counter and compare registers.
100 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
101 // store 16-bit counter/compare calculation results.
103 using counter_t = decltype(FUEL1_COUNTER /* <-- Arbitrary choice of macro, assumes all have the same type */);
105 using compare_t = decltype(FUEL1_COMPARE /* <-- Arbitrary choice of macro, assumes all have the same type */);
106
115 , _compare(compare)
116 {
117 }
118
119 using callback = void(*)(void);
120
133
136
137protected:
138 virtual void reset(void);
139};
140
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
160void setCallbacks(Schedule &schedule, Schedule::callback pStartCallback, Schedule::callback pEndCallback) noexcept;
161
171
189struct IgnitionSchedule : public Schedule {
190
191 using Schedule::Schedule;
192
197
198 void reset(void) override;
199};
200
208
213struct FuelSchedule : public Schedule {
214
215 using Schedule::Schedule;
216
217 void reset(void) override;
218};
219
228{
229 // 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
231}
232
240
245#if INJ_CHANNELS >= 5
247#endif
248#if INJ_CHANNELS >= 6
250#endif
251#if INJ_CHANNELS >= 7
253#endif
254#if INJ_CHANNELS >= 8
256#endif
257
260
261#endif // SCHEDULER_H
#define FUEL1_COUNTER
Definition board_avr2560.h:71
uint16_t COMPARE_TYPE
The timer overflow type.
Definition board_avr2560.h:34
#define FUEL1_COMPARE
Definition board_avr2560.h:89
constexpr uint32_t MAX_TIMER_PERIOD
The longest period of time (in uS) that the timer can permit.
Definition board_definition.h:82
Crank revolution based mathematical functions.
uint32_t angleToTimeMicroSecPerDegree(uint16_t angle)
Converts angular degrees to the time interval that amount of rotation will take at current RPM.
return flags
Definition engineProtection.cpp:170
const config2 & page2
Definition engineProtection.cpp:438
const config4 & page4
Definition engineProtection.cpp:155
int16_t 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:41
static TIntegral readSerialIntegralTimeout(void)
Reads an integral type, timing out if necessary.
Definition comms.cpp:175
void changeHalfToFullSync(const config2 &page2, statuses &current)
Definition scheduler.cpp:374
void changeFullToHalfSync(const config2 &page2, const config4 &page4, statuses &current)
Definition scheduler.cpp:429
void moveToNextState(IgnitionSchedule &schedule) noexcept
Shared ignition schedule timer ISR implementation. Should be called by the actual ignition timer ISRs...
Definition scheduler.cpp:315
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:227
FuelSchedule fuelSchedule3
void startFuelSchedulers(void)
Start the timers that drive schedulers
Definition scheduler.cpp:113
void initialiseFuelSchedulers(void)
Initialize all schedulers to the OFF state.
Definition scheduler.cpp:76
FuelSchedule fuelSchedule4
void nullCallback(void)
A scheduler callback that does nothing.
Definition scheduler.cpp:55
FuelSchedule fuelSchedule1
void stopFuelSchedulers(void)
Stop the timers that drive schedulers
Definition scheduler.cpp:133
static bool isRunning(const Schedule &schedule)
Is the schedule running? I.e. the action has started, but not finished. E.g. injector is open.
Definition scheduler.h:145
void setCallbacks(Schedule &schedule, Schedule::callback pStartCallback, Schedule::callback pEndCallback) noexcept
Set the schedule callbacks. I.e the functions called when the action needs to start & stop.
Definition scheduler.cpp:153
FuelSchedule fuelSchedule2
void beginInjectorPriming(void)
Start fuel system priming the fuel.
Definition scheduler.cpp:214
ScheduleStatus
The current state of a schedule.
Definition scheduler.h:62
@ RUNNING
Definition scheduler.h:70
@ PENDING
Definition scheduler.h:68
@ RUNNING_WITHNEXT
Definition scheduler.h:72
@ OFF
Definition scheduler.h:66
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:186
const config4 statuses & current
Definition scheduler_ignition_controller.cpp:367
Definition scheduler.h:213
void reset(void) override
Definition scheduler.cpp:71
An ignition schedule.
Definition scheduler.h:189
void reset(void) override
Definition scheduler.cpp:63
int16_t dischargeAngle
Angle the coil should discharge at. I.e. spark.
Definition scheduler.h:195
volatile uint32_t _startTime
The system time (in uS) that the schedule started, used by the overdwell protection in timers....
Definition scheduler.h:193
int16_t channelDegrees
The number of crank degrees until cylinder is at TDC
Definition scheduler.h:196
int16_t chargeAngle
Angle the coil should begin charging.
Definition scheduler.h:194
A schedule for a single output channel.
Definition scheduler.h:98
COMPARE_TYPE nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:132
counter_t & _counter
Reference to the counter register. E.g. TCNT3
Definition scheduler.h:134
callback pStartCallback
Start Callback function for schedule.
Definition scheduler.h:130
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
void(*)(void) callback
Definition scheduler.h:119
constexpr Schedule(counter_t &counter, compare_t &compare)
Construct a new Schedule object.
Definition scheduler.h:113
decltype(FUEL1_COUNTER) counter_t
The type of a timer counter register (this varies between platforms)
Definition scheduler.h:103
callback pEndCallback
End Callback function for schedule.
Definition scheduler.h:131
compare_t & _compare
**Reference**to the compare register. E.g. OCR3A
Definition scheduler.h:135
virtual void reset(void)
Definition scheduler.cpp:57
decltype(FUEL1_COMPARE) compare_t
The type of a timer compare register (this varies between platforms)
Definition scheduler.h:105
Definition config_pages.h:171
Definition config_pages.h:356
The status struct with current values for all 'live' variables.
Definition statuses.h:36