Subversion Repositories chibiosIgnition

Rev

Rev 6 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6 Rev 8
Line 18... Line 18...
18
// one-dimensional interleaved array
18
// one-dimensional interleaved array
19
#define ADC_BUF_DEPTH 2 // depth of buffer
19
#define ADC_BUF_DEPTH 2 // depth of buffer
20
#define ADC_CH_NUM 2    // number of used ADC channels
20
#define ADC_CH_NUM 2    // number of used ADC channels
21
static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array
21
static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array
22
 
22
 
-
 
23
static int done;
-
 
24
void terminateAdc(ADCDriver *adcp, adcsample_t *buffer, size_t n)
-
 
25
{
-
 
26
        done = true;
-
 
27
        adcStopConversionI(adcp);
-
 
28
}
-
 
29
 
23
// Fill ADCConversionGroup structure fields
30
// Fill ADCConversionGroup structure fields
24
static ADCConversionGroup adccg = {
31
static ADCConversionGroup adccg = {
25
   // this 3 fields are common for all MCUs
32
   // this 3 fields are common for all MCUs
26
      // set to TRUE if need circular buffer, set FALSE otherwise
33
      // set to TRUE if need circular buffer, set FALSE otherwise
27
      TRUE,
34
      TRUE,
28
      // number of channels
35
      // number of channels
29
      (uint16_t)(ADC_CH_NUM),
36
      (uint16_t)(ADC_CH_NUM),
30
      // callback function, set to NULL for begin
37
      // callback function, set to NULL for begin
31
      NULL,
38
      &terminateAdc,
32
          // error callback function
39
          // error callback function
33
          NULL,
40
          NULL,
34
   // Resent fields are stm32 specific. They contain ADC control registers data.
41
   // Resent fields are stm32 specific. They contain ADC control registers data.
35
   // Please, refer to ST manual RM0008.pdf to understand what we do.
42
   // Please, refer to ST manual RM0008.pdf to understand what we do.
36
      // CR1 register content, set to zero for begin
43
      // CR1 register content, set to zero for begin
37
      0,
44
      0,
38
      // CR2 register content, set to zero for begin
45
      // CR2 register content, set to zero for begin
39
      0,
46
          ADC_CR2_EXTTRIG ,
40
      // SMRP1 register content, set to zero for begin
47
      // SMRP1 register content, set to zero for begin
41
      0,
48
      0,
42
      // SMRP2 register content, set to zero for begin
49
      // SMRP2 register content, set to zero for begin
43
      0,
50
      0,
44
      // SQR1 register content. Set channel sequence length
51
      // SQR1 register content. Set channel sequence length
45
      ((ADC_CH_NUM - 1) << 20),
52
          ADC_SQR1_NUM_CH(ADC_CH_NUM),
46
      // SQR2 register content, set to zero for begin
53
      // SQR2 register content, set to zero for begin
47
      0,
54
      0,
48
      // SQR3 register content. We must select 2 channels
55
      // SQR3 register content. We must select 2 channels
49
      // For example 2nd and 3rd channels. Refer to the
56
      // For example 2nd and 3rd channels. Refer to the
50
      // pinout of your MCU to select other pins you need.
57
      // pinout of your MCU to select other pins you need.
Line 54... Line 61...
54
 
61
 
55
// Thats all with configuration
62
// Thats all with configuration
56
 
63
 
57
void useAdc(void)
64
void useAdc(void)
58
{
65
{
59
 
-
 
60
  // Following 3 functions use previously created configuration
66
  // Following 3 functions use previously created configuration
61
  // to initialize ADC block, start ADC block and start conversion.
67
  // to initialize ADC block, start ADC block and start conversion.
62
  // &ADCD1 is pointer to ADC driver structure, defined in the depths of HAL.
68
  // &ADCD1 is pointer to ADC driver structure, defined in the depths of HAL.
63
  // Other arguments defined ourself earlier.
69
  // Other arguments defined ourself earlier.
64
  adcStart(&ADCD1, &adccfg);
70
  adcStart(&ADCD1, &adccfg);
65
  adcStartConversion(&ADCD1, &adccg, &samples_buf[0], ADC_BUF_DEPTH);
-
 
66
 
-
 
67
   // samples in to buffer
-
 
68
}
71
}
69
 
-
 
70
adcsample_t getAdc(uint8_t chan)
72
adcsample_t getAdc(uint8_t chan)
71
{
73
{
72
        return(samples_buf[chan & 1]);
74
        return(samples_buf[chan & 1]);
73
}
75
}
74
 
76
 
-
 
77
// samples into buffer
-
 
78
void adcSample(void)
75
 
79
{
-
 
80
        done = false;
-
 
81
adcConvert(&ADCD1, &adccg, &samples_buf[0], ADC_BUF_DEPTH);
-
 
82
    while(!done)
-
 
83
       chThdSleep(10);
-
 
84
}
76
 
85