deduplicate logic (#1307)

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2020-04-17 13:26:51 -07:00 committed by GitHub
parent 70ba9c9ee2
commit 68403a195a
2 changed files with 7 additions and 37 deletions

View File

@ -20,7 +20,7 @@ class AdcDevice {
public: public:
explicit AdcDevice(ADCConversionGroup* hwConfig); explicit AdcDevice(ADCConversionGroup* hwConfig);
void enableChannel(adc_channel_e hwChannelIndex); void enableChannel(adc_channel_e hwChannelIndex);
void enableChannelAndPin(adc_channel_e hwChannelIndex); void enableChannelAndPin(const char *msg, adc_channel_e hwChannelIndex);
adc_channel_e getAdcHardwareIndexByInternalIndex(int index) const; adc_channel_e getAdcHardwareIndexByInternalIndex(int index) const;
int internalAdcIndexByHardwareIndex[20]; int internalAdcIndexByHardwareIndex[20];
bool isHwUsed(adc_channel_e hwChannel) const; bool isHwUsed(adc_channel_e hwChannel) const;

View File

@ -43,7 +43,6 @@
#define ADC_BUF_DEPTH_FAST 4 #define ADC_BUF_DEPTH_FAST 4
static adc_channel_mode_e adcHwChannelEnabled[HW_MAX_ADC_INDEX]; static adc_channel_mode_e adcHwChannelEnabled[HW_MAX_ADC_INDEX];
static const char * adcHwChannelUsage[HW_MAX_ADC_INDEX];
EXTERN_ENGINE; EXTERN_ENGINE;
@ -278,13 +277,6 @@ PWM_OUTPUT_DISABLED, NULL }, { PWM_OUTPUT_DISABLED, NULL } },
0, 0 }; 0, 0 };
#endif /* HAL_USE_PWM */ #endif /* HAL_USE_PWM */
static void initAdcPin(brain_pin_e pin, const char *msg) {
UNUSED(msg);
// todo: migrate to scheduleMsg if we want this back print("adc %s\r\n", msg);
efiSetPadMode("adc input", pin, PAL_MODE_INPUT_ANALOG);
}
const char * getAdcMode(adc_channel_e hwChannel) { const char * getAdcMode(adc_channel_e hwChannel) {
if (slowAdc.isHwUsed(hwChannel)) { if (slowAdc.isHwUsed(hwChannel)) {
return "slow"; return "slow";
@ -295,12 +287,6 @@ const char * getAdcMode(adc_channel_e hwChannel) {
return "INACTIVE - need restart"; return "INACTIVE - need restart";
} }
static void initAdcHwChannel(adc_channel_e hwChannel) {
brain_pin_e pin = getAdcChannelBrainPin("adc", hwChannel);
initAdcPin(pin, "hw");
}
int AdcDevice::size() const { int AdcDevice::size() const {
return channelCount; return channelCount;
} }
@ -354,10 +340,11 @@ void AdcDevice::enableChannel(adc_channel_e hwChannel) {
// todo: support for more then 12 channels? not sure how needed it would be // todo: support for more then 12 channels? not sure how needed it would be
} }
void AdcDevice::enableChannelAndPin(adc_channel_e hwChannel) { void AdcDevice::enableChannelAndPin(const char *msg, adc_channel_e hwChannel) {
enableChannel(hwChannel); enableChannel(hwChannel);
initAdcHwChannel(hwChannel); brain_pin_e pin = getAdcChannelBrainPin(msg, hwChannel);
efiSetPadMode(msg, pin, PAL_MODE_INPUT_ANALOG);
} }
static void printAdcValue(int channel) { static void printAdcValue(int channel) {
@ -470,13 +457,10 @@ void addChannel(const char *name, adc_channel_e setting, adc_channel_mode_e mode
return; return;
} }
if (adcHwChannelEnabled[setting] != ADC_OFF) {
getPinNameByAdcChannel(name, setting, errorMsgBuff);
firmwareError(CUSTOM_ERR_ADC_USED, "Analog input error: input \"%s\" selected for %s but was already used by %s", errorMsgBuff, name, adcHwChannelUsage[setting]);
}
adcHwChannelUsage[setting] = name;
adcHwChannelEnabled[setting] = mode; adcHwChannelEnabled[setting] = mode;
AdcDevice& dev = (mode == ADC_SLOW) ? slowAdc : fastAdc;
dev.enableChannelAndPin(name, setting);
} }
void removeChannel(const char *name, adc_channel_e setting) { void removeChannel(const char *name, adc_channel_e setting) {
@ -489,7 +473,6 @@ void removeChannel(const char *name, adc_channel_e setting) {
static void configureInputs(void) { static void configureInputs(void) {
memset(adcHwChannelEnabled, 0, sizeof(adcHwChannelEnabled)); memset(adcHwChannelEnabled, 0, sizeof(adcHwChannelEnabled));
memset(adcHwChannelUsage, 0, sizeof(adcHwChannelUsage));
/** /**
* order of analog channels here is totally random and has no meaning * order of analog channels here is totally random and has no meaning
@ -565,19 +548,6 @@ void initAdcInputs() {
adcStart(&ADC_FAST_DEVICE, NULL); adcStart(&ADC_FAST_DEVICE, NULL);
adcSTM32EnableTSVREFE(); // Internal temperature sensor adcSTM32EnableTSVREFE(); // Internal temperature sensor
for (int adc = 0; adc < HW_MAX_ADC_INDEX; adc++) {
adc_channel_mode_e mode = adcHwChannelEnabled[adc];
/**
* in board test mode all currently enabled ADC channels are running in slow mode
*/
if (mode == ADC_SLOW) {
slowAdc.enableChannelAndPin((adc_channel_e) (ADC_CHANNEL_IN0 + adc));
} else if (mode == ADC_FAST) {
fastAdc.enableChannelAndPin((adc_channel_e) (ADC_CHANNEL_IN0 + adc));
}
}
#if defined(ADC_CHANNEL_SENSOR) #if defined(ADC_CHANNEL_SENSOR)
// Internal temperature sensor, Available on ADC1 only // Internal temperature sensor, Available on ADC1 only
slowAdc.enableChannel((adc_channel_e)ADC_CHANNEL_SENSOR); slowAdc.enableChannel((adc_channel_e)ADC_CHANNEL_SENSOR);