clock_generator/firmware/rust/src/main.rs
finga 518f113cc0
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
fw-rust: Use avr-eeprom create to read from EEPROM
Use the `avr-eeprom` crate when reading from EEPROM.
2022-03-12 17:20:53 +01:00

208 lines
7.1 KiB
Rust

#![no_std]
#![no_main]
use atmega_hal::{
clock::MHz8,
delay::Delay,
pins,
port::{mode::Output, Pin, PB0},
spi::{DataOrder, SerialClockRate, Settings, Spi},
Peripherals,
};
use avr_device::interrupt;
use avr_eeprom::{eeprom, Eeprom};
use embedded_hal::{
blocking::delay::DelayMs,
spi::{FullDuplex, Mode, Phase, Polarity},
};
use nb::block;
use panic_halt as _;
eeprom! {
static eeprom CONTRAST: u8 = 8;
static eeprom BACKLIGHT: u8 = 1;
}
// TODO: Use https://github.com/rust-lang/rust/issues/85077 when stabilized
const SACRED_CHAO: [[u8; 40]; 5] = [
[
0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC,
0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00,
],
[
0x80, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x03, 0xC3,
0xE3, 0x73, 0x37, 0x17, 0x07, 0x0F, 0x1E, 0x3C, 0xF0, 0x80,
],
[
0xFF, 0xFF, 0xFF, 0xE7, 0xC3, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0x30, 0x00, 0x00, 0xFF,
],
[
0x01, 0x0F, 0x3F, 0x4F, 0x9F, 0x3F, 0x3E, 0x3C, 0x7C, 0x7C, 0x7C, 0x7C, 0x3E, 0x3E, 0x3E,
0x1F, 0x1F, 0x0F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x07, 0x0F, 0x0F, 0x0F,
0x0F, 0x0F, 0x07, 0x07, 0x03, 0x81, 0x40, 0x30, 0x0E, 0x01,
],
[
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x08, 0x10, 0x20, 0x20, 0x40, 0x40, 0x40,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x40, 0x40, 0x40, 0x20, 0x20,
0x10, 0x08, 0x08, 0x04, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00,
],
];
// TODO: Use https://github.com/rust-lang/rust/issues/85077 when stabilized
const ONDERS_ORG: [[u8; 48]; 2] = [
[
0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x60, 0xF8, 0x00, 0x00,
0xE0, 0xA0, 0xE0, 0x00, 0x00, 0xE0, 0x20, 0x60, 0x00, 0x00, 0xE0, 0xA0, 0xA0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x60, 0xE0, 0x00, 0x00, 0xE0, 0x20, 0x60, 0x00, 0x00,
0xE0, 0x60, 0xE0,
],
[
0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x03, 0x02, 0x03, 0x00, 0x00,
0x03, 0x02, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x03, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x02, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x0B, 0x0A, 0x0F,
],
];
fn clear_screen(spi: &mut Spi, cd: &mut Pin<Output, PB0>) {
let mut delay = Delay::<MHz8>::new();
for page in 0..8 {
block!(spi.send(0x00)).unwrap();
block!(spi.send(0x10)).unwrap();
block!(spi.send(0xB0 + page)).unwrap();
// TODO: This delay fixes issues, try find a better solution
delay.delay_ms(1_u8);
cd.set_high();
for _ in 0..102 {
block!(spi.send(0x00)).unwrap();
}
// TODO: This delay fixes issues, try find a better solution
delay.delay_ms(1_u8);
cd.set_low();
}
}
fn draw_splash(spi: &mut Spi, cd: &mut Pin<Output, PB0>) {
clear_screen(spi, cd);
let mut delay = Delay::<MHz8>::new();
for (i, page) in SACRED_CHAO.iter().enumerate() {
block!(spi.send(0x0F)).unwrap();
block!(spi.send(0x11)).unwrap();
block!(spi.send(0xB1 + i as u8)).unwrap();
// TODO: This delay fixes issues, try find a better solution
delay.delay_ms(1_u8);
cd.set_high();
for segment in page {
block!(spi.send(*segment)).unwrap();
}
// TODO: This delay fixes issues, try find a better solution
delay.delay_ms(1_u8);
cd.set_low();
}
for (i, page) in ONDERS_ORG.iter().enumerate() {
block!(spi.send(0x0A)).unwrap();
block!(spi.send(0x11)).unwrap();
block!(spi.send(0xB6 + i as u8)).unwrap();
// TODO: This delay fixes issues, try find a better solution
delay.delay_ms(1_u8);
cd.set_high();
for segment in page {
block!(spi.send(*segment)).unwrap();
}
// TODO: This delay fixes issues, try find a better solution
delay.delay_ms(1_u8);
cd.set_low();
}
}
#[atmega_hal::entry]
fn main() -> ! {
interrupt::free(|_cs| {
// Get peripherals, pins, eeprom and delay
let dp = Peripherals::take().unwrap();
let pins = pins!(dp);
let eeprom = Eeprom::new(dp.EEPROM);
let mut delay = Delay::<MHz8>::new();
// Get contrast and backlight from EEPROM
let contrast = eeprom.read_value(&CONTRAST);
let backlight = eeprom.read_value(&BACKLIGHT);
// 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) });
tc0.ocr0b.write(|w| unsafe { w.bits(backlight) });
pins.pd5.into_output();
// Init SPI
let (mut spi, _) = Spi::new(
dp.SPI,
pins.pb5.into_output(),
pins.pb3.into_output(),
pins.pb4.into_pull_up_input(),
pins.pb2.into_output(),
Settings {
data_order: DataOrder::MostSignificantFirst,
clock: SerialClockRate::OscfOver2,
mode: Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
},
},
);
// Init LCD
let mut lcd_cd = pins.pb0.into_output();
let mut lcd_rst = pins.pb1.into_output();
// TODO: Test if delay is really needed
delay.delay_ms(1_u8);
lcd_rst.set_high();
// TODO: Try to reduce delay to a minimum
delay.delay_ms(5_u8);
block!(spi.send(0x40)).unwrap(); // (6) Set Scroll Line: Display start line 0
block!(spi.send(0xA1)).unwrap(); // (13) Set SEG direction: SEG reverse
block!(spi.send(0xC0)).unwrap(); // (14) Set COM direction: Normal COM0 - COM63
block!(spi.send(0xA6)).unwrap(); // (11) Set Inverse Display: Display inverse off
block!(spi.send(0xA2)).unwrap(); // (17) Set LCD Bias Ratio: Set Bias 1/9 (Duty 1/65)
block!(spi.send(0x2F)).unwrap(); // (5) Set Power Control: Booster, Regulator and Follower on
block!(spi.send(0x27)).unwrap(); // (8) Set VLCD Resistor Ratio: Set Contrast
block!(spi.send(0x81)).unwrap(); // (9) Set Electronic Volume: Set Contrast
block!(spi.send(contrast)).unwrap(); // (9) Set Electronic Volume: Set Contrast
block!(spi.send(0xAF)).unwrap(); // (12) Set Display Enable: Display on
// Draw splash screen
draw_splash(&mut spi, &mut lcd_cd);
});
#[allow(clippy::empty_loop)]
loop {}
}