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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::{interface, marker, EepromWriteProtection, Error, Mcp794xx};
const EEUNLOCK: u8 = 0b0000_1001;
const EEPROM_STATUS: u8 = 0xFF;
impl<DI, E, IC> Mcp794xx<DI, IC>
where
DI: interface::WriteData<Error = Error<E>> + interface::ReadData<Error = Error<E>>,
IC: marker::WithProtectedEeprom,
{
pub fn read_protected_eeprom_byte(&mut self, address: u8) -> Result<u8, Error<E>> {
if !is_protected_eeprom_address(address) {
return Err(Error::InvalidInputData);
}
self.iface.read_eeprom_byte(address)
}
pub fn read_protected_eeprom_data(
&mut self,
address: u8,
data: &mut [u8],
) -> Result<(), Error<E>> {
if !is_protected_eeprom_address(address)
|| data.len() > 8
|| (data.len() as u8 + address) > 0xF8
{
return Err(Error::InvalidInputData);
}
self.iface.read_eeprom_data(address, data)
}
pub fn write_protected_eeprom_byte(&mut self, address: u8, data: u8) -> Result<(), Error<E>> {
if !is_protected_eeprom_address(address) {
return Err(Error::InvalidInputData);
}
self.iface.write_register(EEUNLOCK, 0x55)?;
self.iface.write_register(EEUNLOCK, 0xAA)?;
self.iface.write_eeprom_byte(address, data)
}
pub fn write_protected_eeprom_data(
&mut self,
address: u8,
data: &[u8],
) -> Result<(), Error<E>> {
if !is_protected_eeprom_address(address)
|| data.len() > 8
|| (data.len() as u8 + address) > 0xF8
{
return Err(Error::InvalidInputData);
}
let mut payload = [0; 9];
payload[0] = address;
payload[1..=data.len()].copy_from_slice(&data);
self.iface.write_register(EEUNLOCK, 0x55)?;
self.iface.write_register(EEUNLOCK, 0xAA)?;
self.iface.write_eeprom_data(&payload[..=data.len()])
}
}
fn is_protected_eeprom_address(address: u8) -> bool {
address >= 0xF0 && address <= 0xF7
}
impl<DI, E, IC> Mcp794xx<DI, IC>
where
DI: interface::WriteData<Error = Error<E>> + interface::ReadData<Error = Error<E>>,
IC: marker::WithEeprom,
{
pub fn set_eeprom_write_protection(
&mut self,
protection: EepromWriteProtection,
) -> Result<(), Error<E>> {
let value = match protection {
EepromWriteProtection::None => 0,
EepromWriteProtection::UpperQuarter => 0b0000_0100,
EepromWriteProtection::UpperHalf => 0b0000_1000,
EepromWriteProtection::All => 0b0000_1100,
};
self.iface.write_eeprom_byte(EEPROM_STATUS, value)
}
pub fn read_eeprom_byte(&mut self, address: u8) -> Result<u8, Error<E>> {
if is_eeprom_address(address) {
self.iface.read_eeprom_byte(address)
} else {
Err(Error::InvalidInputData)
}
}
pub fn read_eeprom_data(&mut self, address: u8, data: &mut [u8]) -> Result<(), Error<E>> {
if is_eeprom_address(address) && data.len() <= 128 && (address + data.len() as u8) < 0x80 {
self.iface.read_eeprom_data(address, data)
} else {
Err(Error::InvalidInputData)
}
}
pub fn write_eeprom_byte(&mut self, address: u8, data: u8) -> Result<(), Error<E>> {
if is_eeprom_address(address) {
self.iface.write_eeprom_byte(address, data)
} else {
Err(Error::InvalidInputData)
}
}
pub fn write_eeprom_data(&mut self, address: u8, data: &[u8]) -> Result<(), Error<E>> {
if is_eeprom_address(address) && data.len() <= 128 && (address + data.len() as u8) < 0x80 {
let mut payload = [0; 128];
payload[0] = address;
payload[1..=data.len()].copy_from_slice(&data);
self.iface.write_eeprom_data(&payload[..=data.len()])
} else {
Err(Error::InvalidInputData)
}
}
}
fn is_eeprom_address(address: u8) -> bool {
address < 0x80
}
impl<DI, E, IC> Mcp794xx<DI, IC>
where
DI: interface::ReadCurrent<Error = Error<E>>,
{
pub fn read_eeprom_current_byte(&mut self) -> Result<u8, Error<E>> {
self.iface.read_eeprom()
}
}
impl<DI, E, IC> Mcp794xx<DI, IC>
where
DI: interface::ReadData<Error = Error<E>>,
IC: marker::WithEui48,
{
pub fn read_eui48(&mut self) -> Result<[u8; 6], Error<E>> {
let mut data = [0; 6];
self.iface.read_eeprom_data(0xF2, &mut data).and(Ok(data))
}
}
impl<DI, E, IC> Mcp794xx<DI, IC>
where
DI: interface::ReadData<Error = Error<E>>,
IC: marker::WithEui64,
{
pub fn read_eui64(&mut self) -> Result<[u8; 8], Error<E>> {
let mut data = [0; 8];
self.iface.read_eeprom_data(0xF0, &mut data).and(Ok(data))
}
}