From aa59bc302dedfcb2904510b6381ee89250e86374 Mon Sep 17 00:00:00 2001 From: finga Date: Fri, 4 Mar 2022 12:59:51 +0100 Subject: [PATCH] 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. --- firmware/rust/Cargo.lock | 1 + firmware/rust/Cargo.toml | 1 + firmware/rust/src/main.rs | 35 +++++++++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/firmware/rust/Cargo.lock b/firmware/rust/Cargo.lock index 068b887..c856d14 100644 --- a/firmware/rust/Cargo.lock +++ b/firmware/rust/Cargo.lock @@ -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", diff --git a/firmware/rust/Cargo.toml b/firmware/rust/Cargo.toml index d86a122..aee0597 100644 --- a/firmware/rust/Cargo.toml +++ b/firmware/rust/Cargo.toml @@ -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" diff --git a/firmware/rust/src/main.rs b/firmware/rust/src/main.rs index cc4d159..88c8d5f 100644 --- a/firmware/rust/src/main.rs +++ b/firmware/rust/src/main.rs @@ -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::::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 {} }