1
- //! # Work in Progress
2
1
//! API for the ADC (Analog to Digital Converter)
3
2
//!
4
- //! Note that the more specific your hardware selection is
5
- //! (e.g. stm32f303**xc** instead of just stm32f303)
6
- //! the more functionality is accessible
7
- //! (in this case: ADC3 and ADC4 additionaly to ADC1 and ADC2).
8
- //!
9
3
//! # Examples
10
- //! For a simple, working example check `adc.rs` in the examples folder.
4
+ //! Check `adc.rs` in the examples folder.
5
+ //! It can be built for the STM32F3Discovery running
6
+ //! `cargo build --example adc --features=stm32f303xc`
7
+ use crate :: {
8
+ gpio:: Analog ,
9
+ rcc:: { Clocks , AHB } ,
10
+ } ;
11
11
use cortex_m:: asm;
12
12
use embedded_hal:: adc:: { Channel , OneShot } ;
13
- #[ cfg( feature = "stm32f303" ) ]
14
- use stm32f3:: stm32f303:: adc1_2:: ccr:: CKMODE_A ;
15
13
16
- use crate :: rcc:: { Clocks , AHB } ;
17
-
18
- #[ cfg( feature = "stm32f303" ) ]
14
+ use crate :: {
15
+ gpio:: { gpioa, gpiob, gpioc} ,
16
+ pac:: { ADC1 , ADC1_2 , ADC2 } ,
17
+ } ;
18
+ use stm32f3:: stm32f303:: adc1_2:: ccr:: CKMODE_A ;
19
19
const MAX_ADVREGEN_STARTUP_US : u32 = 10 ;
20
20
21
- #[ cfg( feature = "stm32f303" ) ]
22
- use crate :: gpio:: { gpioa, gpiob, gpioc, Analog } ;
23
-
24
- #[ cfg( any(
25
- feature = "stm32f303xb" ,
26
- feature = "stm32f303xc" ,
27
- feature = "stm32f303xd" ,
28
- feature = "stm32f303xe" ,
29
- ) ) ]
30
- use crate :: gpio:: { gpiod, gpioe, gpiof} ;
31
-
32
- #[ cfg( feature = "stm32f303" ) ]
33
- use crate :: pac:: { ADC1 , ADC1_2 , ADC2 } ;
34
-
35
21
#[ cfg( any(
36
22
feature = "stm32f303xb" ,
37
23
feature = "stm32f303xc" ,
38
24
feature = "stm32f303xd" ,
39
25
feature = "stm32f303xe" ,
40
26
) ) ]
41
- use crate :: pac:: { ADC3 , ADC3_4 , ADC4 } ;
27
+ use crate :: {
28
+ gpio:: { gpiod, gpioe, gpiof} ,
29
+ pac:: { ADC3 , ADC3_4 , ADC4 } ,
30
+ } ;
42
31
43
32
/// ADC configuration
44
- ///
45
- /// TODO: Remove `pub` from the register block once all functionalities are implemented.
46
- /// Leave it here until then as it allows easy access to the registers.
33
+ // TODO: Remove `pub` from the register block once all functionalities are implemented.
34
+ // Leave it here until then as it allows easy access to the registers.
47
35
pub struct Adc < ADC > {
48
36
pub rb : ADC ,
49
37
clocks : Clocks ,
@@ -54,11 +42,11 @@ pub struct Adc<ADC> {
54
42
/// ADC sampling time
55
43
///
56
44
/// Each channel can be sampled with a different sample time.
57
- /// For Sampletime T_n the total conversion time (in ADC clock cycles) is
58
- /// 12.5 + (n + .5)
59
- ///
60
- /// TODO: there are boundaries on how this can be set depending on the hardware.
61
- /// Check them and implement a sample time setting mechanism.
45
+ /// There is always an overhead of 13 ADC clock cycles.
46
+ /// E.g. For Sampletime T_19 the total conversion time (in ADC clock cycles) is
47
+ /// 13 + 19 = 32 ADC Clock Cycles
48
+ // TODO: there are boundaries on how this can be set depending on the hardware.
49
+ // Check them and implement a sample time setting mechanism.
62
50
pub enum SampleTime {
63
51
T_1 ,
64
52
T_2 ,
@@ -94,16 +82,14 @@ impl SampleTime {
94
82
95
83
#[ derive( Clone , Copy , PartialEq ) ]
96
84
/// ADC operation mode
97
- ///
98
- /// TODO: Implement other modes (DMA, Differential,…)
85
+ // TODO: Implement other modes (DMA, Differential,…)
99
86
pub enum OperationMode {
100
87
OneShot ,
101
88
}
102
89
103
90
#[ derive( Clone , Copy , PartialEq ) ]
104
91
/// ADC CKMODE
105
- ///
106
- /// TODO: Add ASYNCHRONOUS mode
92
+ // TODO: Add ASYNCHRONOUS mode
107
93
pub enum CKMODE {
108
94
// ASYNCHRONOUS = 0,
109
95
SYNCDIV1 = 1 ,
@@ -119,7 +105,7 @@ impl Default for CKMODE {
119
105
120
106
// ADC3_2 returns a pointer to a adc1_2 type, so this from is ok for both.
121
107
#[ cfg( feature = "stm32f303" ) ]
122
- impl From < CKMODE > for stm32f3 :: stm32f303 :: adc1_2 :: ccr :: CKMODE_A {
108
+ impl From < CKMODE > for CKMODE_A {
123
109
fn from ( ckmode : CKMODE ) -> Self {
124
110
match ckmode {
125
111
//CKMODE::ASYNCHRONOUS => CKMODE_A::ASYNCHRONOUS,
@@ -289,10 +275,9 @@ adc_pins!(ADC4,
289
275
gpiod:: PD14 <Analog > => 11_u8 ,
290
276
) ;
291
277
292
- /// Abstract implementation of ADC functionality
293
- ///
294
- /// Do not use directly. See adc12_hal for a applicable Macro.
295
- /// TODO: Extend/generalize beyond f303
278
+ // Abstract implementation of ADC functionality
279
+ // Do not use directly. See adc12_hal for a applicable Macro.
280
+ // TODO: Extend/generalize beyond f303
296
281
macro_rules! adc_hal {
297
282
( $(
298
283
$ADC: ident: ( $adcx: ident, $ADC_COMMON: ident) ,
@@ -474,9 +459,8 @@ macro_rules! adc_hal {
474
459
}
475
460
}
476
461
477
- /// Macro to implement ADC functionallity for ADC1 and ADC2
478
- ///
479
- /// TODO: Extend/differentiate beyond f303.
462
+ // Macro to implement ADC functionallity for ADC1 and ADC2
463
+ // TODO: Extend/differentiate beyond f303.
480
464
macro_rules! adc12_hal {
481
465
( $(
482
466
$ADC: ident: ( $adcx: ident) ,
@@ -504,9 +488,8 @@ macro_rules! adc12_hal {
504
488
}
505
489
}
506
490
507
- /// Macro to implement ADC functionallity for ADC3 and ADC4
508
- ///
509
- /// TODO: Extend/differentiate beyond f303.
491
+ // Macro to implement ADC functionallity for ADC3 and ADC4
492
+ // TODO: Extend/differentiate beyond f303.
510
493
#[ cfg( any(
511
494
feature = "stm32f303xb" ,
512
495
feature = "stm32f303xc" ,
0 commit comments