rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions | Variables
adc_inputs.cpp File Reference

Detailed Description

Low level ADC code.

Date
Jan 14, 2013
Author
Andrey Belomutskiy, (c) 2012-2020

Definition in file adc_inputs.cpp.

Functions

float PUBLIC_API_WEAK getAnalogInputDividerCoefficient (adc_channel_e)
 
float PUBLIC_API_WEAK boardAdjustVoltage (float voltage, adc_channel_e)
 
ObdCode PUBLIC_API_WEAK boardGetAnalogDiagnostic ()
 
int PUBLIC_API_WEAK boardGetAnalogInputDiagnostic (adc_channel_e channel, float)
 
static ObdCode analogGetVrefDiagnostic ()
 
ObdCode analogGetDiagnostic ()
 
expected< floatadcGetRawVoltage (const char *msg, adc_channel_e hwChannel)
 
expected< floatadcGetScaledVoltage (const char *msg, adc_channel_e hwChannel)
 
AdcChannelMode getAdcMode (adc_channel_e hwChannel)
 
adcsample_t adcOnchipSlowGetAvgRaw (adc_channel_e hwChannel)
 
int getInternalAdcValue (const char *msg, adc_channel_e hwChannel)
 
static void printAdcValue (int channel)
 
void adcPrintChannelReport (const char *prefix, int internalIndex, adc_channel_e hwChannel)
 
void adcOnchipSlowShowReport ()
 
void printFullAdcReport (void)
 
static void setAdcDebugReporting (int value)
 
void adcOnchipSlowUpdate (efitick_t nowNt)
 
void adcInputsUpdateSubscribers (efitick_t nowNt)
 
void addFastAdcChannel (const char *, adc_channel_e hwChannel)
 
void removeChannel (const char *, adc_channel_e hwChannel)
 
 __attribute__ ((weak)) void setAdcChannelOverrides()
 
static void configureInputs ()
 
void initAdcInputs ()
 
void printFullAdcReportIfNeeded (void)
 

Variables

AdcDevice fastAdc
 
static AdcChannelMode adcHwChannelMode [EFI_ADC_TOTAL_CHANNELS]
 
static int adcDebugReporting = false
 
 adc_channel_e
 

Function Documentation

◆ __attribute__()

__attribute__ ( (weak)  )

Definition at line 221 of file adc_inputs.cpp.

221{ }

◆ adcGetRawVoltage()

expected< float > adcGetRawVoltage ( const char msg,
adc_channel_e  hwChannel 
)

Definition at line 78 of file adc_inputs.cpp.

78 {
79 float rawVoltage = adcRawValueToRawVoltage(adcGetRawValue(msg, hwChannel));
80 int inputStatus = boardGetAnalogInputDiagnostic(hwChannel, rawVoltage);
81
82 if (inputStatus == 0) {
83 return expected(rawVoltage);
84 }
85
86 /* TODO: convert inputStatus to unexpected? */
87 return unexpected;
88}
int PUBLIC_API_WEAK boardGetAnalogInputDiagnostic(adc_channel_e channel, float)

Referenced by adcGetScaledVoltage(), adcPrintChannelReport(), AdcSubscription::PrintInfo(), printMAPInfo(), and AdcSubscription::UpdateSubscribers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adcGetScaledVoltage()

expected< float > adcGetScaledVoltage ( const char msg,
adc_channel_e  hwChannel 
)

Definition at line 91 of file adc_inputs.cpp.

91 {
92 auto rawVoltage = adcGetRawVoltage(msg, hwChannel);
93
94 if (rawVoltage) {
95 // TODO: merge getAnalogInputDividerCoefficient() and boardAdjustVoltage() into single board hook?
96 float voltage = rawVoltage.value_or(0) * getAnalogInputDividerCoefficient(hwChannel);
97 return expected(boardAdjustVoltage(voltage, hwChannel));
98 }
99
100 return expected(rawVoltage);
101}
float PUBLIC_API_WEAK boardAdjustVoltage(float voltage, adc_channel_e)
float PUBLIC_API_WEAK getAnalogInputDividerCoefficient(adc_channel_e)
expected< float > adcGetRawVoltage(const char *msg, adc_channel_e hwChannel)

