summaryrefslogtreecommitdiffstats
path: root/app/adc.c
blob: 1565af5ae40c6d84ef114277416899a3a68c6649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "project.h"

int host_has_power;

void
adc_tick (void)
{
  uint16_t v;

  if (adc_eoc (ADC1))
    {
      v = adc_read_regular (ADC1);

      host_has_power = ! !(v > 1000);

    }

  adc_start_conversion_direct (ADC1);
}



static uint8_t channels[] = { ADC_CHANNEL0 };

#define n_channels (sizeof(channels)/sizeof(channels[0]))

void
adc_init (void)
{

  rcc_periph_clock_enable (RCC_ADC1);

  //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_DOWN, GPIO0);
  gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0);

  adc_off (ADC1);
  adc_disable_scan_mode (ADC1);
  adc_set_single_conversion_mode (ADC1);
  adc_disable_external_trigger_regular (ADC1);
  adc_set_right_aligned (ADC1);
  adc_set_sample_time_on_all_channels (ADC1, ADC_SMPR_SMP_28DOT5CYC);

  adc_power_on (ADC1);

  adc_reset_calibration (ADC1);
  adc_calibration (ADC1);
  adc_set_regular_sequence (ADC1, n_channels, channels);
  adc_start_conversion_direct (ADC1);

  host_has_power = 0;
}