pub struct PageBuffer { /* private fields */ }
Expand description
Representation of the spm page buffer.
The page buffer is a special area of memory which is write-only, and only writable using the spm
instruction.
Setting a value in the buffer does not write to the program memory - that only happens when PageBuffer::store
is called
§Example
use avr_boot::PageBuffer;
let address:u16 = 0x1000;
let buff = PageBuffer::new(address);
for w in buff.iter() {
w.set(0xabcd);
}
buff.store();
A whole page is written in one go, so if you only want to change part of a page, you need to make sure you have loaded the rest of the page into the buffer first.
There is only one physical buffer in the system, so you should make sure only one of these structs ever exists at any time. This rule is not enforced.
The page address will be aligned downwards to the nearest starting page address
Implementations§
Source§impl PageBuffer
impl PageBuffer
Sourcepub const LENGTH: usize = 128usize
pub const LENGTH: usize = 128usize
The buffer length in words, the value will change depending on the MCU compilation target
Sourcepub fn new(address: impl Into<Address>) -> PageBuffer
pub fn new(address: impl Into<Address>) -> PageBuffer
Create a new PageBuffer with the given address.
§Example
use avr_boot::PageBuffer;
let buff = PageBuffer::new(0x101fu16);
assert_eq!(0x1000u16, buff.address().into());
The page address will be aligned downwards to the nearest starting page address
Sourcepub fn address(&self) -> Address
pub fn address(&self) -> Address
Get the base page address to be operated on
§Example
use avr_boot::{PageBuffer, Address};
let buff = PageBuffer::new(0x1000u16);
assert_eq!(Address::from(0x1000u16), buff.address());
The page address will be aligned downwards to the nearest starting page address
Sourcepub fn copy_from<'a>(&self, data: impl Into<&'a DataPage>)
pub fn copy_from<'a>(&self, data: impl Into<&'a DataPage>)
Fill the buffer from a slice
§Example
use avr_boot::{DataPage, PageBuffer};
let address: u16 = 0x1000;
let data = DataPage(core::array::from_fn(|_| 0x69));
let buff = PageBuffer::new(address);
buff.copy_from(&data);
buff.store();
§Example
use avr_boot::PageBuffer;
let address: u16 = 0x1000;
let data = [0xff; avr_boot::SPM_PAGESIZE_BYTES];
let buff = PageBuffer::new(address);
buff.copy_from(&data);
buff.store();
Sourcepub fn store_from<'a>(self, data: impl Into<&'a DataPage>)
pub fn store_from<'a>(self, data: impl Into<&'a DataPage>)
Fill the buffer from a slice and store it immediately
§Example
use avr_boot::{DataPage, PageBuffer};
let address: u16 = 0x1000;
let data = DataPage(core::array::from_fn(|_| 0x69));
let buff = PageBuffer::new(address);
buff.store_from(&data);
Sourcepub fn fill_from_fn<F>(&self, f: F)
pub fn fill_from_fn<F>(&self, f: F)
Fill the buffer by repeatedly calling the callback function
§Example
use avr_boot::PageBuffer;
let address:u16 = 0x1000;
let buff = PageBuffer::new(address);
buff.fill_from_fn(|| Some(0x1234));
buff.store();
Sourcepub fn fill_from_iter(&self, i: impl IntoIterator<Item = u16>)
pub fn fill_from_iter(&self, i: impl IntoIterator<Item = u16>)
Fill the buffer by repeatedly polling an iterator.
§Example
use avr_boot::PageBuffer;
use core::iter;
let page_address:u16 = 0x1000;
let buff = PageBuffer::new(page_address);
buff.fill_from_iter(iter::repeat(0x69));
buff.store();