-
Notifications
You must be signed in to change notification settings - Fork 22
/
link.c
379 lines (337 loc) · 7.93 KB
/
link.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
#include <string.h>
#include "log.h"
#include "phy.h"
#include "updi.h"
/** \brief
*
* \param
* \param
* \return
*
*/
uint8_t LINK_ldcs(uint8_t address)
{
//Load data from Control/Status space
uint8_t response = 0;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_LDCS | (address & 0x0F)};
LOG_Print(LOG_LEVEL_INFO, "LDCS from 0x%02X", address);
PHY_Send(buf, sizeof(buf));
PHY_Receive(&response, 1);
return response;
}
/** \brief
*
* \param
* \param
* \return
*
*/
void LINK_stcs(uint8_t address, uint8_t value)
{
//Store a value to Control/Status space
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_STCS | (address & 0x0F), value};
LOG_Print(LOG_LEVEL_INFO, "STCS to 0x%02X", address);
PHY_Send(buf, sizeof(buf));
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_Check(void)
{
//Check UPDI by loading CS STATUSA
if (LINK_ldcs(UPDI_CS_STATUSA) != 0)
{
LOG_Print(LOG_LEVEL_INFO, "UPDI init OK");
return true;
}
LOG_Print(LOG_LEVEL_WARNING, "UPDI not OK - reinitialisation required");
return false;
}
/** \brief
*
* \param
* \param
* \return
*
*/
void LINK_Start(void)
{
//Set the inter-byte delay bit and disable collision detection
LINK_stcs(UPDI_CS_CTRLB, 1 << UPDI_CTRLB_CCDETDIS_BIT);
LINK_stcs(UPDI_CS_CTRLA, 1 << UPDI_CTRLA_IBDLY_BIT);
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_Init(char *port, uint32_t baudrate, bool onDTR)
{
uint8_t err = 3;
uint8_t byte;
//Create a UPDI physical connection
if (PHY_Init(port, baudrate, onDTR) == false)
return false;
byte = UPDI_BREAK;
PHY_Send(&byte, sizeof(uint8_t));
while (err-- > 0)
{
LINK_Start();
//Check answer
if (LINK_Check() == true)
return true;
//Send double break if all is not well, and re-check
if (PHY_DoBreak(port) == false)
{
LOG_Print(LOG_LEVEL_ERROR, "UPDI initialisation failed");
return false;
}
if (PHY_Init(port, baudrate, onDTR) == false)
return false;
}
return false;
}
/** \brief
*
* \param
* \param
* \return
*
*/
uint8_t LINK_ld(uint16_t address)
{
//Load a single byte direct from a 16-bit address
uint8_t response;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_LDS | UPDI_ADDRESS_16 | UPDI_DATA_8, address & 0xFF, (address >> 8) & 0xFF};
LOG_Print(LOG_LEVEL_INFO, "LD from 0x%04X", address);
PHY_Send(buf, sizeof(buf));
PHY_Receive(&response, 1);
return response;
}
/** \brief
*
* \param
* \param
* \return
*
*/
uint16_t LINK_ld16(uint16_t address)
{
//Load a 16-bit word directly from a 16-bit address
uint16_t response;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_LDS | UPDI_ADDRESS_16 | UPDI_DATA_16, address & 0xFF, (address >> 8) & 0xFF};
LOG_Print(LOG_LEVEL_INFO, "LD from 0x%04X", address);
PHY_Send(buf, sizeof(buf));
PHY_Receive((uint8_t*)&response, 2);
return response;
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_st(uint16_t address, uint8_t value)
{
//Store a single byte value directly to a 16-bit address
uint8_t response;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_STS | UPDI_ADDRESS_16 | UPDI_DATA_8, address & 0xFF, (address >> 8) & 0xFF};
LOG_Print(LOG_LEVEL_INFO, "ST to 0x%04X", address);
PHY_Send(buf, sizeof(buf));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
PHY_Send(&value, 1);
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
return true;
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_st16(uint16_t address, uint16_t value)
{
//Store a 16-bit word value directly to a 16-bit address
uint8_t response;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_STS | UPDI_ADDRESS_16 | UPDI_DATA_16, address & 0xFF, (address >> 8) & 0xFF};
LOG_Print(LOG_LEVEL_INFO, "ST to 0x%04X", address);
PHY_Send(buf, sizeof(buf));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
PHY_Send((uint8_t*)&value, sizeof(uint16_t));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
return true;
}
/** \brief
*
* \param [out] data Data buffer to write received data in
* \param [in] size Size of received data
* \return true if succeed
*
*/
bool LINK_ld_ptr_inc(uint8_t *data, uint8_t size)
{
//Loads a number of bytes from the pointer location with pointer post-increment
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_LD | UPDI_PTR_INC | UPDI_DATA_8};
LOG_Print(LOG_LEVEL_INFO, "LD8 from ptr++");
PHY_Send(buf, sizeof(buf));
return PHY_Receive(data, size);
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_ld_ptr_inc16(uint8_t *data, uint16_t words)
{
//Load a 16-bit word value from the pointer location with pointer post-increment
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_LD | UPDI_PTR_INC | UPDI_DATA_16};
LOG_Print(LOG_LEVEL_INFO, "LD16 from ptr++");
PHY_Send(buf, sizeof(buf));
return PHY_Receive(data, words << 1);
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_st_ptr(uint16_t address)
{
//Set the pointer location
uint8_t response;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_ST | UPDI_PTR_ADDRESS | UPDI_DATA_16, address & 0xFF, (address >> 8) & 0xFF};
LOG_Print(LOG_LEVEL_INFO, "ST to ptr");
PHY_Send(buf, sizeof(buf));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
return true;
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_st_ptr_inc(uint8_t *data, uint16_t len)
{
//Store data to the pointer location with pointer post-increment
uint8_t response;
uint16_t n;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_ST | UPDI_PTR_INC | UPDI_DATA_8, data[0]};
LOG_Print(LOG_LEVEL_INFO, "ST8 to *ptr++");
PHY_Send(buf, sizeof(buf));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
n = 1;
while (n < len)
{
PHY_Send(&data[n], sizeof(uint8_t));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
n++;
}
return true;
}
bool LINK_st_ptr_inc16(uint8_t *data, uint16_t len) // length in words or in bytes??
{
//Store a 16-bit word value to the pointer location with pointer post-increment
uint8_t response;
uint16_t n;
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_ST | UPDI_PTR_INC | UPDI_DATA_16, data[0], data[1]};
LOG_Print(LOG_LEVEL_INFO, "ST16 to *ptr++");
PHY_Send(buf, sizeof(buf));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
n = 2;
while (n < len)
{
PHY_Send(&data[n], sizeof(uint16_t));
PHY_Receive(&response, 1);
if (response != UPDI_PHY_ACK)
return false;
n += 2;
}
return true;
}
/** \brief
*
* \param
* \param
* \return
*
*/
void LINK_Repeat(uint16_t repeats)
{
//Store a value to the repeat counter
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_REPEAT | UPDI_REPEAT_WORD, (repeats - 1) & 0xFF, ((repeats - 1) >> 8) & 0xFF};
LOG_Print(LOG_LEVEL_INFO, "Repeat %d", repeats);
PHY_Send(buf, sizeof(buf));
}
/** \brief
*
* \param
* \param
* \return
*
*/
void LINK_Read_SIB(uint8_t *data)
{
//Read the SIB
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_KEY | UPDI_KEY_SIB | UPDI_SIB_16BYTES};
PHY_Send(buf, sizeof(buf));
PHY_Receive(data, UPDI_SIB_LENGTH);
}
/** \brief
*
* \param
* \param
* \return
*
*/
bool LINK_SendKey(char *key, uint8_t size)
{
//Write a key
uint8_t buf[] = {UPDI_PHY_SYNC, UPDI_KEY | UPDI_KEY_KEY | size};
uint8_t data[8];
uint8_t n, i;
LOG_Print(LOG_LEVEL_INFO, "Writing key");
if (strlen(key) != (8 << size))
{
LOG_Print(LOG_LEVEL_ERROR, "Invalid KEY length!");
return false;
}
PHY_Send(buf, sizeof(buf));
n = strlen(key);
i = 0;
while (n > 0)
{
data[i++] = key[n - 1];
n--;
}
PHY_Send(data, sizeof(data));
return true;
}