rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes
AdcDevice Class Reference

#include <AdcDevice.h>

Collaboration diagram for AdcDevice:
Collaboration graph
[legend]

Public Member Functions

 AdcDevice (ADCDriver *p_adcp, ADCConversionGroup *p_hwConfig, volatile adcsample_t *p_buf, size_t p_depth)
 
int enableChannel (adc_channel_e hwChannel)
 
void startConversionI (void)
 
adc_channel_e getAdcChannelByInternalIndex (int index) const
 
adcsample_t getAvgAdcValue (adc_channel_e hwChannel)
 
adcsample_t getAdcValueByToken (AdcToken token)
 
AdcToken getAdcChannelToken (adc_channel_e hwChannel)
 
int size () const
 
void init (void)
 

Private Attributes

ADCDriver * adcp
 
ADCConversionGroup * hwConfig
 
volatile adcsample_tsamples
 
size_t depth
 
uint8_t internalAdcIndexByHardwareIndex [EFI_ADC_TOTAL_CHANNELS]
 
size_t channelCount = 0
 

Detailed Description

Definition at line 24 of file AdcDevice.h.

Constructor & Destructor Documentation

◆ AdcDevice()

AdcDevice::AdcDevice ( ADCDriver *  p_adcp,
ADCConversionGroup *  p_hwConfig,
volatile adcsample_t p_buf,
size_t  p_depth 
)
explicit

Definition at line 61 of file adc_inputs_onchip.cpp.

61 {
62 adcp = p_adcp;
63 depth = p_depth;
64 hwConfig = p_hwConfig;
65 samples = p_buf;
66
67 hwConfig->sqr1 = 0;
68 hwConfig->sqr2 = 0;
69 hwConfig->sqr3 = 0;
70#if ADC_MAX_CHANNELS_COUNT > 16
71 hwConfig->sqr4 = 0;
72 hwConfig->sqr5 = 0;
73#endif /* ADC_MAX_CHANNELS_COUNT */
75}
size_t depth
Definition AdcDevice.h:47
uint8_t internalAdcIndexByHardwareIndex[EFI_ADC_TOTAL_CHANNELS]
Definition AdcDevice.h:48
ADCConversionGroup * hwConfig
Definition AdcDevice.h:45
volatile adcsample_t * samples
Definition AdcDevice.h:46
ADCDriver * adcp
Definition AdcDevice.h:44

Member Function Documentation

◆ enableChannel()

int AdcDevice::enableChannel ( adc_channel_e  hwChannel)

Definition at line 229 of file adc_inputs_onchip.cpp.

229 {
230 if ((channelCount + 1) >= ADC_MAX_CHANNELS_COUNT) {
231 criticalError("Too many ADC channels configured");
232 return -1;
233 }
234
235 int logicChannel = channelCount++;
236
237 /* TODO: following is correct for STM32 ADC1/2.
238 * ADC3 has another input to gpio mapping
239 * and should be handled separately */
240 size_t channelAdcIndex = hwChannel - EFI_ADC_0;
241
242 internalAdcIndexByHardwareIndex[hwChannel] = logicChannel;
243 if (logicChannel < 6) {
244 hwConfig->sqr3 |= channelAdcIndex << (5 * logicChannel);
245 } else if (logicChannel < 12) {
246 hwConfig->sqr2 |= channelAdcIndex << (5 * (logicChannel - 6));
247 } else if (logicChannel < 18) {
248 hwConfig->sqr1 |= channelAdcIndex << (5 * (logicChannel - 12));
249 }
250#if ADC_MAX_CHANNELS_COUNT > 16
251 else if (logicChannel < 24) {
252 hwConfig->sqr4 |= channelAdcIndex << (5 * (logicChannel - 18));
253 }
254 else if (logicChannel < 30) {
255 hwConfig->sqr5 |= channelAdcIndex << (5 * (logicChannel - 24));
256 }
257#endif /* ADC_MAX_CHANNELS_COUNT */
258
259 return channelAdcIndex;
260}
size_t channelCount
Definition AdcDevice.h:52

Referenced by addFastAdcChannel().

Here is the caller graph for this function:

◆ getAdcChannelByInternalIndex()

adc_channel_e AdcDevice::getAdcChannelByInternalIndex ( int  index) const

Definition at line 302 of file adc_inputs_onchip.cpp.

302 {
303 for (size_t idx = EFI_ADC_0; idx < EFI_ADC_TOTAL_CHANNELS; idx++) {
304 if (internalAdcIndexByHardwareIndex[idx] == hwChannel) {
305 return (adc_channel_e)idx;
306 }
307 }
308 return EFI_ADC_NONE;
309}

Referenced by printFullAdcReport().

Here is the caller graph for this function:

◆ getAdcChannelToken()

AdcToken AdcDevice::getAdcChannelToken ( adc_channel_e  hwChannel)

Definition at line 311 of file adc_inputs_onchip.cpp.

311 {
313}
AdcDevice fastAdc

Referenced by enableFastAdcChannel().

Here is the caller graph for this function:

◆ getAdcValueByToken()

adcsample_t AdcDevice::getAdcValueByToken ( AdcToken  token)
inline