Referenced by adcPrintChannelReport(), getAfr(), and updateRawSensors().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adcInputsUpdateSubscribers()

void adcInputsUpdateSubscribers ( efitick_t  nowNt)

Definition at line 181 of file adc_inputs.cpp.

181 {
182 adcOnchipSlowUpdate(nowNt);
183
184 {
186
188
189 protectedGpio_check(nowNt);
190 }
191}
void adcOnchipSlowUpdate(efitick_t nowNt)
static void UpdateSubscribers(efitick_t nowNt)
@ AdcProcessSlow
void protectedGpio_check(efitick_t nowNt)

Referenced by initAdcInputs(), and MainLoop::PeriodicTask().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adcOnchipSlowGetAvgRaw()

adcsample_t adcOnchipSlowGetAvgRaw ( adc_channel_e  hwChannel)
extern

Definition at line 59 of file adc_onchip_slow.cpp.

60{
61 return slowAdcSamples[hwChannel - EFI_ADC_0];
62}
static volatile NO_CACHE adcsample_t slowAdcSamples[SLOW_ADC_CHANNEL_COUNT]

Referenced by getInternalAdcValue().

Here is the caller graph for this function:

◆ adcOnchipSlowShowReport()

void adcOnchipSlowShowReport ( )
extern

Definition at line 66 of file adc_onchip_slow.cpp.

67{
68 efiPrintf("slow %lu samples", slowAdcConversionCount);
69
70 /* we assume that all slow ADC channels are enabled */
71 for (int internalIndex = 0; internalIndex < SLOW_ADC_CHANNEL_COUNT; internalIndex++) {
72 adc_channel_e hwChannel = static_cast<adc_channel_e>(internalIndex + EFI_ADC_0);
73
74 adcPrintChannelReport("S", internalIndex, hwChannel);
75 }
76}
void adcPrintChannelReport(const char *prefix, int internalIndex, adc_channel_e hwChannel)
static uint32_t slowAdcConversionCount

Referenced by printFullAdcReport().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adcOnchipSlowUpdate()

void adcOnchipSlowUpdate ( efitick_t  nowNt)
extern

Definition at line 32 of file adc_onchip_slow.cpp.

32 {
33 UNUSED(nowNt);
34
36
37 /* drop volatile type qualifier - this is safe */
40 return;
41 }
42
43 // TODO:
44 //engine->outputChannels.slowAdcConversionCount++;
46
47 // Ask the port to sample the MCU temperature
49 if (mcuTemperature > 150.0f || mcuTemperature < -50.0f) {
50 /*
51 * we have a sporadic issue with this check todo https://github.com/rusefi/rusefi/issues/2552
52 */
53 //criticalError("Invalid CPU temperature measured %f", degrees);
54 }
55
57}
static float mcuTemperature
static float mcuVrefVoltage
TunerStudioOutputChannels outputChannels
Definition engine.h:109
bool readSlowAnalogInputs(adcsample_t *convertedSamples)
Definition mpu_util.cpp:274
float getMcuVrefVoltage()
Definition mpu_util.cpp:269
float getMcuTemperature()
Definition mpu_util.cpp:264
static EngineAccessor engine
Definition engine.h:413
uint16_t adcsample_t
ADC sample data type.
UNUSED(samplingTimeSeconds)
@ AdcConversionSlow

Referenced by adcInputsUpdateSubscribers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ adcPrintChannelReport()

void adcPrintChannelReport ( const char prefix,
int  internalIndex,
adc_channel_e  hwChannel 
)

Definition at line 142 of file adc_inputs.cpp.

