ADC: fixes: (#1979)
* ADC: fixes: -use enums -internalAdcIndexByHardwareIndex array should be adc_channel_e size -add sanity checks -save few bytes of RAM * ADC: use logical OR instead of math add
This commit is contained in:
parent
b60ce1b9f8
commit
a285ba6743
|
@ -289,3 +289,6 @@ typedef enum __attribute__ ((__packed__)) {
|
|||
// todo: bad choice of value since now we have ADC_CHANNEL_SENSOR and could end up with 17 and 18 also
|
||||
EFI_ADC_ERROR = 33,
|
||||
} adc_channel_e;
|
||||
|
||||
/* Plase keep updating this define */
|
||||
#define EFI_ADC_LAST EFI_ADC_31
|
||||
|
|
|
@ -180,3 +180,6 @@ typedef enum __attribute__ ((__packed__)) {
|
|||
// todo: bad choice of value since now we have ADC_CHANNEL_SENSOR and could end up with 17 and 18 also
|
||||
EFI_ADC_ERROR = 17,
|
||||
} adc_channel_e;
|
||||
|
||||
/* Plase keep updating this define */
|
||||
#define EFI_ADC_LAST EFI_ADC_15
|
||||
|
|
|
@ -272,3 +272,6 @@ typedef enum __attribute__ ((__packed__)) {
|
|||
|
||||
EFI_ADC_ERROR = 50,
|
||||
} adc_channel_e;
|
||||
|
||||
/* Plase keep updating this define */
|
||||
#define EFI_ADC_LAST EFI_ADC_TEMP_SENSOR
|
||||
|
|
|
@ -24,7 +24,8 @@ public:
|
|||
void enableChannel(adc_channel_e hwChannelIndex);
|
||||
void enableChannelAndPin(const char *msg, adc_channel_e hwChannelIndex);
|
||||
adc_channel_e getAdcHardwareIndexByInternalIndex(int index) const;
|
||||
int internalAdcIndexByHardwareIndex[ADC_MAX_CHANNELS_COUNT + 4];
|
||||
/* We use adc_channel_e as index for following array */
|
||||
char internalAdcIndexByHardwareIndex[EFI_ADC_LAST + 1];
|
||||
bool isHwUsed(adc_channel_e hwChannel) const;
|
||||
int size() const;
|
||||
void init(void);
|
||||
|
@ -35,16 +36,17 @@ public:
|
|||
|
||||
adcsample_t *samples;
|
||||
|
||||
int getAdcValueByHwChannel(int hwChannel) const;
|
||||
int getAdcValueByHwChannel(adc_channel_e hwChannel) const;
|
||||
|
||||
adc_state values;
|
||||
int channelCount;
|
||||
private:
|
||||
ADCConversionGroup* hwConfig;
|
||||
/**
|
||||
* Number of ADC channels in use
|
||||
*/
|
||||
|
||||
unsigned char channelCount;
|
||||
|
||||
/* STM32 has up-to 4 additional channels routed to internal voltage sources */
|
||||
adc_channel_e hardwareIndexByIndernalAdcIndex[ADC_MAX_CHANNELS_COUNT + 4];
|
||||
};
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ AdcDevice::AdcDevice(ADCConversionGroup* hwConfig, adcsample_t *buf) {
|
|||
hwConfig->sqr5 = 0;
|
||||
#endif /* ADC_MAX_CHANNELS_COUNT */
|
||||
memset(hardwareIndexByIndernalAdcIndex, EFI_ADC_NONE, sizeof(hardwareIndexByIndernalAdcIndex));
|
||||
memset(internalAdcIndexByHardwareIndex, 0xFFFFFFFF, sizeof(internalAdcIndexByHardwareIndex));
|
||||
memset(internalAdcIndexByHardwareIndex, EFI_ADC_ERROR, sizeof(internalAdcIndexByHardwareIndex));
|
||||
}
|
||||
|
||||
#if !defined(GPT_FREQ_FAST) || !defined(GPT_PERIOD_FAST)
|
||||
|
@ -315,13 +315,21 @@ int AdcDevice::size() const {
|
|||
return channelCount;
|
||||
}
|
||||
|
||||
int AdcDevice::getAdcValueByHwChannel(int hwChannel) const {
|
||||
int internalIndex = internalAdcIndexByHardwareIndex[hwChannel];
|
||||
int AdcDevice::getAdcValueByIndex(int internalIndex) const {
|
||||
if (internalIndex >= size()) {
|
||||
firmwareError(OBD_PCM_Processor_Fault, "ADC channel index out of range");
|
||||
return 0;
|
||||
}
|
||||
return values.adc_data[internalIndex];
|
||||
}
|
||||
|
||||
int AdcDevice::getAdcValueByIndex(int internalIndex) const {
|
||||
return values.adc_data[internalIndex];
|
||||
int AdcDevice::getAdcValueByHwChannel(adc_channel_e hwChannel) const {
|
||||
if (hwChannel >= ARRAY_SIZE(internalAdcIndexByHardwareIndex)) {
|
||||
firmwareError(OBD_PCM_Processor_Fault, "ADC hwChannel out of range");
|
||||
return 0;
|
||||
}
|
||||
int internalIndex = internalAdcIndexByHardwareIndex[hwChannel];
|
||||
return getAdcValueByIndex(internalIndex);
|
||||
}
|
||||
|
||||
void AdcDevice::invalidateSamplesCache() {
|
||||
|
@ -351,7 +359,7 @@ bool AdcDevice::isHwUsed(adc_channel_e hwChannelIndex) const {
|
|||
}
|
||||
|
||||
void AdcDevice::enableChannel(adc_channel_e hwChannel) {
|
||||
if (channelCount >= efi::size(values.adc_data)) {
|
||||
if (channelCount >= ADC_MAX_CHANNELS_COUNT) {
|
||||
firmwareError(OBD_PCM_Processor_Fault, "Too many ADC channels configured");
|
||||
return;
|
||||
}
|
||||
|
@ -363,18 +371,18 @@ void AdcDevice::enableChannel(adc_channel_e hwChannel) {
|
|||
internalAdcIndexByHardwareIndex[hwChannel] = logicChannel;
|
||||
hardwareIndexByIndernalAdcIndex[logicChannel] = hwChannel;
|
||||
if (logicChannel < 6) {
|
||||
hwConfig->sqr3 += (channelAdcIndex) << (5 * logicChannel);
|
||||
hwConfig->sqr3 |= channelAdcIndex << (5 * logicChannel);
|
||||
} else if (logicChannel < 12) {
|
||||
hwConfig->sqr2 += (channelAdcIndex) << (5 * (logicChannel - 6));
|
||||
hwConfig->sqr2 |= channelAdcIndex << (5 * (logicChannel - 6));
|
||||
} else if (logicChannel < 18) {
|
||||
hwConfig->sqr1 += (channelAdcIndex) << (5 * (logicChannel - 12));
|
||||
hwConfig->sqr1 |= channelAdcIndex << (5 * (logicChannel - 12));
|
||||
}
|
||||
#if ADC_MAX_CHANNELS_COUNT > 16
|
||||
else if (logicChannel < 24) {
|
||||
hwConfig->sqr4 += (channelAdcIndex) << (5 * (logicChannel - 18));
|
||||
hwConfig->sqr4 |= channelAdcIndex << (5 * (logicChannel - 18));
|
||||
}
|
||||
else if (logicChannel < 30) {
|
||||
hwConfig->sqr5 += (channelAdcIndex) << (5 * (logicChannel - 24));
|
||||
hwConfig->sqr5 |= channelAdcIndex << (5 * (logicChannel - 24));
|
||||
}
|
||||
#endif /* ADC_MAX_CHANNELS_COUNT */
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue