-
Notifications
You must be signed in to change notification settings - Fork 1
/
eeprog.ino
167 lines (142 loc) · 3.02 KB
/
eeprog.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
158
159
160
161
162
163
164
165
166
167
// Arduino NANO EEPROM programmer.
// (C) 2018 Nandor Licker. All rights reserved.
#define OE 2
#define IO0 3
#define IO7 10
#define WE 11
#define CLK 12
#define DATA 13
#define VERSION 1
#define BUFFER_SIZE 1024
uint8_t data[BUFFER_SIZE];
uint16_t len;
void shift(uint16_t addr)
{
for (int i = 15; i >= 0; i -= 1) {
digitalWrite(CLK, LOW);
digitalWrite(DATA, addr & (1 << i) ? HIGH : LOW);
digitalWrite(CLK, HIGH);
}
}
uint8_t read_bus()
{
uint8_t data = 0;
for (int i = 0; i < 8; i += 1) {
pinMode(IO0 + i, INPUT);
data |= digitalRead(IO0 + i) ? (1 << i) : 0;
}
return data;
}
void write(uint16_t addr, uint8_t data)
{
// Set the address.
digitalWrite(OE, HIGH);
shift(addr);
// Write the input.
for (int i = 0; i < 8; i += 1) {
pinMode(IO0 + i, OUTPUT);
digitalWrite(IO0 + i, data & (1 << i) ? HIGH : LOW);
}
// Write.
digitalWrite(WE, LOW);
delayMicroseconds(1);
digitalWrite(WE, HIGH);
delay(10);
}
uint8_t read(uint16_t addr)
{
digitalWrite(OE, LOW);
shift(addr);
uint8_t data = read_bus();
digitalWrite(OE, HIGH);
return data;
}
bool upload(uint16_t offset)
{
// Write data.
for (uint16_t i = 0; i < sizeof(data); i += 64) {
for (uint8_t j = 0; j < 64; ++j) {
write(offset + i + j, data[i + j]);
}
Serial.print('.');
}
// Read to verify.
for (uint16_t i = 0; i < sizeof(data); ++i) {
if (read(offset + i) != data[i]) {
return false;
}
}
return true;
}
uint16_t serial16()
{
while (!Serial.available());
uint8_t lo = Serial.read();
while (!Serial.available());
uint8_t hi = Serial.read();
return ((uint16_t)hi << 8) | lo;
}
void setup()
{
pinMode(OE, OUTPUT);
pinMode(WE, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(DATA, OUTPUT);
digitalWrite(WE, HIGH);
digitalWrite(CLK, HIGH);
Serial.setTimeout(10000);
Serial.begin(115200);
while (!Serial);
}
void loop() {
switch (Serial.read()) {
case -1: {
return;
}
// Read version.
case 'v': {
Serial.println(VERSION, DEC);
return;
}
// Read buffer size.
case 'b': {
Serial.println(BUFFER_SIZE, DEC);
return;
}
// Upload EEPROM.
case 'u': {
uint16_t count = serial16();
if (count % BUFFER_SIZE != 0) {
Serial.print('n');
break;
}
for (uint16_t i = 0; i < count; ++i) {
if (Serial.readBytes(data, BUFFER_SIZE) != BUFFER_SIZE) {
Serial.print('n');
break;
}
if (upload(i * BUFFER_SIZE)) {
Serial.print('y');
} else {
Serial.print('n');
break;
}
}
return;
}
// Download EEPROM.
case 'd': {
uint16_t chunk = serial16();
uint16_t off = chunk * BUFFER_SIZE;
for (uint16_t i = 0; i < BUFFER_SIZE; ++i) {
data[i] = read(off + i);
}
Serial.write(data, BUFFER_SIZE);
return;
}
default: {
Serial.println("Unknown command");
return;
}
}
}