2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file trigger_decoder.h
|
|
|
|
*
|
|
|
|
* @date Dec 24, 2013
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2015
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TRIGGER_DECODER_H_
|
|
|
|
#define TRIGGER_DECODER_H_
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "trigger_structure.h"
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
|
|
|
|
#define NO_LEFT_FILTER -1
|
|
|
|
#define NO_RIGHT_FILTER 1000
|
|
|
|
|
|
|
|
class TriggerState;
|
|
|
|
|
|
|
|
typedef void (*TriggerStateCallback)(TriggerState *);
|
|
|
|
|
2015-09-08 20:01:07 -07:00
|
|
|
typedef struct {
|
|
|
|
/**
|
|
|
|
* index within trigger revolution, from 0 to trigger event count
|
|
|
|
*/
|
|
|
|
uint32_t current_index;
|
|
|
|
/**
|
2015-09-12 13:01:43 -07:00
|
|
|
* Number of actual events of each channel within current trigger cycle, these
|
|
|
|
* values are used to detect trigger signal errors.
|
2015-09-08 20:01:07 -07:00
|
|
|
* see TriggerShape
|
|
|
|
*/
|
|
|
|
uint32_t eventCount[PWM_PHASE_MAX_WAVE_PER_PWM];
|
2015-09-12 13:01:43 -07:00
|
|
|
/**
|
|
|
|
* This array is used to calculate duty cycle of each trigger channel.
|
|
|
|
* Current implementation is a bit funny - it does not really consider if an event
|
|
|
|
* is a rise or a fall, it works based on the event order within synchronization cycle.
|
|
|
|
*
|
|
|
|
* 32 bit value is good enough here, overflows will happen but they would work just fine.
|
|
|
|
*/
|
|
|
|
uint32_t timeOfPreviousEventNt[PWM_PHASE_MAX_WAVE_PER_PWM];
|
|
|
|
/**
|
|
|
|
* Here we accumulate the amount of time this signal was ON within current trigger cycle
|
|
|
|
*/
|
|
|
|
uint32_t totalTimeNt[PWM_PHASE_MAX_WAVE_PER_PWM];
|
2015-09-08 20:01:07 -07:00
|
|
|
} current_cycle_state_s;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
class TriggerState {
|
|
|
|
public:
|
|
|
|
TriggerState();
|
|
|
|
int getCurrentIndex();
|
|
|
|
int getTotalRevolutionCounter();
|
|
|
|
efitime_t getTotalEventCounter();
|
|
|
|
efitime_t getStartOfRevolutionIndex();
|
|
|
|
void decodeTriggerEvent(trigger_event_e const signal, efitime_t nowUs DECLARE_ENGINE_PARAMETER_S);
|
|
|
|
|
2015-09-24 19:02:47 -07:00
|
|
|
bool_t isValidIndex(DECLARE_ENGINE_PARAMETER_F);
|
2015-07-10 06:01:56 -07:00
|
|
|
float getTriggerDutyCycle(int index);
|
|
|
|
TriggerStateCallback cycleCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TRUE if we know where we are
|
|
|
|
*/
|
|
|
|
bool shaft_is_synchronized;
|
|
|
|
|
2015-09-11 22:02:28 -07:00
|
|
|
uint32_t durationBeforePrevious;
|
2015-07-10 06:01:56 -07:00
|
|
|
uint32_t toothed_previous_duration;
|
|
|
|
/**
|
|
|
|
* this could be a local variable, but it's better for debugging to have it as a field
|
|
|
|
*/
|
|
|
|
uint32_t currentDuration;
|
|
|
|
efitime_t toothed_previous_time;
|
|
|
|
|
2015-09-08 20:01:07 -07:00
|
|
|
current_cycle_state_s currentCycle;
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Total time result for previous trigger cycle
|
2015-09-12 13:01:43 -07:00
|
|
|
* See totalTimeNt
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
uint32_t prevTotalTime[PWM_PHASE_MAX_WAVE_PER_PWM];
|
|
|
|
int expectedTotalTime[PWM_PHASE_MAX_WAVE_PER_PWM];
|
2015-09-12 14:01:24 -07:00
|
|
|
|
|
|
|
uint32_t timeOfLastEvent[PWM_PHASE_MAX_COUNT];
|
|
|
|
float instantRpmValue[PWM_PHASE_MAX_COUNT];
|
|
|
|
|
2015-08-17 20:02:01 -07:00
|
|
|
/**
|
|
|
|
* how many times since ECU reboot we had unexpected number of teeth in trigger cycle
|
|
|
|
*/
|
2015-07-10 06:01:56 -07:00
|
|
|
uint32_t totalTriggerErrorCounter;
|
|
|
|
uint32_t runningTriggerErrorCounter;
|
|
|
|
uint32_t orderingErrorCounter;
|
|
|
|
uint32_t runningOrderingErrorCounter;
|
|
|
|
|
2015-09-13 07:01:39 -07:00
|
|
|
void reset();
|
2015-07-10 06:01:56 -07:00
|
|
|
void resetRunningCounters();
|
|
|
|
|
|
|
|
uint32_t runningRevolutionCounter;
|
|
|
|
private:
|
2015-09-08 20:01:07 -07:00
|
|
|
void resetCurrentCycleState();
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
trigger_event_e curSignal;
|
|
|
|
trigger_event_e prevSignal;
|
|
|
|
efitime_t totalEventCountBase;
|
|
|
|
uint32_t totalRevolutionCounter;
|
|
|
|
bool isFirstEvent;
|
|
|
|
efitime_t prevCycleDuration;
|
|
|
|
efitick_t startOfCycleNt;
|
|
|
|
};
|
|
|
|
|
|
|
|
float getEngineCycle(operation_mode_e operationMode);
|
|
|
|
void addSkippedToothTriggerEvents(trigger_wheel_e wheel, TriggerShape *s,
|
|
|
|
int totalTeethCount, int skippedCount,
|
|
|
|
float toothWidth,
|
|
|
|
float offset, float engineCycle, float filterLeft, float filterRight);
|
|
|
|
void initializeSkippedToothTriggerShapeExt(TriggerShape *s, int totalTeethCount, int skippedCount, operation_mode_e operationMode);
|
2015-09-13 13:01:38 -07:00
|
|
|
uint32_t findTriggerZeroEventIndex(TriggerState *state, TriggerShape * shape, trigger_config_s const*triggerConfig DECLARE_ENGINE_PARAMETER_S);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
class Engine;
|
|
|
|
|
|
|
|
void initTriggerDecoder(void);
|
|
|
|
void initTriggerDecoderLogger(Logging *sharedLogger);
|
|
|
|
|
|
|
|
bool_t isTriggerDecoderError(void);
|
|
|
|
|
2015-07-15 21:07:25 -07:00
|
|
|
#define considerEventForGap() (!TRIGGER_SHAPE(useOnlyPrimaryForSync) || isPrimary)
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#define isLessImportant(signal) ((TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_UP) \
|
2015-07-15 20:07:51 -07:00
|
|
|
|| (!TRIGGER_SHAPE(useRiseEdge) && signal != SHAFT_PRIMARY_DOWN) \
|
2015-07-15 21:07:25 -07:00
|
|
|
|| (!considerEventForGap()) \
|
2015-07-15 20:07:51 -07:00
|
|
|
)
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#endif /* TRIGGER_DECODER_H_ */
|