-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathi2c-eeprom.c
157 lines (122 loc) · 3.47 KB
/
i2c-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
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
/*
Plastic Logic EPD project on MSP430
Copyright (C) 2013, 2014 Plastic Logic Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* i2c_eeprom.c -- I2C EEPROM driver
*
* Authors:
* Nick Terry <[email protected]>
* Guillaume Tucker <[email protected]>
*
*/
#include <pl/i2c.h>
#include <stdlib.h>
#include "i2c-eeprom.h"
#include "assert.h"
#define LOG_TAG "i2c-eeprom"
#include "utils.h"
/** Set to 1 to enable verbose log messages */
#define VERBOSE 0
enum i2c_eeprom_flags {
EEPROM_16BIT_ADDRESS = 0x01
};
struct eeprom_data {
uint16_t size; /* size in bytes (-1) */
uint8_t page_size; /* size of a page in bytes */
uint8_t flags; /* flags from i2c_eeprom_flags */
};
static const struct eeprom_data device_data[] = {
{ 0x007F, 16, 0 }, /* 24lc014 (128 Bytes) */
{ 0x7FFF, 64, EEPROM_16BIT_ADDRESS } /* 24aa256 (32 KBytes) */
};
int eeprom_read(const struct i2c_eeprom *eeprom, uint16_t offset,
uint16_t count, uint8_t *data)
{
const struct eeprom_data *device;
struct pl_i2c *i2c;
uint8_t addr[2];
const uint8_t *addr_p;
uint8_t addr_n;
uint8_t i2c_addr;
i2c = eeprom->i2c;
i2c_addr = eeprom->i2c_addr;
#if VERBOSE
LOG("%s (i2c_addr=0x%02x, offset=0x%04X, count=0x%04X)",
__FUNCTION__, i2c_addr, offset, count);
#endif
device = &device_data[eeprom->type];
if ((offset + count) >= device->size)
return -1;
addr[1] = offset & 0x00FF;
if (device->flags & EEPROM_16BIT_ADDRESS) {
addr[0] = (offset >> 8) & 0x00FF;
addr_p = addr;
addr_n = 2;
} else {
addr_p = &addr[1];
addr_n = 1;
}
if (i2c->write(i2c, i2c_addr, addr_p, addr_n, 0))
return -1;
while (count) {
const uint8_t n = min(count, 255);
if (i2c->read(i2c, i2c_addr, data, n, 0))
return -1;
count -= n;
data += n;
}
return 0;
}
#if CONFIG_EEPROM_WRITE /* DANGER: not tested */
int eeprom_write(const struct i2c_eeprom *eeprom, uint16_t offset,
uint16_t count, const uint8_t *data)
{
struct eeprom_data *device;
struct pl_i2c *i2c;
uint8_t addr[2];
int ret;
assert(eeprom != NULL);
assert(data != NULL);
i2c = eeprom->i2c;
#if VERBOSE
LOG("%s (i2c_addr=0x%02x, offset=0x%04X, count=0x%04X)",
__FUNCTION__, eeprom->i2c_addr, offset, count);
#endif
device = &device_data[eeprom->type];
if (offset + count >= device->size)
return -1;
while (count) {
const int n = min(count, (device->page_size -
(offset % device->page_size)));
addr[0] = ((offset >> 8) & 0x00FF);
addr[1] = (offset & 0x00FF);
if (device->flags & EEPROM_16BIT_ADDRESS) {
ret = i2c->write(i2c, eeprom->i2c_addr, addr, 2,
PL_I2C_NO_STOP);
} else {
ret = i2c->write(i2c, eeprom->i2c_addr, &addr[1], 1,
PL_I2C_NO_STOP);
}
if (ret)
return -1;
if (i2c->write(i2c, eeprom->i2c_addr, data, n,
PL_I2C_NO_START))
return -1;
count -= n;
offset += n;
data += n;
}
return 0;
}
#endif