From ed3265f1562b4285db705ea4b2c57978a699aeee Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 13 Sep 2025 13:08:32 +0300 Subject: [PATCH] Remove progmem --- Cargo.lock | 23 ---------------------- Cargo.toml | 1 - src/main.rs | 2 +- src/qoi.rs | 55 +++++++++++++++++++++++++++++++++++------------------ 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6e634c..85d2ba5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,17 +50,6 @@ dependencies = [ "unwrap-infallible", ] -[[package]] -name = "avr-progmem" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e93a9dd7da8f4eb57c912367e337687e06a8db6452d8c89eeb9eff1e0d2ba2d" -dependencies = [ - "cfg-if", - "derivative", - "ufmt", -] - [[package]] name = "az" version = "1.2.1" @@ -91,17 +80,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" -[[package]] -name = "derivative" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "embedded-graphics-core" version = "0.4.0" @@ -206,7 +184,6 @@ version = "0.1.0" dependencies = [ "atmega-hal", "avr-device", - "avr-progmem", "embedded-graphics-core", "embedded-hal 1.0.0", "panic-halt", diff --git a/Cargo.toml b/Cargo.toml index 2ca442a..100fde5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ atmega-hal = { git = "https://github.com/Rahix/avr-hal?tab=readme-ov-file", rev= ufmt = "0.2.0" embedded-graphics-core = "0.4.0" -avr-progmem = "0.4.0" # nb = "1.1.0" # pwm-pca9685 = "1.0.0" # infrared = "0.14.1" diff --git a/src/main.rs b/src/main.rs index 6588c17..5b1d840 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ #![feature(iter_array_chunks)] #![feature(const_slice_make_iter)] #![feature(slice_as_chunks)] -#![feature(generic_const_exprs)] +#![feature(asm_experimental_arch)] use atmega_hal::{ Adc, Eeprom, Usart, diff --git a/src/qoi.rs b/src/qoi.rs index ca987ed..a9f7cec 100644 --- a/src/qoi.rs +++ b/src/qoi.rs @@ -1,5 +1,7 @@ use core::{ + arch::asm, hint::black_box, + ptr::addr_of, slice::{Chunks, Iter}, }; @@ -8,7 +10,6 @@ use atmega_hal::{ pac::USART0, port::{PB3, PB5, PD0, PD1, Pin, PinOps, mode}, }; -use avr_progmem::{progmem, wrapper::ProgMem}; use embedded_hal::delay::DelayNs; use ufmt::derive::uDebug; @@ -18,31 +19,51 @@ use crate::{ qoi, }; -progmem! { - /// Static byte stored in progmem! - static progmem P_BYTE: u8 = 42; - static progmem VERY_LARGE: [u8; 23731] = *include_bytes!("../images/cat.qoi"); -} +// progmem! { +// /// Static byte stored in progmem! +// static progmem P_BYTE: u8 = 42; +// static progmem VERY_LARGE: [u8; 23731] = *include_bytes!("../images/cat.qoi"); +// } // https://qoiformat.org/qoi-specification.pdf -pub static MARIO: [u8; 161] = *include_bytes!("../images/mario.qoi"); -pub static PRESS_BTN: [u8; 174] = *include_bytes!("../images/press_btn.qoi"); -pub struct LargeIterator { - buffer: ProgMem<[u8; LENGTH]>, +#[unsafe(link_section = ".progmem.data")] +static LARGE_CAT: [u8; 23731] = *include_bytes!("../images/cat.qoi"); + +pub struct LargeIterator { + ptr: *const u8, + length: usize, index: usize, } -impl Iterator for LargeIterator { +impl LargeIterator { + pub fn from(addr: *const [u8; N]) -> LargeIterator { + LargeIterator { + ptr: addr.cast(), + length: N, + index: 0, + } + } +} + +impl Iterator for LargeIterator { type Item = u8; fn next(&mut self) -> Option { - if self.index >= LENGTH { + if self.index >= self.length { return None; } - let item = self.buffer.load_at(self.index); + let elem_ptr = self.ptr.wrapping_add(self.index); + let res: u8; + unsafe { + asm!( + "lpm {}, Z", + out(reg) res, + in("Z") elem_ptr, + ) + } self.index += 1; - Some(item) + Some(res) } } @@ -60,10 +81,8 @@ pub fn draw_image( ) -> Result<(), QoiErr> { // let mut iter = buffer.iter().map(|b| *b); - let mut iter = LargeIterator { - buffer: VERY_LARGE, - index: 0, - }; + let addr = addr_of!(LARGE_CAT); + let mut iter = LargeIterator::from(addr); if let (Some(113), Some(111), Some(105), Some(102)) = (iter.next(), iter.next(), iter.next(), iter.next())