spi_memory_async/lib.rs
1//! An [`embedded-hal`]-based SPI-Flash chip driver.
2//!
3//! This crate aims to be compatible with common families of SPI flash chips.
4//! Currently, reading, writing, erasing 25-series chips is supported, and
5//! support for other chip families (eg. 24-series chips) is planned.
6//!
7//! Contributions are welcome!
8//!
9//! [`embedded-hal`]: https://docs.rs/embedded-hal/
10
11#![doc(html_root_url = "https://docs.rs/spi-memory/0.2.0")]
12#![warn(missing_debug_implementations, rust_2018_idioms)]
13#![cfg_attr(not(test), no_std)]
14
15mod error;
16pub mod prelude;
17pub mod series25;
18mod utils;
19
20pub use crate::error::Error;
21
22use embedded_hal_async::spi::SpiDevice;
23
24/// A trait for reading operations from a memory chip.
25pub trait Read<Addr, SPI: SpiDevice<u8>> {
26 /// Reads bytes from a memory chip.
27 ///
28 /// # Parameters
29 /// * `addr`: The address to start reading at.
30 /// * `buf`: The buffer to read `buf.len()` bytes into.
31 fn read(&mut self, addr: Addr, buf: &mut [u8]) -> Result<(), Error<SPI>>;
32}
33
34/// A trait for writing and erasing operations on a memory chip.
35pub trait BlockDevice<Addr, SPI: SpiDevice<u8>> {
36 /// Erases sectors from the memory chip.
37 ///
38 /// # Parameters
39 /// * `addr`: The address to start erasing at. If the address is not on a sector boundary,
40 /// the lower bits can be ignored in order to make it fit.
41 fn erase_sectors(&mut self, addr: Addr, amount: usize) -> Result<(), Error<SPI>>;
42
43 /// Erases the memory chip fully.
44 ///
45 /// Warning: Full erase operations can take a significant amount of time.
46 /// Check your device's datasheet for precise numbers.
47 fn erase_all(&mut self) -> Result<(), Error<SPI>>;
48
49 /// Writes bytes onto the memory chip. This method is supposed to assume that the sectors
50 /// it is writing to have already been erased and should not do any erasing themselves.
51 ///
52 /// # Parameters
53 /// * `addr`: The address to write to.
54 /// * `data`: The bytes to write to `addr`.
55 fn write_bytes(&mut self, addr: Addr, data: &mut [u8]) -> Result<(), Error<SPI>>;
56}