/** Initialize the analogin peripheral * * Configures the pin used by analogin. * @param obj The analogin object to initialize * @param pin The analogin pin name */ void analogin_init(analogin_t *obj, PinName pin) { uint32_t periph; MBED_ASSERT(obj); obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); MBED_ASSERT(obj->adc != (ADCName)NC); uint32_t function = pinmap_function(pin, PinMap_ADC); MBED_ASSERT(function != (uint32_t)NC); obj->channel = GD_PIN_CHANNEL_GET(function); MBED_ASSERT(obj->channel <= ADC_CHANNEL_17); obj->pin = pin; if ((ADC_CHANNEL_17 == obj->channel) || (ADC_CHANNEL_16 == obj->channel)) { /* no need to config port */ } else { pinmap_pinout(pin, PinMap_ADC); } periph = obj->adc; /* when pin >= ADC_TEMP, it indicates that the channel has no external pins */ if (pin < ADC_TEMP) { pinmap_pinout(pin, PinMap_ADC); } /* ADC clock enable */ switch (periph) { case ADC0: rcu_periph_clock_enable(RCU_ADC0); break; case ADC1: rcu_periph_clock_enable(RCU_ADC1); break; #ifndef GD32F30X_CL case ADC2: rcu_periph_clock_enable(RCU_ADC2); break; #endif /* GD32F30X_CL */ } /* ADC clock cannot be greater than 42M */ rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6); /* ADC configuration */ adc_data_alignment_config(obj->adc, ADC_DATAALIGN_RIGHT); adc_channel_length_config(obj->adc, ADC_REGULAR_CHANNEL, 1); adc_special_function_config(obj->adc, ADC_SCAN_MODE, DISABLE); adc_special_function_config(obj->adc, ADC_CONTINUOUS_MODE, DISABLE); adc_external_trigger_config(obj->adc, ADC_REGULAR_CHANNEL, ENABLE); adc_external_trigger_source_config(obj->adc, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE); /* ADC enable */ adc_enable(obj->adc); adc_calibration_enable(obj->adc); }
/** Initialize the analogin peripheral * * Configures the pin used by analogin. * @param obj The analogin object to initialize * @param pin The analogin pin name */ void analogin_init(analogin_t *obj, PinName pin) { uint32_t periph; MBED_ASSERT(obj); obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); MBED_ASSERT(obj->adc != (ADCName)NC); uint32_t function = pinmap_function(pin, PinMap_ADC); MBED_ASSERT(function != (uint32_t)NC); obj->channel = GD_PIN_CHANNEL_GET(function); MBED_ASSERT(obj->channel <= ADC_CHANNEL_17); periph = obj->adc; /* save the pin for future use */ obj->pin = pin; /* ADC clock enable and pin number reset */ switch (periph) { case ADC0: rcu_periph_clock_enable(RCU_ADC0); break; case ADC1: rcu_periph_clock_enable(RCU_ADC1); /* reset pin number */ pin = (PinName)(pin & AND_NUMBER); break; } /* ADC clock cannot be greater than 40M */ rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4); if ((ADC_CHANNEL_16 == obj->channel)) { /* ADC Vrefint enable */ adc_tempsensor_vrefint_enable(); /* set temperature sample flag */ temperature_sample_flag = SET; } if ((ADC_CHANNEL_17 == obj->channel)) { /* ADC Vrefint enable */ adc_tempsensor_vrefint_enable(); } /* when pin >= ADC_TEMP, it indicates that the channel has no external pins */ if (pin < ADC_TEMP) { pinmap_pinout(pin, PinMap_ADC); } /* ADC configuration */ adc_special_function_config(obj->adc, ADC_SCAN_MODE, DISABLE); adc_special_function_config(obj->adc, ADC_CONTINUOUS_MODE, DISABLE); /* ADC trigger config */ adc_external_trigger_source_config(obj->adc, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE); /* ADC mode config */ adc_mode_config(ADC_MODE_FREE); /* ADC data alignment config */ adc_data_alignment_config(obj->adc, ADC_DATAALIGN_RIGHT); /* ADC channel length config */ adc_channel_length_config(obj->adc, ADC_REGULAR_CHANNEL, 1); if (temperature_sample_flag == SET) { /* sample temperature needs more time */ adc_regular_channel_config(obj->adc, 0, obj->channel, ADC_SAMPLETIME_239POINT5); /* clear temperature sample flag */ temperature_sample_flag = RESET; } else { adc_regular_channel_config(obj->adc, 0, obj->channel, ADC_SAMPLETIME_28POINT5); } adc_external_trigger_config(obj->adc, ADC_REGULAR_CHANNEL, ENABLE); /* ADC enable */ adc_enable(obj->adc); /* wait for ADC to stabilize */ _delay(500); adc_calibration_enable(obj->adc); }