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 224 of file adc_onchip_fast.cpp.

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

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

Referenced by printFullAdcReport().

Here is the caller graph for this function:

◆ getAdcChannelToken()

AdcToken AdcDevice::getAdcChannelToken ( adc_channel_e  hwChannel)

Definition at line 306 of file adc_onchip_fast.cpp.

306 {
308}
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 272 of file adc_onchip_fast.cpp.

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

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

204 {
205 return channelCount;
206}

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

Here is the caller graph for this function:

◆ startConversionI()

void AdcDevice::startConversionI ( void  )

Definition at line 257 of file adc_onchip_fast.cpp.

258{
259 chSysLockFromISR();
260 if ((ADC_FAST_DEVICE.state == ADC_READY) ||
261 (ADC_FAST_DEVICE.state == ADC_ERROR)) {
262 /* drop volatile type qualifier - this is safe */
263 adcStartConversionI(adcp, hwConfig, (adcsample_t *)samples, depth);
264 } else {
266 // todo: when? why? criticalError("ADC fast not ready?");
267 // see notes at https://github.com/rusefi/rusefi/issues/6399
268 }
269 chSysUnlockFromISR();
270}
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: