rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hip9011_logic.h
Go to the documentation of this file.
1/*
2 * @file hip9011_logic.h
3 *
4 * Created on: Jan 3, 2019
5 * @author Andrey Belomutskiy, (c) 2012-2020
6 */
7
8#pragma once
9
10#include "rusefi_enums.h"
11
12/* enable debug by default */
13#ifndef EFI_HIP_9011_DEBUG
14#define EFI_HIP_9011_DEBUG EFI_HIP_9011
15#endif
16
17#define HIP9011_ANALOG_OUTPUT_MAX 5.0f
18#define HIP9011_DIGITAL_OUTPUT_MAX 0x03ff /* 10 bit max value */
19#define HIP_INPUT_CHANNELS 2
20
21#define INT_LOOKUP_SIZE 32
22#define GAIN_LOOKUP_SIZE 64
23#define BAND_LOOKUP_SIZE 64
24
25typedef enum {
26 /* initial state and state used during value read and calculations */
28 /* chip is configuread and ready for next integration */
30 /* integration is in progress */
32 /* in default mode driver is waiting for first ADC callback */
34 /* in default mode driver is waiting for second ADC callback, saves it and switched to NOT_READY state */
37
38/**
39 * this interface defines hardware communication layer for HIP9011 chip
40 */
42public:
43 virtual int sendSyncCommand(unsigned char command, uint8_t *rx_ptr) = 0;
44};
45
46#if EFI_PROD_CODE || EFI_SIMULATOR
47#define PASS_HIP_PARAMS
48#define DEFINE_HIP_PARAMS
49#define GET_CONFIG_VALUE(x) engineConfiguration->x
50#define FORWARD_HIP_PARAMS
51#define DEFINE_PARAM_SUFFIX(x)
52#else
53
54#define PASS_HIP_PARAMS \
55 engineConfiguration->cylinderBore, \
56 engineConfiguration->hip9011Gain, \
57 engineConfiguration->hip9011Prescaler, \
58 engineConfiguration->knockDetectionWindowStart, \
59 engineConfiguration->knockSamplingDuration
60
61#define FORWARD_HIP_PARAMS \
62 cylinderBore, \
63 hip9011Gain, \
64 hip9011Prescaler, \
65 knockDetectionWindowStart, \
66 knockSamplingDuration
67
68#define DEFINE_HIP_PARAMS \
69 float cylinderBore, \
70 float hip9011Gain, \
71 uint8_t hip9011Prescaler, \
72 float knockDetectionWindowStart, \
73 float knockSamplingDuration
74
75
76#define GET_CONFIG_VALUE(x) x
77#define DEFINE_PARAM_SUFFIX(x) , x
78#endif
79
80class HIP9011 {
81public:
83 int sendCommand(uint8_t cmd);
84 int sendCommandGetReply(uint8_t cmd, uint8_t *reply);
85
86 float getRpmByAngleWindowAndTimeUs(int timeUs, float angleWindowWidth);
87 void prepareRpmLookup(void);
88 void setAngleWindowWidth(DEFINE_HIP_PARAMS);
89 void handleSettings(int rpm DEFINE_PARAM_SUFFIX(DEFINE_HIP_PARAMS));
90 int cylinderToChannelIdx(int cylinder);
91 int handleChannel(DEFINE_HIP_PARAMS);
92 int readValueAndHandleChannel(DEFINE_HIP_PARAMS);
93 float getBand(DEFINE_HIP_PARAMS);
94 int getIntegrationIndexByRpm(float rpm);
95 int getIntegrationTimeByIndex(int index);
96 int getBandIndex(DEFINE_HIP_PARAMS);
97 int getGainIndex(DEFINE_HIP_PARAMS);
98
99 /* Settings loaded to chip */
100 uint8_t intergratorIdx = 0xff;
101 uint8_t bandIdx = 0xff;
102 uint8_t prescaler = 0xff;
103 uint8_t gainIdx = 0xff;
104 uint8_t channelIdx = 0xff;
105
106 float angleWindowWidth = - 1;
107
109 bool adv_mode = false;
110 /**
111 * Int/Hold pin is controlled from scheduler call-backs which are set according to current RPM
112 *
113 * The following state makes sure that we only have SPI communication while not integrating and that we take
114 * a good ADC reading after integrating.
115 *
116 * Once integration window is over, we wait for the 2nd ADC callback and then initiate SPI communication if needed
117 *
118 * hipOutput should be set to used FAST adc device
119 */
121 int8_t cylinderNumber = -1;
123 uint16_t rawValue[HIP_INPUT_CHANNELS];
124
125 /* No need to have float accuracity, 65535 RPM is reasonable limit */
126 uint16_t rpmLookup[INT_LOOKUP_SIZE];
127
128 // Timestamp of the last sensed event
129 efitick_t knockSampleTimestamp = 0;
130
131 #if EFI_HIP_9011_DEBUG
132 /* SPI counters */
135
136 /* logic error counters */
137 int samples = 0;
138 int overrun = 0;
139 int unsync = 0;
140 #endif
141};
142
143// 0b010x.xxx0, SDO always active
144#define SET_PRESCALER_CMD(v) (0x40 | (((v) & 0x0f) << 1) | 0)
145// 0b1110.000x
146#define SET_CHANNEL_CMD(v) (0xE0 | ((v) & 0x01))
147// 0b00xx.xxxx
148#define SET_BAND_PASS_CMD(v) (0x00 | ((v) & 0x3f))
149/* magic replyed on SET_BAND_PASS_CMD in advanced mode */
150#define SET_BAND_PASS_REP (0x01)
151// 0b10xx.xxxx
152#define SET_GAIN_CMD(v) (0x80 | ((v) & 0x3f))
153/* magic replyed on SET_GAIN_CMD in advanced mode */
154#define SET_GAIN_REP (0xe0)
155// 0b110x.xxxx
156#define SET_INTEGRATOR_CMD(v) (0xC0 | ((v) & 0x1f))
157/* magic replyed on SET_INTEGRATOR_CMD in advanced mode */
158#define SET_INTEGRATOR_REP (0x71)
159// 0b0111.0001
160#define SET_ADVANCED_MODE_CMD (0x71)
161/* magic replyed on SET_ADVANCED_MODE_CMD in advanced mode */
162#define SET_ADVANCED_MODE_REP ((~SET_ADVANCED_MODE_CMD) & 0xff)
163
164// D[4:1] = 0000 : 4 MHz
165#define HIP_4MHZ_PRESCALER (0x0)
166// D[4:1] = 0001 : 5 MHz
167#define HIP_5MHZ_PRESCALER (0x1)
168// D[4:1] = 0010 : 6 MHz
169#define HIP_6MHZ_PRESCALER (0x2)
170// D[4:1] = 0011 ; 8 MHz
171#define HIP_8MHZ_PRESCALER (0x3)
172// D[4:1] = 0100 ; 10 MHz
173#define HIP_10MHZ_PRESCALER (0x4)
174// D[4:1] = 0101 ; 12 MHz
175#define HIP_12MHZ_PRESCALER (0x5)
176// D[4:1] = 0110 : 16 MHz
177#define HIP_16MHZ_PRESCALER (0x6)
178// D[4:1] = 0111 : 20 MHz
179#define HIP_20MHZ_PRESCALER (0x7)
180// D[4:1] = 1000 : 24 MHz
181#define HIP_24MHZ_PRESCALER (0x8)
182
int cylinderToChannelIdx(int cylinder)
float getBand(DEFINE_HIP_PARAMS)
uint8_t channelIdx
int getIntegrationTimeByIndex(int index)
efitick_t knockSampleTimestamp
uint8_t intergratorIdx
void handleSettings(int rpm DEFINE_PARAM_SUFFIX(DEFINE_HIP_PARAMS))
int8_t cylinderNumber
int correctResponsesCount
int8_t expectedCylinderNumber
int getBandIndex(DEFINE_HIP_PARAMS)
int getGainIndex(DEFINE_HIP_PARAMS)
hip_state_e state
uint8_t bandIdx
int readValueAndHandleChannel(DEFINE_HIP_PARAMS)
int handleChannel(DEFINE_HIP_PARAMS)
uint8_t prescaler
void setAngleWindowWidth(DEFINE_HIP_PARAMS)
float getRpmByAngleWindowAndTimeUs(int timeUs, float angleWindowWidth)
int sendCommand(uint8_t cmd)
Hip9011HardwareInterface * hw
int getIntegrationIndexByRpm(float rpm)
void prepareRpmLookup(void)
int sendCommandGetReply(uint8_t cmd, uint8_t *reply)
uint8_t gainIdx
uint16_t rpmLookup[INT_LOOKUP_SIZE]
float angleWindowWidth
uint16_t rawValue[HIP_INPUT_CHANNELS]
bool adv_mode
int invalidResponsesCount
virtual int sendSyncCommand(unsigned char command, uint8_t *rx_ptr)=0
static Hip9011Hardware hardware
Definition hip9011.cpp:81
hip_state_e
@ NOT_READY
@ READY_TO_INTEGRATE
@ WAITING_FOR_RESULT_ADC
@ IS_INTEGRATING
@ WAITING_FOR_ADC_TO_SKIP
Fundamental rusEFI enumerable types live here.