Definition at line 32 of file AdcDevice.h.

33 {
34 /* TODO: validate token? */
35
36 /* TODO: in case depth > 1 this will return random (not last) sample */
37 return samples[token];
38 };

Referenced by getFastAdc().

Here is the caller graph for this function:

◆ getAvgAdcValue()

adcsample_t AdcDevice::getAvgAdcValue ( adc_channel_e  hwChannel)

Definition at line 277 of file adc_inputs_onchip.cpp.

277 {
278 uint32_t result = 0;
279 int numChannels = size();
280 int index = fastAdc.internalAdcIndexByHardwareIndex[hwChannel];
281 if (index == 0xff) {
282 criticalError("Fast ADC attempt to read unconfigured input %d.", hwChannel);
283 return 0;
284 }
285
286 for (size_t i = 0; i < depth; i++) {
287 adcsample_t sample = samples[index];
288 if (sample > ADC_MAX_VALUE) {
289 // 12bit ADC expected right now. An error here usually means major RAM corruption?
290 criticalError("ADC unexpected sample %d at %ld uptime.",
291 sample,
292 (uint32_t)getTimeNowS());
293 }
294 result += sample;
295 index += numChannels;
296 }
297
298 // this truncation is guaranteed to not be lossy - the average can't be larger than adcsample_t
299 return static_cast<adcsample_t>(result / depth);
300}
int size() const
efitimesec_t getTimeNowS()
Current system time in seconds (32 bits)
Definition efitime.cpp:42
uint16_t adcsample_t
ADC sample data type.

Referenced by getInternalAdcValue().

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

◆ init()

void AdcDevice::init ( void  )

Definition at line 213 of file adc_inputs_onchip.cpp.

213 {
214 hwConfig->num_channels = size();
215 /* driver does this internally */
216 //hwConfig->sqr1 += ADC_SQR1_NUM_CH(size());
217
218#if defined(EFI_INTERNAL_FAST_ADC_PWM)
219 // Start the timer running
220 pwmStart(EFI_INTERNAL_FAST_ADC_PWM, &pwmcfg);
221 pwmEnableChannel(EFI_INTERNAL_FAST_ADC_PWM, 0, /* width */ 1);
222 adcStartConversion(adcp, hwConfig, (adcsample_t *)samples, depth);
223#elif defined (EFI_INTERNAL_FAST_ADC_GPT)
224 gptStart(EFI_INTERNAL_FAST_ADC_GPT, &fast_adc_config);
225 gptStartContinuous(EFI_INTERNAL_FAST_ADC_GPT, GPT_PERIOD_FAST);
226#endif
227}
static const PWMConfig pwmcfg
static const GPTConfig fast_adc_config

Referenced by initAdcInputs().

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

◆ size()

int AdcDevice::size ( ) const

Definition at line 209 of file adc_inputs_onchip.cpp.

209 {
210 return channelCount;
211}

Referenced by getAvgAdcValue(), init(), and printFullAdcReport().

Here is the caller graph for this function:

◆ startConversionI()

void AdcDevice::startConversionI ( void  )

Definition at line 262 of file adc_inputs_onchip.cpp.

263{
264 chSysLockFromISR();
265 if ((ADC_FAST_DEVICE.state == ADC_READY) ||
266 (ADC_FAST_DEVICE.state == ADC_ERROR)) {
267 /* drop volatile type qualifier - this is safe */
268 adcStartConversionI(adcp, hwConfig, (adcsample_t *)samples, depth);
269 } else {
271 // todo: when? why? criticalError("ADC fast not ready?");
272 // see notes at https://github.com/rusefi/rusefi/issues/6399
273 }
274 chSysUnlockFromISR();
275}
AdcDevice fastAdc & ADC_FAST_DEVICE
TunerStudioOutputChannels outputChannels
Definition engine.h:108
static EngineAccessor engine
Definition engine.h:410

Referenced by fastAdcStartTrigger().

Here is the caller graph for this function:

Field Documentation

◆ adcp

ADCDriver* AdcDevice::adcp
private

Definition at line 44 of file AdcDevice.h.

Referenced by AdcDevice(), init(), and startConversionI().

◆ channelCount

size_t AdcDevice::channelCount = 0
private

Number of ADC channels in use

Definition at line 52 of file AdcDevice.h.

Referenced by enableChannel(), and size().

◆ depth

size_t AdcDevice::depth
private

Definition at line 47 of file AdcDevice.h.

Referenced by AdcDevice(), getAvgAdcValue(), init(), and startConversionI().

◆ hwConfig

ADCConversionGroup* AdcDevice::hwConfig
private

Definition at line 45 of file AdcDevice.h.

Referenced by AdcDevice(), enableChannel(), init(), and startConversionI().

◆ internalAdcIndexByHardwareIndex

uint8_t AdcDevice::internalAdcIndexByHardwareIndex[EFI_ADC_TOTAL_CHANNELS]
private

◆ samples

volatile adcsample_t* AdcDevice::samples
private

Definition at line 46 of file AdcDevice.h.

Referenced by AdcDevice(), getAdcValueByToken(), getAvgAdcValue(), init(), and startConversionI().


The documentation for this class was generated from the following files: