rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
adc_onchip_slow.cpp
Go to the documentation of this file.
1/**
2 * @file adc_inputs_onchip_slow.cpp
3 * @brief Low level ADC code
4 *
5 * @date Aug 22, 2025
6 * @author Andrey Belomutskiy, (c) 2012-2025
7 */
8
9#include "pch.h"
10
11#if HAL_USE_ADC
12
13#include "adc_device.h"
14
15#ifndef SLOW_ADC_CHANNEL_COUNT
16#ifdef ADC_MUX_PIN
17#define SLOW_ADC_CHANNEL_COUNT 32
18#else // not ADC_MUX_PIN
19#define SLOW_ADC_CHANNEL_COUNT 16
20#endif // def ADC_MUX_PIN
21#endif // SLOW_ADC_CHANNEL_COUNT
22
23/* TODO: Drop NO_CACHE for F4 and F7 couse with ADCv2 driver CPU does averaging and CPU stores result to this array */
24/* TODO: store summ of samples is this array and divide on oversample factor only when converting to float - this will increase accuracity */
25static volatile NO_CACHE adcsample_t slowAdcSamples[SLOW_ADC_CHANNEL_COUNT];
26
27static uint32_t slowAdcConversionCount = 0;
28
29static float mcuTemperature;
30static float mcuVrefVoltage;
31
32void adcOnchipSlowUpdate(efitick_t nowNt) {
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}
58
60{
61 return slowAdcSamples[hwChannel - EFI_ADC_0];
62}
63
64extern void adcPrintChannelReport(const char *prefix, int internalIndex, adc_channel_e hwChannel);
65
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}
77
81
82float getMCUVref() {
83 return mcuVrefVoltage;
84}
85
86/* TODO: kill this */
87void waitForSlowAdc(uint32_t lastAdcCounter) {
88 // note that having ADC reading is one thing while having new sensor API is a totally different thing!
89 // todo: use sync.objects?
90 while (slowAdcConversionCount <= lastAdcCounter) {
91 chThdSleepMilliseconds(1);
92 }
93}
94
95#endif // HAL_USE_ADC
static float mcuTemperature
float getMCUVref()
static float mcuVrefVoltage
void adcPrintChannelReport(const char *prefix, int internalIndex, adc_channel_e hwChannel)
void waitForSlowAdc(uint32_t lastAdcCounter)
static volatile NO_CACHE adcsample_t slowAdcSamples[SLOW_ADC_CHANNEL_COUNT]
static uint32_t slowAdcConversionCount
adcsample_t adcOnchipSlowGetAvgRaw(adc_channel_e hwChannel)
void adcOnchipSlowUpdate(efitick_t nowNt)
float getMCUInternalTemperature()
void adcOnchipSlowShowReport()
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)
static union @47 NO_CACHE
@ AdcConversionSlow