1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use super::super::interface;
use super::super::{Error, Mcp794xx};

impl<DI, E, IC> Mcp794xx<DI, IC>
where
    DI: interface::WriteData<Error = Error<E>> + interface::ReadData<Error = Error<E>>,
{
    /// Read a single byte from an address.
    ///
    /// Valid addresses are from 0x20 to 0x5F. Otherwise an
    /// Error::InvalidInputData will be returned.
    pub fn read_sram_byte(&mut self, address: u8) -> Result<u8, Error<E>> {
        if address < 0x20 || address > 0x5F {
            return Err(Error::InvalidInputData);
        }
        self.iface.read_register(address)
    }

    /// Write a single byte to an address.
    ///
    /// Valid addresses are from 0x20 to 0x5F. Otherwise an
    /// Error::InvalidInputData will be returned.
    pub fn write_sram_byte(&mut self, address: u8, data: u8) -> Result<(), Error<E>> {
        if address < 0x20 || address > 0x5F {
            return Err(Error::InvalidInputData);
        }
        self.iface.write_register(address, data)
    }

    /// Read SRAM starting in an address as many bytes as necessary to fill
    /// the data array provided.
    pub fn read_sram_data(&mut self, address: u8, data: &mut [u8]) -> Result<(), Error<E>> {
        if address < 0x20
            || address > 0x5F
            || data.len() > 64
            || (data.len() as u8 + address) > 0x60
        {
            return Err(Error::InvalidInputData);
        }
        self.iface.read_data(address, data)
    }

    /// Write data array to SRAM starting in an address.
    pub fn write_sram_data(&mut self, address: u8, data: &[u8]) -> Result<(), Error<E>> {
        if address < 0x20
            || address > 0x5F
            || data.len() > 64
            || (data.len() as u8 + address) > 0x60
        {
            return Err(Error::InvalidInputData);
        }
        let mut payload = [0; 65]; // max size
        payload[0] = address;
        payload[1..=data.len()].copy_from_slice(&data);
        self.iface.write_data(&payload[..=data.len()])
    }
}

impl<DI, E, IC> Mcp794xx<DI, IC>
where
    DI: interface::ReadCurrent<Error = Error<E>>,
{
    /// Read a single byte from the current address.
    ///
    /// The current address corresponds to the last accessed address
    /// (including addresses accessed in EEPROM) incremented by 1.
    pub fn read_sram_current_byte(&mut self) -> Result<u8, Error<E>> {
        self.iface.read()
    }
}