Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | |
2 | |||
3 | |||
4 | |||
5 | #include "ch.h" // needs for all ChibiOS programs |
||
6 | #include "hal.h" // hardware abstraction layer header |
||
7 | |||
8 | #include "useAdc.h" |
||
9 | |||
10 | |||
11 | // Lets configure our ADC first |
||
12 | |||
13 | |||
14 | // ADCConfig structure for stm32 MCUs is empty |
||
15 | static ADCConfig adccfg = {}; |
||
16 | |||
17 | // Create buffer to store ADC results. This is |
||
18 | // one-dimensional interleaved array |
||
19 | #define ADC_BUF_DEPTH 2 // depth of buffer |
||
20 | #define ADC_CH_NUM 1 // number of used ADC channels |
||
21 | static adcsample_t samples_buf[ADC_BUF_DEPTH * ADC_CH_NUM]; // results array |
||
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 | |||
30 | // Fill ADCConversionGroup structure fields |
||
31 | static ADCConversionGroup adccg = { |
||
32 | // this 3 fields are common for all MCUs |
||
33 | // set to TRUE if need circular buffer, set FALSE otherwise |
||
34 | TRUE, |
||
35 | // number of channels |
||
36 | (uint16_t)(ADC_CH_NUM), |
||
37 | // callback function, set to NULL for begin |
||
38 | &terminateAdc, |
||
39 | // error callback function |
||
40 | NULL, |
||
41 | // Resent fields are stm32 specific. They contain ADC control registers data. |
||
42 | // Please, refer to ST manual RM0008.pdf to understand what we do. |
||
43 | // CR1 register content, set to zero for begin |
||
44 | 0, |
||
45 | // CR2 register content, set to zero for begin |
||
46 | ADC_CR2_EXTTRIG , |
||
47 | // SMRP1 register content, set to zero for begin |
||
48 | 0, |
||
49 | // SMRP2 register content, set to zero for begin |
||
50 | 0, |
||
51 | // SQR1 register content. Set channel sequence length |
||
52 | ADC_SQR1_NUM_CH(ADC_CH_NUM), |
||
53 | // SQR2 register content, set to zero for begin |
||
54 | 0, |
||
55 | // SQR3 register content. We must select 2 channels |
||
56 | // For example 2nd and 3rd channels. Refer to the |
||
57 | // pinout of your MCU to select other pins you need. |
||
58 | // On STM32-P103 board they connected to PA4 |
||
59 | (4 | (0 << 5)), |
||
60 | }; |
||
61 | |||
62 | // Thats all with configuration |
||
63 | |||
64 | void useAdc(void) |
||
65 | { |
||
66 | // Following 3 functions use previously created configuration |
||
67 | // to initialize ADC block, start ADC block and start conversion. |
||
68 | // &ADCD1 is pointer to ADC driver structure, defined in the depths of HAL. |
||
69 | // Other arguments defined ourself earlier. |
||
70 | adcStart(&ADCD1, &adccfg); |
||
71 | } |
||
72 | adcsample_t getAdc(uint8_t chan) |
||
73 | { |
||
74 | return(samples_buf[chan & 1]); |
||
75 | } |
||
76 | |||
77 | // samples into buffer |
||
78 | void adcSample(void) |
||
79 | { |
||
80 | done = false; |
||
81 | adcConvert(&ADCD1, &adccg, &samples_buf[0], ADC_BUF_DEPTH); |
||
82 | while(!done) |
||
83 | chThdSleep(10); |
||
84 | } |
||
85 |