forked from cyberp/AT24Cx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AT24CX.h
162 lines (142 loc) · 6.95 KB
/
AT24CX.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
/**
AT24CX.h
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.
*/
#ifndef AT24CX_h
#define AT24CX_h
// includes
#include <Arduino.h>
// byte
typedef uint8_t byte;
// AT24Cx I2C adress
// 80
// 0x50
#define AT24CX_ID B1010000
#define DISABLE_WP() do{if(_wp_pin>=0)digitalWrite(_wp_pin, LOW);}while(false);
#define ENABLE_WP() do{if(_wp_pin>=0)digitalWrite(_wp_pin, HIGH);}while(false);
enum eeprom_type_t{
AT24CX_t,
AT24C32_t,
AT24C64_t,
AT24C128_t,
AT24C256_t,
AT24C512_t,
AT24CM01_t,
AT24CM02_t,
};
// general class definition
class AT24CX {
public:
AT24CX(byte index=0, byte pageSize=32, int wp_pin=-1);
void write(unsigned int address, byte data);
void write(unsigned int address, byte *data, int n);
void writeInt(unsigned int address, unsigned int data);
void writeLong(unsigned int address, unsigned long data);
void writeFloat(unsigned int address, float data);
void writeDouble(unsigned int address, double data);
void writeChars(unsigned int address, char *data, int length);
byte read(unsigned int address);
void read(unsigned int address, byte *data, int n);
unsigned int readInt(unsigned int address);
unsigned long readLong(unsigned int address);
float readFloat(unsigned int address);
double readDouble(unsigned int address);
void readChars(unsigned int address, char *data, int n);
protected:
void init(byte index, byte pageSize, int wp_pin);
int _id;
byte _b[8];
uint16_t _pageSize;
int _wp_pin; //write protect pin
eeprom_type_t eepromType;
private:
void read(unsigned int address, byte *data, int offset, int n);
void write(unsigned int address, byte *data, int offset, int n);
};
// AT24C32 class definiton
class AT24C32 : public AT24CX {
public:
AT24C32(byte index=0, int wp_pin=-1);
};
// AT24C64 class definiton
class AT24C64 : public AT24CX {
public:
AT24C64(byte index=0, int wp_pin=-1);
};
// AT24C128 class definiton
class AT24C128 : public AT24CX {
public:
AT24C128(byte index=0, int wp_pin=-1);
};
// AT24C256 class definiton
class AT24C256 : public AT24CX {
public:
AT24C256(byte index=0, int wp_pin=-1);
};
// AT24C512 class definiton
class AT24C512 : public AT24CX {
public:
AT24C512(byte index=0, int wp_pin=-1);
};
//AT24CM02 class definition
class AT24CM02 : public AT24CX {
public:
AT24CM02(byte index=0, int wp_pin=-1);
void write(unsigned int address, byte data){
_id=(_id&0xFC)|(address>>16&0x3); //insert 17th and 16th bit of data word address in i2c address
AT24CX::write(address,data);
}
void write(unsigned int address, byte *data, int n){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::write(address,data,n);}
void writeInt(unsigned int address, unsigned int data){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::writeInt(address,data);}
void writeLong(unsigned int address, unsigned long data){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::writeLong(address,data);}
void writeFloat(unsigned int address, float data){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::writeFloat(address,data);}
void writeDouble(unsigned int address, double data){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::writeDouble(address,data);}
void writeChars(unsigned int address, char *data, int length){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::writeChars(address,data,length);}
byte read(unsigned int address){_id=(_id&0xFC)|(address>>16&0x3);return AT24CX::read(address);}
void read(unsigned int address, byte *data, int n){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::read(address,data,n);}
unsigned int readInt(unsigned int address){_id=(_id&0xFC)|(address>>16&0x3);return AT24CX::readInt(address);}
unsigned long readLong(unsigned int address){_id=(_id&0xFC)|(address>>16&0x3);return AT24CX::readLong(address);}
float readFloat(unsigned int address){_id=(_id&0xFC)|(address>>16&0x3);return AT24CX::readFloat(address);}
double readDouble(unsigned int address){_id=(_id&0xFC)|(address>>16&0x3);return AT24CX::readDouble(address);}
void readChars(unsigned int address, char *data, int n){_id=(_id&0xFC)|(address>>16&0x3);AT24CX::readChars(address,data,n);}
};
//AT24CM01 class definition
class AT24CM01 : public AT24CX {
public:
AT24CM01(byte index=0, int wp_pin=-1);
void write(unsigned int address, byte data){
_id=(_id&0xFE)|(address>>16&0x1); //insert 16th bit of data word address in i2c address
AT24CX::write(address,data);
}
void write(unsigned int address, byte *data, int n){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::write(address,data,n);}
void writeInt(unsigned int address, unsigned int data){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::writeInt(address,data);}
void writeLong(unsigned int address, unsigned long data){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::writeLong(address,data);}
void writeFloat(unsigned int address, float data){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::writeFloat(address,data);}
void writeDouble(unsigned int address, double data){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::writeDouble(address,data);}
void writeChars(unsigned int address, char *data, int length){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::writeChars(address,data,length);}
byte read(unsigned int address){_id=(_id&0xFE)|(address>>16&0x1);return AT24CX::read(address);}
void read(unsigned int address, byte *data, int n){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::read(address,data,n);}
unsigned int readInt(unsigned int address){_id=(_id&0xFE)|(address>>16&0x1);return AT24CX::readInt(address);}
unsigned long readLong(unsigned int address){_id=(_id&0xFE)|(address>>16&0x1);return AT24CX::readLong(address);}
float readFloat(unsigned int address){_id=(_id&0xFE)|(address>>16&0x1);return AT24CX::readFloat(address);}
double readDouble(unsigned int address){_id=(_id&0xFE)|(address>>16&0x1);return AT24CX::readDouble(address);}
void readChars(unsigned int address, char *data, int n){_id=(_id&0xFE)|(address>>16&0x1);AT24CX::readChars(address,data,n);}
};
#endif