11//! # Pulse Width Modulation
22
3+ use cast:: { u16, u32} ;
34use core:: marker:: PhantomData ;
45use core:: mem;
56
6- use cast:: { u16, u32} ;
77use crate :: hal;
88use crate :: stm32:: TIM2 ;
99
10- use crate :: bb;
1110use crate :: gpio:: gpioa:: { PA0 , PA1 , PA2 , PA3 } ;
1211use crate :: gpio:: { Alternate , Output , PushPull , AF1 } ;
13- use crate :: rcc:: { Clocks , /* AHB2, */ APB1R1 } ;
12+ use crate :: rcc:: { Clocks , APB1R1 } ;
1413use crate :: time:: Hertz ;
1514
15+ // NB: REMAP is not implemented!
1616pub trait Pins < TIM > {
17- const REMAP : u8 ;
17+ // const REMAP: u8;
1818 const C1 : bool ;
1919 const C2 : bool ;
2020 const C3 : bool ;
2121 const C4 : bool ;
2222 type Channels ;
2323}
2424
25- /// NB: REMAP is not implemented!
2625impl Pins < TIM2 >
2726 for (
2827 PA0 < Alternate < AF1 , Output < PushPull > > > ,
@@ -31,7 +30,6 @@ impl Pins<TIM2>
3130 PA3 < Alternate < AF1 , Output < PushPull > > > ,
3231 )
3332{
34- const REMAP : u8 = 0b00 ;
3533 const C1 : bool = true ;
3634 const C2 : bool = true ;
3735 const C3 : bool = true ;
@@ -42,28 +40,50 @@ impl Pins<TIM2>
4240// useful for RGB LED
4341impl Pins < TIM2 >
4442 for (
45- PA1 < Alternate < AF1 , Output < PushPull > > > ,
4643 PA2 < Alternate < AF1 , Output < PushPull > > > ,
4744 PA3 < Alternate < AF1 , Output < PushPull > > > ,
45+ PA1 < Alternate < AF1 , Output < PushPull > > > ,
4846 )
4947{
50- const REMAP : u8 = 0b00 ;
51- const C1 : bool = true ;
48+ const C1 : bool = false ;
5249 const C2 : bool = true ;
5350 const C3 : bool = true ;
54- const C4 : bool = false ;
55- type Channels = ( Pwm < TIM2 , C1 > , Pwm < TIM2 , C2 > , Pwm < TIM2 , C3 > ) ;
51+ const C4 : bool = true ;
52+ type Channels = ( Pwm < TIM2 , C3 > , Pwm < TIM2 , C4 > , Pwm < TIM2 , C2 > ) ;
5653}
5754
5855impl Pins < TIM2 > for PA0 < Alternate < AF1 , Output < PushPull > > > {
59- const REMAP : u8 = 0b00 ;
6056 const C1 : bool = true ;
6157 const C2 : bool = false ;
6258 const C3 : bool = false ;
6359 const C4 : bool = false ;
6460 type Channels = Pwm < TIM2 , C1 > ;
6561}
6662
63+ impl Pins < TIM2 > for PA1 < Alternate < AF1 , Output < PushPull > > > {
64+ const C1 : bool = false ;
65+ const C2 : bool = true ;
66+ const C3 : bool = false ;
67+ const C4 : bool = false ;
68+ type Channels = Pwm < TIM2 , C2 > ;
69+ }
70+
71+ impl Pins < TIM2 > for PA2 < Alternate < AF1 , Output < PushPull > > > {
72+ const C1 : bool = false ;
73+ const C2 : bool = false ;
74+ const C3 : bool = true ;
75+ const C4 : bool = false ;
76+ type Channels = Pwm < TIM2 , C3 > ;
77+ }
78+
79+ impl Pins < TIM2 > for PA3 < Alternate < AF1 , Output < PushPull > > > {
80+ const C1 : bool = false ;
81+ const C2 : bool = false ;
82+ const C3 : bool = false ;
83+ const C4 : bool = true ;
84+ type Channels = Pwm < TIM2 , C4 > ;
85+ }
86+
6787pub trait PwmExt : Sized {
6888 fn pwm < PINS , T > (
6989 self ,
@@ -78,13 +98,7 @@ pub trait PwmExt: Sized {
7898}
7999
80100impl PwmExt for TIM2 {
81- fn pwm < PINS , T > (
82- self ,
83- _pins : PINS ,
84- freq : T ,
85- clocks : Clocks ,
86- apb : & mut APB1R1 ,
87- ) -> PINS :: Channels
101+ fn pwm < PINS , T > ( self , _pins : PINS , freq : T , clocks : Clocks , apb : & mut APB1R1 ) -> PINS :: Channels
88102 where
89103 PINS : Pins < Self > ,
90104 T : Into < Hertz > ,
@@ -149,6 +163,7 @@ macro_rules! hal {
149163 let freq = freq. 0 ;
150164 let ticks = clk / freq;
151165
166+ // maybe this is all u32? also, why no `- 1` vs `timer.rs`?
152167 let psc = u16 ( ticks / ( 1 << 16 ) ) . unwrap( ) ;
153168 tim. psc. write( |w| unsafe { w. psc( ) . bits( psc) } ) ;
154169 let arr = u16 ( ticks / u32 ( psc + 1 ) ) . unwrap( ) ;
@@ -169,11 +184,11 @@ macro_rules! hal {
169184 type Duty = u32 ;
170185
171186 fn disable( & mut self ) {
172- unsafe { bb :: clear ( & ( * $TIMX:: ptr( ) ) . ccer, 0 ) }
187+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc1e ( ) . clear_bit ( ) ) }
173188 }
174189
175190 fn enable( & mut self ) {
176- unsafe { bb :: set ( & ( * $TIMX:: ptr( ) ) . ccer, 0 ) }
191+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc1e ( ) . set_bit ( ) ) }
177192 }
178193
179194 fn get_duty( & self ) -> Self :: Duty {
@@ -193,11 +208,11 @@ macro_rules! hal {
193208 type Duty = u32 ;
194209
195210 fn disable( & mut self ) {
196- unsafe { bb :: clear ( & ( * $TIMX:: ptr( ) ) . ccer, 4 ) }
211+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc2e ( ) . clear_bit ( ) ) }
197212 }
198213
199214 fn enable( & mut self ) {
200- unsafe { bb :: set ( & ( * $TIMX:: ptr( ) ) . ccer, 4 ) }
215+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc2e ( ) . set_bit ( ) ) }
201216 }
202217
203218 fn get_duty( & self ) -> Self :: Duty {
@@ -217,11 +232,11 @@ macro_rules! hal {
217232 type Duty = u32 ;
218233
219234 fn disable( & mut self ) {
220- unsafe { bb :: clear ( & ( * $TIMX:: ptr( ) ) . ccer, 8 ) }
235+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc3e ( ) . clear_bit ( ) ) }
221236 }
222237
223238 fn enable( & mut self ) {
224- unsafe { bb :: set ( & ( * $TIMX:: ptr( ) ) . ccer, 8 ) }
239+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc3e ( ) . set_bit ( ) ) }
225240 }
226241
227242 fn get_duty( & self ) -> Self :: Duty {
@@ -241,11 +256,11 @@ macro_rules! hal {
241256 type Duty = u32 ;
242257
243258 fn disable( & mut self ) {
244- unsafe { bb :: clear ( & ( * $TIMX:: ptr( ) ) . ccer, 12 ) }
259+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc4e ( ) . clear_bit ( ) ) }
245260 }
246261
247262 fn enable( & mut self ) {
248- unsafe { bb :: set ( & ( * $TIMX:: ptr( ) ) . ccer, 12 ) }
263+ unsafe { ( * $TIMX:: ptr( ) ) . ccer. write ( |w| w . cc4e ( ) . set_bit ( ) ) }
249264 }
250265
251266 fn get_duty( & self ) -> Self :: Duty {
0 commit comments