fw-rust: Create a PWM signal for the backlight

To have a dimmable backlight, generate a variable duty-cycle PWM
signal which is consumed by the backlight driver circuit.
This commit is contained in:
finga 2022-03-04 12:59:51 +01:00
parent d2772291bf
commit aa59bc302d
3 changed files with 29 additions and 8 deletions

View file

@ -66,6 +66,7 @@ name = "clock-generator"
version = "0.1.0-dev"
dependencies = [
"atmega-hal",
"avr-device",
"embedded-hal",
"nb 1.0.0",
"panic-halt",

View file

@ -14,6 +14,7 @@ bench = false
panic-halt = "0.2"
nb = "1.0"
embedded-hal = "0.2"
avr-device = { version = "0.3", features = ["atmega328p"] }
[dependencies.atmega-hal]
git = "https://github.com/rahix/avr-hal"

View file

@ -1,20 +1,39 @@
#![no_std]
#![no_main]
use atmega_hal::{clock::MHz8, delay::Delay, pins, Peripherals};
use embedded_hal::blocking::delay::DelayMs;
use atmega_hal::{pins, Peripherals};
use avr_device::interrupt;
use panic_halt as _;
#[atmega_hal::entry]
fn main() -> ! {
// Disable interrupts when initializing
interrupt::disable();
// Get peripherals and pins
let dp = Peripherals::take().unwrap();
let pins = pins!(dp);
let mut led = pins.pd5.into_output();
let mut delay = Delay::<MHz8>::new();
// Init display backlight
let tc0 = dp.TC0;
tc0.tccr0a.write(|w| {
w.wgm0().pwm_fast();
w.com0b().match_clear();
w
});
tc0.tccr0b.write(|w| {
w.wgm02().set_bit();
w.cs0().prescale_64();
w
});
tc0.ocr0a.write(|w| unsafe { w.bits(255) });
// TODO: Use EEPROM
tc0.ocr0b.write(|w| unsafe { w.bits(0) });
pins.pd5.into_output();
loop {
led.toggle();
delay.delay_ms(255_u8);
}
// Enable interrupts
unsafe { interrupt::enable() };
#[allow(clippy::empty_loop)]
loop {}
}