143{
144 if (isAdcChannelValid(hwChannel)) {
145 ioportid_t port = getAdcChannelPort("print", hwChannel);
146 int pin = getAdcChannelPin(hwChannel);
147 int adcValue = adcGetRawValue("print", hwChannel);
148 auto volts = adcGetRawVoltage("print", hwChannel);
149 auto voltsInput = adcGetScaledVoltage("print", hwChannel);
150 /* Human index starts from 1 */
151 efiPrintf(" %s ch[%2d] @ %s%d ADC%d 12bit=%4d %.3fV input %.3fV %s",
152 prefix, internalIndex, portname(port), pin,
153 /* TODO: */ hwChannel - EFI_ADC_0 + 1,
154 adcValue, volts.value_or(0), voltsInput.value_or(0), volts ? "valid" : "INVALID");
155 }
156}
expected< float > adcGetScaledVoltage(const char *msg, adc_channel_e hwChannel)
bool isAdcChannelValid(adc_channel_e hwChannel)
Definition adc_inputs.h:23
int getAdcChannelPin(adc_channel_e hwChannel)
ioportid_t getAdcChannelPort(const char *msg, adc_channel_e hwChannel)
const char * portname(ioportid_t GPIOx)
GPIO_TypeDef * ioportid_t
Port Identifier.
brain_pin_e pin
Definition stm32_adc.cpp:15

Referenced by adcOnchipSlowShowReport(), and printFullAdcReport().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addFastAdcChannel()

void addFastAdcChannel ( const char name,
adc_channel_e  hwChannel 
)

Definition at line 193 of file adc_inputs.cpp.

193 {
194 if (!isAdcChannelValid(hwChannel)) {
195 return;
196 }
197
198#if EFI_USE_FAST_ADC
199 fastAdc.enableChannel(hwChannel);
200#endif
201
203 // Nothing to do for slow channels, input is mapped to analog in init_sensors.cpp
204}
static AdcChannelMode adcHwChannelMode[EFI_ADC_TOTAL_CHANNELS]
AdcDevice fastAdc
int enableChannel(adc_channel_e hwChannel)

Referenced by addAdcChannelForTrigger(), configureInputs(), and setAdcChannelOverrides().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ analogGetDiagnostic()

ObdCode analogGetDiagnostic ( )

Definition at line 59 of file adc_inputs.cpp.

60{
61 /* TODO: debounce? */
63 if (code != ObdCode::None) {
64 return code;
65 }
66
68}
static ObdCode analogGetVrefDiagnostic()
ObdCode PUBLIC_API_WEAK boardGetAnalogDiagnostic()
uint8_t code
Definition bluetooth.cpp:40

Referenced by SensorChecker::onSlowCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ analogGetVrefDiagnostic()

static ObdCode analogGetVrefDiagnostic ( )
static

Definition at line 40 of file adc_inputs.cpp.

41{
42#if HAL_USE_ADC
43 float vref = getMCUVref();
44
45 // TODO: +/-10% is way too big?
46 if (vref > engineConfiguration->adcVcc * 1.1) {
48 }
49
50 if (vref < engineConfiguration->adcVcc * 0.9) {
52 }
53#endif
54
55 return ObdCode::None;
56}
float getMCUVref(void)
static constexpr engine_configuration_s * engineConfiguration
@ OBD_Sensor_Refence_Voltate_A_High
@ OBD_Sensor_Refence_Voltate_A_Low

Referenced by analogGetDiagnostic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ boardAdjustVoltage()

float PUBLIC_API_WEAK boardAdjustVoltage ( float  voltage,
adc_channel_e  hwChannel 
)

Definition at line 15 of file adc_inputs.cpp.

15 {
16 // a hack useful when we do not trust voltage just after board EN was turned on. is this just hiding electrical design flaws?
17 return voltage;
18}

Referenced by adcGetScaledVoltage().

Here is the caller graph for this function:

◆ boardGetAnalogDiagnostic()

ObdCode PUBLIC_API_WEAK boardGetAnalogDiagnostic ( )

Definition at line 23 of file adc_inputs.cpp.

23 {
24 return ObdCode::None;
25}

Referenced by analogGetDiagnostic(), and boardGetAnalogInputDiagnostic().

Here is the caller graph for this function:

