Skip to content

Commit 69688dc

Browse files
committed
Enable battery voltage readings again
1 parent 4d45d2f commit 69688dc

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

src/battery.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
#include "battery.h"
22

3-
/**
4-
* @brief ADC1 and ADC2 interruption routine.
5-
*
6-
* - Manage the ADC2 analog watchdog interruption flag.
7-
* - Send a message.
8-
* - Toggle a LED.
9-
* - Disable analog watchdog interruptions on injected channels.
10-
*/
11-
void adc1_2_isr(void)
12-
{
13-
if (adc_get_flag(ADC2, ADC_SR_AWD)) {
14-
adc_clear_flag(ADC2, ADC_SR_AWD);
15-
LOG_WARNING("Battery low!");
16-
led_bluepill_toggle();
17-
adc_disable_analog_watchdog_injected(ADC2);
18-
}
19-
}
20-
213
/**
224
* @brief Function to get battery voltage.
235
*
@@ -33,7 +15,12 @@ void adc1_2_isr(void)
3315
float get_battery_voltage(void)
3416
{
3517
uint16_t battery_bits;
18+
uint8_t channels[16];
3619

37-
battery_bits = adc_read_injected(ADC2, 3);
20+
channels[0] = ADC_CHANNEL0;
21+
adc_set_regular_sequence(ADC2, 1, channels);
22+
adc_start_conversion_direct(ADC2);
23+
while (!adc_eoc(ADC2));
24+
battery_bits = adc_read_regular(ADC2);
3825
return battery_bits * ADC_LSB * VOLT_DIV_FACTOR;
3926
}

src/setup.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
380396
static 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

Comments
 (0)