int getInternalAdcValue(const char *msg, adc_channel_e hwChannel) { if (hwChannel == EFI_ADC_NONE) { warning(CUSTOM_OBD_ANALOG_INPUT_NOT_CONFIGURED, "ADC: %s input is not configured", msg); return -1; } #if EFI_ENABLE_MOCK_ADC if (engine->engineState.mockAdcState.hasMockAdc[hwChannel]) return engine->engineState.mockAdcState.getMockAdcValue(hwChannel); #endif /* EFI_ENABLE_MOCK_ADC */ if (adcHwChannelEnabled[hwChannel] == ADC_FAST) { int internalIndex = fastAdc.internalAdcIndexByHardwareIndex[hwChannel]; // todo if ADC_BUF_DEPTH_FAST EQ 1 // return fastAdc.samples[internalIndex]; int value = getAvgAdcValue(internalIndex, fastAdc.samples, ADC_BUF_DEPTH_FAST, fastAdc.size()); return value; } if (adcHwChannelEnabled[hwChannel] != ADC_SLOW) { warning(CUSTOM_OBD_39, "ADC is off [%s] index=%d", msg, hwChannel); } return slowAdc.getAdcValueByHwChannel(hwChannel); }
static void adc_callback_slow(ADCDriver *adcp, adcsample_t *buffer, size_t n) { (void) buffer; (void) n; efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#9c"); /* Note, only in the ADC_COMPLETE state because the ADC driver fires * an intermediate callback when the buffer is half full. */ if (adcp->state == ADC_COMPLETE) { /* Calculates the average values from the ADC samples.*/ for (int i = 0; i < slowAdc.size(); i++) { int value = getAvgAdcValue(i, slowAdc.samples, ADC_BUF_DEPTH_SLOW, slowAdc.size()); adcsample_t prev = slowAdc.values.adc_data[i]; slowAdc.values.adc_data[i] = (slowAdcCounter == 0) ? value : CONFIG(slowAdcAlpha) * value + (1 - CONFIG(slowAdcAlpha)) * prev; } slowAdcCounter++; } }
static void adc_callback_slow(ADCDriver *adcp, adcsample_t *buffer, size_t n) { (void) buffer; (void) n; efiAssertVoid(getRemainingStack(chThdSelf()) > 128, "lowstck#9c"); /* Note, only in the ADC_COMPLETE state because the ADC driver fires * an intermediate callback when the buffer is half full. */ if (adcp->state == ADC_COMPLETE) { /* Calculates the average values from the ADC samples.*/ adcCallbackCounter_slow++; // newState.time = chimeNow(); for (int i = 0; i < slowAdc.size(); i++) { /** * todo: No need to average since DEPTH is '1' */ int value = getAvgAdcValue(i, slowAdc.samples, ADC_BUF_DEPTH_SLOW, slowAdc.size()); slowAdc.values.adc_data[i] = value; } } }
static void printFullAdcReport(void) { scheduleMsg(&logger, "fast %d slow %d", fastAdc.conversionCount, slowAdc.conversionCount); for (int index = 0; index < slowAdc.size(); index++) { appendMsgPrefix(&logger); adc_channel_e hwIndex = slowAdc.getAdcHardwareIndexByInternalIndex(index); GPIO_TypeDef* port = getAdcChannelPort(hwIndex); int pin = getAdcChannelPin(hwIndex); int adcValue = slowAdc.getAdcValueByIndex(index); appendPrintf(&logger, " ch%d %s%d", index, portname(port), pin); appendPrintf(&logger, " ADC%d 12bit=%d", hwIndex, adcValue); float volts = adcToVolts(adcValue); appendPrintf(&logger, " v=%f", volts); appendMsgPostfix(&logger); scheduleLogging(&logger); } }
static void printFullAdcReport(Logging *logger) { scheduleMsg(logger, "fast %d slow %d", fastAdc.conversionCount, slowAdc.conversionCount); for (int index = 0; index < slowAdc.size(); index++) { appendMsgPrefix(logger); adc_channel_e hwIndex = slowAdc.getAdcHardwareIndexByInternalIndex(index); if(hwIndex != EFI_ADC_NONE && hwIndex != EFI_ADC_ERROR) { ioportid_t port = getAdcChannelPort("print", hwIndex); int pin = getAdcChannelPin(hwIndex); int adcValue = slowAdc.getAdcValueByIndex(index); appendPrintf(logger, " ch%d %s%d", index, portname(port), pin); appendPrintf(logger, " ADC%d 12bit=%d", hwIndex, adcValue); float volts = adcToVolts(adcValue); appendPrintf(logger, " v=%.2f", volts); appendMsgPostfix(logger); scheduleLogging(logger); } } }