-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnuvotool.c
More file actions
357 lines (322 loc) · 10.7 KB
/
Copy pathnuvotool.c
File metadata and controls
357 lines (322 loc) · 10.7 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// nuvotool
//
// Nuvoton ISP serial programmer
// Write HEX/BIN file to APROM
//
// https://github.com/matveyt/nuvotool
//
#include "stdz.h"
#include "ihx.h"
#include "isp.h"
#include "ucomm.h"
enum {
CONFIG_LOCK, CONFIG_RPD, CONFIG_OCDEN, CONFIG_OCDPWM, CONFIG_CBS, CONFIG_LDSIZE,
CONFIG_CBORST, CONFIG_BOIAP, CONFIG_CBOV, CONFIG_CBODEN, CONFIG_WDTEN
};
static void list_ports(void);
static size_t nuvoton_flashsize(uint32_t id);
static size_t nuvoton_pagesize(uint32_t id);
static size_t nuvoton_ldromsize(uint8_t ldsize);
static uint8_t nuvoton_ldsize(size_t ldsz);
static void print_config(const CONFIG* configp);
static int str2bit(const char* str, int value_on);
static int str2int(const char* const* tokens, const int* numbers, size_t n,
const char* str);
// user options
static struct {
char* file;
char* port;
bool erase;
unsigned config_flags; // 1 << CONFIG_XXX
CONFIG config;
} opt = {0};
/*noreturn*/
static void usage(int status)
{
if (status != 0)
fprintf(stderr, "Try '%s --help' for more information.\n", z_getprogname());
else
printf(
"Usage: %s [OPTION]... [FILE]\n"
"Nuvoton ISP serial programmer. Write HEX/BIN file to APROM.\n"
"\n"
"-p, --port=PORT Select serial device\n"
"-x, --erase Erase APROM first\n"
"-c, --config=X[,X...] Setup CONFIG\n"
"-l, --list-ports List available ports only\n"
"-h, --help Show this message and exit\n"
"\n"
"Valid CONFIG fields: lock, rpd, ocden, ocdpwm, cbs, ldsize=0,1024,2048,3072,4096,\n"
"\tcborst, boiap, cboden, cbov=2.2,2.7,3.7,4.4, wdten=disable,enable,always\n"
"Note that '--config rpd' or '--config rpd=yes' stands for '--config rpd=0',\n"
"\twhile '--config cborst' for '--config cborst=1', etc.\n",
z_getprogname());
exit(status);
}
static void parse_args(int argc, char* argv[])
{
z_setprogname(argv[0]);
static struct z_option lopts[] = {
{ "port", z_required_argument, NULL, 'p' },
{ "erase", z_no_argument, NULL, 'x' },
{ "config", z_required_argument, NULL, 'c' },
{ "list-ports", z_no_argument, NULL, 'l' },
{ "help", z_no_argument, NULL, 'h' },
{0}
};
static char* const subopts[] = {
[CONFIG_LOCK] = "lock",
[CONFIG_RPD] = "rpd",
[CONFIG_OCDEN] = "ocden",
[CONFIG_OCDPWM] = "ocdpwm",
[CONFIG_CBS] = "cbs",
[CONFIG_LDSIZE] = "ldsize",
[CONFIG_CBORST] = "cborst",
[CONFIG_BOIAP] = "boiap",
[CONFIG_CBOV] = "cbov",
[CONFIG_CBODEN] = "cboden",
[CONFIG_WDTEN] = "wdten",
NULL
};
int c;
while ((c = z_getopt_long(argc, argv, "p:xc:lh", lopts, NULL)) != -1) {
switch (c) {
case 'p':
free(opt.port);
opt.port = z_strdup(z_optarg);
break;
case 'x':
opt.erase = true;
break;
case 'c':
do {
char* subarg;
int subopt = z_getsubopt(&z_optarg, subopts, &subarg);
if (subopt < 0)
continue;
opt.config_flags |= 1 << subopt;
switch (subopt) {
case CONFIG_LOCK:
opt.config.bit.LOCK = str2bit(subarg, 0);
break;
case CONFIG_RPD:
opt.config.bit.RPD = str2bit(subarg, 0);
break;
case CONFIG_OCDEN:
opt.config.bit.OCDEN = str2bit(subarg, 0);
break;
case CONFIG_OCDPWM:
opt.config.bit.OCDPWM = str2bit(subarg, 0);
break;
case CONFIG_CBS:
opt.config.bit.CBS = str2bit(subarg, 0);
break;
case CONFIG_LDSIZE:
opt.config.bit.LDSIZE = nuvoton_ldsize(strtoul(subarg, NULL, 0));
break;
case CONFIG_CBORST:
opt.config.bit.CBORST = str2bit(subarg, 1);
break;
case CONFIG_BOIAP:
opt.config.bit.BOIAP = str2bit(subarg, 1);
break;
case CONFIG_CBOV:
opt.config.bit.CBOV = str2int(
(const char*[]){ "2.2", "2.7", "3.7", "4.4" },
(const int[]){ 3, 2, 1, 0 }, 4, subarg);
break;
case CONFIG_CBODEN:
opt.config.bit.CBODEN = str2bit(subarg, 1);
break;
case CONFIG_WDTEN:
opt.config.bit.WDTEN = str2int(
(const char*[]){ "disable", "enable", "always" },
(const int[]){ 15, 5, 0 }, 3, subarg);
break;
default:
break;
}
} while (*z_optarg != 0);
break;
case 'l':
list_ports();
exit(EXIT_SUCCESS);
break;
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
}
}
if (z_optind < argc)
opt.file = z_strdup(argv[z_optind]);
}
int main(int argc, char* argv[])
{
parse_args(argc, argv);
// ISP connection
uint8_t data[ISP_DATA_SIZE];
intptr_t isp = ucomm_open(opt.port, 115200, 0x801/*8-N-1*/);
if (isp < 0) {
if (opt.port != NULL)
z_error(EXIT_FAILURE, errno, "ucomm_open(%s)", opt.port);
z_warnx("missing port name");
usage(EXIT_FAILURE);
}
free(opt.port);
// assert RTS then DTR (aka nodemcu reset)
ucomm_rts(isp, 1);
ucomm_dtr(isp, 1);
ucomm_rts(isp, 0);
ucomm_dtr(isp, 0);
// wait for connect
puts("Wait for connection...");
do {
// ISP_CONNECT
} while (!isp_command(ISP_CONNECT, data, isp));
ucomm_purge(isp);
// Chip Info
uint32_t did;
size_t fsz, psz, ldsz;
uint8_t fw_version;
CONFIG config;
#define ISP(code) \
if (!isp_command(ISP_##code, data, isp)) \
z_error(EXIT_FAILURE, errno, "%s failed", #code)
// may be required by bootloader
ISP(SYNC_PACKNO);
ISP(GET_DEVICEID);
did = (data[1] << 8) | (data[0]);
fsz = nuvoton_flashsize(did);
psz = nuvoton_pagesize(did);
ISP(GET_FWVER);
fw_version = data[0];
ISP(READ_CONFIG);
memcpy(config.raw, data, sizeof(CONFIG));
ldsz = nuvoton_ldromsize(config.bit.LDSIZE);
printf("Device ID: %#x\n", did);
printf("Flash Memory: %zuKB,%zup,x%zu\n", fsz / 1024, fsz / psz, psz);
printf("FW Version: %#x\n", fw_version);
print_config(&config);
// Erase
if (opt.erase) {
ucomm_timeout(isp, 3000);
puts("Erase APROM");
ISP(ERASE_ALL);
ucomm_timeout(isp, UCOMM_DEFAULT_TIMEOUT);
}
// Write
if (opt.file != NULL) {
FILE* fin = z_fopen(opt.file, "rb");
IHX ihx;
if (ihx_load(&ihx, 0xff, fin) < 0)
z_error(EXIT_FAILURE, errno, "ihx_load file=%s", opt.file);
if (ihx.entry > 0)
z_error(EXIT_FAILURE, EFAULT, "ihx_load entry=%#zx", ihx.entry);
if (ihx.sz > fsz - ldsz)
z_error(EXIT_FAILURE, EFBIG, "ihx_load sz=%#zx", ihx.sz);
printf("Write APROM[%zu]\n", ihx.sz);
if (!isp_write(ihx.base, ihx.image, ihx.sz, isp))
z_error(EXIT_FAILURE, errno, "isp_write(%zu)", ihx.sz);
free(ihx.image);
fclose(fin);
}
// CONFIG
if (opt.config_flags != 0) {
#define MOVE_BIT(flag) \
if (opt.config_flags & (1 << CONFIG_##flag)) \
config.bit.flag = opt.config.bit.flag
MOVE_BIT(LOCK);
MOVE_BIT(RPD);
MOVE_BIT(OCDEN);
MOVE_BIT(OCDPWM);
MOVE_BIT(CBS);
MOVE_BIT(LDSIZE);
MOVE_BIT(CBORST);
MOVE_BIT(BOIAP);
MOVE_BIT(CBOV);
MOVE_BIT(CBODEN);
MOVE_BIT(WDTEN);
memcpy(data, config.raw, sizeof(CONFIG));
puts("Update CONFIG");
ISP(UPDATE_CONFIG);
}
ISP(RUN_APROM);
ucomm_close(isp);
exit(EXIT_SUCCESS);
}
void list_ports(void)
{
char** ports;
size_t n = ucomm_ports(&ports);
for (size_t i = 0; i < n; ++i)
puts(ports[i]);
printf("%zu ports found\n", n);
free(ports);
}
// Nuvoton ID => Flash Size
size_t nuvoton_flashsize(uint32_t id)
{
unsigned nib1 = (uint8_t)id >> 4;
return (nib1 > 4) ? (18 * 1024) : (4096 << nib1);
}
// Nuvoton ID => Page Size
size_t nuvoton_pagesize(uint32_t id)
{
return (id == 0x2f50/*N76E616*/) ? 256 : 128;
}
// LDSIZE bits => LDROM Size
size_t nuvoton_ldromsize(uint8_t ldsz)
{
unsigned sz = (7 - ldsz) * 1024;
return min(sz, 4096);
}
// LDROM Size => LDSIZE bits
uint8_t nuvoton_ldsize(size_t ldsz)
{
unsigned kb = (ldsz + 1023) / 1024;
return (kb >= 4) ? 0 : (7 - kb);
}
void print_config(const CONFIG* configp)
{
printf("CONFIG0 = %#02x\n", configp->byte.CONFIG0);
printf("\tChip Lock\t\t%s\n", configp->bit.LOCK ? "no" : "yes");
printf("\tReset Pin\t\t%s\n", configp->bit.RPD ? "enable" : "disable");
printf("\tOn-Chip-Debugger\t%s\n", configp->bit.OCDEN ? "disable" : "enable");
printf("\tPWM While OCD Halt\t%s\n", configp->bit.OCDPWM ? "tri-state" : "yes");
printf("\tBoot Select\t\t%s\n", configp->bit.CBS ? "APROM" : "LDROM");
printf("CONFIG1 = %#02x\n", configp->byte.CONFIG1);
printf("\tLDROM Size\t\t%zu\n", nuvoton_ldromsize(configp->bit.LDSIZE));
printf("CONFIG2 = %#02x\n", configp->byte.CONFIG2);
printf("\tBrown-Out Reset\t\t%s\n", configp->bit.CBORST ? "yes" : "no");
printf("\tBrown-Out Inhibit IAP\t%s\n", configp->bit.BOIAP ? "yes" : "no");
printf("\tBrown-Out Voltage\t%s V\n",
(const char*[]){ "2.2", "2.7", "3.7", "4.4" }[3 - configp->bit.CBOV]);
printf("\tBrown-Out Detect\t%s\n", configp->bit.CBODEN ? "yes" : "no");
printf("CONFIG3 = %#02x\n", configp->byte.CONFIG3);
printf("CONFIG4 = %#02x\n", configp->byte.CONFIG4);
printf("\tWatchdog Timer\t\t%s\n\n", configp->bit.WDTEN == 15 ? "disable" :
configp->bit.WDTEN == 5 ? "enable" : "always");
}
int str2bit(const char* str, int value_on)
{
if (!str || z_strcasecmp(str, "enable") == 0 || z_strcasecmp(str, "on") == 0
|| z_strcasecmp(str, "true") == 0 || z_strcasecmp(str, "yes") == 0)
return value_on;
if (z_strcasecmp(str, "disable") == 0 || z_strcasecmp(str, "off") == 0
|| z_strcasecmp(str, "false") == 0 || z_strcasecmp(str, "no") == 0)
return !value_on;
return (str[0] != '0');
}
int str2int(const char* const* tokens, const int* numbers, size_t n, const char* str)
{
if (str != NULL) {
for (size_t i = 0; i < n; ++i)
if (z_strcasecmp(tokens[i], str) == 0)
return numbers[i];
}
return numbers[0];
}