-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_eeprom.ino
158 lines (144 loc) · 3.74 KB
/
i2c_eeprom.ino
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
void EEPROM_Print(us8 address)
{
Wire.beginTransmission((int)address); //0x53 quicK 0x50 Kard
//Wire.send(0xFA);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom((int)address, 6); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
char c = Wire.receive(); // receive a byte as character
MySerial.print(c,HEX); // print the character
MySerial.print(".");
}
PrintCR();
}
us8 EEPROM_Init()
{
//set all eeprom chip selects to low
us8 i;
for( i = 0; i < 7; i++ ) {
pinMode(KardIO[i][5], OUTPUT);
digitalWrite(KardIO[i][5],LOW);
}
delay(10);
}
us8 EEPROM_Read(us8 cs, us8 ADDR, us16 *bytes_read)
{
*bytes_read = 0;
us8 c;
digitalWrite(cs,HIGH);
Wire.beginTransmission(0x51);//0x53 quicK 0x51 Kard
Wire.send(ADDR);
Wire.endTransmission();
Wire.requestFrom(0x51, 1); // request 1 bytes from slave device
// can be up to limit of memory
while(Wire.available()) // slave may send less than requested
{
c = Wire.receive(); // receive a byte as character
(*bytes_read)++;
}
digitalWrite(cs,LOW);
return c;
}
void EEPROM_Write(us8 cs, us8 ADDR, us8 val)
{
// MySerial.print(ADDR, HEX);
// MySerial.print(":[");
// MySerial.print(val, HEX);
// MySerial.print("]");
// MySerial.write(val);
// PrintCR();
digitalWrite(cs,HIGH);
Wire.beginTransmission(0x51);//0x53 quicK 0x51 Kard
Wire.send(ADDR);
Wire.send(val);//up to 8 in sequence
Wire.endTransmission();
delay(10); //delay while writing
digitalWrite(cs,LOW);
}
void EEPROM_Write(us8 cs, us8 ADDR, String str)
{
EEPROM_Init();
us16 len = str.length();
for(int i = 0; i < len; i++) {
EEPROM_Write(cs, ADDR + i, str[i]);
}
}
void EEPROM_Write_Pad(us8 cs, us16 address, us16 last_address, us8 pad)
{
EEPROM_Init();
for(int i = address; i <= last_address; i++) {
EEPROM_Write(cs, i, pad);
}
}
String EEPROM_Write_JSON(us8 kard, us16 json)
{
EEPROM_Init();
MySerial.print("Kard");
MySerial.print(kard, DEC);
MySerial.print(":");
Sha1.init();
Sha1.print(kard_json[json]);
String sha_result = HashToString(Sha1.result(), 4);
EEPROM_Write(KardIO[kard][5], 0, sha_result);
EEPROM_Write(KardIO[kard][5], 8, kard_json[json]);
EEPROM_Write_Pad(KardIO[kard][5], 8 + kard_json[json].length(), 208, 0);
EEPROM_Write_Pad(KardIO[kard][5], 209, 255, 255);
MySerial.print(sha_result);
MySerial.print(kard_json[json]);
PrintCR();
return sha_result + kard_json[json];
}
String EEPROM_Read_JSON(us8 kard)
{
EEPROM_Init();
String json;
us16 address;
MySerial.print("kard");
MySerial.print(kard,HEX);
MySerial.print(":");
us16 bytes_read;
for( address = 0; address < 209; address++ ) {
us8 c = EEPROM_Read( KardIO[kard][5], address, &bytes_read);
if( bytes_read == 0 )
{
MySerial.print("EEPROM Not Found");
break;
}
if( c == 0 ) ; // do nothing
else if( c < 128 ) {
MySerial.write(c);
json += c;
}
else {
MySerial.print("[");
MySerial.print(c,HEX);
MySerial.print("]");
}
}
PrintCR();
return json;
}
//////////////////////////////////////////////////
// High Level EEPROM Commands
//////////////////////////////////////////////////
void EEPROM_JSON_WRITE(us8 json, us8 kard)
{
EEPROM_Init();
String json_write = EEPROM_Write_JSON(kard, json);
String json_read = EEPROM_Read_JSON(kard);
if( json_write == json_read ) {
for(us8 j = 0; j < 6; j++ )
{
pinMode(KardIO[kard][j], OUTPUT);
digitalWrite(KardIO[kard][j], LOW);
}
}
else {
MySerial.println("Mismatch:");
MySerial.println(json_write);
MySerial.println(json_read);
}
MySerial.println("=========================");
}