-
Notifications
You must be signed in to change notification settings - Fork 6
/
jtag_functions.c
363 lines (301 loc) · 10.1 KB
/
jtag_functions.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
#include <avr/io.h>
#include <util/delay_basic.h>
#include "Descriptors.h"
#ifdef DEBUG
#include <stdio.h>
#endif //DEBUG
#include "jtag_defs.h"
#include "jtag_functions.h"
uint16_t jtag_delay=0;
//! initialize JTAG interface
void jtag_init(void)
{
JTAG_OUT=0;
JTAG_DIR=JTAG_OUTPUT_MASK;
JTAG_OUT=(1<<JTAG_PIN_TRST)|(1<<JTAG_PIN_SRST); //passive state high
/* while(1)
{
asm("nop");
}*/
}
//! send taps through JTAG interface and recieve responce from TDO pin only
//! \parameter out_buffer - buffer of taps for output, data is packed TDI and TMS values a stored together
//! \parameter out_length - total number of pairs to send (maximum length is 4*255 samples)
//! \parameter in_buffer - buffer which will hold recieved data data will be packed
//! \return number of bytes used in the in_buffer
uint16_t jtag_tap_output_max_speed(const uint8_t *out_buffer, uint16_t out_length, uint8_t *in_buffer)
{
uint8_t tms_tdi;
uint8_t out_data;
uint8_t in_data = 0;
uint16_t out_buffer_index = 0; //output byte counter
uint16_t out_length_index = 0; //output tms/tdo counter
uint16_t in_buffer_index = 0; //input byte counter
//cli();
#ifdef DEBUG
printf("jtag_tap_output_max_speed:Sending %d bits \r\n", out_length);
#endif
while(1)
{
out_data = out_buffer[out_buffer_index++];
//First TMS/TDI/TDO
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
asm("nop");//asm("nop");asm("nop");
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
in_data >>= 1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Second TMS/TDI/TDO
out_data >>= 2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
asm("nop");//asm("nop");asm("nop");
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
in_data >>= 1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Third TMS/TDI/TDO
out_data >>= 2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
asm("nop");//asm("nop");asm("nop");
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
in_data >>= 1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Fourth TMS/TDI/TDO
out_data >>= 2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
asm("nop");//asm("nop");asm("nop");
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
in_data >>= 1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(!(out_length_index & 0x7)) //check if we fiilled the byte
{
in_buffer[in_buffer_index++] = in_data;
in_data = 0;
}
if(out_length_index>=out_length)
break;
}
if(out_length_index&0x7)//leftover
in_buffer[in_buffer_index++] = in_data>>(8-(out_length_index&0x7));
//sei();
return in_buffer_index;
}
//! send taps through JTAG interface and recieve responce from TDO pin only
//! \parameter out_buffer - buffer of taps for output, data is packed TDI and TMS values a stored together
//! \parameter out_length - total number of pairs to send (maximum length is 4*255 samples)
//! \parameter in_buffer - buffer which will hold recieved data data will be packed
//! \return number of bytes used in the in_buffer
uint16_t jtag_tap_output_with_delay(const uint8_t *out_buffer, uint16_t out_length, uint8_t *in_buffer)
{
uint8_t tms_tdi;
uint8_t out_data;
uint8_t in_data = 0;
uint16_t out_buffer_index = 0;
uint16_t in_buffer_index = 0;
uint16_t out_length_index = 0;
#ifdef DEBUG
printf("jtag_tap_output_with_delay:Sending %d bits \r\n", out_length);
#endif
while(1)
{
out_data = out_buffer[out_buffer_index++];
//First TMS/TDI/TDO
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Second TMS/TDI/TDO
out_data = out_data>>2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Third TMS/TDI/TDO
out_data = out_data>>2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Fourth TMS/TDI/TDO
out_data = out_data>>2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
out_length_index++;
if(!(out_length_index%8))
{
in_buffer[in_buffer_index] = in_data;
in_buffer_index++;
in_data = 0;
}
if(out_length_index>=out_length)
break;
}
if(out_length_index%8)
in_buffer[in_buffer_index] = in_data>>(8-(out_length_index%8));
return (out_length+7)/8;
}
//! send taps through JTAG interface and recieve responce from TDO and EMU pins
//! \parameter out_buffer - buffer of taps for output, data is packed TDI and TMS values a stored together
//! \parameter out_length - total number of pairs to send (maximum length is 4*255 samples)
//! \parameter in_buffer - buffer which will hold recieved data data will be packed
//! \return number of bytes used in the in_buffer (equal to the input (length+3)/4
uint16_t jtag_tap_output_emu(const uint8_t *out_buffer,uint16_t out_length,uint8_t *in_buffer)
{
uint8_t tms_tdi;
uint8_t out_data;
uint8_t in_data = 0;
uint16_t out_buffer_index = 0;
uint16_t in_buffer_index = 0;
uint16_t out_length_index = 0;
while(1)
{
out_data = out_buffer[out_buffer_index++];
//First TMS/TDI/TDO
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_EMU))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Second TMS/TDI/TDO
out_data = out_data>>2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_EMU))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Third TMS/TDI/TDO
out_data = out_data>>2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_EMU))&0x80);
out_length_index++;
if(out_length_index>=out_length)
break;
//Fourth TMS/TDI/TDO
out_data = out_data>>2;
tms_tdi = out_data & JTAG_SIGNAL_MASK;
JTAG_OUT = ( JTAG_OUT & ( ~JTAG_SIGNAL_MASK ) ) | tms_tdi;
JTAG_OUT|=JTAG_CLK_HI;//CLK hi
_delay_loop_2(jtag_delay);
JTAG_OUT&=JTAG_CLK_LO;//CLK lo
_delay_loop_2(jtag_delay);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_TDO))&0x80);
in_data = in_data>>1;
in_data |= ((JTAG_IN<<(7-JTAG_PIN_EMU))&0x80);
out_length_index++;
if(!(out_length_index%4))
{
in_buffer[in_buffer_index] = in_data;
in_buffer_index++;
in_data = 0;
}
if(out_length_index>=out_length)
break;
}
if(out_length_index%4)
in_buffer[in_buffer_index] = in_data>>(8-2*(out_length_index%4));
return (out_length+3)/4;
}
//! return current status of TDO & EMU pins
//! \return packed result TDO - bit 0 , EMU bit 1
uint16_t jtag_read_input(void)
{
uint8_t data=JTAG_IN;
return ((data>>JTAG_PIN_TDO)&1)|(((data>>JTAG_PIN_EMU)&1)<<1);
}
//! set pin TRST
void jtag_set_trst(uint8_t trst)
{
JTAG_OUT= (JTAG_OUT&(~(1<<JTAG_PIN_TRST)))|(trst<<JTAG_PIN_TRST);
}
//! set pin SRST
void jtag_set_srst(uint8_t srst)
{
JTAG_OUT=(JTAG_OUT&(~(1<<JTAG_PIN_SRST))) |(srst<<JTAG_PIN_SRST);
}
//! set both srst and trst simultaneously
void jtag_set_trst_srst(uint8_t trst,uint8_t srst)
{
uint8_t mask=(trst<<JTAG_PIN_TRST)|(srst<<JTAG_PIN_SRST);
uint8_t out=JTAG_OUT& (~ ((1<<JTAG_PIN_SRST)|(1<<JTAG_PIN_TRST)));
JTAG_OUT=out|mask;
}
opendous_jtag_config_t PROGMEM opendous_jtag_config =
{
.version_major=0,
.version_minor=4,
.max_tap_length=OPENDOUS_USB_BUFFER_SIZE*4
};
uint16_t jtag_read_config(uint8_t *in_buffer)
{
//*((opendous_jtag_config*)(in_buffer))=
memcpy_P(in_buffer,&opendous_jtag_config,sizeof(opendous_jtag_config));
return sizeof(opendous_jtag_config);
}