Trait kernel::hil::pwm::Pwm

source ·
pub trait Pwm {
    type Pin;

    // Required methods
    fn start(
        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§


type Pin

The chip-dependent type of a PWM pin.

Required Methods§


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 call get_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 be PWM0.get_maximum_duty_cycle() / 4.

fn stop(&self, pin: &Self::Pin) -> Result<(), ErrorCode>

Stop a PWM pin output.


fn get_maximum_frequency_hz(&self) -> usize

Return the maximum PWM frequency supported by the PWM implementation. The frequency will be specified in Hertz.


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);