@@ -25,7 +25,6 @@ use embedded_dma::*;
25
25
#[ derive( Debug ) ]
26
26
pub struct Pwm < T : Instance > {
27
27
pwm : T ,
28
- duty : RefCell < [ u16 ; 4 ] > ,
29
28
}
30
29
31
30
impl < T > Pwm < T >
35
34
/// Takes ownership of the peripheral and applies sane defaults.
36
35
pub fn new ( pwm : T ) -> Pwm < T > {
37
36
compiler_fence ( Ordering :: SeqCst ) ;
38
- let duty = RefCell :: new ( [ 0u16 ; 4 ] ) ;
39
37
pwm. enable . write ( |w| w. enable ( ) . enabled ( ) ) ;
40
38
pwm. mode . write ( |w| w. updown ( ) . up ( ) ) ;
41
39
pwm. prescaler . write ( |w| w. prescaler ( ) . div_1 ( ) ) ;
51
49
pwm. seq1 . refresh . write ( |w| unsafe { w. bits ( 0 ) } ) ;
52
50
pwm. seq1 . enddelay . write ( |w| unsafe { w. bits ( 0 ) } ) ;
53
51
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 }
64
53
}
65
54
66
55
/// Sets the PWM clock prescaler.
@@ -274,7 +263,7 @@ where
274
263
// Internal helper function that returns 15 bit duty cycle value.
275
264
#[ inline( always) ]
276
265
fn duty_on_value ( & self , index : usize ) -> u16 {
277
- let val = self . duty . borrow ( ) [ index] ;
266
+ let val = T :: buffer ( ) . borrow ( ) [ index] ;
278
267
let is_inverted = ( val >> 15 ) & 1 == 0 ;
279
268
match is_inverted {
280
269
false => val,
@@ -285,7 +274,7 @@ where
285
274
// Internal helper function that returns 15 bit inverted duty cycle value.
286
275
#[ inline( always) ]
287
276
fn duty_off_value ( & self , index : usize ) -> u16 {
288
- let val = self . duty . borrow ( ) [ index] ;
277
+ let val = T :: buffer ( ) . borrow ( ) [ index] ;
289
278
let is_inverted = ( val >> 15 ) & 1 == 0 ;
290
279
match is_inverted {
291
280
false => self . max_duty ( ) - val,
@@ -297,15 +286,15 @@ where
297
286
/// Will replace any ongoing sequence playback.
298
287
pub fn set_duty_on_common ( & self , duty : u16 ) {
299
288
compiler_fence ( Ordering :: SeqCst ) ;
300
- self . duty
289
+ T :: buffer ( )
301
290
. borrow_mut ( )
302
291
. copy_from_slice ( & [ duty. min ( self . max_duty ( ) ) & 0x7FFF ; 4 ] [ ..] ) ;
303
292
self . one_shot ( ) ;
304
293
self . set_load_mode ( LoadMode :: Common ) ;
305
294
self . pwm
306
295
. seq0
307
296
. 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 ) } ) ;
309
298
self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 1 ) } ) ;
310
299
self . start_seq ( Seq :: Seq0 ) ;
311
300
}
@@ -314,15 +303,15 @@ where
314
303
/// Will replace any ongoing sequence playback.
315
304
pub fn set_duty_off_common ( & self , duty : u16 ) {
316
305
compiler_fence ( Ordering :: SeqCst ) ;
317
- self . duty
306
+ T :: buffer ( )
318
307
. borrow_mut ( )
319
308
. copy_from_slice ( & [ duty. min ( self . max_duty ( ) ) | 0x8000 ; 4 ] [ ..] ) ;
320
309
self . one_shot ( ) ;
321
310
self . set_load_mode ( LoadMode :: Common ) ;
322
311
self . pwm
323
312
. seq0
324
313
. 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 ) } ) ;
326
315
self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 1 ) } ) ;
327
316
self . start_seq ( Seq :: Seq0 ) ;
328
317
}
@@ -343,13 +332,13 @@ where
343
332
/// Will replace any ongoing sequence playback.
344
333
pub fn set_duty_on_group ( & self , group : Group , duty : u16 ) {
345
334
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 ;
347
336
self . one_shot ( ) ;
348
337
self . set_load_mode ( LoadMode :: Grouped ) ;
349
338
self . pwm
350
339
. seq0
351
340
. 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 ) } ) ;
353
342
self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 2 ) } ) ;
354
343
self . start_seq ( Seq :: Seq0 ) ;
355
344
}
@@ -358,13 +347,13 @@ where
358
347
/// Will replace any ongoing sequence playback.
359
348
pub fn set_duty_off_group ( & self , group : Group , duty : u16 ) {
360
349
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 ;
362
351
self . one_shot ( ) ;
363
352
self . set_load_mode ( LoadMode :: Grouped ) ;
364
353
self . pwm
365
354
. seq0
366
355
. 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 ) } ) ;
368
357
self . pwm . seq0 . cnt . write ( |w| unsafe { w. bits ( 2 ) } ) ;
369
358
self . start_seq ( Seq :: Seq0 ) ;
370
359
}
@@ -385,10 +374,10 @@ where
385
374
/// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
386
375
pub fn set_duty_on ( & self , channel : Channel , duty : u16 ) {
387
376
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 ;
389
378
self . one_shot ( ) ;
390
379
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 ( ) {
392
381
self . start_seq ( Seq :: Seq0 ) ;
393
382
}
394
383
}
@@ -397,10 +386,10 @@ where
397
386
/// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
398
387
pub fn set_duty_off ( & self , channel : Channel , duty : u16 ) {
399
388
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 ;
401
390
self . one_shot ( ) ;
402
391
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 ( ) {
404
393
self . start_seq ( Seq :: Seq0 ) ;
405
394
}
406
395
}
@@ -507,6 +496,7 @@ where
507
496
#[ inline( always) ]
508
497
pub fn start_seq ( & self , seq : Seq ) {
509
498
compiler_fence ( Ordering :: SeqCst ) ;
499
+ self . pwm . enable . write ( |w| w. enable ( ) . enabled ( ) ) ;
510
500
self . pwm . tasks_seqstart [ usize:: from ( seq) ] . write ( |w| w. tasks_seqstart ( ) . set_bit ( ) ) ;
511
501
while self . pwm . events_seqstarted [ usize:: from ( seq) ] . read ( ) . bits ( ) == 0 { }
512
502
self . pwm . events_seqend [ 0 ] . write ( |w| w) ;
@@ -966,22 +956,45 @@ pub enum Error {
966
956
967
957
pub trait Instance : private:: Sealed + Deref < Target = crate :: pac:: pwm0:: RegisterBlock > {
968
958
const INTERRUPT : Interrupt ;
959
+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > ;
969
960
}
970
961
962
+ static mut BUF0 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
971
963
impl Instance for PWM0 {
972
964
const INTERRUPT : Interrupt = Interrupt :: PWM0 ;
965
+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
966
+ unsafe { & BUF0 }
967
+ }
973
968
}
969
+
970
+ #[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
971
+ static mut BUF1 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
974
972
#[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
975
973
impl Instance for PWM1 {
976
974
const INTERRUPT : Interrupt = Interrupt :: PWM1 ;
975
+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
976
+ unsafe { & BUF1 }
977
+ }
977
978
}
979
+
980
+ #[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
981
+ static mut BUF2 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
978
982
#[ cfg( not( any( feature = "52810" , feature = "52811" ) ) ) ]
979
983
impl Instance for PWM2 {
980
984
const INTERRUPT : Interrupt = Interrupt :: PWM2 ;
985
+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
986
+ unsafe { & BUF2 }
987
+ }
981
988
}
989
+
990
+ #[ cfg( not( any( feature = "52810" , feature = "52811" , feature = "52832" ) ) ) ]
991
+ static mut BUF3 : RefCell < [ u16 ; 4 ] > = RefCell :: new ( [ 0 ; 4 ] ) ;
982
992
#[ cfg( not( any( feature = "52810" , feature = "52811" , feature = "52832" ) ) ) ]
983
993
impl Instance for PWM3 {
984
994
const INTERRUPT : Interrupt = Interrupt :: PWM3 ;
995
+ fn buffer ( ) -> & ' static RefCell < [ u16 ; 4 ] > {
996
+ unsafe { & BUF3 }
997
+ }
985
998
}
986
999
987
1000
mod private {
0 commit comments