-
Notifications
You must be signed in to change notification settings - Fork 22
/
app.c
346 lines (283 loc) · 8.4 KB
/
app.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
#include <stdio.h>
#include <unistd.h>
#include "devices.h"
#include "link.h"
#include "log.h"
#include "sleep.h"
#include "updi.h"
void APP_Reset(bool apply_reset)
{
//Applies or releases an UPDI reset condition
if (apply_reset == true)
{
LOG_Print(LOG_LEVEL_INFO, "Apply reset");
LINK_stcs(UPDI_ASI_RESET_REQ, UPDI_RESET_REQ_VALUE);
} else
{
LOG_Print(LOG_LEVEL_INFO, "Release reset");
LINK_stcs(UPDI_ASI_RESET_REQ, 0x00);
}
}
bool APP_InProgMode(void)
{
//Checks whether the NVM PROG flag is up
if (LINK_ldcs(UPDI_ASI_SYS_STATUS) & (1 << UPDI_ASI_SYS_STATUS_NVMPROG))
return true;
return false;
}
bool APP_WaitUnlocked(uint16_t timeout_ms)
{
//Waits for the device to be unlocked.
//All devices boot up as locked until proven otherwise
while (timeout_ms-- > 0)
{
msleep(1);
if (!(LINK_ldcs(UPDI_ASI_SYS_STATUS) & (1 << UPDI_ASI_SYS_STATUS_LOCKSTATUS)))
return true;
}
LOG_Print(LOG_LEVEL_WARNING, "Timeout by waiting for device to unlock");
return false;
}
bool APP_EnterProgmode(void)
{
//Enters into NVM programming mode
uint8_t key_status;
//First check if NVM is already enabled
if (APP_InProgMode() == true)
{
LOG_Print(LOG_LEVEL_WARNING, "Already in NVM programming mode");
return true;
}
LOG_Print(LOG_LEVEL_WARNING, "Entering NVM programming mode");
// Put in the key
LINK_SendKey(UPDI_KEY_NVM, UPDI_KEY_64);
// Check key status
key_status = LINK_ldcs(UPDI_ASI_KEY_STATUS);
LOG_Print(LOG_LEVEL_INFO, "Key status = 0x%02X", key_status);
if (!(key_status & (1 << UPDI_ASI_KEY_STATUS_NVMPROG)))
{
LOG_Print(LOG_LEVEL_WARNING, "Key not accepted");
return false;
}
// Toggle reset
APP_Reset(true);
APP_Reset(false);
// And wait for unlock
if (!APP_WaitUnlocked(100))
{
LOG_Print(LOG_LEVEL_ERROR, "Failed to enter NVM programming mode: device is locked");
return false;
}
// Check for NVMPROG flag
if (APP_InProgMode() == false)
{
LOG_Print(LOG_LEVEL_ERROR, "Failed to enter NVM programming mode");
return false;
}
LOG_Print(LOG_LEVEL_INFO, "Now in NVM programming mode");
return true;
}
void APP_LeaveProgmode(void)
{
//Disables UPDI which releases any keys enabled
LOG_Print(LOG_LEVEL_WARNING, "Leaving NVM programming mode");
APP_Reset(true);
APP_Reset(false);
LINK_stcs(UPDI_CS_CTRLB, (1 << UPDI_CTRLB_UPDIDIS_BIT) | (1 << UPDI_CTRLB_CCDETDIS_BIT));
}
bool APP_Unlock(void)
{
//Unlock and erase
uint8_t key_status;
// Put in the key
LINK_SendKey(UPDI_KEY_CHIPERASE, UPDI_KEY_64);
// Check key status
key_status = LINK_ldcs(UPDI_ASI_KEY_STATUS);
LOG_Print(LOG_LEVEL_INFO, "Key status = 0x%02X", key_status);
if (!(key_status & (1 << UPDI_ASI_KEY_STATUS_CHIPERASE)))
{
LOG_Print(LOG_LEVEL_WARNING, "Key not accepted");
return false;
}
// Toggle reset
APP_Reset(true);
APP_Reset(false);
// And wait for unlock
if (!APP_WaitUnlocked(100))
{
LOG_Print(LOG_LEVEL_ERROR, "Failed to chip erase using key!");
return false;
}
return true;
}
bool APP_WaitFlashReady(void)
{
//Waits for the NVM controller to be ready
uint8_t status;
uint16_t timeout = 10000; // 10 sec timeout, just to be sure
LOG_Print(LOG_LEVEL_INFO, "Wait flash ready");
while (timeout-- > 0)
{
msleep(1);
status = LINK_ld(DEVICES_GetNvmctrlAddress() + UPDI_NVMCTRL_STATUS);
if (status & (1 << UPDI_NVM_STATUS_WRITE_ERROR))
{
LOG_Print(LOG_LEVEL_ERROR, "NVM error");
return false;
}
if (!(status & ((1 << UPDI_NVM_STATUS_EEPROM_BUSY) | (1 << UPDI_NVM_STATUS_FLASH_BUSY))))
return true;
}
LOG_Print(LOG_LEVEL_WARNING, "Waiting for flash ready timed out");
return false;
}
bool APP_ExecuteNvmCommand(uint8_t command)
{
//Executes an NVM COMMAND on the NVM CTRL
//self.logger.info("NVMCMD {:d} executing".format(command))
LOG_Print(LOG_LEVEL_INFO, "NVMCMD %d executing", command);
return LINK_st(DEVICES_GetNvmctrlAddress() + UPDI_NVMCTRL_CTRLA, command);
}
bool APP_ChipErase(void)
{
// Does a chip erase using the NVM controller
// Note that on locked devices this it not possible
// and the ERASE KEY has to be used instead
LOG_Print(LOG_LEVEL_INFO, "Chip erase using NVM CTRL");
// Wait until NVM CTRL is ready to erase
if (!APP_WaitFlashReady())
{
LOG_Print(LOG_LEVEL_WARNING, "Timeout waiting for flash ready before erase ");
return false;
}
// Erase
APP_ExecuteNvmCommand(UPDI_NVMCTRL_CTRLA_CHIP_ERASE);
// And wait for it
if (!APP_WaitFlashReady())
{
LOG_Print(LOG_LEVEL_WARNING, "Timeout by waiting for flash ready after erase");
return false;
}
return true;
}
bool APP_WriteDataWords(uint16_t address, uint8_t *data, uint16_t len)
{
//Writes a number of words to memory
uint16_t value;
// Special-case of 1 word
if (len == 2)
{
value = (uint16_t)data[0] + (data[1] << 8);
return LINK_st16(address, value);
}
// Range check
if (len > (UPDI_MAX_REPEAT_SIZE + 1) << 1)
{
LOG_Print(LOG_LEVEL_WARNING, "Invalid length");
return false;
}
// Store the address
LINK_st_ptr(address);
// Fire up the repeat
LINK_Repeat(len >> 1);
return LINK_st_ptr_inc16(data, len);
}
bool APP_WriteData(uint16_t address, uint8_t *data, uint16_t len)
{
//Writes a number of bytes to memory
// Special case of 1 byte
if (len == 1)
return LINK_st(address, data[0]);
// Special case of 2 byte
if (len == 2)
{
LINK_st(address, data[0]);
return LINK_st(address + 1, data[1]);
}
// Range check
if (len > (UPDI_MAX_REPEAT_SIZE + 1))
{
LOG_Print(LOG_LEVEL_WARNING, "Invalid length");
return false;
}
// Store the address
LINK_st_ptr(address);
// Fire up the repeat
LINK_Repeat(len);
return LINK_st_ptr_inc(data, len);
}
bool APP_WriteNvm(uint16_t address, uint8_t *data, uint16_t len, bool use_word_access)
{
//Writes a page of data to NVM.APP_ExecuteNvmCommand
//By default the PAGE_WRITE command is used, which
//requires that the page is already erased.
//By default word access is used (flash)
// Check that NVM controller is ready
if (!APP_WaitFlashReady())
{
LOG_Print(LOG_LEVEL_WARNING, "Timeout by waiting for flash ready before page buffer clear ");
return false;
}
// Clear the page buffer
LOG_Print(LOG_LEVEL_INFO, "Clear page buffer");
APP_ExecuteNvmCommand(UPDI_NVMCTRL_CTRLA_PAGE_BUFFER_CLR);
// Waif for NVM controller to be ready
if (!APP_WaitFlashReady())
{
LOG_Print(LOG_LEVEL_WARNING, "Timeout by waiting for flash ready after page buffer clear");
return false;
}
// Load the page buffer by writing directly to location
if (use_word_access == true)
APP_WriteDataWords(address, data, len);
else
APP_WriteData(address, data, len);
// Write the page to NVM, maybe erase first
LOG_Print(LOG_LEVEL_INFO, "Committing page");
APP_ExecuteNvmCommand(UPDI_NVMCTRL_CTRLA_WRITE_PAGE);
// Wait for NVM controller to be ready again
if (!APP_WaitFlashReady())
{
LOG_Print(LOG_LEVEL_WARNING, "Timeout by waiting for flash ready after page write");
return false;
}
return true;
}
bool APP_ReadData(uint16_t address, uint8_t *data, uint16_t size)
{
//Reads a number of bytes of data from UPDI
LOG_Print(LOG_LEVEL_INFO, "Reading %d bytes from 0x%04X", size, address);
// Range check
if (size > UPDI_MAX_REPEAT_SIZE + 1)
{
LOG_Print(LOG_LEVEL_ERROR, "Cant read that many bytes in one go");
return false;
}
// Store the address
LINK_st_ptr(address);
// Fire up the repeat
if (size > 1)
LINK_Repeat(size);
// Do the read(s)
return LINK_ld_ptr_inc(data, size);
}
bool APP_ReadDataWords(uint16_t address, uint8_t *data, uint16_t words)
{
//Reads a number of words of data from UPDI
LOG_Print(LOG_LEVEL_INFO, "Reading %d words from 0x%04X", words, address);
// Range check
if (words > (UPDI_MAX_REPEAT_SIZE >> 1) + 1)
{
LOG_Print(LOG_LEVEL_ERROR, "Cant read that many words in one go");
return false;
}
// Store the address
LINK_st_ptr(address);
// Fire up the repeat
if (words > 1)
{
LINK_Repeat(words);
}
// Do the read
return LINK_ld_ptr_inc16(data, words);
}