-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirmUpdator.ino
321 lines (263 loc) · 8.01 KB
/
FirmUpdator.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
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
void updi_send_cmd(byte cmd) {
int l = 0;
updi_send_cmd(cmd, NULL, l);
}
static void updi_send_cmd(byte cmd, char* payload, int &length) {
switch (cmd) {
case CMD_SIGN_ON: {
UPDI_io::put(UPDI_io::double_break);
UPDI::stcs(UPDI::reg::Control_B, 8);
UPDI::stcs(UPDI::reg::Control_A, 0x80);
break;
}
case CMD_RST: {
UPDI::stcs(UPDI::reg::ASI_Reset_Request, UPDI::RESET_ON);
UPDI::stcs(UPDI::reg::ASI_Reset_Request, UPDI::RESET_OFF);
while ( UPDI::CPU_mode<0x0E>() == 0 );
break;
}
case CMD_ENTER_PROGMODE: {
updi_send_cmd(CMD_RST);
const uint8_t system_status = UPDI::CPU_mode<0xEF>();
switch (system_status) {
case 0x82:
UPDI::write_key(UPDI::NVM_Prog);
updi_send_cmd(CMD_RST);
case 0x08: {
UPDI::sts_b(NVM::NVM_base | NVM::CTRLA, NVM::PBC);
break;
}
}
break;
}
case CMD_LEAVE_PROGMODE: {
if (UPDI::CPU_mode<0xEF>() == 0x08) updi_send_cmd(CMD_RST);
break;
}
case CMD_READ_SINGNATURE: {
UPDI_io::put(UPDI::SYNCH);
UPDI_io::put(0xE5);
for (uint8_t i = 0; i < 16; i++) {
payload[i] = UPDI_io::get();
}
break;
}
case CMD_READ_MEM: {
if (UPDI::CPU_mode() != 0x08) {
Serial.println("[ERROR] Not in the programming mode!");
return;
}
const uint16_t NumBytes = payload[1] | (payload[2] << 8);
// Get physical address for reading
const uint16_t address = payload[3] | (payload[4] << 8);
// Set UPDI pointer to address
UPDI::stptr_w(address);
// Read block
UPDI::rep(NumBytes - 1);
payload[1] = UPDI::ldinc_b();
for (uint16_t i = 2; i <= NumBytes; i++) {
payload[i] = UPDI_io::get();
}
length = NumBytes;
payload[0] = 0x01; //OK
break;
}
case CMD_WRITE_MEM: {
if (UPDI::CPU_mode() != 0x08) {
Serial.println(UPDI::CPU_mode(), HEX);
return;
}
const uint8_t mem_type = payload[1];
const uint16_t buffer_length = payload[2] | (payload[3] << 8); /* number of bytes to write */
const uint16_t address = payload[4] | (payload[5] << 8);
const bool is_flash = ((mem_type == MTYPE_FLASH) || (mem_type == MTYPE_BOOT_FLASH));
const uint8_t buff_size = is_flash ? 128 : 64;
const uint8_t write_cmnd = is_flash ? NVM::WP : NVM::ERWP;
switch (mem_type) {
case MTYPE_FUSE_BITS:
case MTYPE_LOCK_BITS: {
NVM_fuse_write (address, payload[6]);
break;
}
case MTYPE_FLASH:
case MTYPE_BOOT_FLASH:
case MTYPE_EEPROM_XMEGA:
case MTYPE_USERSIG: {
NVM_buffered_write(address, buffer_length, buff_size, write_cmnd, payload);
break;
}
}
break;
}
case CMD_ERASE_CHIP: {
const uint8_t erase_type = payload[1];
const uint16_t address = payload[2] | (payload[3] << 8);
switch (erase_type) {
case 0: {
UPDI::write_key(UPDI::Chip_Erase);
updi_send_cmd(CMD_RST);
updi_send_cmd(CMD_ENTER_PROGMODE);
break;
}
case 4:
case 5: {
NVM::wait<false>();
UPDI::sts_b(address, 0xFF);
NVM::command<false>(NVM::ER);
break;
}
case 6:
case 7: {
break;
}
}
break;
}
}
}
void uplaodSketch() {
/*const char* SERVER = "10.144.1.242"; // Set your correct hostname
const unsigned short SERVER_PORT = 8080; // Commonly 80 (HTTP) | 443 (HTTPS)
const char* PATH = "/slave.hex";
HttpClient client(wifiClient, SERVER, SERVER_PORT);
Serial.print("Get the update file:");
client.get(PATH);
int statusCode = client.responseStatusCode();
Serial.print("Update status code: ");
Serial.println(statusCode);
if (statusCode != 200) {
client.stop();
Serial.print("Cannot get the update file!");
return;
}
long length = client.contentLength();
if (length == HttpClient::kNoContentLengthHeader) {
client.stop();
Serial.println("Server didn't provide Content-length header. Can't continue with update.");
return;
}
Serial.print("Server returned update file of size ");
Serial.print(length);
Serial.println(" bytes");
byte b;
char file_buf[length];
int i = 0;
while (length > 0) {
if (!client.readBytes(&b, 1)) break;
file_buf[i] = b;
i++;
length--;
}
client.stop();
if (length > 0) {
Serial.print("Timeout downloading update file at ");
Serial.print(length);
Serial.println(" bytes. Can't continue with update.");
return;
}
Serial.println("Sketch update apply and reset.");
delay(1000);
int line = 1;
for (int i = 0; i < sizeof(file_buf); i++)
if (file_buf[i] == '\n') line++;
char flash_buf[line * 16];
processFile(file_buf, sizeof(file_buf), line, flash_buf);*/
/*Serial.println("Reboot");
char p[1] = {0x01};
sendCmd(Serial3, CMD_UPDATE_FIRM, p, 1);*/
int l;
char *p;
UPDI_io::init();
/*** ***/
updi_send_cmd(CMD_SIGN_ON);
/*** Enter programming mode ***/
updi_send_cmd(CMD_ENTER_PROGMODE);
Serial.println("[Done] Enter Programming Mode");
/*** Read Device Signature ***/
/*l = 16;
p = (char *)malloc(16 * sizeof(char));
updi_send_cmd(CMD_READ_SINGNATURE, p, l);
Serial.print("RECEIVE BYTE: ");
printHEX(p, l);
free(p);
Serial.println("[Done] Read Signature");*/
/*** Erase Chip ***/
l = 2;
p = (char *)malloc(l * sizeof(char));
p[1] = 0;
updi_send_cmd(CMD_ERASE_CHIP, p, l);
free(p);
Serial.println("[Done] Erase Chip");
/*** Write MEM ***/
l = 512 + 6; //6 + 255(payload size)
p = (char *)calloc(l, sizeof(char));
p[1] = MTYPE_BOOT_FLASH;
p[2] = l - 6;
p[3] = l - 6 >> 8;
p[4] = UPDI_FLASH_APP_START;
p[5] = UPDI_FLASH_APP_START >> 8;
for (int i = 0; i < l - 6; i++) p[6 + i] = i;
updi_send_cmd(CMD_WRITE_MEM, p, l);
free(p);
Serial.println("[Done] Write Mem");
/*** Read MEM ***/
l = (255) + 2;
p = (char *)malloc(l * sizeof(char));
p[1] = l - 2; //size = 10
p[2] = (l - 2) >> 8;
p[3] = UPDI_FLASH_APP_START + 256;
p[4] = UPDI_FLASH_APP_START + 256 >> 8;
updi_send_cmd(CMD_READ_MEM, p, l);
Serial.print("RECEIVE BYTE: ");
printHEX(p, l + 1);
free(p);
Serial.println("[Done] Read Mem");
/*** Leaving programming mode ***/
updi_send_cmd(CMD_LEAVE_PROGMODE);
Serial.println("[Done] Leave Programming Mode");
//uploadHEXFile(flash_buf, sizeof(flash_buf));
}
/*void uploadHEXFile(char *buf, int length) {
int page_num = length / 128 + (length % 128 > 0 ? 1 : 0);
int count = 0;
int adr = 0x00;
Serial.print("Total pages # is ");
Serial.println(page_num);
for (int i = 0; i < page_num; i++) { //looping each page
Serial.print("uploading page #");
Serial.println(i + 1);
// Set memory address
char addrs[2] = {
(adr & 0xff),
((adr >> 8) & 0xff),
};
updi_send_cmd(CMD_LOAD_ADDRESS, addrs, sizeof(addrs));
char ret[2];
readch(ret, sizeof(ret));
printHEX(ret, sizeof(ret));
delay(10);
// Upload page
char page[128];
for (int j = 0; j < sizeof(page); j++) page[j] = 0xff;
for (int j = 0; j < sizeof(page); j++) {
if (count < length) {
page[j] = buf[count];
count++;
}
}
for (int j = 0; j < sizeof(page); j++) Serial.print(page[j]);
Serial.println();
Serial.println("-----");
updi_send_cmd(CMD_PROG_PAGE, page, sizeof(page));
char ret2[2];
readch(ret2, sizeof(ret2));
printHEX(ret2, sizeof(ret2));
delay(10);
updi_send_cmd(CMD_READ_PAGE);
char ret3[130];
readch(ret3, sizeof(ret3));
printHEX(ret3, sizeof(ret3));
delay(10);
adr += 0x40;
}
}*/