refactor: improve adc driver
1. Calibration is required when ADC is enabled, and calibration is not required when enabling
ADC.
2. format code style
@@ -124,27 +124,23 @@ static rt_err_t n32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channe
ADC_InitStructure.ChsNumber = 1;
ADC_Init(n32_adc_handler, &ADC_InitStructure);
- /* ADCx regular channels configuration */
- ADC_ConfigRegularChannel(n32_adc_handler, n32_adc_get_channel(channel), 1, ADC_SAMP_TIME_28CYCLES5);
-
if (((n32_adc_handler == ADC2) || (n32_adc_handler == ADC2))
- && ((n32_adc_get_channel(channel) == ADC_CH_16) || (n32_adc_get_channel(channel) == ADC_CH_18)))
+ && ((n32_adc_get_channel(channel) == ADC_CH_16)
+ || (n32_adc_get_channel(channel) == ADC_CH_18)))
{
ADC_EnableTempSensorVrefint(ENABLE);
}
- /* Enable ADCx */
+ if (enabled)
+ {
+ /* Enable ADC1 */
ADC_Enable(n32_adc_handler, ENABLE);
-
+ /*Check ADC Ready*/
+ while (ADC_GetFlagStatusNew(n32_adc_handler, ADC_FLAG_RDY) == RESET);
/* Start ADCx calibration */
ADC_StartCalibration(n32_adc_handler);
/* Check the end of ADCx calibration */
while (ADC_GetCalibrationStatus(n32_adc_handler));
-
- if (enabled)
- {
- /* Enable ADC1 */
- ADC_Enable(n32_adc_handler, ENABLE);
}
else
{
@@ -164,12 +160,17 @@ static rt_err_t n32_get_adc_value(struct rt_adc_device *device, rt_uint32_t chan
n32_adc_handler = device->parent.user_data;
+ /* ADCx regular channels configuration */
+ ADC_ConfigRegularChannel(n32_adc_handler, n32_adc_get_channel(channel), 1, ADC_SAMP_TIME_28CYCLES5);
+
/* Start ADCx Software Conversion */
ADC_EnableSoftwareStartConv(n32_adc_handler, ENABLE);
/* Wait for the ADC to convert */
while (ADC_GetFlagStatus(n32_adc_handler, ADC_FLAG_ENDC) == RESET);
+ ADC_ClearFlag(n32_adc_handler, ADC_FLAG_ENDC);
+
/* get ADC value */
*value = ADC_GetDat(n32_adc_handler);
