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#define USE_IGN_REFRESH
48#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
49
50#define DWELL_AVERAGE_ALPHA 30
51#define DWELL_AVERAGE(input) LOW_PASS_FILTER((input), DWELL_AVERAGE_ALPHA, currentStatus.actualDwell)
52//#define DWELL_AVERAGE(input) (currentStatus.dwell) //Can be use to disable the above for testing
53
54void initialiseSchedulers(void);
55void beginInjectorPriming(void);
56
59
60void refreshIgnitionSchedule1(unsigned long timeToEnd);
61
62//The ARM cores use separate functions for their ISRs
63#if defined(ARDUINO_ARCH_STM32) || defined(CORE_TEENSY)
64 void fuelSchedule1Interrupt(void);
65 void fuelSchedule2Interrupt(void);
66 void fuelSchedule3Interrupt(void);
67 void fuelSchedule4Interrupt(void);
68#if (INJ_CHANNELS >= 5)
69 void fuelSchedule5Interrupt(void);
70#endif
71#if (INJ_CHANNELS >= 6)
72 void fuelSchedule6Interrupt(void);
73#endif
74#if (INJ_CHANNELS >= 7)
75 void fuelSchedule7Interrupt(void);
76#endif
77#if (INJ_CHANNELS >= 8)
78 void fuelSchedule8Interrupt(void);
79#endif
80#if (IGN_CHANNELS >= 1)
82#endif
83#if (IGN_CHANNELS >= 2)
85#endif
86#if (IGN_CHANNELS >= 3)
88#endif
89#if (IGN_CHANNELS >= 4)
91#endif
92#if (IGN_CHANNELS >= 5)
94#endif
95#if (IGN_CHANNELS >= 6)
97#endif
98#if (IGN_CHANNELS >= 7)
100#endif
101#if (IGN_CHANNELS >= 8)
103#endif
104#endif
111enum ScheduleStatus {OFF, PENDING, STAGED, RUNNING}; //The statuses that a schedule can have
112
116
117 // Deduce the real types of the counter and compare registers.
118 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
119 // store 16-bit counter/compare calculation results.
120 using counter_t = decltype(IGN1_COUNTER);
121 using compare_t = decltype(IGN1_COMPARE);
122
131
132 volatile unsigned long duration;
136 volatile unsigned long startTime;
139
142 volatile bool hasNextSchedule = false;
143 volatile bool endScheduleSetByDecoder = false;
144
145 counter_t &counter; // Reference to the counter register. E.g. TCNT3
146 compare_t &compare; // Reference to the compare register. E.g. OCR3A
147 void (&pTimerDisable)(); // Reference to the timer disable function
148 void (&pTimerEnable)(); // Reference to the timer enable function
149};
150
151void _setIgnitionScheduleRunning(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration);
152void _setIgnitionScheduleNext(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration);
153
154inline __attribute__((always_inline)) void setIgnitionSchedule(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
155{
157 {
158 if(schedule.Status != RUNNING)
159 { //Check that we're not already part way through a schedule
161 }
162 // Check whether timeout exceeds the maximum future time. This can potentially occur on sequential setups when below ~115rpm
164 {
166 }
167 }
168}
169
175
176 // Deduce the real types of the counter and compare registers.
177 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
178 // store 16-bit counter/compare calculation results.
179 using counter_t = decltype(FUEL1_COUNTER);
180 using compare_t = decltype(FUEL1_COMPARE);
181
190
191 volatile unsigned long duration;
197 volatile bool hasNextSchedule = false;
198
199 counter_t &counter; // Reference to the counter register. E.g. TCNT3
200 compare_t &compare; // Reference to the compare register. E.g. OCR3A
201 void (&pTimerDisable)(); // Reference to the timer disable function
202 void (&pTimerEnable)(); // Reference to the timer enable function
203};
204
205void _setFuelScheduleRunning(FuelSchedule &schedule, unsigned long timeout, unsigned long duration);
206void _setFuelScheduleNext(FuelSchedule &schedule, unsigned long timeout, unsigned long duration);
207
208inline __attribute__((always_inline)) void setFuelSchedule(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
209{
211 {
212 if(schedule.Status != RUNNING)
213 { //Check that we're not already part way through a schedule
215 }
216 //If the schedule is already running, we can queue up the next pulse. Only do this however if the maximum time between pulses (Based on CRANK_ANGLE_MAX_INJ) is less than the max timer period
218 {
220 }
221 }
222}
223
228#if INJ_CHANNELS >= 5
230#endif
231#if INJ_CHANNELS >= 6
233#endif
234#if INJ_CHANNELS >= 7
236#endif
237#if INJ_CHANNELS >= 8
239#endif
240
246#if IGN_CHANNELS >= 6
248#endif
249#if IGN_CHANNELS >= 7
251#endif
252#if IGN_CHANNELS >= 8
254#endif
255
256#endif // SCHEDULER_H
uint32_t angleToTimeMicroSecPerDegree(uint16_t angle)
Definition crankMaths.cpp:35
int CRANK_ANGLE_MAX_IGN
Definition globals.cpp:129
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:130
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:349
void fuelSchedule1Interrupt()
Definition scheduler.cpp:366
void fuelSchedule3Interrupt()
Definition scheduler.cpp:386
void fuelSchedule2Interrupt()
Definition scheduler.cpp:376
void fuelSchedule4Interrupt()
Definition scheduler.cpp:396
void ignitionSchedule1Interrupt(void)
Definition scheduler.cpp:489
void _setIgnitionScheduleNext(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:259
void setFuelSchedule(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.h:208
void _setFuelScheduleNext(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:229
void initialiseSchedulers(void)
Definition scheduler.cpp:79
void _setFuelScheduleRunning(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:213
IgnitionSchedule ignitionSchedule5
FuelSchedule fuelSchedule3
void refreshIgnitionSchedule1(unsigned long timeToEnd)
Definition scheduler.cpp:272
FuelSchedule fuelSchedule4
void disablePendingIgnSchedule(byte channel)
Definition scheduler.cpp:612
void _setIgnitionScheduleRunning(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:242
IgnitionSchedule ignitionSchedule1
FuelSchedule fuelSchedule1
IgnitionSchedule ignitionSchedule4
void disablePendingFuelSchedule(byte channel)
Definition scheduler.cpp:572
void setIgnitionSchedule(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.h:154
FuelSchedule fuelSchedule2
void beginInjectorPriming(void)
Definition scheduler.cpp:289
ScheduleStatus
Definition scheduler.h:111
@ RUNNING
Definition scheduler.h:111
@ PENDING
Definition scheduler.h:111
@ STAGED
Definition scheduler.h:111
@ OFF
Definition scheduler.h:111
IgnitionSchedule ignitionSchedule3
IgnitionSchedule ignitionSchedule2
Definition scheduler.h:174
void(&) pTimerDisable()
Definition scheduler.h:201
void(* pEndFunction)(void)
Definition scheduler.h:195
void(&) pTimerEnable()
Definition scheduler.h:202
COMPARE_TYPE nextStartCompare
Definition scheduler.h:196
decltype(FUEL1_COUNTER) counter_t
Definition scheduler.h:179
volatile COMPARE_TYPE startCompare
The counter value of the timer when this will start.
Definition scheduler.h:193
FuelSchedule(counter_t &counter, compare_t &compare, void(&_pTimerDisable)(), void(&_pTimerEnable)())
Definition scheduler.h:182
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:192
counter_t & counter
Definition scheduler.h:199
decltype(FUEL1_COMPARE) compare_t
Definition scheduler.h:180
compare_t & compare
Definition scheduler.h:200
volatile unsigned long duration
Scheduled duration (uS)
Definition scheduler.h:191
volatile bool hasNextSchedule
Definition scheduler.h:197
void(* pStartFunction)(void)
Definition scheduler.h:194
Definition scheduler.h:115
decltype(IGN1_COMPARE) compare_t
Definition scheduler.h:121
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:133
COMPARE_TYPE nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:140
void(* pStartCallback)(void)
Start Callback function for schedule.
Definition scheduler.h:134
void(&) pTimerEnable()
Definition scheduler.h:148
IgnitionSchedule(counter_t &counter, compare_t &compare, void(&_pTimerDisable)(), void(&_pTimerEnable)())
Definition scheduler.h:123
compare_t & compare
Definition scheduler.h:146
volatile unsigned long startTime
Definition scheduler.h:136
decltype(IGN1_COUNTER) counter_t
Definition scheduler.h:120
COMPARE_TYPE nextEndCompare
Planned end of next schedule (when current schedule is RUNNING)
Definition scheduler.h:141
volatile unsigned long duration
Scheduled duration (uS ?)
Definition scheduler.h:132
volatile bool hasNextSchedule
Enable flag for planned next schedule (when current schedule is RUNNING)
Definition scheduler.h:142
volatile COMPARE_TYPE startCompare
The counter value of the timer when this will start.
Definition scheduler.h:137
void(* pEndCallback)(void)
End Callback function for schedule.
Definition scheduler.h:135
counter_t & counter
Definition scheduler.h:145
void(&) pTimerDisable()
Definition scheduler.h:147
volatile COMPARE_TYPE endCompare
The counter value of the timer when this will end.
Definition scheduler.h:138
volatile bool endScheduleSetByDecoder
Definition scheduler.h:143