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
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
208struct FuelSchedule : public Schedule {
209
210 using Schedule::Schedule;
211
214
215 void reset(void) override;
216};
217
225
226#include "schedule_calcs.hpp"
227
228#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
Crank revolution based mathematical functions.
return flags
Definition engineProtection.cpp:170
static TIntegral readSerialIntegralTimeout(void)
Reads an integral type, timing out if necessary.
Definition comms.cpp:175
void moveToNextState(IgnitionSchedule &schedule) noexcept
Shared ignition schedule timer ISR implementation. Should be called by the actual ignition timer ISRs...
Definition scheduler.cpp:180
void nullCallback(void)
A scheduler callback that does nothing.
Definition scheduler.cpp:37
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:59
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:92
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
A fuel injection schedule.
Definition scheduler.h:208
void reset(void) override
Definition scheduler.cpp:53
uint16_t pw
Pulse width in uS.
Definition scheduler.h:213
uint16_t channelDegrees
The number of crank degrees until cylinder is at TDC
Definition scheduler.h:212
An ignition schedule.
Definition scheduler.h:178
void reset(void) override
Definition scheduler.cpp:45
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:39
decltype(FUEL1_COMPARE) compare_t
The type of a timer compare register (this varies between platforms)
Definition scheduler.h:94