1
|
#include <stdio.h>
|
2
|
#include <stdlib.h>
|
3
|
#include <errno.h>
|
4
|
#include <string.h>
|
5
|
#include "sam3u.h"
|
6
|
#include "serial.h"
|
7
|
|
8
|
int sam3uRead32(HANDLE fd, uint32_t address, uint32_t* value)
|
9
|
{
|
10
|
char str[32];
|
11
|
sprintf(str, "w%08X,4#", address);
|
12
|
if(serialPutS(fd, str) < 0)
|
13
|
return -1;
|
14
|
if(serialExpect(fd, "\n\r0x", 100) < 0)
|
15
|
return -1;
|
16
|
if(serialGetS(fd, str, 8, 100) < 0)
|
17
|
return -1;
|
18
|
str[8] = '\0';
|
19
|
errno = 0;
|
20
|
*value = strtoll(str, NULL, 16);
|
21
|
if(errno != 0) {
|
22
|
fprintf(stderr, "number conversion failed: %s", strerror(errno));
|
23
|
return -1;
|
24
|
}
|
25
|
if(serialExpect(fd, "\n\r>", 100) < 0)
|
26
|
return -1;
|
27
|
return 0;
|
28
|
}
|
29
|
|
30
|
int sam3uRead16(HANDLE fd, uint32_t address, uint16_t* value)
|
31
|
{
|
32
|
char str[32];
|
33
|
sprintf(str, "h%08X,2#", address);
|
34
|
if(serialPutS(fd, str) < 0)
|
35
|
return -1;
|
36
|
if(serialExpect(fd, "\n\r0x", 100) < 0)
|
37
|
return -1;
|
38
|
if(serialGetS(fd, str, 4, 100) < 0)
|
39
|
return -1;
|
40
|
str[4] = '\0';
|
41
|
errno = 0;
|
42
|
*value = strtol(str, NULL, 16);
|
43
|
if(errno != 0) {
|
44
|
fprintf(stderr, "number conversion failed: %s", strerror(errno));
|
45
|
return -1;
|
46
|
}
|
47
|
if(serialExpect(fd, "\n\r>", 100) < 0)
|
48
|
return -1;
|
49
|
return 0;
|
50
|
}
|
51
|
|
52
|
int sam3uRead8(HANDLE fd, uint32_t address, uint8_t* value)
|
53
|
{
|
54
|
char str[32];
|
55
|
sprintf(str, "o%08X,1#", address);
|
56
|
if(serialPutS(fd, str) < 0)
|
57
|
return -1;
|
58
|
if(serialExpect(fd, "\n\r0x", 100) < 0)
|
59
|
return -1;
|
60
|
if(serialGetS(fd, str, 2, 100) < 0)
|
61
|
return -1;
|
62
|
str[2] = '\0';
|
63
|
errno = 0;
|
64
|
*value = strtol(str, NULL, 16);
|
65
|
if(errno != 0) {
|
66
|
fprintf(stderr, "number conversion failed: %s", strerror(errno));
|
67
|
return -1;
|
68
|
}
|
69
|
if(serialExpect(fd, "\n\r>", 100) < 0)
|
70
|
return -1;
|
71
|
return 0;
|
72
|
}
|
73
|
|
74
|
int sam3uWrite32(HANDLE fd, uint32_t address, uint32_t value)
|
75
|
{
|
76
|
char str[32];
|
77
|
sprintf(str, "W%08X,%08X#", address, value);
|
78
|
if(serialPutS(fd, str) < 0)
|
79
|
return -1;
|
80
|
if(serialExpect(fd, "\n\r>", 100) < 0)
|
81
|
return -1;
|
82
|
return 0;
|
83
|
}
|
84
|
|
85
|
int sam3uWrite16(HANDLE fd, uint32_t address, uint16_t value)
|
86
|
{
|
87
|
char str[32];
|
88
|
sprintf(str, "H%08X,%04X#", address, value);
|
89
|
if(serialPutS(fd, str) < 0)
|
90
|
return -1;
|
91
|
if(serialExpect(fd, "\n\r>", 100) < 0)
|
92
|
return -1;
|
93
|
return 0;
|
94
|
}
|
95
|
|
96
|
int sam3uWrite8(HANDLE fd, uint32_t address, uint8_t value)
|
97
|
{
|
98
|
char str[32];
|
99
|
sprintf(str, "O%08X,%02X#", address, value);
|
100
|
if(serialPutS(fd, str) < 0)
|
101
|
return -1;
|
102
|
if(serialExpect(fd, "\n\r>", 100) < 0)
|
103
|
return -1;
|
104
|
return 0;
|
105
|
}
|
106
|
|
107
|
int sam3uRun(HANDLE fd, uint32_t address)
|
108
|
{
|
109
|
char str[32];
|
110
|
sprintf(str, "G%08X#", address);
|
111
|
if(serialPutS(fd, str) < 0)
|
112
|
return -1;
|
113
|
return 0;
|
114
|
}
|
115
|
|
116
|
int sam3uDetect(HANDLE fd, uint32_t* chipID)
|
117
|
{
|
118
|
uint8_t c;
|
119
|
|
120
|
while(serialGetC(fd, &c, 100) >= 0) ;
|
121
|
|
122
|
if(serialPutS(fd, "T#") < 0)
|
123
|
return -1;
|
124
|
|
125
|
if(serialExpect(fd, "\n\r\n\r>", 100) < 0) {
|
126
|
fprintf(stderr, "did not receive expected answer\n");
|
127
|
return -1;
|
128
|
}
|
129
|
|
130
|
return sam3uRead32(fd, 0x400e0740, chipID);
|
131
|
}
|
132
|
|
133
|
int sam3uReadUniqueID(HANDLE fd, int bank, uint8_t* uniqueID)
|
134
|
{
|
135
|
uint32_t regBase;
|
136
|
uint32_t flashBase;
|
137
|
uint32_t fsr;
|
138
|
int i;
|
139
|
|
140
|
switch(bank) {
|
141
|
case 0:
|
142
|
regBase = 0x400e0800;
|
143
|
flashBase = 0x80000;
|
144
|
break;
|
145
|
case 1:
|
146
|
regBase = 0x400e0a00;
|
147
|
flashBase = 0x100000;
|
148
|
break;
|
149
|
default:
|
150
|
fprintf(stderr, "illegal flash bank");
|
151
|
return -1;
|
152
|
}
|
153
|
|
154
|
if(sam3uWrite32(fd, regBase + 0x04, 0x5a00000e) < 0)
|
155
|
return -1;
|
156
|
|
157
|
do {
|
158
|
if(sam3uRead32(fd, regBase + 0x08, &fsr) < 0)
|
159
|
return -1;
|
160
|
} while((fsr & 1) != 0);
|
161
|
|
162
|
for(i = 0; i < 16; i++) {
|
163
|
if(sam3uRead8(fd, flashBase + i, uniqueID + i) < 0)
|
164
|
return -1;
|
165
|
}
|
166
|
|
167
|
if(sam3uWrite32(fd, regBase + 0x04, 0x5a00000f) < 0)
|
168
|
return -1;
|
169
|
|
170
|
do {
|
171
|
if(sam3uRead32(fd, regBase + 0x08, &fsr) < 0)
|
172
|
return -1;
|
173
|
} while((fsr & 1) == 0);
|
174
|
|
175
|
return 0;
|
176
|
}
|
177
|
|
178
|
int sam3uFlash(HANDLE fd, int bank, const void* bin, size_t binSize)
|
179
|
{
|
180
|
uint32_t regBase;
|
181
|
uint32_t flashBase;
|
182
|
uint32_t fsr;
|
183
|
uint32_t flID;
|
184
|
uint32_t flSize;
|
185
|
uint32_t flPageSize;
|
186
|
uint32_t flNbPlane;
|
187
|
uint8_t buf[8192];
|
188
|
uint32_t ofs;
|
189
|
uint32_t todo;
|
190
|
uint32_t len;
|
191
|
uint32_t idx;
|
192
|
|
193
|
switch(bank) {
|
194
|
case 0:
|
195
|
regBase = 0x400e0800;
|
196
|
flashBase = 0x80000;
|
197
|
break;
|
198
|
case 1:
|
199
|
regBase = 0x400e0a00;
|
200
|
flashBase = 0x100000;
|
201
|
break;
|
202
|
default:
|
203
|
fprintf(stderr, "illegal flash bank");
|
204
|
return -1;
|
205
|
}
|
206
|
|
207
|
printf("reading flash descriptor");
|
208
|
|
209
|
if(sam3uWrite32(fd, regBase + 0x04, 0x5a000000) < 0)
|
210
|
goto error;
|
211
|
|
212
|
do {
|
213
|
if(sam3uRead32(fd, regBase + 0x08, &fsr) < 0)
|
214
|
goto error;
|
215
|
} while((fsr & 1) == 0);
|
216
|
|
217
|
|
218
|
if(sam3uRead32(fd, regBase + 0x0c, &flID) < 0)
|
219
|
goto error;
|
220
|
if(sam3uRead32(fd, regBase + 0x0c, &flSize) < 0)
|
221
|
goto error;
|
222
|
if(sam3uRead32(fd, regBase + 0x0c, &flPageSize) < 0)
|
223
|
goto error;
|
224
|
if(sam3uRead32(fd, regBase + 0x0c, &flNbPlane) < 0)
|
225
|
goto error;
|
226
|
|
227
|
printf(" -- ok\n");
|
228
|
printf("Flash ID: 0x%08x\n", flID);
|
229
|
printf("Flash size: %d Bytes\n", flSize);
|
230
|
printf("Page size: %d Bytes\n", flPageSize);
|
231
|
printf("Number of planes: %d\n", flNbPlane);
|
232
|
|
233
|
#if 1
|
234
|
printf("erasing flash"); fflush(stdout);
|
235
|
|
236
|
if(sam3uWrite32(fd, regBase + 0x04, 0x5a000005) < 0)
|
237
|
goto error;
|
238
|
|
239
|
do {
|
240
|
if(sam3uRead32(fd, regBase + 0x08, &fsr) < 0)
|
241
|
goto error;
|
242
|
} while((fsr & 1) == 0);
|
243
|
|
244
|
printf(" -- ok\n");
|
245
|
#endif
|
246
|
|
247
|
ofs = 0;
|
248
|
for(todo = binSize; todo > 0; todo -= len) {
|
249
|
printf("flashing @ 0x%08x", ofs); fflush(stdout);
|
250
|
len = todo;
|
251
|
if(len > flPageSize)
|
252
|
len = flPageSize;
|
253
|
memset(buf, 0xff, sizeof(buf));
|
254
|
memcpy(buf, ((uint8_t*)bin) + ofs, len);
|
255
|
for(idx = 0; idx < flPageSize / 4; idx++) {
|
256
|
if(sam3uWrite32(fd, flashBase + ofs + (idx * 4), ((uint32_t*)buf)[idx]) < 0)
|
257
|
goto error;
|
258
|
}
|
259
|
|
260
|
if(sam3uWrite32(fd, regBase + 0x04, 0x5a000000 | ((ofs / flPageSize) << 8) | 0x03) < 0)
|
261
|
goto error;
|
262
|
|
263
|
do {
|
264
|
if(sam3uRead32(fd, regBase + 0x08, &fsr) < 0)
|
265
|
goto error;
|
266
|
} while((fsr & 1) == 0);
|
267
|
|
268
|
printf(" -- ok\n");
|
269
|
|
270
|
ofs += flPageSize;
|
271
|
}
|
272
|
|
273
|
ofs = 0;
|
274
|
for(todo = binSize; todo > 0; todo -= len) {
|
275
|
printf("verifying @ 0x%08x", ofs); fflush(stdout);
|
276
|
len = todo;
|
277
|
if(len > flPageSize)
|
278
|
len = flPageSize;
|
279
|
|
280
|
for(idx = 0; idx < flPageSize / 4; idx++) {
|
281
|
if(sam3uRead32(fd, flashBase + ofs + (idx * 4), ((uint32_t*)buf) + idx) < 0)
|
282
|
goto error;
|
283
|
}
|
284
|
|
285
|
if(memcmp(buf, ((uint8_t*)bin) + ofs, len) == 0)
|
286
|
printf(" -- ok\n");
|
287
|
else printf(" -- ERROR\n");
|
288
|
|
289
|
ofs += flPageSize;
|
290
|
}
|
291
|
|
292
|
printf("disabling SAM-BA"); fflush(stdout);
|
293
|
if(sam3uWrite32(fd, regBase + 0x04, 0x5a000000 | (1 << 8) | 0x0b) < 0)
|
294
|
goto error;
|
295
|
|
296
|
do {
|
297
|
if(sam3uRead32(fd, regBase + 0x08, &fsr) < 0)
|
298
|
goto error;
|
299
|
} while((fsr & 1) == 0);
|
300
|
|
301
|
printf(" -- ok\n");
|
302
|
|
303
|
return 0;
|
304
|
|
305
|
error:
|
306
|
printf(" -- ERROR\n");
|
307
|
return -1;
|
308
|
}
|