@@ -131,8 +131,6 @@ int adc_init(adc_t line)
131131 ADC -> CCR |= ADC_CCR_TSVREFE ;
132132 }
133133
134- /* enable the ADC module */
135- ADC1 -> CR2 = ADC_CR2_ADON ;
136134 /* turn off during idle phase*/
137135 ADC1 -> CR1 = ADC_CR1_PDI ;
138136
@@ -157,12 +155,17 @@ int32_t adc_sample(adc_t line, adc_res_t res)
157155 /* lock and power on the ADC device */
158156 prep ();
159157
160- /* set resolution, conversion channel and single read */
161- ADC1 -> CR1 |= res & ADC_CR1_RES ;
158+ /* mask and set resolution, conversion channel and single read */
159+ ADC1 -> CR1 = ( ADC1 -> CR1 & ~ ADC_CR1_RES ) | ( res & ADC_CR1_RES ) ;
162160 ADC1 -> SQR1 &= ~ADC_SQR1_L ;
163161 ADC1 -> SQR5 = adc_config [line ].chan ;
164162
165- /* wait for regulat channel to be ready*/
163+ /* only set ADON when ADONS bit is cleared (ADC not ready) */
164+ if (!(ADC1 -> SR & ADC_SR_ADONS )) {
165+ ADC1 -> CR2 |= ADC_CR2_ADON ;
166+ }
167+
168+ /* wait for regular channel to be ready*/
166169 while (!(ADC1 -> SR & ADC_SR_RCNR )) {}
167170 /* start conversion and wait for results */
168171 ADC1 -> CR2 |= ADC_CR2_SWSTART ;
@@ -171,6 +174,10 @@ int32_t adc_sample(adc_t line, adc_res_t res)
171174 sample = (int )ADC1 -> DR ;
172175 ADC1 -> SR &= ~ADC_SR_STRT ;
173176
177+ /* wait for ADC to become ready before disabling it */
178+ while (!(ADC1 -> SR & ADC_SR_ADONS )) {}
179+ ADC1 -> CR2 &= ~ADC_CR2_ADON ;
180+
174181 /* power off and unlock device again */
175182 done ();
176183
0 commit comments