mx25r/lib.rs
1#![no_std]
2//! This is a platform agnostic library for the Macronix MX25R NOR flash series using [embedded-hal](https://github.com/rust-embedded/embedded-hal).
3//!
4//! Multiple chips are supported:
5//! * [MX25R512F](https://www.macronix.com/Lists/Datasheet/Attachments/7399/MX25R512F,%20Wide%20Range,%20512Kb,%20v1.3.pdf)
6//! * [MX25R1035F](https://www.macronix.com/Lists/Datasheet/Attachments/7400/MX25R1035F,%20Wide%20Range,%201Mb,%20v1.4.pdf)
7//! * [MX25R2035F](https://www.macronix.com/Lists/Datasheet/Attachments/7478/MX25R2035F,%20Wide%20Range,%202Mb,%20v1.6.pdf)
8//! * [MX25R4035F](https://www.macronix.com/Lists/Datasheet/Attachments/7425/MX25R4035F,%20Wide%20Range,%204Mb,%20v1.4.pdf)
9//! * [MX25R8035F](https://www.macronix.com/Lists/Datasheet/Attachments/7934/MX25R8035F,%20Wide%20Range,%208Mb,%20v1.6.pdf)
10//! * [MX25R1635F](https://www.macronix.com/Lists/Datasheet/Attachments/7595/MX25R1635F,%20Wide%20Range,%2016Mb,%20v1.6.pdf)
11//! * [MX25R3235F](https://www.macronix.com/Lists/Datasheet/Attachments/7966/MX25R3235F,%20Wide%20Range,%2032Mb,%20v1.8.pdf)
12//! * [MX25R6435F](https://www.macronix.com/Lists/Datasheet/Attachments/7913/MX25R6435F,%20Wide%20Range,%2064Mb,%20v1.5.pdf)
13
14pub mod asynchronous;
15pub mod blocking;
16mod command;
17pub mod error;
18pub mod register;
19
20use crate::error::Error;
21
22pub const BLOCK64_SIZE: u32 = 0x010000;
23pub const BLOCK32_SIZE: u32 = BLOCK64_SIZE / 2;
24
25pub const SECTOR_SIZE: u32 = 0x1000;
26pub const PAGE_SIZE: u32 = 0x100;
27
28pub(crate) fn check_erase<E>(capacity: usize, from: u32, to: u32) -> Result<(), Error<E>> {
29 let capacity = capacity as u32;
30 if from > to || to > capacity {
31 return Err(Error::OutOfBounds);
32 }
33 if !from.is_multiple_of(SECTOR_SIZE) || !to.is_multiple_of(SECTOR_SIZE) {
34 return Err(Error::NotAligned);
35 }
36 Ok(())
37}
38
39pub(crate) fn check_write<E>(capacity: usize, offset: u32, length: usize) -> Result<(), Error<E>> {
40 let capacity = capacity as u32;
41 let length = length as u32;
42 if length > capacity || offset > capacity - length {
43 return Err(Error::OutOfBounds);
44 }
45 Ok(())
46}