-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathm24c02.rs
51 lines (42 loc) · 1.43 KB
/
m24c02.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Driver for the M24C02-WMN6TP EEPROM attached to a MWOCP68 power shelf
use crate::Validate;
use drv_i2c_api::{I2cDevice, ResponseCode};
////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
pub enum Error {
BadRead { cmd: u8, code: ResponseCode },
BadWrite { cmd: u8, code: ResponseCode },
BadValidation { code: ResponseCode },
}
impl From<Error> for ResponseCode {
fn from(err: Error) -> Self {
match err {
Error::BadRead { code, .. } => code,
Error::BadWrite { code, .. } => code,
Error::BadValidation { code, .. } => code,
}
}
}
////////////////////////////////////////////////////////////////////////////////
pub struct M24C02 {
device: I2cDevice,
}
impl M24C02 {
pub fn read_eeprom(&self) -> Result<[u8; 256], ResponseCode> {
self.device.read_reg::<u8, _>(0)
}
}
impl Validate<Error> for M24C02 {
fn validate(device: &I2cDevice) -> Result<bool, Error> {
// Attempt to read a byte at address 0
//
// TODO: actually check against an expected pattern
device
.read_reg::<u8, u8>(0)
.map_err(|e| Error::BadValidation { code: e })?;
Ok(true)
}
}