@@ -9,12 +9,7 @@ use panic_halt as _;
9
9
10
10
use cortex_m:: asm;
11
11
use cortex_m_rt:: entry;
12
- use stm32f1xx_hal:: {
13
- pac,
14
- prelude:: * ,
15
- time:: ms,
16
- timer:: { Channel , Tim2NoRemap } ,
17
- } ;
12
+ use stm32f1xx_hal:: { pac, prelude:: * , time:: ms} ;
18
13
19
14
#[ entry]
20
15
fn main ( ) -> ! {
@@ -25,90 +20,70 @@ fn main() -> ! {
25
20
26
21
let clocks = rcc. cfgr . freeze ( & mut flash. acr ) ;
27
22
28
- let mut afio = p. AFIO . constrain ( ) ;
29
-
30
- let mut gpioa = p. GPIOA . split ( ) ;
31
- // let mut gpiob = p.GPIOB.split();
23
+ let gpioa = p. GPIOA . split ( ) ;
24
+ // let gpiob = p.GPIOB.split();
32
25
33
26
// TIM2
34
- let c1 = gpioa. pa0 . into_alternate_push_pull ( & mut gpioa . crl ) ;
35
- let c2 = gpioa. pa1 . into_alternate_push_pull ( & mut gpioa . crl ) ;
36
- let c3 = gpioa. pa2 . into_alternate_push_pull ( & mut gpioa . crl ) ;
27
+ let c1 = gpioa. pa0 ;
28
+ let c2 = gpioa. pa1 ;
29
+ let c3 = gpioa. pa2 ;
37
30
// If you don't want to use all channels, just leave some out
38
- // let c4 = gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl);
39
- let pins = ( c1, c2, c3) ;
31
+ // let c4 = gpioa.pa3;
40
32
41
33
// TIM3
42
- // let c1 = gpioa.pa6.into_alternate_push_pull(&mut gpioa.crl) ;
43
- // let c2 = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl) ;
44
- // let c3 = gpiob.pb0.into_alternate_push_pull(&mut gpiob.crl) ;
45
- // let c4 = gpiob.pb1.into_alternate_push_pull(&mut gpiob.crl) ;
34
+ // let c1 = gpioa.pa6;
35
+ // let c2 = gpioa.pa7;
36
+ // let c3 = gpiob.pb0;
37
+ // let c4 = gpiob.pb1;
46
38
47
39
// TIM4 (Only available with the "medium" density feature)
48
- // let c1 = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl) ;
49
- // let c2 = gpiob.pb7.into_alternate_push_pull(&mut gpiob.crl) ;
50
- // let c3 = gpiob.pb8.into_alternate_push_pull(&mut gpiob.crh) ;
51
- // let c4 = gpiob.pb9.into_alternate_push_pull(&mut gpiob.crh) ;
40
+ // let c1 = gpiob.pb6;
41
+ // let c2 = gpiob.pb7;
42
+ // let c3 = gpiob.pb8;
43
+ // let c4 = gpiob.pb9;
52
44
53
45
//let mut pwm =
54
- // Timer::new(p.TIM2, &clocks).pwm_hz::<Tim2NoRemap, _, _> (pins, &mut afio.mapr , 1.kHz());
46
+ // Timer::new(p.TIM2, &clocks).pwm_hz(pins, 1.kHz());
55
47
// or
56
- let mut pwm = p
57
- . TIM2
58
- . pwm_hz :: < Tim2NoRemap , _ , _ > ( pins, & mut afio. mapr , 1 . kHz ( ) , & clocks) ;
48
+ let ( mut pwm_mgr, ( pwm_c1, pwm_c2, pwm_c3, ..) ) = p. TIM2 . pwm_hz ( 1 . kHz ( ) , & clocks) ;
59
49
60
50
// Enable clock on each of the channels
61
- pwm. enable ( Channel :: C1 ) ;
62
- pwm. enable ( Channel :: C2 ) ;
63
- pwm. enable ( Channel :: C3 ) ;
51
+ let mut c1 = pwm_c1. with ( c1) ;
52
+ c1. enable ( ) ;
53
+ let mut c2 = pwm_c2. with ( c2) ;
54
+ c2. enable ( ) ;
55
+ let mut c3 = pwm_c3. with ( c3) ;
56
+ c3. enable ( ) ;
64
57
65
58
//// Operations affecting all defined channels on the Timer
66
59
67
60
// Adjust period to 0.5 seconds
68
- pwm . set_period ( ms ( 500 ) . into_rate ( ) ) ;
61
+ pwm_mgr . set_period ( ms ( 500 ) . into_rate ( ) ) ;
69
62
70
63
asm:: bkpt ( ) ;
71
64
72
65
// Return to the original frequency
73
- pwm . set_period ( 1 . kHz ( ) ) ;
66
+ pwm_mgr . set_period ( 1 . kHz ( ) ) ;
74
67
75
68
asm:: bkpt ( ) ;
76
69
77
- let max = pwm . get_max_duty ( ) ;
70
+ let max = pwm_mgr . get_max_duty ( ) ;
78
71
79
72
//// Operations affecting single channels can be accessed through
80
73
//// the Pwm object or via dereferencing to the pin.
81
74
82
75
// Use the Pwm object to set C3 to full strength
83
- pwm . set_duty ( Channel :: C3 , max) ;
76
+ c3 . set_duty ( max) ;
84
77
85
78
asm:: bkpt ( ) ;
86
79
87
80
// Use the Pwm object to set C3 to be dim
88
- pwm . set_duty ( Channel :: C3 , max / 4 ) ;
81
+ c3 . set_duty ( max / 4 ) ;
89
82
90
83
asm:: bkpt ( ) ;
91
84
92
85
// Use the Pwm object to set C3 to be zero
93
- pwm. set_duty ( Channel :: C3 , 0 ) ;
94
-
95
- asm:: bkpt ( ) ;
96
-
97
- // Extract the PwmChannel for C3
98
- let mut pwm_channel = pwm. split ( ) . 2 ;
99
-
100
- // Use the PwmChannel object to set C3 to be full strength
101
- pwm_channel. set_duty ( max) ;
102
-
103
- asm:: bkpt ( ) ;
104
-
105
- // Use the PwmChannel object to set C3 to be dim
106
- pwm_channel. set_duty ( max / 4 ) ;
107
-
108
- asm:: bkpt ( ) ;
109
-
110
- // Use the PwmChannel object to set C3 to be zero
111
- pwm_channel. set_duty ( 0 ) ;
86
+ c3. set_duty ( 0 ) ;
112
87
113
88
asm:: bkpt ( ) ;
114
89
0 commit comments