riscv-rust / e310x-hal

Implementation of the `embedded-hal` traits for E310x microcontrollers
17 stars 18 forks source link

Add PWM driver #47

Closed timbz closed 1 year ago

timbz commented 1 year ago

Basic PWM implementation. Tested on Hifive1 rev 1. Should also work on rev B.

I am new to Rust and embedded hal. Let me know if this is the correct way of doing things. References:

Sample code that fades the green led:

    let mut pwm1 = Pwm::new(p.PWM1);
    pwm1.set_period(u16::MAX);
    let pwm1_1 = Channel::from(pin!(pins, dig3).into_iof1());

    type Duty = u16;
    const DELAY: u32 = 30;
    const INCREMENT: Duty = Duty::MAX / 51;
    let mut duty: Duty = 0;
    let mut increment = true;

    loop {
        if increment {
            duty = duty.saturating_add(INCREMENT);
        } else {
            duty = duty.saturating_sub(INCREMENT);
        }
        if duty == 0 || duty == Duty::MAX {
            increment = !increment;
        }
        pwm1.set_duty(pwm1_1, duty);
        sleep.delay_ms(DELAY);
    }
almindor commented 1 year ago

@timbz looking great thanks! Please fix formatting (just run cargo fmt over and push that). I'll do a deeper dive over the weekend or so.

timbz commented 1 year ago

@timbz looking great thanks! Please fix formatting (just run cargo fmt over and push that). I'll do a deeper dive over the weekend or so.

done

almindor commented 1 year ago

@timbz looking great thanks! Please fix formatting (just run cargo fmt over and push that). I'll do a deeper dive over the weekend or so.

done

Sorry, one more nit, it needs a changelog entry (unrelased section). All good otherwise I'll merge as soon as that's done.

timbz commented 1 year ago

needs a CHANGELOG entry

done