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
46#define USE_IGN_REFRESH
47#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
48
49#define DWELL_AVERAGE_ALPHA 30
50#define DWELL_AVERAGE(input) (((long)input * (256 - DWELL_AVERAGE_ALPHA) + ((long)currentStatus.actualDwell * DWELL_AVERAGE_ALPHA))) >> 8
51//#define DWELL_AVERAGE(input) (currentStatus.dwell) //Can be use to disable the above for testing
52
53void initialiseSchedulers(void);
54void beginInjectorPriming(void);
55
58
59void refreshIgnitionSchedule1(unsigned long timeToEnd);
60
61//The ARM cores use separate functions for their ISRs
62#if defined(ARDUINO_ARCH_STM32) || defined(CORE_TEENSY)
63 void fuelSchedule1Interrupt(void);
64 void fuelSchedule2Interrupt(void);
65 void fuelSchedule3Interrupt(void);
66 void fuelSchedule4Interrupt(void);
67#if (INJ_CHANNELS >= 5)
68 void fuelSchedule5Interrupt(void);
69#endif
70#if (INJ_CHANNELS >= 6)
71 void fuelSchedule6Interrupt(void);
72#endif
73#if (INJ_CHANNELS >= 7)
74 void fuelSchedule7Interrupt(void);
75#endif
76#if (INJ_CHANNELS >= 8)
77 void fuelSchedule8Interrupt(void);
78#endif
79#if (IGN_CHANNELS >= 1)
81#endif
82#if (IGN_CHANNELS >= 2)
84#endif
85#if (IGN_CHANNELS >= 3)
87#endif
88#if (IGN_CHANNELS >= 4)
90#endif
91#if (IGN_CHANNELS >= 5)
93#endif
94#if (IGN_CHANNELS >= 6)
96#endif
97#if (IGN_CHANNELS >= 7)
99#endif
100#if (IGN_CHANNELS >= 8)
102#endif
103#endif
110enum ScheduleStatus {OFF, PENDING, STAGED, RUNNING}; //The statuses that a schedule can have
111
115
116 // Deduce the real types of the counter and compare registers.
117 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
118 // store 16-bit counter/compare calculation results.
119 using counter_t = decltype(IGN1_COUNTER);
120 using compare_t = decltype(IGN1_COMPARE);
121
130
131 volatile unsigned long duration;
135 volatile unsigned long startTime;
138
141 volatile bool hasNextSchedule = false;
142 volatile bool endScheduleSetByDecoder = false;
143
144 counter_t &counter; // Reference to the counter register. E.g. TCNT3
145 compare_t &compare; // Reference to the compare register. E.g. OCR3A
146 void (&pTimerDisable)(); // Reference to the timer disable function
147 void (&pTimerEnable)(); // Reference to the timer enable function
148};
149
150void _setIgnitionScheduleRunning(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration);
151void _setIgnitionScheduleNext(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration);
152
153inline __attribute__((always_inline)) void setIgnitionSchedule(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration) {
154 if(schedule.Status != RUNNING) { //Check that we're not already part way through a schedule
156 }
157 // Check whether timeout exceeds the maximum future time. This can potentially occur on sequential setups when below ~115rpm
158 else if(timeout < MAX_TIMER_PERIOD){
160 }
161}
162
168
169 // Deduce the real types of the counter and compare registers.
170 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
171 // store 16-bit counter/compare calculation results.
172 using counter_t = decltype(FUEL1_COUNTER);
173 using compare_t = decltype(FUEL1_COMPARE);
174
183
184 volatile unsigned long duration;
192 volatile bool hasNextSchedule = false;
193
194 counter_t &counter; // Reference to the counter register. E.g. TCNT3
195 compare_t &compare; // Reference to the compare register. E.g. OCR3A
196 void (&pTimerDisable)(); // Reference to the timer disable function
197 void (&pTimerEnable)(); // Reference to the timer enable function
198};
199
200void _setFuelScheduleRunning(FuelSchedule &schedule, unsigned long timeout, unsigned long duration);
201void _setFuelScheduleNext(FuelSchedule &schedule, unsigned long timeout, unsigned long duration);
202
203inline __attribute__((always_inline)) void setFuelSchedule(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
204{
205 if(schedule.Status != RUNNING)
206 { //Check that we're not already part way through a schedule
208 }
209 else if(timeout < MAX_TIMER_PERIOD)
210 {
212 }
213}
214
219#if INJ_CHANNELS >= 5
221#endif
222#if INJ_CHANNELS >= 6
224#endif
225#if INJ_CHANNELS >= 7
227#endif
228#if INJ_CHANNELS >= 8
230#endif
231
237#if IGN_CHANNELS >= 6
239#endif
240#if IGN_CHANNELS >= 7
242#endif
243#if IGN_CHANNELS >= 8
245#endif
246
247#endif // SCHEDULER_H
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:349
void fuelSchedule1Interrupt()
Definition scheduler.cpp:364
void fuelSchedule3Interrupt()
Definition scheduler.cpp:384
void fuelSchedule2Interrupt()
Definition scheduler.cpp:374
void fuelSchedule4Interrupt()
Definition scheduler.cpp:394
void ignitionSchedule1Interrupt(void)
Definition scheduler.cpp:487
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:203
void _setFuelScheduleNext(FuelSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:232
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:269
FuelSchedule fuelSchedule4
void disablePendingIgnSchedule(byte channel)
Definition scheduler.cpp:610
void _setIgnitionScheduleRunning(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.cpp:241
IgnitionSchedule ignitionSchedule1
FuelSchedule fuelSchedule1
IgnitionSchedule ignitionSchedule4
void disablePendingFuelSchedule(byte channel)
Definition scheduler.cpp:570
void setIgnitionSchedule(IgnitionSchedule &schedule, unsigned long timeout, unsigned long duration)
Definition scheduler.h:153
FuelSchedule fuelSchedule2
void beginInjectorPriming(void)
Definition scheduler.cpp:286
ScheduleStatus
Definition scheduler.h:110
@ RUNNING
Definition scheduler.h:110
@ PENDING
Definition scheduler.h:110
@ STAGED
Definition scheduler.h:110
@ OFF
Definition scheduler.h:110
IgnitionSchedule ignitionSchedule3
IgnitionSchedule ignitionSchedule2
Definition scheduler.h:167
void(&) pTimerDisable()
Definition scheduler.h:196
void(* pEndFunction)(void)
Definition scheduler.h:189
void(&) pTimerEnable()
Definition scheduler.h:197
COMPARE_TYPE nextStartCompare
Definition scheduler.h:190
decltype(FUEL1_COUNTER) counter_t
Definition scheduler.h:172
COMPARE_TYPE nextEndCompare
Definition scheduler.h:191
volatile COMPARE_TYPE startCompare
The counter value of the timer when this will start.
Definition scheduler.h:186
volatile COMPARE_TYPE endCompare
The counter value of the timer when this will end.
Definition scheduler.h:187
FuelSchedule(counter_t &counter, compare_t &compare, void(&_pTimerDisable)(), void(&_pTimerEnable)())
Definition scheduler.h:175
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:185
counter_t & counter
Definition scheduler.h:194
decltype(FUEL1_COMPARE) compare_t
Definition scheduler.h:173
compare_t & compare
Definition scheduler.h:195
volatile unsigned long duration
Scheduled duration (uS ?)
Definition scheduler.h:184
volatile bool hasNextSchedule
Definition scheduler.h:192
void(* pStartFunction)(void)
Definition scheduler.h:188
Definition scheduler.h:114
decltype(IGN1_COMPARE) compare_t
Definition scheduler.h:120
volatile ScheduleStatus Status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:132
COMPARE_TYPE nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:139
void(* pStartCallback)(void)
Start Callback function for schedule.
Definition scheduler.h:133
void(&) pTimerEnable()
Definition scheduler.h:147
IgnitionSchedule(counter_t &counter, compare_t &compare, void(&_pTimerDisable)(), void(&_pTimerEnable)())
Definition scheduler.h:122
compare_t & compare
Definition scheduler.h:145
volatile unsigned long startTime
Definition scheduler.h:135
decltype(IGN1_COUNTER) counter_t
Definition scheduler.h:119
COMPARE_TYPE nextEndCompare
Planned end of next schedule (when current schedule is RUNNING)
Definition scheduler.h:140
volatile unsigned long duration
Scheduled duration (uS ?)
Definition scheduler.h:131
volatile bool hasNextSchedule
Enable flag for planned next schedule (when current schedule is RUNNING)
Definition scheduler.h:141
volatile COMPARE_TYPE startCompare
The counter value of the timer when this will start.
Definition scheduler.h:136
void(* pEndCallback)(void)
End Callback function for schedule.
Definition scheduler.h:134
counter_t & counter
Definition scheduler.h:144
void(&) pTimerDisable()
Definition scheduler.h:146
volatile COMPARE_TYPE endCompare
The counter value of the timer when this will end.
Definition scheduler.h:137
volatile bool endScheduleSetByDecoder
Definition scheduler.h:142