forked from Polarisru/updiprog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvm.c
533 lines (450 loc) · 11.7 KB
/
nvm.c
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include <stdlib.h>
#include <string.h>
#include "app.h"
#include "devices.h"
#include "ihex.h"
#include "link.h"
#include "log.h"
#include "nvm.h"
#include "progress.h"
#include "sleep.h"
#include "updi.h"
#include "msgs.h"
#ifdef __cplusplus
extern "C"
{
#endif
/** \brief Read info about current device
*
* \return
*
*/
bool NVM_GetDeviceInfo(UPDI_APP * app)
{
if (!app) return false;
uint16_t address;
// Must be in prog mode
if (app->NVM_Progmode == false)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_ENTER_PROG_MODE_FIRST);
return false;
}
LOG_Print(app->logger, LOG_LEVEL_INFO, MSG_READING_DEVICE_INFO);
address = DEVICES_GetSigRowAddress(app->DEVICE_Id);
int offset = 0;
while (offset < 6) {
((uint16_t *)&app->DEVICE_sigrow[0])[offset] = LINK_ld16(app, address + offset * 2);
offset++;
}
app->DEVICE_sigrow[12] = LINK_ld(app, address + 12);
return true;//self.application.device_info()
}
/** \brief Enter programming mode
*
* \return true if succeed
*
*/
bool NVM_EnterProgmode(UPDI_APP * app)
{
app->NVM_Progmode = APP_EnterProgmode(app);
return app->NVM_Progmode;
}
/** \brief Leave programming mode
*
* \return Nothing
*
*/
void NVM_LeaveProgmode(UPDI_APP * app)
{
if (app->NVM_Progmode)
APP_LeaveProgmode(app);
app->NVM_Progmode = false;
}
/** \brief Unlock and erase a device
*
* \return Nothing
*
*/
bool NVM_UnlockDevice(UPDI_APP * app)
{
if (app->NVM_Progmode == true)
{
LOG_Print(app->logger, LOG_LEVEL_WARNING, MSG_DEVICE_ALREADY_UNLOCKED);
} else
{
// Unlock after using the NVM key results in prog mode.
if (APP_Unlock(app) == true)
{
app->NVM_Progmode = true;
} else
{
return false;
}
}
return true;
}
/** \brief Erase chip flash memory
*
* \return true if succeed
*
*/
bool NVM_ChipErase(UPDI_APP * app)
{
if (app->NVM_Progmode == false)
{
LOG_Print(app->logger,LOG_LEVEL_ERROR, MSG_ENTER_PROG_MODE_FIRST);
return false;
}
return APP_ChipErase(app);
}
#ifdef UPDI_CLI_mode
const static UPDI_cli_ud read_p_intf = { MSG_READING, '#' };
const static UPDI_cli_ud write_p_intf = { MSG_WRITING, '#' };
#endif
/** \brief Read data from flash memory
*
* \param [in] address Starting address
* \param [out] data Buffer to write data
* \param [in] size Length of data to read
* \return true if succeed
*
*/
bool NVM_ReadFlash(UPDI_APP * app, uint16_t address, uint8_t *data, uint16_t size)
{
uint16_t i;
uint16_t pages;
uint8_t page_size;
uint8_t err_counter;
// Must be in prog mode here
if (app->NVM_Progmode == false)
{
LOG_Print(app->logger,LOG_LEVEL_ERROR, MSG_ENTER_PROG_MODE_FIRST);
return false;
}
page_size = DEVICES_GetPageSize(app->DEVICE_Id);
// Find the number of pages
pages = size / page_size;
if (size % page_size != 0)
pages++;
#ifdef UPDI_CLI_mode
PROGRESS_SetUserData(app->progress, (void*)&read_p_intf);
#endif
PROGRESS_Start(app->progress, pages);
i = 0;
err_counter = 0;
// Read out page-wise for convenience
while (i < pages)
{
LOG_Print(app->logger,LOG_LEVEL_VERBOSE, MSG_READING_PAGE, address);
if (APP_ReadDataWords(app, address, &data[i * page_size], DEVICES_GetPageSize(app->DEVICE_Id) >> 1) == false)
{
// error occurred, try once more
err_counter++;
//msleep(err_counter << 8);
if (err_counter > NVM_MAX_ERRORS)
{
PROGRESS_Break(app->progress);
return false;
}
continue;
} else
{
err_counter = 0;
}
i++;
// show progress bar
PROGRESS_Step(app->progress, i);
address += page_size;
}
PROGRESS_Break(app->progress);
return true;
}
/** \brief Write data buffer to flash
*
* \param [in] address Address to start writing
* \param [in] data Data buffer to write
* \param [in] size Length of data
* \return true if succeed
*
*/
bool NVM_WriteFlash(UPDI_APP * app, uint16_t address, uint8_t *data, uint16_t size)
{
uint8_t page_size;
uint16_t pages;
uint16_t i;
uint8_t err_counter;
// Must be in prog mode
if (app->NVM_Progmode == false)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_ENTER_PROG_MODE_FIRST);
return false;
}
page_size = DEVICES_GetPageSize(app->DEVICE_Id);
// Divide up into pages
pages = size / page_size;
if (size % page_size != 0)
pages++;
#ifdef UPDI_CLI_mode
PROGRESS_SetUserData(app->progress, (void*)&write_p_intf);
#endif
PROGRESS_Start(app->progress, pages);
i = 0;
err_counter = 0;
// Program each page
while (i < pages)
{
LOG_Print(app->logger, LOG_LEVEL_VERBOSE, MSG_WRITING_PAGE, address);
if (APP_WriteNvm(app, address, &data[i * page_size], page_size, true) == false)
{
err_counter++;
if (err_counter > NVM_MAX_ERRORS)
{
PROGRESS_Break(app->progress);
return false;
}
continue;
} else
{
err_counter = 0;
}
i++;
// show progress bar
PROGRESS_Step(app->progress, i);
address += page_size;
}
PROGRESS_Break(app->progress);
return true;
}
/** \brief Read fuse value
*
* \param [in] fusenum Number of the fuse
* \return Fuse value as uint8_t
*
*/
uint8_t NVM_ReadFuse(UPDI_APP * app, uint8_t fusenum)
{
uint16_t address;
// Must be in prog mode
if (app->NVM_Progmode == false)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_ENTER_PROG_MODE_FIRST);
return false;
}
address = DEVICES_GetFusesAddress(app->DEVICE_Id) + fusenum;
return LINK_ld(app, address);
}
/** \brief Write fuse value
*
* \param [in] fusenum Number of the fuse
* \param [in] value Fuse value
* \return true if succeed
*
*/
bool NVM_WriteFuse(UPDI_APP * app, uint8_t fusenum, uint8_t value)
{
uint16_t fuse_address;
uint16_t address;
uint8_t data;
// Must be in prog mode
if (app->NVM_Progmode == false)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_ENTER_PROG_MODE_FIRST);
return false;
}
if (!APP_WaitFlashReady(app))
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_FLASH_NOT_READY_FUSE);
return false;
}
fuse_address = DEVICES_GetFusesAddress(app->DEVICE_Id) + fusenum;
address = DEVICES_GetNvmctrlAddress(app->DEVICE_Id) + UPDI_NVMCTRL_ADDRL;
data = (uint8_t)(fuse_address & 0xff);
APP_WriteData(app, address, &data, sizeof(uint8_t));
address = DEVICES_GetNvmctrlAddress(app->DEVICE_Id) + UPDI_NVMCTRL_ADDRH;
data = (uint8_t)(fuse_address >> 8);
APP_WriteData(app, address, &data, sizeof(uint8_t));
address = DEVICES_GetNvmctrlAddress(app->DEVICE_Id) + UPDI_NVMCTRL_DATAL;
APP_WriteData(app, address, &value, sizeof(uint8_t));
address = DEVICES_GetNvmctrlAddress(app->DEVICE_Id) + UPDI_NVMCTRL_CTRLA;
data = UPDI_NVMCTRL_CTRLA_WRITE_FUSE;
APP_WriteData(app, address, &data, sizeof(uint8_t));
return true;
}
bool NVM_LoadIhexStream(UPDI_APP * app, IHEX_Stream * stream, uint16_t address, uint16_t len) {
uint8_t *fdata;
uint8_t errCode;
uint16_t max_addr, min_addr;
bool res = false;
fdata = malloc(len);
if (!fdata)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_UNABLE_ALLOC_MEM, (int)len);
return false;
}
max_addr = 0;
min_addr = 0xFFFF;
errCode = IHEX_ReadStream(stream, fdata, len, &min_addr, &max_addr);
switch (errCode)
{
case IHEX_ERROR_FILE:
case IHEX_ERROR_SIZE:
case IHEX_ERROR_FMT:
case IHEX_ERROR_CRC:
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_PROBLEM_READING_HEX);
res = false;
break;
case IHEX_ERROR_NONE:
// write data buffer to flash
if (min_addr < max_addr)
res = NVM_WriteFlash(app, address + min_addr, &fdata[min_addr], max_addr - min_addr);
break;
}
free(fdata);
return res;
}
bool NVM_raw_eof(void* ud) {
NVM_raw_data *src = (NVM_raw_data *)ud;
return src->pos >= src->len;
}
bool NVM_raw_gets(void* ud, char * dst, int32_t cnt) {
NVM_raw_data *src = (NVM_raw_data *)ud;
int len = src->len - src->pos;
cnt--;
if (cnt <= 0) return false;
if (cnt > len)
cnt = len;
cnt += src->pos;
char * c = &(src->data[src->pos]);
char * d = dst;
bool eol = false;
while (src->pos < cnt) {
switch (*c) {
case '\r':
eol = true;
break;
case '\n':
eol = true;
break;
default:
if (eol)
goto whs;
}
*d = *c;
d++;
c++;
src->pos++;
}
whs:
*d = '\0';
return true;
}
bool NVM_LoadIhexRaw(UPDI_APP * app, NVM_raw_data *src, uint16_t address, uint16_t len) {
IHEX_Stream raw_stream;
raw_stream.ud = src;
raw_stream.eof = &NVM_raw_eof;
raw_stream.gets = &NVM_raw_gets;
bool res = NVM_LoadIhexStream(app, &raw_stream, address, len);
return res;
}
bool NVM_file_eof(void* ud) {
return (feof((FILE *) ud));
}
bool NVM_file_gets(void* ud, char * dst, int32_t cnt) {
return (fgets( dst, cnt, (FILE *) ud ) != NULL);
}
/** \brief Load data from Intel HEX format
*
* \param [in] filename Name of the HEX file
* \param [in] address Chip starting address
* \param [in] len Length of the data
* \return true if succeed
*
*/
bool NVM_LoadIhexFile(UPDI_APP * app, char *filename, uint16_t address, uint16_t len)
{
FILE *fp;
if ((fp = fopen(filename, "rt")) == NULL)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_UNABLE_OPEN_FILE, filename);
return false;
}
IHEX_Stream file_stream;
file_stream.ud = (void*)fp;
file_stream.eof = &NVM_file_eof;
file_stream.gets = &NVM_file_gets;
bool res = NVM_LoadIhexStream(app, &file_stream, address, len);
fclose(fp);
// Size check: not implemented yet
return res;
}
bool NVM_SaveIhexStream(UPDI_APP * app, IHEX_Stream * stream, uint16_t address, uint16_t len) {
uint8_t *fdata;
bool res = false;
fdata = malloc(len);
if (!fdata)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_UNABLE_ALLOC_MEM, (int)len);
return false;
}
memset(fdata, 0xff, len);
if (NVM_ReadFlash(app, address, fdata, len) == false)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_READING_DEVICE_FAIL);
} else
{
if (IHEX_WriteStream(stream, fdata, len) == IHEX_ERROR_NONE)
res = true;
else
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_PROBLEM_WRITING_HEX);
}
free(fdata);
return res;
}
bool NVM_raw_write(void* ud, const void *ptr, int32_t len) {
NVM_raw_data *src = (NVM_raw_data *)ud;
int cnt = src->len - src->pos;
if (cnt <= 0) return false;
if (len > cnt)
len = cnt;
memcpy(&(src->data[src->pos]), ptr, len);
src->pos+=len;
return true;
}
bool NVM_SaveIhexRaw(UPDI_APP * app, NVM_raw_data *dst, uint16_t address, uint16_t len) {
IHEX_Stream raw_stream;
raw_stream.ud = dst;
raw_stream.write = &NVM_raw_write;
bool res = NVM_SaveIhexStream(app, &raw_stream, address, len);
return res;
}
bool NVM_file_write(void* ud, const void *ptr, int32_t len) {
return fwrite(ptr, (size_t)len, 1, (FILE*)ud) > 0;
}
/** \brief Save file to Intel HEX format
*
* \param [in] filename Name of the HEX file
* \param [in] address Chip starting address
* \param [in] len Length of data
* \return true if succeed
*
*/
bool NVM_SaveIhexFile(UPDI_APP * app, char *filename, uint16_t address, uint16_t len)
{
FILE *fp;
bool res = false;
if ((fp = fopen(filename, "w")) == NULL)
{
LOG_Print(app->logger, LOG_LEVEL_ERROR, MSG_UNABLE_OPEN_FILE, filename);
} else
{
IHEX_Stream file_stream;
file_stream.ud = (void*)fp;
file_stream.write = &NVM_file_write;
res = NVM_SaveIhexStream(app, &file_stream, address, len);
fclose(fp);
}
return res;
}
#ifdef __cplusplus
}
#endif