◆ boardGetAnalogInputDiagnostic()

int PUBLIC_API_WEAK boardGetAnalogInputDiagnostic ( adc_channel_e  channel,
float  voltage 
)

Definition at line 28 of file adc_inputs.cpp.

28 {
29#if EFI_PROD_CODE
30 /* for on-chip ADC inputs we check common analog health */
32 return (boardGetAnalogDiagnostic() == ObdCode::None) ? 0 : -1;
33 }
34#endif // EFI_PROD_CODE
35
36 /* input is outside chip/ECU */
37 return 0;
38}
uint16_t channel
Definition adc_inputs.h:104
bool isAdcChannelOnChip(adc_channel_e hwChannel)
Definition adc_inputs.h:36

Referenced by adcGetRawVoltage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ configureInputs()

static void configureInputs ( )
static

order of analog channels here is totally random and has no meaning we also have some weird implementation with internal indices - that all has no meaning, it's just a random implementation which does not mean anything.

Definition at line 223 of file adc_inputs.cpp.

223 {
225
226 /**
227 * order of analog channels here is totally random and has no meaning
228 * we also have some weird implementation with internal indices - that all has no meaning, it's just a random implementation
229 * which does not mean anything.
230 */
231
233
234 // not currently used addFastAdcChannel("Vref", engineConfiguration->vRefAdcChannel, ADC_SLOW);
235
237
239}
void addFastAdcChannel(const char *, adc_channel_e hwChannel)
void setAdcChannelOverrides()

Referenced by initAdcInputs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAdcMode()

AdcChannelMode getAdcMode ( adc_channel_e  hwChannel)

Definition at line 110 of file adc_inputs.cpp.

110 {
111 return adcHwChannelMode[hwChannel];
112}

Referenced by AdcSubscription::PrintInfo().

Here is the caller graph for this function:

◆ getAnalogInputDividerCoefficient()

float PUBLIC_API_WEAK getAnalogInputDividerCoefficient ( adc_channel_e  hwChannel)

Definition at line 11 of file adc_inputs.cpp.

Referenced by adcGetScaledVoltage().

Here is the caller graph for this function:

◆ getInternalAdcValue()

int getInternalAdcValue ( const char msg,
adc_channel_e  hwChannel 
)

Definition at line 116 of file adc_inputs.cpp.

116 {
117 if (!isAdcChannelValid(hwChannel)) {
118 warning(ObdCode::CUSTOM_OBD_ANALOG_INPUT_NOT_CONFIGURED, "ADC: %s input is not configured", msg);
119 return -1;
120 }
121
122#if EFI_USE_FAST_ADC
123 if (adcHwChannelMode[hwChannel] == AdcChannelMode::Fast) {
124 return fastAdc.getAvgAdcValue(hwChannel);
125 }
126#endif // EFI_USE_FAST_ADC
127
128 return adcOnchipSlowGetAvgRaw(hwChannel);
129}
adcsample_t adcOnchipSlowGetAvgRaw(adc_channel_e hwChannel)
adcsample_t getAvgAdcValue(adc_channel_e hwChannel)
bool warning(ObdCode code, const char *fmt,...)
@ CUSTOM_OBD_ANALOG_INPUT_NOT_CONFIGURED
Here is the call graph for this function:

◆ initAdcInputs()

void initAdcInputs ( )

Definition at line 241 of file adc_inputs.cpp.

241 {
242 efiPrintf("initAdcInputs()");
243
245
246 // migrate to 'enable adcdebug'
248
249#if EFI_INTERNAL_ADC
250 // This will start HW for all used ADCs
251 portInitAdc();
252
253#if EFI_USE_FAST_ADC
254 // After this point fastAdc is not allowed to add channels
255 fastAdc.init();
256#endif // EFI_USE_FAST_ADC
257
259#else // ! EFI_INTERNAL_ADC
260 efiPrintf("ADC disabled");
261#endif // EFI_INTERNAL_ADC
262
263 // Workaround to pre-feed all sensors with some data...
264 chThdSleepMilliseconds(1);
266}
void adcInputsUpdateSubscribers(efitick_t nowNt)
static void setAdcDebugReporting(int value)
static void printAdcValue(int channel)
static void configureInputs()
void init(void)
void addConsoleActionI(const char *token, VoidInt callback)
Register a console command with one Integer parameter.
void(* VoidInt)(int)
void portInitAdc()
Definition mpu_util.cpp:256
efitick_t getTimeNowNt()
Definition efitime.cpp:19

