78
78
}
79
79
}
80
80
81
- /// Sets the maximum duty cycle value.
81
+ /// Sets the maximum duty cycle value. Values above `32767`
82
+ /// will be reduced to `32767`.
82
83
#[ inline( always) ]
83
84
pub fn set_max_duty ( & self , duty : u16 ) -> & Self {
84
85
self . pwm
92
93
self . pwm . countertop . read ( ) . countertop ( ) . bits ( )
93
94
}
94
95
95
- /// Sets the PWM output frequency.
96
+ /// Sets the PWM output period from the given input
97
+ /// frequency. (This calculates the period by dividing
98
+ /// the current PWM clock rate by `freq`.)
99
+ ///
100
+ /// This calculation takes the current [Prescaler]
101
+ /// setting into account.
102
+ ///
103
+ /// Subsequent use of `set_duty_on_common()`,
104
+ /// `set_duty_on_group()`, *etc* will override this duty
105
+ /// cycle setting.
96
106
#[ inline( always) ]
97
107
pub fn set_period ( & self , freq : Hertz ) -> & Self {
98
108
let duty = match self . prescaler ( ) {
@@ -112,7 +122,11 @@ where
112
122
self
113
123
}
114
124
115
- /// Returns the PWM output frequency.
125
+ /// Returns the PWM output frequency (in spite of the name).
126
+ ///
127
+ /// The frequency calculation divides the current PWM
128
+ /// clock by the PWM period in ticks. The PWM period
129
+ /// takes the current [Prescaler] setting into account.
116
130
#[ inline( always) ]
117
131
pub fn period ( & self ) -> Hertz {
118
132
let max_duty = self . max_duty ( ) as u32 ;
@@ -174,6 +188,9 @@ where
174
188
let psel = & self . pwm . psel . out [ usize:: from ( channel) ] ;
175
189
let old = psel. read ( ) ;
176
190
let old = if old. connect ( ) . is_connected ( ) {
191
+ // Safety: We owned the pin previously, and
192
+ // are about to give up that ownership. Thus
193
+ // the newly-constructed pin must be unique.
177
194
unsafe { Some ( Pin :: from_psel_bits ( old. bits ( ) ) ) }
178
195
} else {
179
196
None
@@ -240,10 +257,14 @@ where
240
257
self
241
258
}
242
259
243
- /// Cofigures how a sequence is read from RAM and is spread to the compare register.
260
+ /// Configures how a sequence is read from RAM and is
261
+ /// spread to the compare register. Disables
262
+ /// [Channel::C3] for [LoadMode::Waveform], and disables
263
+ /// it otherwise.
244
264
#[ inline( always) ]
245
265
pub fn set_load_mode ( & self , mode : LoadMode ) -> & Self {
246
266
self . pwm . decoder . modify ( |_r, w| w. load ( ) . bits ( mode. into ( ) ) ) ;
267
+ // Why?
247
268
if mode == LoadMode :: Waveform {
248
269
self . disable_channel ( Channel :: C3 ) ;
249
270
} else {
@@ -252,7 +273,8 @@ where
252
273
self
253
274
}
254
275
255
- /// Returns how a sequence is read from RAM and is spread to the compare register.
276
+ /// Returns how a sequence is read from RAM and is
277
+ /// spread to the compare register.
256
278
#[ inline( always) ]
257
279
pub fn load_mode ( & self ) -> LoadMode {
258
280
match self . pwm . decoder . read ( ) . load ( ) . bits ( ) {
@@ -264,14 +286,14 @@ where
264
286
}
265
287
}
266
288
267
- /// Selects operating mode of the wave counter.
289
+ /// Selects operating counter mode of the wave counter.
268
290
#[ inline( always) ]
269
291
pub fn set_counter_mode ( & self , mode : CounterMode ) -> & Self {
270
292
self . pwm . mode . write ( |w| w. updown ( ) . bit ( mode. into ( ) ) ) ;
271
293
self
272
294
}
273
295
274
- /// Returns selected operating mode of the wave counter.
296
+ /// Returns selected operating counter mode of the wave counter.
275
297
#[ inline( always) ]
276
298
pub fn counter_mode ( & self ) -> CounterMode {
277
299
match self . pwm . mode . read ( ) . updown ( ) . bit ( ) {
@@ -320,6 +342,10 @@ where
320
342
321
343
/// Sets duty cycle (15 bit) for all PWM channels.
322
344
/// Will replace any ongoing sequence playback.
345
+ ///
346
+ /// Subsequent use of `set_frequency()`,
347
+ /// `set_duty_on_group()`, *etc* will override this duty
348
+ /// cycle setting.
323
349
pub fn set_duty_on_common ( & self , duty : u16 ) {
324
350
let buffer = T :: buffer ( ) ;
325
351
unsafe {
@@ -337,6 +363,10 @@ where
337
363
338
364
/// Sets inverted duty cycle (15 bit) for all PWM channels.
339
365
/// Will replace any ongoing sequence playback.
366
+ ///
367
+ /// Subsequent use of `set_frequency()`,
368
+ /// `set_duty_on_common()`, *etc* will override this duty
369
+ /// cycle setting.
340
370
pub fn set_duty_off_common ( & self , duty : u16 ) {
341
371
let buffer = T :: buffer ( ) ;
342
372
unsafe {
@@ -352,7 +382,8 @@ where
352
382
self . start_seq ( Seq :: Seq0 ) ;
353
383
}
354
384
355
- /// Returns the common duty cycle value for all PWM channels in `Common` load mode.
385
+ /// Returns the common duty cycle value for all PWM
386
+ /// channels in `Common` load mode.
356
387
#[ inline( always) ]
357
388
pub fn duty_on_common ( & self ) -> u16 {
358
389
self . duty_on_value ( 0 )
@@ -366,6 +397,10 @@ where
366
397
367
398
/// Sets duty cycle (15 bit) for a PWM group.
368
399
/// Will replace any ongoing sequence playback.
400
+ ///
401
+ /// Subsequent use of `set_frequency()`,
402
+ /// `set_duty_on_common()`, *etc* will override this duty
403
+ /// cycle setting.
369
404
pub fn set_duty_on_group ( & self , group : Group , duty : u16 ) {
370
405
let buffer = T :: buffer ( ) ;
371
406
unsafe {
@@ -383,6 +418,10 @@ where
383
418
384
419
/// Sets inverted duty cycle (15 bit) for a PWM group.
385
420
/// Will replace any ongoing sequence playback.
421
+ ///
422
+ /// Subsequent use of `set_frequency()`,
423
+ /// `set_duty_on_group()`, *etc* will override this duty
424
+ /// cycle setting.
386
425
pub fn set_duty_off_group ( & self , group : Group , duty : u16 ) {
387
426
let buffer = T :: buffer ( ) ;
388
427
unsafe {
@@ -411,7 +450,14 @@ where
411
450
}
412
451
413
452
/// Sets duty cycle (15 bit) for a PWM channel.
414
- /// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
453
+ ///
454
+ /// Will replace any ongoing sequence playback and the
455
+ /// other channels will return to their previously set
456
+ /// value.
457
+ ///
458
+ /// Subsequent use of `set_frequency()`,
459
+ /// `set_duty_on_group()`, *etc* will override this duty
460
+ /// cycle setting.
415
461
pub fn set_duty_on ( & self , channel : Channel , duty : u16 ) {
416
462
let buffer = T :: buffer ( ) ;
417
463
unsafe {
@@ -428,7 +474,14 @@ where
428
474
}
429
475
430
476
/// Sets inverted duty cycle (15 bit) for a PWM channel.
431
- /// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
477
+ ///
478
+ /// Will replace any ongoing sequence playback and the
479
+ /// other channels will return to their previously set
480
+ /// value.
481
+ ///
482
+ /// Subsequent use of `set_frequency()`,
483
+ /// `set_duty_on_group()`, *etc* will override this duty
484
+ /// cycle setting.
432
485
pub fn set_duty_off ( & self , channel : Channel , duty : u16 ) {
433
486
let buffer = T :: buffer ( ) ;
434
487
unsafe {
@@ -491,7 +544,8 @@ where
491
544
self
492
545
}
493
546
494
- /// Sets number of additional PWM periods between samples loaded into compare register.
547
+ /// Sets number of additional PWM periods between
548
+ /// samples loaded into compare register.
495
549
#[ inline( always) ]
496
550
pub fn set_seq_refresh ( & self , seq : Seq , periods : u32 ) -> & Self {
497
551
match seq {
@@ -511,7 +565,9 @@ where
511
565
self
512
566
}
513
567
514
- /// Loads the first PWM value on all enabled channels from a sequence and starts playing that sequence.
568
+ /// Loads the first PWM value on all enabled channels
569
+ /// from a sequence and starts playing that sequence.
570
+ ///
515
571
/// Causes PWM generation to start if not running.
516
572
#[ inline( always) ]
517
573
pub fn start_seq ( & self , seq : Seq ) {
@@ -523,23 +579,32 @@ where
523
579
self . pwm . events_seqend [ 1 ] . write ( |w| w) ;
524
580
}
525
581
526
- /// Steps by one value in the current sequence on all enabled channels, if the `NextStep` step mode is selected.
582
+ /// Steps by one value in the current sequence on all
583
+ /// enabled channels, if the `NextStep` step mode is
584
+ /// selected.
585
+ ///
527
586
/// Does not cause PWM generation to start if not running.
528
587
#[ inline( always) ]
529
588
pub fn next_step ( & self ) {
530
589
self . pwm . tasks_nextstep . write ( |w| unsafe { w. bits ( 1 ) } ) ;
531
590
}
532
591
533
- /// Stops PWM pulse generation on all channels at the end of current PWM period, and stops sequence playback.
592
+ /// Stops PWM pulse generation on all channels at the
593
+ /// end of current PWM period, and stops sequence
594
+ /// playback.
534
595
#[ inline( always) ]
535
596
pub fn stop ( & self ) {
536
597
compiler_fence ( Ordering :: SeqCst ) ;
537
598
self . pwm . tasks_stop . write ( |w| unsafe { w. bits ( 1 ) } ) ;
538
599
while self . pwm . events_stopped . read ( ) . bits ( ) == 0 { }
539
600
}
540
601
541
- /// Loads the given sequence buffers and optionally (re-)starts sequence playback.
542
- /// Returns a `PemSeq`, containing `Pwm<T>` and the buffers.
602
+ /// Loads the given sequence buffers and optionally
603
+ /// (re-)starts sequence playback.
604
+ ///
605
+ /// On success, returns a `PwmSeq` containing `Pwm<T>`
606
+ /// and the buffers. This can be used for later buffer
607
+ /// loading and PWM control.
543
608
#[ allow( unused_mut) ]
544
609
pub fn load < B0 , B1 > (
545
610
mut self ,
@@ -756,6 +821,8 @@ where
756
821
}
757
822
758
823
/// Consumes `self` and returns back the raw peripheral.
824
+ /// (This functionality is often named `into_inner()` in
825
+ /// other Rust code.)
759
826
pub fn free ( self ) -> ( T , Pins ) {
760
827
let ch0 = self . pwm . psel . out [ 0 ] . read ( ) ;
761
828
let ch1 = self . pwm . psel . out [ 1 ] . read ( ) ;
0 commit comments