Skip to content

Commit 4a46c56

Browse files
committed
cleaned up PWM rustdoc
1 parent dcf6c1e commit 4a46c56

File tree

1 file changed

+83
-16
lines changed

1 file changed

+83
-16
lines changed

nrf-hal-common/src/pwm.rs

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ where
7878
}
7979
}
8080

81-
/// Sets the maximum duty cycle value.
81+
/// Sets the maximum duty cycle value. Values above `32767`
82+
/// will be reduced to `32767`.
8283
#[inline(always)]
8384
pub fn set_max_duty(&self, duty: u16) -> &Self {
8485
self.pwm
@@ -92,7 +93,16 @@ where
9293
self.pwm.countertop.read().countertop().bits()
9394
}
9495

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.
96106
#[inline(always)]
97107
pub fn set_period(&self, freq: Hertz) -> &Self {
98108
let duty = match self.prescaler() {
@@ -112,7 +122,11 @@ where
112122
self
113123
}
114124

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.
116130
#[inline(always)]
117131
pub fn period(&self) -> Hertz {
118132
let max_duty = self.max_duty() as u32;
@@ -174,6 +188,9 @@ where
174188
let psel = &self.pwm.psel.out[usize::from(channel)];
175189
let old = psel.read();
176190
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.
177194
unsafe { Some(Pin::from_psel_bits(old.bits())) }
178195
} else {
179196
None
@@ -240,10 +257,14 @@ where
240257
self
241258
}
242259

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.
244264
#[inline(always)]
245265
pub fn set_load_mode(&self, mode: LoadMode) -> &Self {
246266
self.pwm.decoder.modify(|_r, w| w.load().bits(mode.into()));
267+
// Why?
247268
if mode == LoadMode::Waveform {
248269
self.disable_channel(Channel::C3);
249270
} else {
@@ -252,7 +273,8 @@ where
252273
self
253274
}
254275

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.
256278
#[inline(always)]
257279
pub fn load_mode(&self) -> LoadMode {
258280
match self.pwm.decoder.read().load().bits() {
@@ -264,14 +286,14 @@ where
264286
}
265287
}
266288

267-
/// Selects operating mode of the wave counter.
289+
/// Selects operating counter mode of the wave counter.
268290
#[inline(always)]
269291
pub fn set_counter_mode(&self, mode: CounterMode) -> &Self {
270292
self.pwm.mode.write(|w| w.updown().bit(mode.into()));
271293
self
272294
}
273295

274-
/// Returns selected operating mode of the wave counter.
296+
/// Returns selected operating counter mode of the wave counter.
275297
#[inline(always)]
276298
pub fn counter_mode(&self) -> CounterMode {
277299
match self.pwm.mode.read().updown().bit() {
@@ -320,6 +342,10 @@ where
320342

321343
/// Sets duty cycle (15 bit) for all PWM channels.
322344
/// 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.
323349
pub fn set_duty_on_common(&self, duty: u16) {
324350
let buffer = T::buffer();
325351
unsafe {
@@ -337,6 +363,10 @@ where
337363

338364
/// Sets inverted duty cycle (15 bit) for all PWM channels.
339365
/// 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.
340370
pub fn set_duty_off_common(&self, duty: u16) {
341371
let buffer = T::buffer();
342372
unsafe {
@@ -352,7 +382,8 @@ where
352382
self.start_seq(Seq::Seq0);
353383
}
354384

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.
356387
#[inline(always)]
357388
pub fn duty_on_common(&self) -> u16 {
358389
self.duty_on_value(0)
@@ -366,6 +397,10 @@ where
366397

367398
/// Sets duty cycle (15 bit) for a PWM group.
368399
/// 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.
369404
pub fn set_duty_on_group(&self, group: Group, duty: u16) {
370405
let buffer = T::buffer();
371406
unsafe {
@@ -383,6 +418,10 @@ where
383418

384419
/// Sets inverted duty cycle (15 bit) for a PWM group.
385420
/// 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.
386425
pub fn set_duty_off_group(&self, group: Group, duty: u16) {
387426
let buffer = T::buffer();
388427
unsafe {
@@ -411,7 +450,14 @@ where
411450
}
412451

413452
/// 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.
415461
pub fn set_duty_on(&self, channel: Channel, duty: u16) {
416462
let buffer = T::buffer();
417463
unsafe {
@@ -428,7 +474,14 @@ where
428474
}
429475

430476
/// 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.
432485
pub fn set_duty_off(&self, channel: Channel, duty: u16) {
433486
let buffer = T::buffer();
434487
unsafe {
@@ -491,7 +544,8 @@ where
491544
self
492545
}
493546

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.
495549
#[inline(always)]
496550
pub fn set_seq_refresh(&self, seq: Seq, periods: u32) -> &Self {
497551
match seq {
@@ -511,7 +565,9 @@ where
511565
self
512566
}
513567

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+
///
515571
/// Causes PWM generation to start if not running.
516572
#[inline(always)]
517573
pub fn start_seq(&self, seq: Seq) {
@@ -523,23 +579,32 @@ where
523579
self.pwm.events_seqend[1].write(|w| w);
524580
}
525581

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+
///
527586
/// Does not cause PWM generation to start if not running.
528587
#[inline(always)]
529588
pub fn next_step(&self) {
530589
self.pwm.tasks_nextstep.write(|w| unsafe { w.bits(1) });
531590
}
532591

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.
534595
#[inline(always)]
535596
pub fn stop(&self) {
536597
compiler_fence(Ordering::SeqCst);
537598
self.pwm.tasks_stop.write(|w| unsafe { w.bits(1) });
538599
while self.pwm.events_stopped.read().bits() == 0 {}
539600
}
540601

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.
543608
#[allow(unused_mut)]
544609
pub fn load<B0, B1>(
545610
mut self,
@@ -756,6 +821,8 @@ where
756821
}
757822

758823
/// Consumes `self` and returns back the raw peripheral.
824+
/// (This functionality is often named `into_inner()` in
825+
/// other Rust code.)
759826
pub fn free(self) -> (T, Pins) {
760827
let ch0 = self.pwm.psel.out[0].read();
761828
let ch1 = self.pwm.psel.out[1].read();

0 commit comments

Comments
 (0)