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

#include <adc_device.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 12 of file adc_device.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 53 of file adc_onchip_fast.cpp.

53 {
54 adcp = p_adcp;
55 depth = p_depth;
56 hwConfig = p_hwConfig;
57 samples = p_buf;
58
59 hwConfig->sqr1 = 0;
60 hwConfig->sqr2 = 0;
61 hwConfig->sqr3 = 0;
62#if ADC_MAX_CHANNELS_COUNT > 16
63 hwConfig->sqr4 = 0;
64 hwConfig->sqr5 = 0;
65#endif /* ADC_MAX_CHANNELS_COUNT */
67}
size_t depth
Definition adc_device.h:35
uint8_t internalAdcIndexByHardwareIndex[EFI_ADC_TOTAL_CHANNELS]
Definition adc_device.h:36
ADCConversionGroup * hwConfig
Definition adc_device.h:33
volatile adcsample_t * samples
Definition adc_device.h:34
ADCDriver * adcp
Definition adc_device.h:32

Member Function Documentation

◆ enableChannel()

int AdcDevice::enableChannel ( adc_channel_e  hwChannel)

Definition at line 221 of file adc_onchip_fast.cpp.

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

Referenced by addFastAdcChannel().

Here is the caller graph for this function:

◆ getAdcChannelByInternalIndex()

adc_channel_e AdcDevice::getAdcChannelByInternalIndex ( int  index) const

Definition at line 294 of file adc_onchip_fast.cpp.

294 {
295 for (size_t idx = EFI_ADC_0; idx < EFI_ADC_TOTAL_CHANNELS; idx++) {
296 if (internalAdcIndexByHardwareIndex[idx] == hwChannel) {
297 return (adc_channel_e)idx;
298 }
299 }
300 return EFI_ADC_NONE;
301}

Referenced by printFullAdcReport().

Here is the caller graph for this function:

◆ getAdcChannelToken()

AdcToken AdcDevice::getAdcChannelToken ( adc_channel_e  hwChannel)

Definition at line 303 of file adc_onchip_fast.cpp.

303 {
305}
AdcDevice fastAdc

Referenced by enableFastAdcChannel().

Here is the caller graph for this function:

◆ getAdcValueByToken()

adcsample_t AdcDevice::getAdcValueByToken ( AdcToken  token)
inline

Definition at line 20 of file adc_device.h.

21 {
22 /* TODO: validate token? */
23
24 /* TODO: in case depth > 1 this will return random (not last) sample */
25 return samples[token];
26 };

Referenced by getFastAdc().

Here is the caller graph for this function:

◆ getAvgAdcValue()

adcsample_t AdcDevice::getAvgAdcValue ( adc_channel_e  hwChannel)

Definition at line 269 of file adc_onchip_fast.cpp.

269 {
270 uint32_t result = 0;
271 int numChannels = size();
272 int index = fastAdc.internalAdcIndexByHardwareIndex[hwChannel];
273 if (index == 0xff) {
274 criticalError("Fast ADC attempt to read unconfigured input %d.", hwChannel);
275 return 0;
276 }
277
278 for (size_t i = 0; i < depth; i++) {
279 adcsample_t sample = samples[index];
280 if (sample > ADC_MAX_VALUE) {
281 // 12bit ADC expected right now. An error here usually means major RAM corruption?
282 criticalError("ADC unexpected sample %d at %ld uptime.",
283 sample,
284 (uint32_t)getTimeNowS());
285 }
286 result += sample;
287 index += numChannels;
288 }
289
290 // this truncation is guaranteed to not be lossy - the average can't be larger than adcsample_t
291 return static_cast<adcsample_t>(result / depth);
292}
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 205 of file adc_onchip_fast.cpp.

205 {
206 hwConfig->num_channels = size();
207 /* driver does this internally */
208 //hwConfig->sqr1 += ADC_SQR1_NUM_CH(size());
209
210#if defined(EFI_INTERNAL_FAST_ADC_PWM)
211 // Start the timer running
212 pwmStart(EFI_INTERNAL_FAST_ADC_PWM, &pwmcfg);
213 pwmEnableChannel(EFI_INTERNAL_FAST_ADC_PWM, 0, /* width */ 1);
214 adcStartConversion(adcp, hwConfig, (adcsample_t *)samples, depth);
215#elif defined (EFI_INTERNAL_FAST_ADC_GPT)
216 gptStart(EFI_INTERNAL_FAST_ADC_GPT, &fast_adc_config);
217 gptStartContinuous(EFI_INTERNAL_FAST_ADC_GPT, GPT_PERIOD_FAST);
218#endif
219}
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 201 of file adc_onchip_fast.cpp.

201 {
202 return channelCount;
203}

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

Here is the caller graph for this function:

◆ startConversionI()

void AdcDevice::startConversionI ( void  )

Definition at line 254 of file adc_onchip_fast.cpp.

255{
256 chSysLockFromISR();
257 if ((ADC_FAST_DEVICE.state == ADC_READY) ||
258 (ADC_FAST_DEVICE.state == ADC_ERROR)) {
259 /* drop volatile type qualifier - this is safe */
260 adcStartConversionI(adcp, hwConfig, (adcsample_t *)samples, depth);
261 } else {
263 // todo: when? why? criticalError("ADC fast not ready?");
264 // see notes at https://github.com/rusefi/rusefi/issues/6399
265 }
266 chSysUnlockFromISR();
267}
AdcDevice fastAdc & ADC_FAST_DEVICE
TunerStudioOutputChannels outputChannels
Definition engine.h:109
static EngineAccessor engine
Definition engine.h:413

Referenced by fastAdcStartTrigger().

Here is the caller graph for this function:

Field Documentation

◆ adcp

ADCDriver* AdcDevice::adcp
private

Definition at line 32 of file adc_device.h.

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

◆ channelCount

size_t AdcDevice::channelCount = 0
private

Number of ADC channels in use

Definition at line 40 of file adc_device.h.

Referenced by enableChannel(), and size().

◆ depth

size_t AdcDevice::depth
private

Definition at line 35 of file adc_device.h.

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

◆ hwConfig

ADCConversionGroup* AdcDevice::hwConfig
private

Definition at line 33 of file adc_device.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 34 of file adc_device.h.

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


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