This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnero.c
366 lines (345 loc) · 10.7 KB
/
libnero.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
/*
* Copyright (C) 2009-2012 Chris McClelland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <makestuff.h>
#include <libusbwrap.h>
#include <liberror.h>
#include <vendorCommands.h>
#include "libnero.h"
// -------------------------------------------------------------------------------------------------
// Declaration of private types & functions
// -------------------------------------------------------------------------------------------------
typedef enum {
SEND_ZEROS,
SEND_ONES,
SEND_DATA,
SEND_MASK
} SendType;
enum {
IS_RESPONSE_NEEDED = 0,
IS_LAST = 1,
SEND_TYPE = 2
};
static NeroStatus beginShift(
struct NeroHandle *handle, uint32 numBits, SendType sendType, bool isLast,
bool isResponseNeeded, const char **error
) WARN_UNUSED_RESULT;
static NeroStatus doSend(
struct NeroHandle *handle, const uint8 *sendPtr, uint16 chunkSize, const char **error
) WARN_UNUSED_RESULT;
static NeroStatus doReceive(
struct NeroHandle *handle, uint8 *receivePtr, uint16 chunkSize, const char **error
) WARN_UNUSED_RESULT;
//static NeroStatus setEndpointSize(
// struct NeroHandle *handle, const char **error
//) WARN_UNUSED_RESULT;
static NeroStatus setJtagMode(
struct NeroHandle *handle, bool enable, const char **error
) WARN_UNUSED_RESULT;
// -------------------------------------------------------------------------------------------------
// Public functions
// -------------------------------------------------------------------------------------------------
// Find the NeroJTAG device, open it.
//
NeroStatus neroInitialise(
struct USBDevice *device, struct NeroHandle *handle, uint8 outEndpoint, uint8 inEndpoint,
const char **error)
{
NeroStatus returnCode, nStatus;
handle->device = device;
handle->outEndpoint = outEndpoint;
handle->inEndpoint = inEndpoint;
handle->endpointSize = 64;
nStatus = setJtagMode(handle, true, error);
CHECK_STATUS(nStatus, "neroInitialise()", NERO_ENABLE);
return NERO_SUCCESS;
cleanup:
handle->device = NULL;
handle->endpointSize = 0;
return returnCode;
}
// Close the cable...drop the USB connection.
//
NeroStatus neroClose(struct NeroHandle *handle, const char **error) {
NeroStatus returnCode, nStatus;
if ( handle->device ) {
nStatus = setJtagMode(handle, false, error);
CHECK_STATUS(nStatus, "neroClose()", NERO_ENABLE);
}
returnCode = NERO_SUCCESS;
cleanup:
handle->device = NULL;
handle->endpointSize = 0;
return returnCode;
}
// Shift data into and out of JTAG chain.
// In pointer may be ZEROS (shift in zeros) or ONES (shift in ones).
// Out pointer may be NULL (not interested in data shifted out of the chain).
//
NeroStatus neroShift(
struct NeroHandle *handle, uint32 numBits, const uint8 *inData, uint8 *outData, bool isLast,
const char **error)
{
NeroStatus returnCode, nStatus;
uint32 numBytes;
uint16 chunkSize;
SendType sendType;
bool isResponseNeeded;
if ( inData == ZEROS ) {
sendType = SEND_ZEROS;
} else if ( inData == ONES ) {
sendType = SEND_ONES;
} else {
sendType = SEND_DATA;
}
if ( outData ) {
isResponseNeeded = true;
} else {
isResponseNeeded = false;
}
nStatus = beginShift(handle, numBits, sendType, isLast, isResponseNeeded, error);
CHECK_STATUS(nStatus, "neroShift()", NERO_BEGIN_SHIFT);
numBytes = bitsToBytes(numBits);
while ( numBytes ) {
chunkSize = (numBytes>=handle->endpointSize) ? handle->endpointSize : (uint16)numBytes;
if ( sendType == SEND_DATA ) {
nStatus = doSend(handle, inData, chunkSize, error);
CHECK_STATUS(nStatus, "neroShift()", NERO_SEND);
inData += chunkSize;
}
if ( isResponseNeeded ) {
nStatus = doReceive(handle, outData, chunkSize, error);
CHECK_STATUS(nStatus, "neroShift()", NERO_RECEIVE);
outData += chunkSize;
}
numBytes -= chunkSize;
}
return NERO_SUCCESS;
cleanup:
return returnCode;
}
// Apply the supplied bit pattern to TMS, to move the TAP to a specific state.
//
NeroStatus neroClockFSM(
struct NeroHandle *handle, uint32 bitPattern, uint8 transitionCount, const char **error)
{
NeroStatus returnCode = NERO_SUCCESS;
int uStatus;
union {
uint32 u32;
uint8 bytes[4];
} lePattern;
lePattern.u32 = littleEndian32(bitPattern);
uStatus = usbControlWrite(
handle->device,
CMD_JTAG_CLOCK_FSM, // bRequest
(uint16)transitionCount, // wValue
0x0000, // wIndex
lePattern.bytes, // bit pattern
4, // wLength
5000, // timeout (ms)
error
);
CHECK_STATUS(uStatus, "neroClockFSM()", NERO_CLOCKFSM);
cleanup:
return returnCode;
}
// Cycle the TCK line for the given number of times.
//
NeroStatus neroClocks(struct NeroHandle *handle, uint32 numClocks, const char **error) {
NeroStatus returnCode = NERO_SUCCESS;
int uStatus = usbControlWrite(
handle->device,
CMD_JTAG_CLOCK, // bRequest
numClocks&0xFFFF, // wValue
numClocks>>16, // wIndex
NULL, // no data
0, // wLength
5000, // timeout (ms)
error
);
CHECK_STATUS(uStatus, "neroClocks()", NERO_CLOCKS);
cleanup:
return returnCode;
}
// -------------------------------------------------------------------------------------------------
// Implementation of private functions
// -------------------------------------------------------------------------------------------------
// Kick off a shift operation on the micro. This will be followed by a bunch of sends and receives.
//
static NeroStatus beginShift(
struct NeroHandle *handle, uint32 numBits, SendType sendType, bool isLast,
bool isResponseNeeded, const char **error)
{
NeroStatus returnCode = NERO_SUCCESS;
uint16 wValue = 0x0000;
int uStatus;
union {
uint32 u32;
uint8 bytes[4];
} leNumBits;
leNumBits.u32 = littleEndian32(numBits);
if ( isLast ) {
wValue |= (1<<IS_LAST);
}
if ( isResponseNeeded ) {
wValue |= (1<<IS_RESPONSE_NEEDED);
}
wValue |= sendType << SEND_TYPE;
uStatus = usbControlWrite(
handle->device,
CMD_JTAG_CLOCK_DATA, // bRequest
wValue, // wValue
0x0000, // wIndex
leNumBits.bytes, // send bit count
4, // wLength
5000, // timeout (ms)
error
);
CHECK_STATUS(uStatus, "beginShift()", NERO_BEGIN_SHIFT);
cleanup:
return returnCode;
}
// Send a chunk of data to the micro.
//
static NeroStatus doSend(
struct NeroHandle *handle, const uint8 *sendPtr, uint16 chunkSize, const char **error)
{
NeroStatus returnCode = NERO_SUCCESS;
int uStatus = usbBulkWrite(
handle->device,
handle->outEndpoint, // write to out endpoint
sendPtr, // write from send buffer
chunkSize, // write this many bytes
5000, // timeout in milliseconds
error
);
CHECK_STATUS(uStatus, "doSend()", NERO_SEND);
cleanup:
return returnCode;
}
// Receive a chunk of data from the micro.
//
static NeroStatus doReceive(
struct NeroHandle *handle, uint8 *receivePtr, uint16 chunkSize, const char **error)
{
NeroStatus returnCode = NERO_SUCCESS;
int uStatus = usbBulkRead(
handle->device,
handle->inEndpoint, // read from in endpoint
receivePtr, // read into the receive buffer
chunkSize, // read this many bytes
5000, // timeout in milliseconds
error
);
CHECK_STATUS(uStatus, "doReceive()", NERO_RECEIVE);
cleanup:
return returnCode;
}
/*
// Find the size of the out & in bulk endpoints (they must be the same)
//
static NeroStatus setEndpointSize(struct NeroHandle *handle, const char **error) {
NeroStatus returnCode;
int uStatus;
char descriptorBuffer[1024]; // TODO: Fix by doing two queries
char *ptr = descriptorBuffer;
uint8 endpointNum;
uint16 outEndpointSize = 0;
uint16 inEndpointSize = 0;
struct usb_config_descriptor *configDesc;
struct usb_interface_descriptor *interfaceDesc;
struct usb_endpoint_descriptor *endpointDesc;
handle->endpointSize = 0;
uStatus = usb_control_msg(
handle->device,
USB_ENDPOINT_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
USB_REQ_GET_DESCRIPTOR, // bRequest
0x0200, // wValue
0x0000, // wIndex
descriptorBuffer,
1024, // wLength
5000 // timeout (ms)
);
if ( uStatus < 0 ) {
errRender(
error, "setEndpointSize(): Failed to get config descriptor: %s (%d)", usb_strerror(), uStatus);
FAIL(NERO_ENDPOINTS);
}
if ( uStatus > 0 ) {
configDesc = (struct usb_config_descriptor *)ptr;
ptr += configDesc->bLength;
interfaceDesc = (struct usb_interface_descriptor *)ptr;
ptr += interfaceDesc->bLength;
endpointNum = interfaceDesc->bNumEndpoints;
while ( endpointNum-- ) {
endpointDesc = (struct usb_endpoint_descriptor *)ptr;
if ( endpointDesc-> bmAttributes == 0x02 ) {
if ( endpointDesc->bEndpointAddress == handle->outEndpoint ) {
outEndpointSize = littleEndian16(endpointDesc->wMaxPacketSize);
} else if ( endpointDesc->bEndpointAddress == (handle->inEndpoint | 0x80) ) {
inEndpointSize = littleEndian16(endpointDesc->wMaxPacketSize);
}
}
ptr += endpointDesc->bLength;
}
}
if ( !outEndpointSize ) {
errRender(
error, "setEndpointSize(): EP%dOUT not found or not configured as a bulk endpoint!",
handle->outEndpoint
);
FAIL(NERO_ENDPOINTS);
}
if ( !inEndpointSize ) {
errRender(
error, "setEndpointSize(): EP%dIN not found or not configured as a bulk endpoint!",
handle->inEndpoint
);
FAIL(NERO_ENDPOINTS);
}
if ( outEndpointSize != inEndpointSize ) {
errRender(
error, "setEndpointSize(): EP%dOUT's wMaxPacketSize differs from that of EP%dIN",
handle->outEndpoint, handle->inEndpoint
);
FAIL(NERO_ENDPOINTS);
}
handle->endpointSize = outEndpointSize;
return NERO_SUCCESS;
cleanup:
return returnCode;
}
*/
// Put the device in jtag mode (i.e drive or tristate the JTAG lines)
//
static NeroStatus setJtagMode(struct NeroHandle *handle, bool enable, const char **error) {
NeroStatus returnCode;
int uStatus = usbControlWrite(
handle->device,
CMD_MODE_STATUS, // bRequest
enable ? MODE_JTAG : 0, // wValue
MODE_JTAG, // wMask
NULL, // no data
0, // wLength
5000, // timeout (ms)
error
);
CHECK_STATUS(uStatus, "setJtagMode()", NERO_ENABLE);
return NERO_SUCCESS;
cleanup:
return returnCode;
}