@@ -14,8 +14,7 @@ pub trait OpmExt: Sized {
14
14
pub struct OpmPin < TIM , CH > {
15
15
tim : PhantomData < TIM > ,
16
16
channel : PhantomData < CH > ,
17
- clk : Hertz ,
18
- delay : MicroSecond ,
17
+ delay : u32 ,
19
18
}
20
19
21
20
pub struct Opm < TIM > {
@@ -32,8 +31,7 @@ impl<TIM> Opm<TIM> {
32
31
OpmPin {
33
32
tim : PhantomData ,
34
33
channel : PhantomData ,
35
- clk : self . clk ,
36
- delay : 0 . ms ( ) ,
34
+ delay : 1 ,
37
35
}
38
36
}
39
37
}
@@ -42,34 +40,39 @@ macro_rules! opm {
42
40
( $( $TIMX: ident: ( $timX: ident, $arr: ident $( , $arr_h: ident) * ) , ) +) => {
43
41
$(
44
42
impl OpmExt for $TIMX {
45
- fn opm( self , period : MicroSecond , rcc: & mut Rcc ) -> Opm <Self > {
46
- $timX( self , period , rcc)
43
+ fn opm( self , pulse : MicroSecond , rcc: & mut Rcc ) -> Opm <Self > {
44
+ $timX( self , pulse , rcc)
47
45
}
48
46
}
49
47
50
- fn $timX( tim : $TIMX, period : MicroSecond , rcc: & mut Rcc ) -> Opm <$TIMX> {
48
+ fn $timX( _tim : $TIMX, pulse : MicroSecond , rcc: & mut Rcc ) -> Opm <$TIMX> {
51
49
$TIMX:: enable( rcc) ;
52
50
$TIMX:: reset( rcc) ;
53
51
54
- let cycles_per_period = rcc. clocks. apb_tim_clk / period. into( ) ;
55
- let psc = ( cycles_per_period - 1 ) / 0xffff ;
56
- tim. psc. write( |w| unsafe { w. psc( ) . bits( psc as u16 ) } ) ;
57
-
58
- let freq = ( rcc. clocks. apb_tim_clk. 0 / ( psc + 1 ) ) . hz( ) ;
59
- let reload = period. cycles( freq) ;
60
- unsafe {
61
- tim. arr. write( |w| w. $arr( ) . bits( reload as u16 ) ) ;
62
- $(
63
- tim. arr. modify( |_, w| w. $arr_h( ) . bits( ( reload >> 16 ) as u16 ) ) ;
64
- ) *
65
- }
66
- Opm {
67
- clk: freq,
52
+ let mut opm = Opm :: <$TIMX> {
53
+ clk: rcc. clocks. apb_tim_clk,
68
54
tim: PhantomData ,
69
- }
55
+ } ;
56
+ opm. set_pulse( pulse) ;
57
+ opm
70
58
}
71
59
72
60
impl Opm <$TIMX> {
61
+ pub fn set_pulse( & mut self , pulse: MicroSecond ) {
62
+ let cycles_per_period = self . clk / pulse. into( ) ;
63
+ let psc = ( cycles_per_period - 1 ) / 0xffff ;
64
+ let freq = ( self . clk. 0 / ( psc + 1 ) ) . hz( ) ;
65
+ let reload = pulse. cycles( freq) ;
66
+ unsafe {
67
+ let tim = & * $TIMX:: ptr( ) ;
68
+ tim. psc. write( |w| w. psc( ) . bits( psc as u16 ) ) ;
69
+ tim. arr. write( |w| w. $arr( ) . bits( reload as u16 ) ) ;
70
+ $(
71
+ tim. arr. modify( |_, w| w. $arr_h( ) . bits( ( reload >> 16 ) as u16 ) ) ;
72
+ ) *
73
+ }
74
+ }
75
+
73
76
pub fn generate( & mut self ) {
74
77
let tim = unsafe { & * $TIMX:: ptr( ) } ;
75
78
tim. cr1. write( |w| w. opm( ) . set_bit( ) . cen( ) . set_bit( ) ) ;
@@ -96,20 +99,19 @@ macro_rules! opm_hal {
96
99
tim. ccer. modify( |_, w| w. $ccxe( ) . clear_bit( ) ) ;
97
100
}
98
101
99
- pub fn set_delay( & mut self , delay: MicroSecond ) {
102
+ pub fn get_max_delay( & mut self ) -> u32 {
103
+ unsafe { ( * $TIMX:: ptr( ) ) . arr. read( ) . bits( ) }
104
+ }
105
+
106
+ pub fn set_delay( & mut self , delay: u32 ) {
100
107
self . delay = delay;
101
108
self . setup( ) ;
102
109
}
103
110
104
111
fn setup( & mut self ) {
105
- let tim = unsafe { & * $TIMX:: ptr( ) } ;
106
- let compare = if self . delay. 0 > 0 {
107
- self . delay. cycles( self . clk)
108
- } else {
109
- 1
110
- } ;
111
112
unsafe {
112
- tim. $ccrx. write( |w| w. bits( compare) ) ;
113
+ let tim = & * $TIMX:: ptr( ) ;
114
+ tim. $ccrx. write( |w| w. bits( self . delay) ) ;
113
115
tim. $ccmrx_output( ) . modify( |_, w| w. $ocxm( ) . bits( 7 ) . $ocxfe( ) . set_bit( ) ) ;
114
116
}
115
117
}
0 commit comments