microchip_eeprom_25x/
status.rs

1use bit_field::BitField;
2
3pub struct Status {
4    pub value: u8
5}
6
7#[allow(dead_code)]
8#[repr(u8)]
9pub enum WriteProtection {
10    None = 0b00,
11    Quarter = 0b01,
12    Half = 0b10,
13    All = 0b11
14}
15
16impl Status {
17    /// Get whether the write latch enabled bit is enabled
18    pub fn write_latch_enabled(&self) -> bool {
19        self.value.get_bit(1)
20    }
21
22    /// Get whether there is a write in progress
23    pub fn write_in_progress(&self) -> bool {
24        self.value.get_bit(0)
25    }
26
27    /// Get the protection level
28    pub fn write_protection_level(&self) -> WriteProtection {
29        let val = self.value.get_bits(2..3);
30        match val {
31            0b00 => WriteProtection::None,
32            0b01 => WriteProtection::Quarter,
33            0b10 => WriteProtection::Half,
34            0b11 => WriteProtection::All,
35            _ => WriteProtection::None
36        }
37    }
38
39    /// Set the write protection level bits
40    pub fn set_write_protection_level(&mut self, protection: WriteProtection) {
41        self.value.set_bits(2..3, protection as u8);
42    }
43
44    /// Get the status of the WPEN bit. This makes the WP line effective
45    /// If this is 0, then the WP line's data is ignored.
46    pub fn write_protection_enabled(&mut self) -> bool {
47        self.value.get_bit(7)
48    }
49
50    /// Change the WPEN bit
51    pub fn set_write_protection_enabled(&mut self, enabled: bool) {
52        self.value.set_bit(7, enabled);
53    }
54}