Referenced by initHardware().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printAdcValue()

static void printAdcValue ( int  channel)
static

Definition at line 131 of file adc_inputs.cpp.

131 {
132 /* Do this check before conversion to adc_channel_e that is uint8_t based */
133 if ((channel < EFI_ADC_NONE) || (channel >= EFI_ADC_TOTAL_CHANNELS)) {
134 efiPrintf("Invalid ADC channel %d", channel);
135 return;
136 }
137 int adcValue = adcGetRawValue("print", (adc_channel_e)channel);
138 float voltsInput = adcRawValueToScaledVoltage(adcValue, (adc_channel_e)channel);
139 efiPrintf("adc %d input %.3fV", channel, voltsInput);
140}
adc_channel_e

Referenced by initAdcInputs().

Here is the caller graph for this function:

◆ printFullAdcReport()

void printFullAdcReport ( void  )

Definition at line 160 of file adc_inputs.cpp.

160 {
161#if EFI_USE_FAST_ADC
162 efiPrintf("fast %u samples", engine->outputChannels.fastAdcConversionCount);
163
164 for (int internalIndex = 0; internalIndex < fastAdc.size(); internalIndex++) {
165 adc_channel_e hwChannel = fastAdc.getAdcChannelByInternalIndex(internalIndex);
166
167 adcPrintChannelReport("F", internalIndex, hwChannel);
168 }
169#endif // EFI_USE_FAST_ADC
170
172}
void adcPrintChannelReport(const char *prefix, int internalIndex, adc_channel_e hwChannel)
void adcOnchipSlowShowReport()
int size() const
adc_channel_e getAdcChannelByInternalIndex(int index) const

Referenced by initSettings(), and printFullAdcReportIfNeeded().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ printFullAdcReportIfNeeded()

void printFullAdcReportIfNeeded ( void  )

Definition at line 268 of file adc_inputs.cpp.

268 {
270 return;
272}
static int adcDebugReporting
void printFullAdcReport(void)

Referenced by updateDevConsoleState().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeChannel()

void removeChannel ( const char name,
adc_channel_e  hwChannel 
)

Definition at line 206 of file adc_inputs.cpp.

206 {
207 if (!isAdcChannelValid(hwChannel)) {
208 return;
209 }
210#if EFI_USE_FAST_ADC
211 if (adcHwChannelMode[hwChannel] == AdcChannelMode::Fast) {
212 /* TODO: */
213 //fastAdc.disableChannel(hwChannel);
214 }
215#endif
216
218}

Referenced by setAdcChannelOverrides().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setAdcDebugReporting()

static void setAdcDebugReporting ( int  value)
static

Definition at line 174 of file adc_inputs.cpp.

174 {
175 adcDebugReporting = value;
176 efiPrintf("adcDebug=%d", adcDebugReporting);
177}

Referenced by initAdcInputs().

Here is the caller graph for this function:

Variable Documentation

◆ adc_channel_e

adc_channel_e
Initial value:
{
return expected(0.0f)

Definition at line 277 of file adc_inputs.cpp.

Referenced by printAdcValue(), and printFullAdcReport().

◆ adcDebugReporting

int adcDebugReporting = false
static

Definition at line 108 of file adc_inputs.cpp.

Referenced by printFullAdcReportIfNeeded(), and setAdcDebugReporting().

◆ adcHwChannelMode

AdcChannelMode adcHwChannelMode[EFI_ADC_TOTAL_CHANNELS]
static

◆ fastAdc

AdcDevice fastAdc
extern

Go to the source code of this file.