-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2ceeprom.h
408 lines (371 loc) · 18 KB
/
i2ceeprom.h
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* @file
* @code #include "i2ceeprom.h"
* @endcode
*
* @brief A Library for Interfacing AVR with 24CXX EEPROMs.
*
* Basic functions for use with 24CXX EEPROMs.
*
* Requires: I2C Master library by Peter Fleury [email protected]
*
* @version 1.0
* @author Sandeep Kumar http://www.github.com/w0qs1
* @copyright (C) 2021 Sandeep Kumar, GNU General Public License Version 3
*
* @par Usage Example:
*
* @code
* #include <avr/io.h>
* #include <util/delay.h>
* #include <i2cmaster.h>
* #include <i2ceeprom.h>
*
* #define EEPROM_ADDRESS 0xA0
*
* int main(void) {
* eeprom eep1; // Create a new EEPROM instance
* eeprom_init(&eep1, EEPROM_ADDRESS, 8); // AT24C08 -> 8Kbit EEPROM
*
* uint8_t data_write[] = {0xCA, 0xFE, 0xBA, 0xBE};
* eeprom_write(&eep1, 0x00, data_write, 4); // Write 4 bytes
*
* uint8_t data_read[4];
* eeprom_read(&eep1, 0x00, data_read, 4); // Read 4 bytes
*
* uint8_t data_byte;
* eeprom_byte_read(&eep1, 0x00, &data_byte); // Read a single byte
*
* eeprom_byte_write(&eep1, 0x100, data_byte); // Write a single byte
* }
* @endcode
*/
// 1K, 2K - 1 0 1 0 E2 E1 E0 RW
// 4K - 1 0 1 0 E2 E1 A8 RW
// 8K - 1 0 1 0 E2 A9 A8 RW
// 16K - 1 0 1 0 A10 A9 A8 RW
// 32K, 64K - 1 0 1 0 E2 E1 E0 RW
// 128K, 256K - 1 0 1 0 0 E1 E0 RW
// 512K - 1 0 1 0 0 E1 E0 RW
// 1M - 1 0 1 0 0 E1 E0 RW
// Ex - Device Address selection bits
// Ax - Memory Address selection bits
/**
* @brief This creates a new EEPROM instance
* @note The address of the instance must be passed in a function call
*/
typedef struct eeprom {
uint8_t eeprom_address; // Device Address
uint16_t eeprom_size; // Size of EEPROM in Kbits
}eeprom;
/**
* @brief To set the EEPROM properties
* @note This is used to set the I2C address and the size of the EEPROM.
* @param *a: Address of the EEPROM instance
* @param dev_address: I2C address of the EEPROM
* @param size: Size of the EEPROM in Kbits
* @return None
*/
void eeprom_init(eeprom *a, uint8_t dev_address, uint16_t size) {
i2c_init();
a->eeprom_address = dev_address; // Set the device address
a->eeprom_size = size; // Set the eeprom size (size in kbits)
}
/**
* @brief To write a byte array of data
* @param *a: Address of the EEPROM instance
* @param mem_address: Starting address
* @param *data: Data Array
* @param datasize: Size of the data array
* @return None
*/
void eeprom_write(eeprom *a, uint32_t mem_address, uint8_t *data, uint16_t datasize) {
uint8_t i2caddress;
uint16_t pos_in_page;
uint16_t bytes_written = 0;
uint16_t pages_written = 0;
if (a->eeprom_size == 1 || a->eeprom_size == 2) {
pos_in_page = mem_address % 8; // 8 byte page write
while (bytes_written < datasize) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE); // set the device address
i2c_write((uint8_t) mem_address + (8 * pos_in_page)); // set the write address (one byte address)
// write one page of data
for (uint8_t i = pos_in_page; (i < 8) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
} else if (a->eeprom_size == 4) {
pos_in_page = mem_address % 16; // 16 byte page write
while (bytes_written < datasize) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x2));// set the device address from the MSB bits
i2c_start_wait(i2caddress + I2C_WRITE); // set the device address
i2c_write(((uint8_t) mem_address) + (16 * pos_in_page)); // set the write address (one byte address)
// write one page of data
for (uint8_t i = pos_in_page; (i < 16) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
} else if (a->eeprom_size == 8) {
pos_in_page = mem_address % 16; // 16 byte page write
while (bytes_written < datasize) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x6));// set the device address from the MSB bits
i2c_start_wait(i2caddress + I2C_WRITE); // set the device address
i2c_write(((uint8_t) mem_address) + (16 * pos_in_page)); // set the write address (one byte address)
// write one page of data
for (uint8_t i = pos_in_page; (i < 16) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
} else if (a->eeprom_size == 16) {
pos_in_page = mem_address % 16; // 16 byte page write
while (bytes_written < datasize) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0xE));// set the device address from the MSB bits
i2c_start_wait(i2caddress + I2C_WRITE); // set the device address
i2c_write(((uint8_t) mem_address) + (16 * pos_in_page)); // set the write address (one byte address)
// write one page of data
for (uint8_t i = pos_in_page; (i < 16) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
} else if (a->eeprom_size == 32 || a->eeprom_size == 64) {
// fixed device address
pos_in_page = mem_address % 32; // 32 byte page write
while (bytes_written < datasize) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE); // set the device address
i2c_write((uint8_t) ((mem_address & 0xFF00) >> 8)); // write the MSB address first
i2c_write((uint8_t) ((mem_address & 0x00FF) + (32 * pos_in_page))); // write the LSB address next
// write one page of data
for (uint8_t i = pos_in_page; (i < 32) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
} else if (a->eeprom_size == 128 || a->eeprom_size == 256) {
// fixed device address
pos_in_page = mem_address % 64; // 64 byte page write
while (bytes_written < datasize) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE); // set the device address
i2c_write((uint8_t) ((mem_address & 0xFF00) >> 8)); // write the MSB address first
i2c_write((uint8_t) ((mem_address & 0x00FF) + (64 * pos_in_page))); // write the LSB address next
// write one page of data
for (uint8_t i = pos_in_page; (i < 64) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
} else if (a->eeprom_size == 512) {
// fixed device address
pos_in_page = mem_address % 128; // 128 byte page write
while (bytes_written < datasize) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE); // set the device address
i2c_write((uint8_t) ((mem_address & 0xFF00) >> 8)); // write the MSB address first
i2c_write((uint8_t) ((mem_address & 0x00FF) + (128 * pos_in_page))); // write the LSB address next
// write one page of data
for (uint8_t i = pos_in_page; (i < 128) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
} else if (a->eeprom_size == 1024) {
// fixed device address
pos_in_page = mem_address % 256; // 256 byte page write
while (bytes_written < datasize) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE);
i2c_write((uint8_t) ((mem_address & 0xFF00) >> 8)); // write the MSB address first
i2c_write((uint8_t) ((mem_address & 0x00FF) + (256 * pos_in_page))); // write the LSB address next
// write one page of data
for (uint16_t i = pos_in_page; (i < 256) && (bytes_written < datasize); ++i) {
i2c_write(*(data + bytes_written));
++bytes_written;
}
// go to the next page and set the position in the page to 0
++pages_written;
pos_in_page = 0;
}
}
i2c_stop();
}
/**
* @brief To read a byte array
* @param *a: Address of the EEPROM instance
* @param mem_address: Starting address
* @param *data: Data Array
* @param datasize: Size of the data array
* @return None
*/
void eeprom_read(eeprom *a, uint16_t mem_address, uint8_t *data, uint16_t datasize) {
uint8_t i2caddress;
uint16_t bytes_read = 0;
if (a->eeprom_size == 1 || a->eeprom_size == 2) {
i2c_start_wait(a->eeprom_address + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(a->eeprom_address + I2C_READ);
while(bytes_read < datasize - 1) {
*(data + bytes_read) = i2c_readAck(); // Read datasize - 1 bytes
++bytes_read;
}
*(data + bytes_read) = i2c_readNak(); // Read the last byte
} else if (a->eeprom_size == 4) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x2));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(i2caddress + I2C_READ);
while(bytes_read < datasize - 1) {
*(data + bytes_read) = i2c_readAck(); // Read datasize - 1 bytes
++bytes_read;
}
*(data + bytes_read) = i2c_readNak(); // Read the last byte
} else if (a->eeprom_size == 8) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x6));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(i2caddress + I2C_READ);
while(bytes_read < datasize - 1) {
*(data + bytes_read) = i2c_readAck(); // Read datasize - 1 bytes
++bytes_read;
}
*(data + bytes_read) = i2c_readNak(); // Read the last byte
} else if (a->eeprom_size == 16) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0xE));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(i2caddress + I2C_READ);
while(bytes_read < datasize - 1) {
*(data + bytes_read) = i2c_readAck(); // Read datasize - 1 bytes
++bytes_read;
}
*(data + bytes_read) = i2c_readNak(); // Read the last byte
} else if (a->eeprom_size == 32 || a->eeprom_size == 64 || a->eeprom_size == 128 || a->eeprom_size == 256 || a->eeprom_size == 512 || a->eeprom_size == 1024) {
i2c_start_wait(a->eeprom_address + I2C_WRITE);
i2c_write((uint8_t) ((mem_address & 0xFF00) >> 8)); // Write MSB address first
i2c_write((uint8_t) (mem_address & 0x00FF)); // Write LSB address
i2c_stop();
i2c_start_wait(a->eeprom_address + I2C_READ);
while(bytes_read < datasize - 1) {
*(data + bytes_read) = i2c_readAck(); // Read datasize - 1 bytes
++bytes_read;
}
*(data + bytes_read) = i2c_readNak(); // Read the last byte
}
i2c_stop();
}
/**
* @brief To write a byte
* @param *a: Address of the EEPROM instance
* @param mem_address: Address for the byte to be stored
* @param data: Byte data
* @return None
*/
void eeprom_byte_write(eeprom *a, uint16_t mem_address, uint8_t data) {
uint8_t i2caddress;
if (a->eeprom_size == 1 || a->eeprom_size == 2) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE); // Set the device address
i2c_write((uint8_t) mem_address); // Write the location to store the byte
i2c_write(data); // Write the data
} else if (a->eeprom_size == 4) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x2));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_write(data);
} else if (a->eeprom_size == 8) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x6));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_write(data);
} else if (a->eeprom_size == 16) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0xE));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_write(data);
} else if (a->eeprom_size == 32 || a->eeprom_size == 64 || a->eeprom_size == 128 || a->eeprom_size == 256 || a->eeprom_size == 512 || a->eeprom_size == 1024) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE); // Set the device address
i2c_write((uint8_t) ((mem_address & 0xFF00) >> 8)); // Write the MSB address first
i2c_write((uint8_t) (mem_address & 0x00FF)); // Write the LSB address next
i2c_write(data); // Write the data
}
i2c_stop();
}
/**
* @brief To read a byte
* @param *a: Address of the EEPROM instance
* @param mem_address: Location of the byte in EEPROM
* @param *data: Address of the data byte
* @return None
*/
void eeprom_byte_read(eeprom *a, uint16_t mem_address, uint8_t *data) {
uint8_t i2caddress;
if (a->eeprom_size == 1 || a->eeprom_size == 2) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(a->eeprom_address + I2C_READ);
*data = i2c_readNak();
} else if (a->eeprom_size == 4) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x2));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(i2caddress + I2C_READ);
*data = i2c_readNak();
} else if (a->eeprom_size == 8) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0x6));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(i2caddress + I2C_READ);
*data = i2c_readNak();
} else if (a->eeprom_size == 16) {
i2caddress = a->eeprom_address | ((uint8_t) ((mem_address >> 7) & 0xE));
i2c_start_wait(i2caddress + I2C_WRITE);
i2c_write((uint8_t) mem_address);
i2c_stop();
i2c_start_wait(i2caddress + I2C_READ);
*data = i2c_readNak();
} else if (a->eeprom_size == 32 || a->eeprom_size == 64 || a->eeprom_size == 128 || a->eeprom_size == 256 || a->eeprom_size == 512 || a->eeprom_size == 1024) {
// fixed device address
i2c_start_wait(a->eeprom_address + I2C_WRITE); // Set the device address
i2c_write((uint8_t) ((mem_address & 0xFF00) >> 8)); // Write the MSB address first
i2c_write((uint8_t) (mem_address & 0x00FF)); // Write the LSB address next
i2c_stop();
i2c_start_wait(a->eeprom_address + I2C_READ);
*data = i2c_readNak();
}
i2c_stop();
}