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
60void disableAllIgnSchedules(void);
61
62void refreshIgnitionSchedule1(unsigned long timeToEnd);
63
64//The ARM cores use separate functions for their ISRs
65#if defined(ARDUINO_ARCH_STM32) || defined(CORE_TEENSY)
66 void fuelSchedule1Interrupt(void);
67 void fuelSchedule2Interrupt(void);
68 void fuelSchedule3Interrupt(void);
69 void fuelSchedule4Interrupt(void);
70#if (INJ_CHANNELS >= 5)
71 void fuelSchedule5Interrupt(void);
72#endif
73#if (INJ_CHANNELS >= 6)
74 void fuelSchedule6Interrupt(void);
75#endif
76#if (INJ_CHANNELS >= 7)
77 void fuelSchedule7Interrupt(void);
78#endif
79#if (INJ_CHANNELS >= 8)
80 void fuelSchedule8Interrupt(void);
81#endif
82#if (IGN_CHANNELS >= 1)
84#endif
85#if (IGN_CHANNELS >= 2)
87#endif
88#if (IGN_CHANNELS >= 3)
90#endif
91#if (IGN_CHANNELS >= 4)
93#endif
94#if (IGN_CHANNELS >= 5)
96#endif
97#if (IGN_CHANNELS >= 6)
99#endif
100#if (IGN_CHANNELS >= 7)
102#endif
103#if (IGN_CHANNELS >= 8)
105#endif
106#endif
113enum ScheduleStatus {OFF, PENDING, STAGED, RUNNING}; //The statuses that a schedule can have
114
118
119 // Deduce the real types of the counter and compare registers.
120 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
121 // store 16-bit counter/compare calculation results.
122 using counter_t = decltype(IGN1_COUNTER);
123 using compare_t = decltype(IGN1_COMPARE);
124
133
134 volatile unsigned long duration;
138 volatile unsigned long startTime;
141
144 volatile bool hasNextSchedule = false;
145 volatile bool endScheduleSetByDecoder = false;
146
147 counter_t &counter; // Reference to the counter register. E.g. TCNT3
148 compare_t &compare; // Reference to the compare register. E.g. OCR3A
149 void (&pTimerDisable)(); // Reference to the timer disable function
150 void (&pTimerEnable)(); // Reference to the timer enable function
151};
152
153void _setIgnitionScheduleRunning(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration);
154void _setIgnitionScheduleNext(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration);
155
156inline __attribute__((always_inline)) void setIgnitionSchedule(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
157{
159 {
160 if(schedule.Status != RUNNING)
161 { //Check that we're not already part way through a schedule
163 }
164 // Check whether timeout exceeds the maximum future time. This can potentially occur on sequential setups when below ~115rpm
166 {
168 }
169 }
170}
171
177
178 // Deduce the real types of the counter and compare registers.
179 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
180 // store 16-bit counter/compare calculation results.
181 using counter_t = decltype(FUEL1_COUNTER);
182 using compare_t = decltype(FUEL1_COMPARE);
183
192
193 volatile unsigned long duration;
199 volatile bool hasNextSchedule = false;
200
201 counter_t &counter; // Reference to the counter register. E.g. TCNT3
202 compare_t &compare; // Reference to the compare register. E.g. OCR3A
203 void (&pTimerDisable)(); // Reference to the timer disable function
204 void (&pTimerEnable)(); // Reference to the timer enable function
205};
206
207void _setFuelScheduleRunning(FuelSchedule &schedule, unsigned long timeout, unsigned long duration);
208void _setFuelScheduleNext(FuelSchedule &schedule, unsigned long timeout, unsigned long duration);
209
210inline __attribute__((always_inline)) void setFuelSchedule(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
211{
213 {
214 if(schedule.Status != RUNNING)
215 { //Check that we're not already part way through a schedule
217 }
218 //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
220 {
222 }
223 }
224}
225
230#if INJ_CHANNELS >= 5
232#endif
233#if INJ_CHANNELS >= 6
235#endif
236#if INJ_CHANNELS >= 7
238#endif
239#if INJ_CHANNELS >= 8
241#endif
242
248#if IGN_CHANNELS >= 6
250#endif
251#if IGN_CHANNELS >= 7
253#endif
254#if IGN_CHANNELS >= 8
256#endif
257
258#endif // SCHEDULER_H
uint32_t angleToTimeMicroSecPerDegree(uint16_t angle)
Definition crankMaths.cpp:35
int CRANK_ANGLE_MAX_IGN
Definition globals.cpp:103
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:104
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:348
void fuelSchedule1Interrupt()
Definition scheduler.cpp:370
void fuelSchedule3Interrupt()
Definition scheduler.cpp:390
void fuelSchedule2Interrupt()
Definition scheduler.cpp:380
void fuelSchedule4Interrupt()
Definition scheduler.cpp:400
void ignitionSchedule1Interrupt(void)
Definition scheduler.cpp:493
void disableIgnSchedule(byte channel)
Definition scheduler.cpp:624
void _setIgnitionScheduleNext(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:261
void setFuelSchedule(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.h:210
void _setFuelScheduleNext(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:231
void initialiseSchedulers(void)
Definition scheduler.cpp:81
void _setFuelScheduleRunning(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:215
IgnitionSchedule ignitionSchedule5
FuelSchedule fuelSchedule3
void refreshIgnitionSchedule1(unsigned long timeToEnd)
Definition scheduler.cpp:274
void disableFuelSchedule(byte channel)
Definition scheduler.cpp:576
void disableAllFuelSchedules(void)
Definition scheduler.cpp:671
FuelSchedule fuelSchedule4
void _setIgnitionScheduleRunning(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:244
IgnitionSchedule ignitionSchedule1
FuelSchedule fuelSchedule1
IgnitionSchedule ignitionSchedule4
void disableAllIgnSchedules(void)
Definition scheduler.cpp:682
void setIgnitionSchedule(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.h:156
FuelSchedule fuelSchedule2
void beginInjectorPriming(void)
Definition scheduler.cpp:293
ScheduleStatus
Definition scheduler.h:113
@ RUNNING
Definition scheduler.h:113
@ PENDING
Definition scheduler.h:113
@ STAGED
Definition scheduler.h:113
@ OFF
Definition scheduler.h:113
IgnitionSchedule ignitionSchedule3
IgnitionSchedule ignitionSchedule2
Definition scheduler.h:176
void(&) pTimerDisable()
Definition scheduler.h:203
void(* pEndFunction)(void)
Definition scheduler.h:197
void(&) pTimerEnable()
Definition scheduler.h:204
COMPARE_TYPE nextStartCompare
Definition scheduler.h:198
decltype(FUEL1_COUNTER) counter_t
Definition scheduler.h:181
volatile COMPARE_TYPE startCompare
The counter value of the timer when this will start.
Definition scheduler.h:195
FuelSchedule(counter_t &counter, compare_t &compare, void(&_pTimerDisable)(), void(&_pTimerEnable)())
Definition scheduler.h:184
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:194
counter_t & counter
Definition scheduler.h:201
decltype(FUEL1_COMPARE) compare_t
Definition scheduler.h:182
compare_t & compare
Definition scheduler.h:202
volatile unsigned long duration
Scheduled duration (uS)
Definition scheduler.h:193
volatile bool hasNextSchedule
Definition scheduler.h:199
void(* pStartFunction)(void)
Definition scheduler.h:196
Definition scheduler.h:117
decltype(IGN1_COMPARE) compare_t
Definition scheduler.h:123
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:135
COMPARE_TYPE nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:142
void(* pStartCallback)(void)
Start Callback function for schedule.
Definition scheduler.h:136
void(&) pTimerEnable()
Definition scheduler.h:150
IgnitionSchedule(counter_t &counter, compare_t &compare, void(&_pTimerDisable)(), void(&_pTimerEnable)())
Definition scheduler.h:125
compare_t & compare
Definition scheduler.h:148
volatile unsigned long startTime
Definition scheduler.h:138
decltype(IGN1_COUNTER) counter_t
Definition scheduler.h:122
COMPARE_TYPE nextEndCompare
Planned end of next schedule (when current schedule is RUNNING)
Definition scheduler.h:143
volatile unsigned long duration
Scheduled duration (uS ?)
Definition scheduler.h:134
volatile bool hasNextSchedule
Enable flag for planned next schedule (when current schedule is RUNNING)
Definition scheduler.h:144
volatile COMPARE_TYPE startCompare
The counter value of the timer when this will start.
Definition scheduler.h:139
void(* pEndCallback)(void)
End Callback function for schedule.
Definition scheduler.h:137
counter_t & counter
Definition scheduler.h:147
void(&) pTimerDisable()
Definition scheduler.h:149
volatile COMPARE_TYPE endCompare
The counter value of the timer when this will end.
Definition scheduler.h:140
volatile bool endScheduleSetByDecoder
Definition scheduler.h:145