rusefi/firmware/hw_layer/HIP9011_logic.h

119 lines
2.9 KiB
C
Raw Normal View History

2019-01-03 21:22:35 -08:00
/*
* @file HIP9011_logic.h
*
* Created on: Jan 3, 2019
* @author Andrey Belomutskiy, (c) 2012-2019
*/
#ifndef HW_LAYER_HIP9011_LOGIC_H_
#define HW_LAYER_HIP9011_LOGIC_H_
2019-01-03 21:51:32 -08:00
#include "efifeatures.h"
2019-01-03 21:22:35 -08:00
#include "rusefi_enums.h"
2019-01-03 21:51:32 -08:00
#include "hip9011_lookup.h"
2019-01-03 21:22:35 -08:00
/**
* this interface defines hardware communication layer for HIP9011 chip
2019-01-03 21:22:35 -08:00
*/
class Hip9011HardwareInterface {
2019-01-03 21:22:35 -08:00
public:
virtual void sendSyncCommand(unsigned char command) = 0;
virtual void sendCommand(unsigned char command) = 0;
2019-01-03 21:22:35 -08:00
};
#if EFI_PROD_CODE || EFI_SIMULATOR
#define PASS_HIP_PARAMS
#define DEFINE_HIP_PARAMS
#define GET_CONFIG_VALUE(x) CONFIG(x)
#define FORWARD_HIP_PARAMS
#define DEFINE_PARAM_SUFFIX(x)
#else
#define PASS_HIP_PARAMS CONFIG(knockBandCustom), \
CONFIG(cylinderBore), \
2019-01-19 06:14:48 -08:00
CONFIG(hip9011Gain), \
2019-01-19 06:40:39 -08:00
CONFIG(hip9011PrescalerAndSDO), \
CONFIG(knockDetectionWindowStart), \
CONFIG(knockDetectionWindowEnd)
#define FORWARD_HIP_PARAMS knockBandCustom, \
cylinderBore, \
2019-01-19 06:14:48 -08:00
hip9011Gain, \
2019-01-19 06:40:39 -08:00
hip9011PrescalerAndSDO, \
knockDetectionWindowStart, \
knockDetectionWindowEnd
#define DEFINE_HIP_PARAMS float knockBandCustom,\
float cylinderBore, \
2019-01-19 06:14:48 -08:00
float hip9011Gain, \
2019-01-19 06:40:39 -08:00
int hip9011PrescalerAndSDO, \
float knockDetectionWindowStart, \
float knockDetectionWindowEnd
#define GET_CONFIG_VALUE(x) x
#define DEFINE_PARAM_SUFFIX(x) , x
#endif
2019-01-03 21:22:35 -08:00
class HIP9011 {
public:
HIP9011(Hip9011HardwareInterface *hardware);
2019-01-07 20:23:50 -08:00
void prepareHip9011RpmLookup(float angleWindowWidth);
int getIntegrationIndexByRpm(float rpm);
void setStateAndCommand(unsigned char cmd);
2019-01-19 06:40:39 -08:00
void setAngleWindowWidth(DEFINE_HIP_PARAMS);
2019-01-19 06:14:48 -08:00
void handleValue(int rpm DEFINE_PARAM_SUFFIX(DEFINE_HIP_PARAMS));
2019-01-07 20:23:50 -08:00
/**
* band index is only send to HIP chip on startup
*/
int currentBandIndex;
int currentGainIndex;
int correctResponsesCount;
int invalidHip9011ResponsesCount;
float angleWindowWidth;
2019-01-07 20:23:50 -08:00
int currentIntergratorIndex;
2019-01-03 21:22:35 -08:00
bool needToInit;
2019-01-07 20:23:50 -08:00
int settingUpdateCount;
int totalKnockEventsCount;
int currentPrescaler;
Hip9011HardwareInterface *hardware;
2019-01-03 21:22:35 -08:00
/**
* Int/Hold pin is controlled from scheduler call-backs which are set according to current RPM
*
* The following state makes sure that we only have SPI communication while not integrating and that we take
* a good ADC reading after integrating.
*
* Once integration window is over, we wait for the 2nd ADC callback and then initiate SPI communication if needed
*
* hipOutput should be set to used FAST adc device
*/
hip_state_e state;
2019-01-07 20:23:50 -08:00
float rpmLookup[INT_LOOKUP_SIZE];
};
2019-01-03 21:22:35 -08:00
float getHIP9011Band(DEFINE_HIP_PARAMS);
int getBandIndex(DEFINE_HIP_PARAMS);
int getHip9011GainIndex(DEFINE_HIP_PARAMS);
2019-01-03 21:51:32 -08:00
// 0b01000000
#define SET_PRESCALER_CMD 0x40
2019-01-03 21:51:32 -08:00
// 0b11100000
#define SET_CHANNEL_CMD 0xE0
2019-01-04 06:24:23 -08:00
// 0b11000000
#define SET_INTEGRATOR_CMD 0xC0
2019-01-03 21:51:32 -08:00
// 0b00000000
#define SET_BAND_PASS_CMD 0x0
2019-01-03 21:51:32 -08:00
// 0b10000000
#define SET_GAIN_CMD 0x80
2019-01-03 21:51:32 -08:00
2019-01-07 22:20:18 -08:00
#define _8MHZ_PRESCALER 6
2019-01-03 21:22:35 -08:00
#endif /* HW_LAYER_HIP9011_LOGIC_H_ */