-
Notifications
You must be signed in to change notification settings - Fork 24
/
eeprom.c
64 lines (53 loc) · 1.25 KB
/
eeprom.c
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
#include <fx2eeprom.h>
#include <fx2i2c.h>
bool eeprom_read(uint8_t chip, uint16_t addr, uint8_t *buf, uint16_t len, bool double_byte) {
uint8_t addr_bytes[2];
if(double_byte) {
addr_bytes[0] = addr >> 8;
addr_bytes[1] = addr & 0xff;
} else {
addr_bytes[0] = addr;
}
if(!i2c_start(chip << 1))
goto stop;
if(!i2c_write(addr_bytes, 1 + double_byte))
goto stop;
if(!i2c_start((chip << 1) | 1))
goto stop;
if(!i2c_read(buf, len))
return false;
return true;
stop:
i2c_stop();
return false;
}
bool eeprom_write(uint8_t chip, uint16_t addr, uint8_t *buf, uint16_t len, bool double_byte,
uint8_t timeout) {
uint8_t xfer_bytes[3];
uint16_t i = 0;
uint8_t j;
bool started = false;
if(!i2c_start(chip << 1))
goto stop;
for(; i < len; i++) {
if(double_byte) {
xfer_bytes[0] = addr >> 8;
xfer_bytes[1] = addr & 0xff;
} else {
xfer_bytes[0] = addr;
}
xfer_bytes[1 + double_byte] = buf[i];
if(!i2c_write(xfer_bytes, 2 + double_byte))
goto stop;
if(!i2c_stop())
return false;
for(j = 0; timeout == 0 || j < timeout; j++)
started = i2c_start(chip << 1);
if(!started)
break;
addr++;
}
stop:
i2c_stop();
return i == len;
}