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 "board_definition.h"
45#include "crankMaths.h"
46#include "preprocessor.h"
47
52 // We use powers of 2 so we can check multiple states with a single bitwise AND
53
55 OFF = 0b00000000U,
57 PENDING = 0b00000001U,
59 RUNNING = 0b00000010U,
61 RUNNING_WITHNEXT = 0b00000100U,
62};
63
65void nullCallback(void);
66
87struct Schedule {
88 // Deduce the real types of the counter and compare registers.
89 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
90 // store 16-bit counter/compare calculation results.
92 using counter_t = decltype(FUEL1_COUNTER /* <-- Arbitrary choice of macro, assumes all have the same type */);
94 using compare_t = decltype(FUEL1_COMPARE /* <-- Arbitrary choice of macro, assumes all have the same type */);
95
104 , _compare(compare)
105 {
106 }
107
108 using callback = void(*)(void);
109
122
125
126protected:
127 virtual void reset(void);
128};
129
134static inline bool isRunning(const Schedule &schedule) noexcept {
135 // Using flags and bitwise AND (&) to check multiple states is much quicker
136 // than a logical or (||) (one less branch & 30% less instructions)
137 static constexpr uint8_t flags = RUNNING | RUNNING_WITHNEXT;
138 return ((uint8_t)schedule._status & flags)!=0U;
139}
140
150
160
178struct IgnitionSchedule : public Schedule {
179
180 using Schedule::Schedule;
181
182 volatile uint32_t _startTime = 0U;
186
187 void reset(void) override;
188};
189
197
209
220struct FuelSchedule : public Schedule {
221
222 using Schedule::Schedule;
223
226
227 void reset(void) override;
228};
229
237
241 if ( (isRunning(schedule)) || (schedule._status == OFF)) {
242 while(delta < 0) { delta += (int16_t)maxAngle; }
243 }
244
245 return delta > 0 ? angleToTime((uint16_t)delta) : 0U;
246}
247
250 if( angle < 0) { return angle + (int16_t)maxAngle; }
251 return angle;
252}
253
255 if (angleOffset==0U) { // Optimize for zero channel angle - no need to adjust start & crank angles
257 }
258 // Realign the current crank angle and the desired start angle around 0 degrees for the given cylinder/output
259 // Eg: If cylinder 2 TDC is 180 degrees after cylinder 1 (E.g. a standard 4 cylinder engine), then
260 // adjusted crank angle is 180* less than the current crank angle and adjusted start angle is the desired open angle less 180*.
261 // Thus the cylinder is being treated relative to its own TDC, regardless of its offset
262 //
263 // This is done to avoid very small or very large deltas between crank angle and start angle.
267 maxAngle);
268}
269
271
272#endif // SCHEDULER_H
#define FUEL1_COUNTER
Definition board_avr2560.h:69
uint16_t COMPARE_TYPE
The timer overflow type.
Definition board_avr2560.h:32
#define FUEL1_COMPARE
Definition board_avr2560.h:87
Inclusion of board specific header files and board related definitions.
Crank revolution based mathematical functions.
uint32_t angleToTime(uint16_t angle) noexcept
Converts angular degrees to the time interval that amount of rotation will take at current RPM.
return flags
Definition engineProtection.cpp:170
void adjustCrankAngle(const statuses &current, IgnitionSchedule &schedule, int16_t crankAngle)
Adjust the crank angle used to originally set the schedule.
Definition scheduler.cpp:183
void moveToNextState(IgnitionSchedule &schedule) noexcept
Shared ignition schedule timer ISR implementation. Should be called by the actual ignition timer ISRs...
Definition scheduler.cpp:178
#define FORCE_INLINE
Definition preprocessor.h:82
void nullCallback(void)
A scheduler callback that does nothing.
Definition scheduler.cpp:35
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:57
ScheduleStatus
The current state of a schedule.
Definition scheduler.h:51
@ RUNNING
Definition scheduler.h:59
@ PENDING
Definition scheduler.h:57
@ RUNNING_WITHNEXT
Definition scheduler.h:61
@ OFF
Definition scheduler.h:55
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:90
static bool isRunning(const Schedule &schedule) noexcept
Is the schedule running? I.e. the action has started, but not finished. E.g. injector is open.
Definition scheduler.h:134
const config2 const config4 const config6 statuses & current
Definition scheduler_fuel_controller.cpp:506
uint16_t crankAngle
Definition scheduler_ignition_controller.cpp:695
A fuel injection schedule.
Definition scheduler.h:220
void reset(void) override
Definition scheduler.cpp:51
uint16_t pw
Pulse width in uS.
Definition scheduler.h:225
uint16_t channelDegrees
The number of crank degrees until cylinder is at TDC
Definition scheduler.h:224
An ignition schedule.
Definition scheduler.h:178
void reset(void) override
Definition scheduler.cpp:43
uint16_t channelDegrees
The number of crank degrees until cylinder is at TDC
Definition scheduler.h:185
int16_t dischargeAngle
Angle the coil should discharge at. I.e. spark.
Definition scheduler.h:184
volatile uint32_t _startTime
The system time (in uS) that the schedule started, used by the overdwell protection in timers....
Definition scheduler.h:182
int16_t chargeAngle
Angle the coil should begin charging.
Definition scheduler.h:183
A schedule for a single output channel.
Definition scheduler.h:87
COMPARE_TYPE _nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:121
counter_t & _counter
Reference to the counter register. E.g. TCNT3
Definition scheduler.h:123
callback _pEndCallback
End Callback function for schedule.
Definition scheduler.h:120
void(*)(void) callback
Definition scheduler.h:108
constexpr Schedule(counter_t &counter, compare_t &compare)
Construct a new Schedule object.
Definition scheduler.h:102
decltype(FUEL1_COUNTER) counter_t
The type of a timer counter register (this varies between platforms)
Definition scheduler.h:92
volatile ScheduleStatus _status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:118
compare_t & _compare
**Reference**to the compare register. E.g. OCR3A
Definition scheduler.h:124
volatile COMPARE_TYPE _duration
Scheduled duration (timer ticks)
Definition scheduler.h:117
callback _pStartCallback
Start Callback function for schedule.
Definition scheduler.h:119
virtual void reset(void)
Definition scheduler.cpp:37
decltype(FUEL1_COMPARE) compare_t
The type of a timer compare register (this varies between platforms)
Definition scheduler.h:94
Definition array.h:14
The status struct with current values for all 'live' variables.
Definition statuses.h:47