pub trait Pwm {
type Pin;
// Required methods
fn start(
&self,
pin: &Self::Pin,
frequency_hz: usize,
duty_cycle: usize,
) -> Result<(), ErrorCode>;
fn stop(&self, pin: &Self::Pin) -> Result<(), ErrorCode>;
fn get_maximum_frequency_hz(&self) -> usize;
fn get_maximum_duty_cycle(&self) -> usize;
}
Expand description
PWM control for a single pin.
Required Associated Types§
Required Methods§
Sourcefn start(
&self,
pin: &Self::Pin,
frequency_hz: usize,
duty_cycle: usize,
) -> Result<(), ErrorCode>
fn start( &self, pin: &Self::Pin, frequency_hz: usize, duty_cycle: usize, ) -> Result<(), ErrorCode>
Generate a PWM signal on the given pin at the given frequency and duty cycle.
frequency_hz
is specified in Hertz.duty_cycle
is specified as a portion of the max duty cycle supported by the chip. Clients should callget_maximum_duty_cycle()
to get the value that corresponds to 100% duty cycle, and divide that appropriately to get the desired duty cycle value. For example, a 25% duty cycle would bePWM0.get_maximum_duty_cycle() / 4
.
Sourcefn get_maximum_frequency_hz(&self) -> usize
fn get_maximum_frequency_hz(&self) -> usize
Return the maximum PWM frequency supported by the PWM implementation. The frequency will be specified in Hertz.
Sourcefn get_maximum_duty_cycle(&self) -> usize
fn get_maximum_duty_cycle(&self) -> usize
Return an opaque number that represents a 100% duty cycle. This value will be hardware specific, and essentially represents the precision of the underlying PWM hardware.
Users of this HIL should divide this number to calculate a duty cycle
value suitable for calling start()
. For example, to generate a 50%
duty cycle:
ⓘ
let max = PWM0.get_maximum_duty_cycle();
let dc = max / 2;
PWM0.start(pin, freq, dc);