forked from cyberp/AT24Cx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAT24CX.cpp
328 lines (295 loc) · 7.42 KB
/
AT24CX.cpp
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
/**
AT24CX.cpp
Library for using the EEPROM AT24C32-512/M01/M02
Original work Copyright (c) 2014 Christian Paul
Modified work Copyright (c) 2019 Rushikesh Patel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "AT24CX.h"
#include <Wire.h>
/**
* Constructor with AT24Cx EEPROM at given index and size of page
*/
AT24CX::AT24CX(byte index, byte pageSize, int wp_pin) {
init(index, pageSize, wp_pin);
eepromType=AT24CX_t;
}
/**
* Constructor with AT24Cx EEPROM at given index(default 0)
*/
AT24C32::AT24C32(byte index, int wp_pin) {
init(index, 32, wp_pin);
eepromType=AT24C32_t;
}
/**
* Constructor with AT24C64 EEPROM at given index(default 0)
*/
AT24C64::AT24C64(byte index, int wp_pin) {
init(index, 32, wp_pin);
eepromType=AT24C64_t;
}
/**
* Constructor with AT24C128 EEPROM at given index(default 0)
*/
AT24C128::AT24C128(byte index, int wp_pin) {
init(index, 64, wp_pin);
eepromType=AT24C128_t;
}
/**
* Constructor with AT24C128 EEPROM at given index(default 0)
*/
AT24C256::AT24C256(byte index, int wp_pin) {
init(index, 64, wp_pin);
eepromType=AT24C256_t;
}
/**
* Constructor with AT24C512 EEPROM at given index(default 0)
*/
AT24C512::AT24C512(byte index, int wp_pin) {
init(index, 128, wp_pin);
eepromType=AT24C512_t;
}
/*
* Constructor with AT24CM02 EEPROM at given index(default 0)
*/
AT24CM02::AT24CM02(byte index, int wp_pin){
_id = AT24CX_ID | (index<<2 & 0x4);
_pageSize = 256;
eepromType=AT24CM02_t;
_wp_pin = wp_pin;
Wire.begin();
if(wp_pin>=0)
pinMode(wp_pin, OUTPUT);
}
/*
* Constructor with AT24CM01 EEPROM at given index(default 0)
*/
AT24CM01::AT24CM01(byte index, int wp_pin){
_id = AT24CX_ID | (index<<1 & 0x6);
_pageSize = 256;
eepromType=AT24CM01_t;
_wp_pin = wp_pin;
Wire.begin();
if(wp_pin>=0)
pinMode(wp_pin, OUTPUT);
}
/**
* Init
*/
void AT24CX::init(byte index, byte pageSize, int wp_pin) {
_id = AT24CX_ID | (index & 0x7);
_pageSize = pageSize;
_wp_pin = wp_pin;
Wire.begin();
if(wp_pin>=0)
pinMode(wp_pin, OUTPUT);
}
/**
* Write byte
*/
void AT24CX::write(unsigned int address, byte data) {
Wire.beginTransmission(_id);
if(Wire.endTransmission()==0) {
DISABLE_WP();
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
Wire.write(data);
Wire.endTransmission();
ENABLE_WP();
delay(10);
}
}
/**
* Write integer
*/
void AT24CX::writeInt(unsigned int address, unsigned int data) {
write(address, (byte*)&data, 2);
}
/**
* Write long
*/
void AT24CX::writeLong(unsigned int address, unsigned long data) {
write(address, (byte*)&data, 4);
}
/**
* Write float
*/
void AT24CX::writeFloat(unsigned int address, float data) {
write(address, (byte*)&data, 4);
}
/**
* Write double
*/
void AT24CX::writeDouble(unsigned int address, double data) {
write(address, (byte*)&data, 8);
}
/**
* Write chars
*/
void AT24CX::writeChars(unsigned int address, char *data, int length) {
write(address, (byte*)data, length);
}
/**
* Read integer
*/
unsigned int AT24CX::readInt(unsigned int address) {
memset(_b,0,sizeof(unsigned int));//for platforms that have 32bit integers
read(address, _b, 2);
return *(unsigned int*)&_b[0];
}
/**
* Read long
*/
unsigned long AT24CX::readLong(unsigned int address) {
read(address, _b, 4);
return *(unsigned long*)&_b[0];
}
/**
* Read float
*/
float AT24CX::readFloat(unsigned int address) {
read(address, _b, 4);
return *(float*)&_b[0];
}
/**
* Read double
*/
double AT24CX::readDouble(unsigned int address) {
read(address, _b, 8);
return *(double*)&_b[0];
}
/**
* Read chars
*/
void AT24CX::readChars(unsigned int address, char *data, int n) {
read(address, (byte*)data, n);
}
/**
* Write sequence of n bytes
*/
void AT24CX::write(unsigned int address, byte *data, int n) {
// status quo
int c = n; // bytes left to write
int offD = 0; // current offset in data pointer
int offP; // current offset in page
int nc = 0; // next n bytes to write
// write all bytes in multiple steps
while (c > 0) {
// calc offset in page
offP = address % _pageSize;
// maximal 30 bytes to write
nc = min(min(c, 30), _pageSize - offP);
write(address, data, offD, nc);
c-=nc;
offD+=nc;
address+=nc;
if(address%65536==0)
{
//re-calculate i2c address in case of AT24CM01 and AT24CM02 when crossing 65536 bytes border
if(eepromType==AT24CM02_t)
_id=(_id&0xFC)|(address>>16&0x3);
else if(eepromType==AT24CM01_t)
_id=(_id&0xFE)|(address>>16&0x1);
}
}
}
/**
* Write sequence of n bytes from offset
*/
void AT24CX::write(unsigned int address, byte *data, int offset, int n) {
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
DISABLE_WP(); //disable write protection.
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
byte *adr = data+offset;
Wire.write(adr, n);
Wire.endTransmission();
ENABLE_WP(); //enable write protection
delay(10);
}
}
/**
* Read byte
*/
byte AT24CX::read(unsigned int address) {
byte b = 0;
int r = 0;
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
Wire.requestFrom(_id, 1);
while (Wire.available() > 0 && r<1) {
b = (byte)Wire.read();
r++;
}
}
}
return b;
}
/**
* Read sequence of n bytes
*/
void AT24CX::read(unsigned int address, byte *data, int n) {
int c = n;
int offD = 0;
int offP; // current offset in page
int nc = c;
// read until are n bytes read
while (c > 0) {
// calc offset in page
offP = address % _pageSize;
// read maximal 32 bytes
nc = min(min(c,32), _pageSize - offP);
read(address, data, offD, nc);
address+=nc;
offD+=nc;
c-=nc;
if(address%65536==0)
{
//re-calculate i2c id in case of CM01 and CM02, when page border encountered
if(eepromType==AT24CM02_t)
_id=(_id&0xFC)|(address>>16&0x3);
else if(eepromType==AT24CM01_t)
_id=(_id&0xFE)|(address>>16&0x1);
}
}
}
/**
* Read sequence of n bytes to offset
*/
void AT24CX::read(unsigned int address, byte *data, int offset, int n) {
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
int r = 0;
Wire.requestFrom(_id, n);
while (Wire.available() > 0 && r<n) {
data[offset+r] = (byte)Wire.read();
r++;
}
}
}
}