2019-12-14 08:55:17 +08:00
|
|
|
use bit_field::BitField;
|
|
|
|
|
|
|
|
pub trait SpiFlashRegister {
|
|
|
|
fn inst_code() -> u8;
|
2019-12-16 06:52:47 +08:00
|
|
|
fn new(src: u8) -> Self;
|
2019-12-14 08:55:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! u8_register {
|
2019-12-16 07:46:53 +08:00
|
|
|
($name: ident, $doc: tt, $inst_code: expr) => {
|
2019-12-14 08:55:17 +08:00
|
|
|
#[derive(Clone)]
|
2019-12-16 07:46:53 +08:00
|
|
|
#[doc=$doc]
|
2019-12-14 08:55:17 +08:00
|
|
|
pub struct $name {
|
|
|
|
pub inner: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpiFlashRegister for $name {
|
|
|
|
fn inst_code() -> u8 {
|
|
|
|
$inst_code
|
|
|
|
}
|
|
|
|
|
2019-12-16 06:52:47 +08:00
|
|
|
fn new(src: u8) -> Self {
|
2019-12-14 08:55:17 +08:00
|
|
|
$name {
|
2019-12-16 06:52:47 +08:00
|
|
|
inner: src,
|
2019-12-14 08:55:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 06:52:47 +08:00
|
|
|
|
|
|
|
impl $name {
|
2019-12-18 06:35:58 +08:00
|
|
|
#[allow(unused)]
|
2019-12-16 06:52:47 +08:00
|
|
|
pub fn is_zeroed(&self) -> bool {
|
|
|
|
self.inner == 0
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 08:55:17 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-16 07:46:53 +08:00
|
|
|
u8_register!(CR, "Configuration Register", 0x35);
|
|
|
|
u8_register!(SR1, "Status Register-1", 0x05);
|
2019-12-14 08:55:17 +08:00
|
|
|
impl SR1 {
|
|
|
|
/// Write In Progress
|
|
|
|
pub fn wip(&self) -> bool {
|
|
|
|
self.inner.get_bit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write Enable Latch
|
|
|
|
pub fn wel(&self) -> bool {
|
|
|
|
self.inner.get_bit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Erase Error Occurred
|
|
|
|
pub fn e_err(&self) -> bool {
|
|
|
|
self.inner.get_bit(5)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Programming Error Occurred
|
|
|
|
pub fn p_err(&self) -> bool {
|
|
|
|
self.inner.get_bit(6)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 07:46:53 +08:00
|
|
|
u8_register!(SR2, "Status Register-2", 0x07);
|
|
|
|
u8_register!(BA, "Bank Address Register", 0xB9);
|