@@ -355,6 +355,23 @@ void disable_systick_interruption(void)
355355 systick_interrupt_disable ();
356356}
357357
358+ /**
359+ * @brief Start the given ADC register base address.
360+ *
361+ * - Power on the ADC and wait for ADC starting up (at least 3 us).
362+ * - Calibrate the ADC.
363+ *
364+ * @see Reference manual (RM0008) "Analog-to-digital converter".
365+ */
366+ static void start_adc (uint32_t adc )
367+ {
368+ adc_power_on (adc );
369+ for (int i = 0 ; i < 800000 ; i ++ )
370+ __asm__("nop" );
371+ adc_reset_calibration (adc );
372+ adc_calibrate (adc );
373+ }
374+
358375/**
359376 * @brief Setup for ADC 1: Four injected channels on scan mode.
360377 *
@@ -366,8 +383,7 @@ void disable_systick_interruption(void)
366383 * - Configure the alignment (right) and the sample time (13.5 cycles of ADC
367384 * clock).
368385 * - Set injected sequence with channel_sequence structure.
369- * - Power on the ADC and wait for ADC starting up (at least 3 us).
370- * - Calibrate the ADC.
386+ * - Start the ADC.
371387 *
372388 * @note This ADC reads phototransistor sensors measurements.
373389 *
@@ -379,8 +395,6 @@ void disable_systick_interruption(void)
379395 */
380396static void setup_adc1 (void )
381397{
382- int i ;
383-
384398 uint8_t channel_sequence [4 ] = {ADC_CHANNEL4 , ADC_CHANNEL3 , ADC_CHANNEL5 ,
385399 ADC_CHANNEL2 };
386400
@@ -393,11 +407,32 @@ static void setup_adc1(void)
393407 adc_set_injected_sequence (
394408 ADC1 , sizeof (channel_sequence ) / sizeof (channel_sequence [0 ]),
395409 channel_sequence );
396- adc_power_on (ADC1 );
397- for (i = 0 ; i < 800000 ; i ++ )
398- __asm__("nop" );
399- adc_reset_calibration (ADC1 );
400- adc_calibrate (ADC1 );
410+ start_adc (ADC1 );
411+ }
412+
413+ /**
414+ * @brief Setup for ADC 2: configured for regular conversion.
415+ *
416+ * - Power off the ADC to be sure that does not run during configuration.
417+ * - Disable scan mode.
418+ * - Set single conversion mode triggered by software.
419+ * - Configure the alignment (right) and the sample time (13.5 cycles of ADC
420+ * clock).
421+ * - Start the ADC.
422+ *
423+ * @note This ADC reads the battery status.
424+ *
425+ * @see Reference manual (RM0008) "Analog-to-digital converter".
426+ */
427+ static void setup_adc2 (void )
428+ {
429+ adc_power_off (ADC2 );
430+ adc_disable_scan_mode (ADC2 );
431+ adc_set_single_conversion_mode (ADC2 );
432+ adc_disable_external_trigger_regular (ADC2 );
433+ adc_set_right_aligned (ADC2 );
434+ adc_set_sample_time_on_all_channels (ADC2 , ADC_SMPR_SMP_13DOT5CYC );
435+ start_adc (ADC2 );
401436}
402437
403438/**
@@ -409,6 +444,7 @@ void setup(void)
409444 setup_exceptions ();
410445 setup_gpio ();
411446 setup_adc1 ();
447+ setup_adc2 ();
412448 setup_usart ();
413449 setup_encoders ();
414450 setup_motor_driver ();
0 commit comments