forked from Polarisru/updiprog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvm.c
367 lines (323 loc) · 7.84 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
#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 "updi.h"
static bool NVM_Progmode = false;
/** \brief Read info about current device
*
* \return
*
*/
bool NVM_GetDeviceInfo(void)
{
LOG_Print(LOG_LEVEL_INFO, "Reading device info");
return true;//self.application.device_info()
}
/** \brief Enter programming mode
*
* \return true if succeed
*
*/
bool NVM_EnterProgmode()
{
LOG_Print(LOG_LEVEL_INFO, "Entering NVM programming mode");
APP_EnterProgmode(false);
NVM_Progmode = APP_EnterProgmode(true);
return NVM_Progmode;
}
/** \brief Leave programming mode
*
* \return Nothing
*
*/
void NVM_LeaveProgmode(void)
{
LOG_Print(LOG_LEVEL_INFO, "Leaving NVM programming mode");
APP_LeaveProgmode();
NVM_Progmode = false;
}
/** \brief Unlock and erase a device
*
* \return Nothing
*
*/
void NVM_UnlockDevice(void)
{
if (NVM_Progmode == true)
{
LOG_Print(LOG_LEVEL_WARNING, "Device already unlocked");
return;
}
// Unlock after using the NVM key results in prog mode.
if (APP_Unlock() == true)
NVM_Progmode = true;
}
/** \brief Erase chip flash memory
*
* \return true if succeed
*
*/
bool NVM_ChipErase(void)
{
if (NVM_Progmode == false)
{
LOG_Print(LOG_LEVEL_ERROR, "Enter progmode first!");
return false;
}
return APP_ChipErase();
}
/** \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(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 (NVM_Progmode == false)
{
LOG_Print(LOG_LEVEL_ERROR, "Enter progmode first!");
return false;
}
page_size = DEVICES_GetPageSize();
// Find the number of pages
pages = size / page_size;
if (size % page_size != 0)
pages++;
PROGRESS_Print(0, pages, "Reading: ", '#');
i = 0;
err_counter = 0;
// Read out page-wise for convenience
while (i < pages)
{
LOG_Print(LOG_LEVEL_INFO, "Reading page at 0x%04X", address);
if (APP_ReadDataWords(address, &data[i * page_size], DEVICES_GetPageSize() >> 1) == false)
{
// error occurred, try once more
err_counter++;
if (err_counter > NVM_MAX_ERRORS)
{
PROGRESS_Break();
return false;
}
continue;
} else
{
err_counter = 0;
}
i++;
// show progress bar
PROGRESS_Print(i, pages, "Reading: ", '#');
address += page_size;
}
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(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 (NVM_Progmode == false)
{
LOG_Print(LOG_LEVEL_ERROR, "Enter progmode first!");
return false;
}
page_size = DEVICES_GetPageSize();
// Divide up into pages
pages = size / page_size;
if (size % page_size != 0)
pages++;
PROGRESS_Print(0, pages, "Writing: ", '#');
i = 0;
err_counter = 0;
// Program each page
while (i < pages)
{
LOG_Print(LOG_LEVEL_INFO, "Writing page at 0x%04X", address);
if (APP_WriteNvm(address, &data[i * page_size], page_size, true) == false)
{
err_counter++;
if (err_counter > NVM_MAX_ERRORS)
{
PROGRESS_Break();
return false;
}
continue;
} else
{
err_counter = 0;
}
i++;
// show progress bar
PROGRESS_Print(i, pages, "Writing: ", '#');
address += page_size;
}
return true;
}
/** \brief Read fuse value
*
* \param [in] fusenum Number of the fuse
* \return Fuse value as uint8_t
*
*/
uint8_t NVM_ReadFuse(uint8_t fusenum)
{
uint16_t address;
// Must be in prog mode
if (NVM_Progmode == false)
{
LOG_Print(LOG_LEVEL_ERROR, "Enter progmode first!");
return false;
}
address = DEVICES_GetFusesAddress() + fusenum;
return LINK_ld(address);
}
/** \brief Write fuse value
*
* \param [in] fusenum Number of the fuse
* \param [in] value Fuse value
* \return true if succeed
*
*/
bool NVM_WriteFuse(uint8_t fusenum, uint8_t value)
{
uint16_t fuse_address;
uint16_t address;
uint8_t data;
// Must be in prog mode
if (NVM_Progmode == false)
{
LOG_Print(LOG_LEVEL_ERROR, "Enter progmode first!");
return false;
}
if (!APP_WaitFlashReady())
{
LOG_Print(LOG_LEVEL_ERROR, "Flash not ready for fuse setting");
return false;
}
fuse_address = DEVICES_GetFusesAddress() + fusenum;
address = DEVICES_GetNvmctrlAddress() + UPDI_NVMCTRL_ADDRL;
data = (uint8_t)(fuse_address & 0xff);
APP_WriteData(address, &data, sizeof(uint8_t));
address = DEVICES_GetNvmctrlAddress() + UPDI_NVMCTRL_ADDRH;
data = (uint8_t)(fuse_address >> 8);
APP_WriteData(address, &data, sizeof(uint8_t));
address = DEVICES_GetNvmctrlAddress() + UPDI_NVMCTRL_DATAL;
APP_WriteData(address, &value, sizeof(uint8_t));
address = DEVICES_GetNvmctrlAddress() + UPDI_NVMCTRL_CTRLA;
data = UPDI_NVMCTRL_CTRLA_WRITE_FUSE;
APP_WriteData(address, &data, sizeof(uint8_t));
return true;
}
/** \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_LoadIhex(char *filename, uint16_t address, uint16_t len)
{
uint8_t *fdata;
uint8_t errCode;
uint16_t max_addr, min_addr;
FILE *fp;
bool res = false;
fdata = malloc(len);
if (!fdata)
{
LOG_Print(LOG_LEVEL_ERROR, "Unable to allocate %d bytes\n", (int)len);
return false;
}
if ((fp = fopen(filename, "rt")) == NULL)
{
LOG_Print(LOG_LEVEL_ERROR, "Unable to open file: %s", filename);
free(fdata);
return false;
}
max_addr = 0;
min_addr = 0;
errCode = IHEX_ReadFile(fp, fdata, len, &max_addr);
switch (errCode)
{
case IHEX_ERROR_FILE:
case IHEX_ERROR_SIZE:
case IHEX_ERROR_FMT:
case IHEX_ERROR_CRC:
LOG_Print(LOG_LEVEL_ERROR, "Problem reading Hex file");
res = false;
break;
case IHEX_ERROR_NONE:
// write data buffer to flash
res = NVM_WriteFlash(address, fdata, max_addr - min_addr);
break;
}
free(fdata);
fclose(fp);
// Size check: not implemented yet
return res;
}
/** \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_SaveIhex(char *filename, uint16_t address, uint16_t len)
{
uint8_t *fdata;
FILE *fp;
bool res = false;
fdata = malloc(len);
if (!fdata)
{
LOG_Print(LOG_LEVEL_ERROR, "Unable to allocate %d bytes", (int)len);
return false;
}
memset(fdata, 0xff, len);
if ((fp = fopen(filename, "w")) == NULL)
{
LOG_Print(LOG_LEVEL_ERROR, "Unable to open file: %s", filename);
} else
{
if (NVM_ReadFlash(address, fdata, len) == false)
{
LOG_Print(LOG_LEVEL_ERROR, "Reading from device failed");
} else
{
if (IHEX_WriteFile(fp, fdata, len) == IHEX_ERROR_NONE)
res = true;
else
LOG_Print(LOG_LEVEL_ERROR, "Problem writing Hex file");
}
fclose(fp);
}
free(fdata);
return res;
}