@@ -25,7 +25,6 @@ use embedded_dma::*;
2525#[ derive( Debug ) ]
2626pub struct Pwm < T : Instance > {
2727 pwm : T ,
28- duty : RefCell < [ u16 ; 4 ] > ,
2928}
3029
3130impl < T > Pwm < T >
3534 /// Takes ownership of the peripheral and applies sane defaults.
3635 pub fn new ( pwm : T ) -> Pwm < T > {
3736 compiler_fence ( Ordering :: SeqCst ) ;
38- let duty = RefCell :: new ( [ 0u16 ; 4 ] ) ;
3937 pwm. enable . write ( |w| w. enable ( ) . enabled ( ) ) ;
4038 pwm. mode . write ( |w| w. updown ( ) . up ( ) ) ;
4139 pwm. prescaler . write ( |w| w. prescaler ( ) . div_1 ( ) ) ;
5149 pwm. seq1 . refresh . write ( |w| unsafe { w. bits ( 0 ) } ) ;
5250 pwm. seq1 . enddelay . write ( |w| unsafe { w. bits ( 0 ) } ) ;
5351
54- pwm. seq0
55- . ptr
56- . write ( |w| unsafe { w. bits ( duty. as_ptr ( ) as u32 ) } ) ;
57- pwm. seq0 . cnt . write ( |w| unsafe { w. bits ( 4 ) } ) ;
58- pwm. seq1
59- . ptr
60- . write ( |w| unsafe { w. bits ( duty. as_ptr ( ) as u32 ) } ) ;
61- pwm. seq1 . cnt . write ( |w| unsafe { w. bits ( 4 ) } ) ;
62-
63- Self { pwm, duty }
52+ Self { pwm }
6453 }
6554
6655 /// Sets the PWM clock prescaler.
@@ -274,7 +263,7 @@ where
274263 // Internal helper function that returns 15 bit duty cycle value.
275264 #[ inline( always) ]
276265 fn duty_on_value ( & self , index : usize ) -> u16 {
277- let val = self . duty . borrow ( ) [ index] ;
266+ let val = T :: buffer ( ) . borrow ( ) [ index] ;
278267 let is_inverted = ( val >> 15 ) & 1 == 0 ;
279268 match is_inverted {
280269 false => val,
@@ -285,7 +274,7 @@ where
285274 // Internal helper function that returns 15 bit inverted duty cycle value.
286275 #[ inline( always) ]
287276 fn duty_off_value ( & self , index : usize ) -> u16 {
288- let val = self . duty . borrow ( ) [ index] ;
277+ let val = T :: buffer ( ) . borrow ( ) [ index] ;
289278 let is_inverted = ( val >> 15 ) & 1 == 0 ;
290279 match is_inverted {
291280 false => self . max_duty ( ) - val,
@@ -297,15 +286,15 @@ where
297286 /// Will replace any ongoing sequence playback.
298287 pub fn set_duty_on_common ( & self , duty : u16 ) {
299288 compiler_fence ( Ordering :: SeqCst ) ;
300- self . duty
289+ T :: buffer ( )
301290 . borrow_mut ( )
302291 . copy_from_slice ( & [ duty. min ( self . max_duty ( ) ) & 0x7FFF ; 4 ] [ ..] ) ;
303292 self . one_shot ( ) ;
304293 self . set_load_mode ( LoadMode :: Common ) ;
305294 self . pwm
306295 . seq0
307296 . ptr
308- . write ( |w| unsafe { w. bits ( self . duty . as_ptr ( ) as u32 ) } ) ;
297+ . write ( |w| unsafe { w. bits ( T :: buffer ( ) . as_ptr ( ) as u32 ) } ) ;
309298 self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 1 ) } ) ;
310299 self . start_seq ( Seq :: Seq0 ) ;
311300 }
@@ -314,15 +303,15 @@ where
314303 /// Will replace any ongoing sequence playback.
315304 pub fn set_duty_off_common ( & self , duty : u16 ) {
316305 compiler_fence ( Ordering :: SeqCst ) ;
317- self . duty
306+ T :: buffer ( )
318307 . borrow_mut ( )
319308 . copy_from_slice ( & [ duty. min ( self . max_duty ( ) ) | 0x8000 ; 4 ] [ ..] ) ;
320309 self . one_shot ( ) ;
321310 self . set_load_mode ( LoadMode :: Common ) ;
322311 self . pwm
323312 . seq0
324313 . ptr
325- . write ( |w| unsafe { w. bits ( self . duty . as_ptr ( ) as u32 ) } ) ;
314+ . write ( |w| unsafe { w. bits ( T :: buffer ( ) . as_ptr ( ) as u32 ) } ) ;
326315 self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 1 ) } ) ;
327316 self . start_seq ( Seq :: Seq0 ) ;
328317 }
@@ -343,13 +332,13 @@ where
343332 /// Will replace any ongoing sequence playback.
344333 pub fn set_duty_on_group ( & self , group : Group , duty : u16 ) {
345334 compiler_fence ( Ordering :: SeqCst ) ;
346- self . duty . borrow_mut ( ) [ usize:: from ( group) ] = duty. min ( self . max_duty ( ) ) & 0x7FFF ;
335+ T :: buffer ( ) . borrow_mut ( ) [ usize:: from ( group) ] = duty. min ( self . max_duty ( ) ) & 0x7FFF ;
347336 self . one_shot ( ) ;
348337 self . set_load_mode ( LoadMode :: Grouped ) ;
349338 self . pwm
350339 . seq0
351340 . ptr
352- . write ( |w| unsafe { w. bits ( self . duty . as_ptr ( ) as u32 ) } ) ;
341+ . write ( |w| unsafe { w. bits ( T :: buffer ( ) . as_ptr ( ) as u32 ) } ) ;
353342 self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 2 ) } ) ;
354343 self . start_seq ( Seq :: Seq0 ) ;
355344 }
@@ -358,13 +347,13 @@ where
358347 /// Will replace any ongoing sequence playback.
359348 pub fn set_duty_off_group ( & self , group : Group , duty : u16 ) {
360349 compiler_fence ( Ordering :: SeqCst ) ;
361- self . duty . borrow_mut ( ) [ usize:: from ( group) ] = duty. min ( self . max_duty ( ) ) | 0x8000 ;
350+ T :: buffer ( ) . borrow_mut ( ) [ usize:: from ( group) ] = duty. min ( self . max_duty ( ) ) | 0x8000 ;
362351 self . one_shot ( ) ;
363352 self . set_load_mode ( LoadMode :: Grouped ) ;
364353 self . pwm
365354 . seq0
366355 . ptr
367- . write ( |w| unsafe { w. bits ( self . duty . as_ptr ( ) as u32 ) } ) ;
356+ . write ( |w| unsafe { w. bits ( T :: buffer ( ) . as_ptr ( ) as u32 ) } ) ;
368357 self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 2 ) } ) ;
369358 self . start_seq ( Seq :: Seq0 ) ;
370359 }
@@ -385,10 +374,10 @@ where
385374 /// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
386375 pub fn set_duty_on ( & self , channel : Channel , duty : u16 ) {
387376 compiler_fence ( Ordering :: SeqCst ) ;
388- self . duty . borrow_mut ( ) [ usize:: from ( channel) ] = duty. min ( self . max_duty ( ) ) & 0x7FFF ;
377+ T :: buffer ( ) . borrow_mut ( ) [ usize:: from ( channel) ] = duty. min ( self . max_duty ( ) ) & 0x7FFF ;
389378 self . one_shot ( ) ;
390379 self . set_load_mode ( LoadMode :: Individual ) ;
391- if self . load_seq ( Seq :: Seq0 , & * self . duty . borrow ( ) ) . is_ok ( ) {
380+ if self . load_seq ( Seq :: Seq0 , T :: buffer ( ) . borrow ( ) ) . is_ok ( ) {
392381 self . start_seq ( Seq :: Seq0 ) ;
393382 }
394383 }
@@ -397,10 +386,10 @@ where
397386 /// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
398387 pub fn set_duty_off ( & self , channel : Channel , duty : u16 ) {
399388 compiler_fence ( Ordering :: SeqCst ) ;
400- self . duty . borrow_mut ( ) [ usize:: from ( channel) ] = duty. min ( self . max_duty ( ) ) | 0x8000 ;
389+ T :: buffer ( ) . borrow_mut ( ) [ usize:: from ( channel) ] = duty. min ( self . max_duty ( ) ) | 0x8000 ;
401390 self . one_shot ( ) ;
402391 self . set_load_mode ( LoadMode :: Individual ) ;
403- if self . load_seq ( Seq :: Seq0 , & * self . duty . borrow ( ) ) . is_ok ( ) {
392+ if self . load_seq ( Seq :: Seq0 , T :: buffer ( ) . borrow ( ) ) . is_ok ( ) {
404393 self . start_seq ( Seq :: Seq0 ) ;
405394 }
406395 }
@@ -507,6 +496,7 @@ where
507496 #[ inline( always) ]
508497 pub fn start_seq ( & self , seq : Seq ) {
509498 compiler_fence ( Ordering :: SeqCst ) ;
499+ self . pwm . enable . write ( |w| w. enable ( ) . enabled ( ) ) ;
510500 self . pwm . tasks_seqstart [ usize:: from ( seq) ] . write ( |w| w. tasks_seqstart ( ) . set_bit ( ) ) ;
511501 while self . pwm . events_seqstarted [ usize:: from ( seq) ] . read ( ) . bits ( ) == 0 { }
512502 self . pwm . events_seqend [ 0 ] . write ( |w| w) ;
@@ -966,22 +956,45 @@ pub enum Error {
966956
967957pub trait Instance : private:: Sealed + Deref < Target = crate :: pac:: pwm0:: RegisterBlock > {
968958 const INTERRUPT : Interrupt ;
959+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > ;
969960}
970961
962+ static mut BUF0 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
971963impl Instance for PWM0 {
972964 const INTERRUPT : Interrupt = Interrupt :: PWM0 ;
965+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
966+ unsafe { & BUF0 }
967+ }
973968}
969+
970+ #[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
971+ static mut BUF1 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
974972#[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
975973impl Instance for PWM1 {
976974 const INTERRUPT : Interrupt = Interrupt :: PWM1 ;
975+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
976+ unsafe { & BUF1 }
977+ }
977978}
979+
980+ #[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
981+ static mut BUF2 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
978982#[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
979983impl Instance for PWM2 {
980984 const INTERRUPT : Interrupt = Interrupt :: PWM2 ;
985+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
986+ unsafe { & BUF2 }
987+ }
981988}
989+
990+ #[ cfg( not( any( feature = "52810" , feature = "52811" , feature = "52832" ) ) ) ]
991+ static mut BUF3 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
982992#[ cfg( not( any( feature = "52810" , feature = "52811" , feature = "52832" ) ) ) ]
983993impl Instance for PWM3 {
984994 const INTERRUPT : Interrupt = Interrupt :: PWM3 ;
995+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
996+ unsafe { & BUF3 }
997+ }
985998}
986999
9871000mod private {
0 commit comments