Struct PageBuffer

Source
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

Source

pub const LENGTH: usize = 128usize

The buffer length in words, the value will change depending on the MCU compilation target

Source

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

Source

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

Source

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();
Source

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);
Source

pub fn fill_from_fn<F>(&self, f: F)
where F: FnMut() -> Option<u16>,

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();
Source

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();
Source

pub fn store(self)

Erase the page from program memory, then write the contents of the buffer to it

Source

pub fn iter(&self) -> impl Iterator<Item = BufferCell>

Iterate the buffer as writable word cells

§Example
use avr_boot::PageBuffer;

let address: u16 = 0x1000;
let buff = PageBuffer::new(address);
for w in buff.iter() {
    w.set(0x69);
}
buff.store();

Trait Implementations§

Source§

impl Drop for PageBuffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.