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" version = "0.1.0-dev"
dependencies = [ dependencies = [
"atmega-hal", "atmega-hal",
"avr-device",
"embedded-hal", "embedded-hal",
"nb 1.0.0", "nb 1.0.0",
"panic-halt", "panic-halt",

View file

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

View file

@ -1,20 +1,39 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
use atmega_hal::{clock::MHz8, delay::Delay, pins, Peripherals}; use atmega_hal::{pins, Peripherals};
use embedded_hal::blocking::delay::DelayMs; use avr_device::interrupt;
use panic_halt as _; use panic_halt as _;
#[atmega_hal::entry] #[atmega_hal::entry]
fn main() -> ! { fn main() -> ! {
// Disable interrupts when initializing
interrupt::disable();
// Get peripherals and pins
let dp = Peripherals::take().unwrap(); let dp = Peripherals::take().unwrap();
let pins = pins!(dp); let pins = pins!(dp);
let mut led = pins.pd5.into_output(); // Init display backlight
let mut delay = Delay::<MHz8>::new(); 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 { // Enable interrupts
led.toggle(); unsafe { interrupt::enable() };
delay.delay_ms(255_u8);
} #[allow(clippy::empty_loop)]
loop {}
} }