From 8b9d21a0123c64856b3460eac9b80363d34a83de Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 8 Mar 2022 19:05:40 +0100 Subject: [PATCH] fw-rust: Start to use the avr-eeprom crate Start to use the avr-eeprom crate to handle values residing inside the EEPROM. --- firmware/rust/Cargo.lock | 6 ++++++ firmware/rust/Cargo.toml | 1 + firmware/rust/src/main.rs | 16 +++++++--------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/firmware/rust/Cargo.lock b/firmware/rust/Cargo.lock index c856d14..ecc84d6 100644 --- a/firmware/rust/Cargo.lock +++ b/firmware/rust/Cargo.lock @@ -32,6 +32,11 @@ dependencies = [ "syn", ] +[[package]] +name = "avr-eeprom" +version = "0.1.0-dev" +source = "git+https://git.onders.org/finga/avr-eeprom-rs.git?branch=main#d7653567fb2f7ca7bf5100a44c233015a6e03e51" + [[package]] name = "avr-hal-generic" version = "0.1.0" @@ -67,6 +72,7 @@ version = "0.1.0-dev" dependencies = [ "atmega-hal", "avr-device", + "avr-eeprom", "embedded-hal", "nb 1.0.0", "panic-halt", diff --git a/firmware/rust/Cargo.toml b/firmware/rust/Cargo.toml index aee0597..9a1d6e9 100644 --- a/firmware/rust/Cargo.toml +++ b/firmware/rust/Cargo.toml @@ -15,6 +15,7 @@ panic-halt = "0.2" nb = "1.0" embedded-hal = "0.2" avr-device = { version = "0.3", features = ["atmega328p"] } +avr-eeprom = { git = "https://git.onders.org/finga/avr-eeprom-rs.git", branch = "main" } [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 8b9338d..87042c3 100644 --- a/firmware/rust/src/main.rs +++ b/firmware/rust/src/main.rs @@ -9,6 +9,7 @@ use atmega_hal::{ Peripherals, }; use avr_device::interrupt; +use avr_eeprom::eeprom; use embedded_hal::{ blocking::delay::DelayMs, spi::{FullDuplex, Mode, Phase, Polarity}, @@ -16,13 +17,10 @@ use embedded_hal::{ use nb::block; use panic_halt as _; -#[used] -#[link_section = ".eeprom"] -static CONTRAST: u8 = 8; - -#[used] -#[link_section = ".eeprom"] -static BACKLIGHT: u8 = 1; +eeprom! { + static eeprom CONTRAST: u8 = 8; + static eeprom BACKLIGHT: u8 = 1; +} // TODO: Use https://github.com/rust-lang/rust/issues/85077 when stabilized static SACRED_CHAO: [[u8; 40]; 5] = [ @@ -82,7 +80,7 @@ fn main() -> ! { // Write address into eeprom address register eeprom .eear - .write(|w| unsafe { w.bits(&CONTRAST as *const u8 as u16) }); + .write(|w| unsafe { w.bits(CONTRAST.ptr() as u16) }); // Start to read from eeprom by setting EERE eeprom.eecr.write(|w| w.eere().set_bit()); // Return data from eeprom data register @@ -95,7 +93,7 @@ fn main() -> ! { // Write address into eeprom address register eeprom .eear - .write(|w| unsafe { w.bits(&BACKLIGHT as *const u8 as u16) }); + .write(|w| unsafe { w.bits(BACKLIGHT.ptr() as u16) }); // Start to read from eeprom by setting EERE eeprom.eecr.write(|w| w.eere().set_bit()); // Return data from eeprom data register