Speeduino
Loading...
Searching...
No Matches
decoder_state.h
Go to the documentation of this file.
1#pragma once
2
4#include <algorithm>
5#include <stdint.h>
6#ifdef USE_LIBDIVIDE
7#include <libdivide.h>
8#endif
9#include "config_pages.h"
10#include "scheduler.h"
11
12namespace decoders {
13
14namespace shared {
15
16 // Stores the internal decoder state(s)
17 struct state_t {
18 volatile unsigned long curGap = 0U;
19 volatile unsigned long curGap2 = 0U;
20 volatile unsigned long curGap3 = 0U;
21 volatile unsigned long lastGap = 0U;
22 volatile unsigned long targetGap = 0U;
23 unsigned long MAX_STALL_TIME = 0U; // //The maximum time (in uS) that the system will continue to function before the engine is considered stalled/stopped. This is unique to each decoder, depending on the number of teeth etc. 500000 (half a second) is used as the default value, most decoders will be much less.
24 uint16_t toothCurrentCount = 0; //The current number of teeth. Once sync has been achieved, this can never actually be 0.
25 volatile byte toothSystemCount = 0; //Used for decoders such as Audi 135 where not every tooth is used for calculating crank angle. This variable stores the actual number of teeth, not the number being used to calculate crank angle };
26 volatile unsigned long toothSystemLastToothTime = 0; //As below, but used for decoders where not every tooth count is used for calculation
27 volatile uint32_t toothLastToothTime = 0; //The time (micros()) that the last tooth was registered
28 volatile unsigned long toothLastSecToothTime = 0; //The time (micros()) that the last tooth was registered on the secondary input
29 volatile unsigned long toothLastThirdToothTime = 0; //The time (micros()) that the last tooth was registered on the second cam input
30 volatile uint32_t toothLastMinusOneToothTime = 0; //The time (micros()) that the tooth before the last tooth was registered
31 volatile unsigned long toothLastMinusOneSecToothTime = 0; //The time (micros()) that the tooth before the last tooth was registered on secondary input
32 volatile unsigned long toothLastToothRisingTime = 0; //The time (micros()) that the last tooth rose (used by special decoders to determine missing teeth polarity)
33 volatile unsigned long toothLastSecToothRisingTime = 0; //The time (micros()) that the last tooth rose on the secondary input (used by special decoders to determine missing teeth polarity)
34 volatile unsigned long targetGap2 = 0;
35 volatile unsigned long targetGap3 = 0;
36 volatile unsigned long toothOneTime = 0; //The time (micros()) that tooth 1 last triggered
37 volatile unsigned long toothOneMinusOneTime = 0; //The 2nd to last time (micros()) that tooth 1 last triggered
38 volatile unsigned long lastSyncRevolution = 0; // the revolution value of last valid sync
39 volatile bool revolutionOne = 0; // For sequential operation, this tracks whether the current revolution is 1 or 2 (not 1)
40 uint16_t triggerActualTeeth = 0;
41 volatile uint16_t secondaryToothCount = 0; //Used for identifying the current secondary (Usually cam) tooth for patterns with multiple secondary teeth
42 volatile unsigned long triggerFilterTime = 0; // The shortest time (in uS) that pulses will be accepted (Used for debounce filtering)
43 volatile unsigned long triggerSecFilterTime = 0; // The shortest time (in uS) that pulses will be accepted (Used for debounce filtering) for the secondary input
44 volatile unsigned long triggerThirdFilterTime = 0; // The shortest time (in uS) that pulses will be accepted (Used for debounce filtering) for the Third input
45 volatile uint16_t triggerToothAngle = 0; //The number of crank degrees that elapse per tooth
46 uint8_t checkSyncToothCount = 0; //How many teeth must've been seen on this revolution before we try to confirm sync (Useful for missing tooth type decoders)
47 unsigned long lastVVTtime = 0; //The time between the vvt reference pulse and the last crank pulse
49 int16_t toothAngles[24]{}; //An array for storing fixed tooth angles. Currently sized at 24 for the GM 24X decoder, but may grow later if there are other decoders that use this style
52#ifdef USE_LIBDIVIDE
53 libdivide::libdivide_s16_t divTriggerToothAngle{};
54#endif
55
60 void setFilter(unsigned long curGap, const config4 &page4);
61
63 void reset(void);
64
72 bool toothWithinMaxStallTime(uint32_t curTime);
73
78 void setTriggerToothAngle(uint16_t angle);
79
80 inline uint16_t clampToActualTeeth(const config4 &page4, uint16_t toothNum, uint8_t toothAdder) const {
81 if(toothNum > triggerActualTeeth && toothNum <= page4.triggerTeeth) { toothNum = triggerActualTeeth; }
82 return (std::min)(toothNum, (uint16_t)(triggerActualTeeth + toothAdder));
83 }
84
85 int16_t toothNumFromScheduleAngles(const config4 &page4, const IgnitionSchedule &schedule) const {
86 int16_t angle = schedule.dischargeAngle - page4.triggerAngle;
87#ifdef USE_LIBDIVIDE
88 // LCOV_EXCL_BR_START
89 return libdivide::libdivide_s16_do(angle, &divTriggerToothAngle);
90 // LCOV_EXCL_BR_STOP
91#else
92 return angle / (int16_t)triggerToothAngle;
93#endif
94 }
95 };
96}
97}
#define IGN_CHANNELS
Definition board_avr2560.h:25
The tune page structs and related defines.
const config4 & page4
Definition engineProtection.cpp:172
Definition basicDistributor.cpp:8
Workaround when min() & max() are defined as macros, which generates compile errors in most C++ stand...
An ignition schedule.
Definition scheduler.h:178
int16_t dischargeAngle
Angle the coil should discharge at. I.e. spark.
Definition scheduler.h:184
Definition config_pages.h:357
byte triggerTeeth
The full count of teeth on the trigger wheel if there were no gaps.
Definition config_pages.h:393
int16_t triggerAngle
Angle (ATDC) when tooth No:1 on the primary wheel sends signal (-360 to +360 deg.)
Definition config_pages.h:359
Definition decoder_t.h:76
Current decoder status.
Definition decoder_t.h:69
Definition decoder_state.h:17
volatile uint16_t secondaryToothCount
Definition decoder_state.h:41
bool toothWithinMaxStallTime(uint32_t curTime)
Check how long ago the last tooth was seen compared to now. If it was more than MAX_STALL_TIME then t...
Definition decoder_state.cpp:40
volatile unsigned long curGap3
Definition decoder_state.h:20
volatile unsigned long triggerSecFilterTime
Definition decoder_state.h:43
volatile unsigned long triggerFilterTime
Definition decoder_state.h:42
int16_t toothNumFromScheduleAngles(const config4 &page4, const IgnitionSchedule &schedule) const
Definition decoder_state.h:85
void reset(void)
Reset tooth statues & times.
Definition decoder_state.cpp:29
unsigned long MAX_STALL_TIME
Definition decoder_state.h:23
uint16_t triggerActualTeeth
Definition decoder_state.h:40
volatile unsigned long toothLastSecToothTime
Definition decoder_state.h:28
volatile unsigned long toothLastThirdToothTime
Definition decoder_state.h:29
void setTriggerToothAngle(uint16_t angle)
Set the angle between teeth.
Definition decoder_state.cpp:51
volatile unsigned long toothLastMinusOneSecToothTime
Definition decoder_state.h:31
uint8_t checkSyncToothCount
Definition decoder_state.h:46
volatile unsigned long toothOneTime
Definition decoder_state.h:36
int16_t toothAngles[24]
Definition decoder_state.h:49
uint16_t clampToActualTeeth(const config4 &page4, uint16_t toothNum, uint8_t toothAdder) const
Definition decoder_state.h:80
volatile uint16_t triggerToothAngle
Definition decoder_state.h:45
unsigned long lastVVTtime
Definition decoder_state.h:47
volatile unsigned long toothLastSecToothRisingTime
Definition decoder_state.h:33
uint16_t toothCurrentCount
Definition decoder_state.h:24
decoder_status_t decoderStatus
Definition decoder_state.h:50
volatile bool revolutionOne
Definition decoder_state.h:39
volatile unsigned long targetGap3
Definition decoder_state.h:35
volatile unsigned long curGap
Definition decoder_state.h:18
volatile unsigned long toothSystemLastToothTime
Definition decoder_state.h:26
volatile unsigned long targetGap2
Definition decoder_state.h:34
volatile unsigned long targetGap
Definition decoder_state.h:22
volatile unsigned long toothLastToothRisingTime
Definition decoder_state.h:32
volatile unsigned long curGap2
Definition decoder_state.h:19
volatile unsigned long lastSyncRevolution
Definition decoder_state.h:38
void setFilter(unsigned long curGap, const config4 &page4)
Definition decoder_state.cpp:9
volatile uint32_t toothLastToothTime
Definition decoder_state.h:27
decoder_features_t decoderFeatures
Definition decoder_state.h:51
volatile uint32_t toothLastMinusOneToothTime
Definition decoder_state.h:30
uint16_t ignitionEndTeeth[IGN_CHANNELS]
Definition decoder_state.h:48
volatile unsigned long triggerThirdFilterTime
Definition decoder_state.h:44
volatile unsigned long toothOneMinusOneTime
Definition decoder_state.h:37
volatile byte toothSystemCount
Definition decoder_state.h:25
volatile unsigned long lastGap
Definition decoder_state.h:21