static int mcux_adc16_init(struct device *dev) { const struct mcux_adc16_config *config = dev->config->config_info; struct mcux_adc16_data *data = dev->driver_data; ADC_Type *base = config->base; adc16_config_t adc_config; k_sem_init(&data->sync, 0, UINT_MAX); ADC16_GetDefaultConfig(&adc_config); ADC16_Init(base, &adc_config); ADC16_EnableHardwareTrigger(base, false); ADC16_SetHardwareAverage(base, kADC16_HardwareAverageCount4); config->irq_config_func(dev); return 0; }
/*! * @brief calibrate parameters: VDD and ADCR_TEMP25 */ static void ADC16_CalibrateParams(ADC_Type *base) { uint32_t bandgapValue = 0; /*! ADC value of BANDGAP */ uint32_t vdd = 0; /*! VDD in mV */ adc16_config_t adcUserConfig; adc16_channel_config_t adcChnConfig; pmc_bandgap_buffer_config_t pmcBandgapConfig; pmcBandgapConfig.enable = true; #if (defined(FSL_FEATURE_PMC_HAS_BGEN) && FSL_FEATURE_PMC_HAS_BGEN) pmcBandgapConfig.enableInLowPowerMode = false; #endif #if (defined(FSL_FEATURE_PMC_HAS_BGBDS) && FSL_FEATURE_PMC_HAS_BGBDS) pmcBandgapConfig.drive = kPmcBandgapBufferDriveLow; #endif /* Enable BANDGAP reference voltage */ PMC_ConfigureBandgapBuffer(PMC, &pmcBandgapConfig); /* * Initialization ADC for * 16bit resolution, interrupt mode, hw trigger disabled. * normal convert speed, VREFH/L as reference, * disable continuous convert mode */ /* * adcUserConfig.referenceVoltageSource = kADC16_ReferenceVoltageSourceVref; * adcUserConfig.clockSource = kADC16_ClockSourceAsynchronousClock; * adcUserConfig.enableAsynchronousClock = true; * adcUserConfig.clockDivider = kADC16_ClockDivider8; * adcUserConfig.resolution = kADC16_ResolutionSE12Bit; * adcUserConfig.longSampleMode = kADC16_LongSampleDisabled; * adcUserConfig.enableHighSpeed = false; * adcUserConfig.enableLowPower = false; * adcUserConfig.enableContinuousConversion = false; */ ADC16_GetDefaultConfig(&adcUserConfig); adcUserConfig.resolution = kADC16_Resolution16Bit; adcUserConfig.enableContinuousConversion = false; adcUserConfig.clockSource = kADC16_ClockSourceAsynchronousClock; adcUserConfig.enableLowPower = 1; adcUserConfig.longSampleMode = kADC16_LongSampleCycle24; #ifdef BOARD_ADC_USE_ALT_VREF adcUserConfig.referenceVoltageSource = kADC16_ReferenceVoltageSourceValt; #endif ADC16_Init(base, &adcUserConfig); #if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION /* Auto calibration */ ADC16_DoAutoCalibration(base); #endif #if defined(FSL_FEATURE_ADC16_HAS_HW_AVERAGE) && FSL_FEATURE_ADC16_HAS_HW_AVERAGE /* Use hardware average to increase stability of the measurement */ ADC16_SetHardwareAverage(base, kADC16_HardwareAverageCount32); #endif /* FSL_FEATURE_ADC16_HAS_HW_AVERAGE */ adcChnConfig.channelNumber = kAdcChannelBandgap; #if defined(FSL_FEATURE_ADC16_HAS_DIFF_MODE) && FSL_FEATURE_ADC16_HAS_DIFF_MODE adcChnConfig.enableDifferentialConversion = false; #endif adcChnConfig.enableInterruptOnConversionCompleted = false; ADC16_SetChannelConfig(base, DEMO_ADC16_CHANNEL_GROUP, &adcChnConfig); /* Wait for the conversion to be done */ while (!ADC16_GetChannelStatusFlags(base, DEMO_ADC16_CHANNEL_GROUP)) { } /* Get current ADC BANDGAP value */ bandgapValue = ADC16_GetChannelConversionValue(base, DEMO_ADC16_CHANNEL_GROUP); ADC16_PauseConversion(base); /* Get VDD value measured in mV: VDD = (ADCR_VDD x V_BG) / ADCR_BG */ vdd = ADCR_VDD * V_BG / bandgapValue; /* Calibrate ADCR_TEMP25: ADCR_TEMP25 = ADCR_VDD x V_TEMP25 / VDD */ adcrTemp25 = ADCR_VDD * V_TEMP25 / vdd; /* ADCR_100M = ADCR_VDD x M x 100 / VDD */ adcr100m = (ADCR_VDD * M) / (vdd * 10); /* Disable BANDGAP reference voltage */ pmcBandgapConfig.enable = false; PMC_ConfigureBandgapBuffer(PMC, &pmcBandgapConfig); }