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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! This is a platform agnostic Rust driver for the 24x series serial EEPROM,
//! based on the [`embedded-hal`] traits.
//!
//! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal
//!
//! This driver allows you to:
//! - Read a single byte from a memory address. See [`read_byte()`].
//! - Read a byte array starting on a memory address. See: [`read_data()`].
//! - Read the current memory address (please read notes). See: [`read_current_address()`].
//! - Write a byte to a memory address. See: [`write_byte()`].
//! - Write a byte array (up to a memory page) to a memory address. See: [`write_page()`].
//! - Read `CSx`-variant devices' factory-programmed unique serial. See: [`read_unique_serial()`].
//! - Use the device in generic code via the [`Eeprom24xTrait`].
//!
//! [`read_byte()`]: Eeprom24x::read_byte
//! [`read_data()`]: Eeprom24x::read_data
//! [`read_current_address()`]: Eeprom24x::read_current_address
//! [`write_byte()`]: Eeprom24x::write_byte
//! [`write_page()`]: Eeprom24x::write_page
//! [`read_unique_serial()`]: Eeprom24x::read_unique_serial
//! [`Eeprom24xTrait`]: Eeprom24xTrait
//!
//! If an `embedded_hal::timer::CountDown` is available, the [`embedded-storage`] traits can
//! additionally be used which allow to read the device capacity and write over page boundaries. To
//! achieve the latter, the [`Eeprom24x`] has to be wrapped with [`Storage::new`].
//!
//! [`embedded-storage`]: https://github.com/rust-embedded-community/embedded-storage
//!
//! Can be used at least with the devices listed below.
//!
//! ## The devices
//!
//! These devices provides a number of bits of serial electrically erasable and
//! programmable read only memory (EEPROM) organized as a number of words of 8 bits
//! each. The devices' cascadable feature allows up to 8 devices to share a common
//! 2-wire bus. The devices are optimized for use in many industrial and commercial
//! applications where low power and low voltage operation are essential.
//!
//! | Device | Memory bits | 8-bit words | Page size | Datasheet |
//! |-------:|------------:|------------:|----------:|:-----------|
//! | 24x00 | 128 bits | 16 | N/A | [24C00] |
//! | 24x01 | 1 Kbit | 128 | 8 bytes | [AT24C01] |
//! | M24x01 | 1 Kbit | 128 | 16 bytes | [M24C01] |
//! | 24x02 | 2 Kbit | 256 | 8 bytes | [AT24C02] |
//! | M24x02 | 2 Kbit | 256 | 16 bytes | [M24C02] |
//! | 24x04 | 4 Kbit | 512 | 16 bytes | [AT24C04] |
//! | 24x08 | 8 Kbit | 1,024 | 16 bytes | [AT24C08] |
//! | 24x16 | 16 Kbit | 2,048 | 16 bytes | [AT24C16] |
//! | 24x32 | 32 Kbit | 4,096 | 32 bytes | [AT24C32] |
//! | 24x64 | 64 Kbit | 8,192 | 32 bytes | [AT24C64] |
//! | 24x128 | 128 Kbit | 16,384 | 64 bytes | [AT24C128] |
//! | 24x256 | 256 Kbit | 32,768 | 64 bytes | [AT24C256] |
//! | 24x512 | 512 Kbit | 65,536 | 128 bytes | [AT24C512] |
//! | 24xM01 | 1 Mbit | 131,072 | 256 bytes | [AT24CM01] |
//! | 24xM02 | 2 Mbit | 262,144 | 256 bytes | [AT24CM02] |
//!
//! [24C00]: https://ww1.microchip.com/downloads/en/DeviceDoc/24AA00-24LC00-24C00-Data-Sheet-20001178J.pdf
//! [AT24C01]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8871F-SEEPROM-AT24C01D-02D-Datasheet.pdf
//! [M24C01]: https://www.st.com/resource/en/datasheet/m24c01-r.pdf
//! [AT24C02]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8871F-SEEPROM-AT24C01D-02D-Datasheet.pdf
//! [M24C02]: https://www.st.com/resource/en/datasheet/m24c02-r.pdf
//! [AT24C04]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8896E-SEEPROM-AT24C04D-Datasheet.pdf
//! [AT24C08]: https://ww1.microchip.com/downloads/en/DeviceDoc/AT24C08D-I2C-Compatible-2-Wire-Serial-EEPROM-20006022A.pdf
//! [AT24C16]: https://ww1.microchip.com/downloads/en/DeviceDoc/20005858A.pdf
//! [AT24C32]: https://ww1.microchip.com/downloads/en/devicedoc/doc0336.pdf
//! [AT24C64]: https://ww1.microchip.com/downloads/en/devicedoc/doc0336.pdf
//! [AT24C128]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8734-SEEPROM-AT24C128C-Datasheet.pdf
//! [AT24C256]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8568-SEEPROM-AT24C256C-Datasheet.pdf
//! [AT24C512]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8720-SEEPROM-AT24C512C-Datasheet.pdf
//! [AT24CM01]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8812-SEEPROM-AT24CM01-Datasheet.pdf
//! [AT24CM02]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-8828-SEEPROM-AT24CM02-Datasheet.pdf
//!
//! ## Features
//!
//! ### defmt-03
//!
//! To enable [defmt](https://crates.io/crates/defmt) (version `0.3.x`) support, when specifying the dependency on `eeprom24x`, add the feature "`defmt-03`".
//!
//! ```toml
//! [dependencies]
//! eeprom24x = { version = "0.7.2", features = ["defmt-03"] }
//! ```
//!
//! ## Usage examples (see also examples folder)
//!
//! To create a new instance you can use the `new_<device>` methods.
//! There are many compatible vendors so the method has a somewhat generic name.
//! For example, if you are using an AT24C32, you can create a device by calling
//! `Eeprom24x::new_24x32(...)`.
//! Please refer to the [device table](#the-devices) above for further examples
//! of device names.
//!
//! Please find additional examples using hardware in this repository: [driver-examples]
//!
//! [driver-examples]: https://github.com/eldruin/driver-examples
//!
//! ### Instantiating with the default address
//!
//! Import this crate and an `embedded_hal` implementation, then instantiate
//! the device:
//!
//! ```no_run
//! use linux_embedded_hal::I2cdev;
//! use eeprom24x::{ Eeprom24x, SlaveAddr };
//!
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let address = SlaveAddr::default();
//! // using the AT24C256
//! let mut eeprom = Eeprom24x::new_24x256(dev, address);
//! ```
//!
//! ### Providing an alternative address
//!
//! ```no_run
//! use linux_embedded_hal::I2cdev;
//! use eeprom24x::{ Eeprom24x, SlaveAddr };
//!
//! # fn main() {
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let (a2, a1, a0) = (false, false, true);
//! let address = SlaveAddr::Alternative(a2, a1, a0);
//! let mut eeprom = Eeprom24x::new_24x256(dev, address);
//! # }
//! ```
//!
//! ### Writing and reading a byte
//!
//! ```no_run
//! use linux_embedded_hal::I2cdev;
//! use eeprom24x::{ Eeprom24x, SlaveAddr };
//!
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let mut eeprom = Eeprom24x::new_24x256(dev, SlaveAddr::default());
//! let address = 0x1234;
//! let data = 0xAB;
//! eeprom.write_byte(address, data);
//! // EEPROM enters internally-timed write cycle. Will not respond for some time.
//! let retrieved_data = eeprom.read_byte(address);
//! ```
//!
//! ### Writing a page
//!
//! ```no_run
//! use linux_embedded_hal::I2cdev;
//! use eeprom24x::{ Eeprom24x, SlaveAddr };
//!
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let mut eeprom = Eeprom24x::new_24x256(dev, SlaveAddr::default());
//! let address = 0x1234;
//! let data = [0xAB; 64];
//! eeprom.write_page(address, &data);
//! // EEPROM enters internally-timed write cycle. Will not respond for some time.
//! ```
//!
//! ### Using embedded-storage traits
//!
//! ```no_run
//! use linux_embedded_hal::{I2cdev, Delay};
//! use eeprom24x::{ Eeprom24x, SlaveAddr, Storage };
//! use embedded_storage::{ReadStorage, Storage as _};
//!
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
//! let eeprom = Eeprom24x::new_24x256(dev, SlaveAddr::default());
//! let mut storage = Storage::new(eeprom, Delay {});
//! let _capacity = storage.capacity();
//! let address = 0x1234;
//! let data = [0xAB; 256];
//! storage.write(address, &data);
//! // EEPROM writes four pages. This introduces a delay of at least 20 ms, 5 ms per page.
//! ```
use PhantomData;
/// All possible errors in this crate
/// Possible slave addresses
///
/// Note that in some devices some of the address bits are used for memory addressing and
/// will therefore be ignored.
/// Memory address size markers
/// Page size markers
/// Factory-supplied unique serial number markers
/// EEPROM24X driver
/// `Eeprom24x` type trait for use in generic code
/// EEPROM24X extension which supports the `embedded-storage` traits but requires an
/// `embedded_hal::delay::DelayNs` to handle the timeouts when writing over page boundaries