comparison src/jtag.c @ 13:1ea479d26fce tip

Make sure shifting phase and add bypass.py. - shifting phase is started after entering SHIFT state Transition from CAP to SHIFT does not induce shifting. - shifting phase is stoped after leaving SHIFT state. Transition from SHIFT to EXIT1 also induce a bit of shifting.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 25 Feb 2009 20:08:29 +0800
parents cc106f278d7d
children
comparison
equal deleted inserted replaced
12:68ecd42850d3 13:1ea479d26fce
2 #include <util/delay.h> 2 #include <util/delay.h>
3 #include "jtag.h" 3 #include "jtag.h"
4 #include "avriotools.h" 4 #include "avriotools.h"
5 5
6 /* It is supposed to work at 1Mbps */ 6 /* It is supposed to work at 1Mbps */
7 #define CLK_DELAY() _delay_us(1) 7 #define CLK_DELAY() _delay_us(2)
8 8
9 void jtag_init(void) { 9 void jtag_init(void) {
10 pin_mode(&JTAG_PORT, JTAG_TCK, PM_OUTPUT); 10 pin_mode(&JTAG_PORT, JTAG_TCK, PM_OUTPUT);
11 pin_mode(&JTAG_PORT, JTAG_TMS, PM_OUTPUT); 11 pin_mode(&JTAG_PORT, JTAG_TMS, PM_OUTPUT);
12 pin_mode(&JTAG_PORT, JTAG_TDI, PM_OUTPUT); 12 pin_mode(&JTAG_PORT, JTAG_TDI, PM_OUTPUT);
16 pin_lo(JTAG_PORT, JTAG_TMS); 16 pin_lo(JTAG_PORT, JTAG_TMS);
17 pin_lo(JTAG_PORT, JTAG_TDI); 17 pin_lo(JTAG_PORT, JTAG_TDI);
18 pin_hi(JTAG_PORT, JTAG_TRST); 18 pin_hi(JTAG_PORT, JTAG_TRST);
19 } 19 }
20 20
21 #define TCK_LO() pin_lo(JTAG_PORT, JTAG_TCK) 21 #define _TDI_TMS_TCK(tdi, tms, tck) \
22 #define TCK_HI() pin_hi(JTAG_PORT, JTAG_TCK) 22 ((tms? _BV(JTAG_TMS): 0) | \
23 #define SEND_BIT(pin, bit) \ 23 (tdi? _BV(JTAG_TDI): 0) | \
24 (tck? _BV(JTAG_TCK): 0))
25 #define _SET_PINS(pv, tdi, tms, tck) \
24 do { \ 26 do { \
25 if(bit) \ 27 pv = JTAG_PORT; \
26 pin_hi(JTAG_PORT, pin); \ 28 pv &= ~_TDI_TMS_TCK(1, 1, 1); \
27 else \ 29 pv |= _TDI_TMS_TCK(tdi, tms, tck); \
28 pin_lo(JTAG_PORT, pin); \ 30 JTAG_PORT = pv; \
29 CLK_DELAY(); \ 31 } while(0)
30 TCK_HI(); \ 32 #define SEND_BIT(pv, tdi, tms) \
31 CLK_DELAY(); \ 33 do { \
32 TCK_LO(); \ 34 _SET_PINS(pv, tdi, tms, 0); \
35 CLK_DELAY(); \
36 _SET_PINS(pv, tdi, tms, 1); \
37 CLK_DELAY(); \
33 } while(0) 38 } while(0)
34 #define GET_TDO() (JTAG_PIN & _BV(JTAG_TDO)) 39 #define GET_TDO() (JTAG_PIN & _BV(JTAG_TDO))
35 #define SEND_GET_BIT(pin, bit, out) \ 40 #define SEND_GET_BIT(pv, tdi, tms, out) \
36 do { \ 41 do { \
37 if(bit) \ 42 _SET_PINS(pv, tdi, tms, 0); \
38 pin_hi(JTAG_PORT, pin); \
39 else \
40 pin_lo(JTAG_PORT, pin); \
41 CLK_DELAY(); \
42 TCK_HI(); \
43 CLK_DELAY(); \ 43 CLK_DELAY(); \
44 out = GET_TDO(); \ 44 out = GET_TDO(); \
45 TCK_LO(); \ 45 _SET_PINS(pv, tdi, tms, 1); \
46 CLK_DELAY(); \
46 } while(0) 47 } while(0)
47 48
48 void jtag_trst(void) { 49 void jtag_trst(void) {
50 unsigned char pv;
51
52 pin_lo(JTAG_PORT, JTAG_TCK);
49 pin_lo(JTAG_PORT, JTAG_TRST); 53 pin_lo(JTAG_PORT, JTAG_TRST);
50 SEND_BIT(JTAG_TMS, 1); 54
51 SEND_BIT(JTAG_TMS, 1); 55 SEND_BIT(pv, 1, 1);
52 SEND_BIT(JTAG_TMS, 1); 56 SEND_BIT(pv, 1, 1);
53 SEND_BIT(JTAG_TMS, 1); 57 SEND_BIT(pv, 1, 1);
54 SEND_BIT(JTAG_TMS, 1); 58 SEND_BIT(pv, 1, 1);
59 SEND_BIT(pv, 1, 1);
60
61 pin_lo(JTAG_PORT, JTAG_TCK);
55 pin_hi(JTAG_PORT, JTAG_TRST); 62 pin_hi(JTAG_PORT, JTAG_TRST);
63
64 SEND_BIT(pv, 1, 1);
56 } 65 }
57 66
58 /*! 67 /*!
59 * Before shifting registers, TAP controller must move to last state before 68 * Before shifting registers, TAP controller must move to last state before
60 * shift state. The state machine transite to shift state when shifting 69 * shift state. The state machine transite to shift state when shifting
61 * starts. 70 * starts.
62 */ 71 */
63 void jtag_tms(unsigned char *buf, int nbits) { 72 void jtag_tms(unsigned char *buf, int nbits) {
64 int i; 73 int i;
65 int nbytes, byte; 74 int nbytes;
66 int byteoff, bitoff; 75 int byteoff, bitoff;
67 int bit; 76 unsigned char byte;
68 77 unsigned char bit;
69 pin_hi(JTAG_PORT, JTAG_TDI); 78 unsigned char pv;
70 79
71 nbytes = nbits / 8; 80 nbytes = nbits / 8;
72 for(i = 0; i < nbytes; i++) { 81 for(i = 0; i < nbytes; i++) {
73 byte = buf[i]; 82 byte = buf[i];
74 83
75 bit = byte & 0x01; 84 bit = byte & 0x01;
76 SEND_BIT(JTAG_TMS, bit); 85 SEND_BIT(pv, 1, bit);
77 86
78 bit = byte & 0x02; 87 bit = byte & 0x02;
79 SEND_BIT(JTAG_TMS, bit); 88 SEND_BIT(pv, 1, bit);
80 89
81 bit = byte & 0x04; 90 bit = byte & 0x04;
82 SEND_BIT(JTAG_TMS, bit); 91 SEND_BIT(pv, 1, bit);
83 92
84 bit = byte & 0x08; 93 bit = byte & 0x08;
85 SEND_BIT(JTAG_TMS, bit); 94 SEND_BIT(pv, 1, bit);
86 95
87 bit = byte & 0x10; 96 bit = byte & 0x10;
88 SEND_BIT(JTAG_TMS, bit); 97 SEND_BIT(pv, 1, bit);
89 98
90 bit = byte & 0x20; 99 bit = byte & 0x20;
91 SEND_BIT(JTAG_TMS, bit); 100 SEND_BIT(pv, 1, bit);
92 101
93 bit = byte & 0x40; 102 bit = byte & 0x40;
94 SEND_BIT(JTAG_TMS, bit); 103 SEND_BIT(pv, 1, bit);
95 104
96 bit = byte & 0x80; 105 bit = byte & 0x80;
97 SEND_BIT(JTAG_TMS, bit); 106 SEND_BIT(pv, 1, bit);
98 } 107 }
99 108
100 byte = buf[i]; 109 byte = buf[i];
101 nbits %= 8; 110 nbits %= 8;
102 for(i = 0; i < nbits; i++) { 111 for(i = 0; i < nbits; i++) {
103 bit = byte & (1 << i); 112 bit = byte & (1 << i);
104 SEND_BIT(JTAG_TMS, bit); 113 SEND_BIT(pv, 1, bit);
105 } 114 }
106 } 115 }
107 116
108 void jtag_shift(unsigned char *buf, int nbits) { 117 void jtag_shift(unsigned char *buf, int nbits) {
109 int i; 118 int i;
110 int nbits_1; 119 int nbits_1;
111 int nbytes, byte; 120 int nbytes;
121 int remain;
112 int byteoff, bitoff; 122 int byteoff, bitoff;
113 int bit, remain; 123 unsigned char byte;
124 unsigned char bit;
125 unsigned char pv;
114 126
115 if(nbits == 0) 127 if(nbits == 0)
116 return; 128 return;
117 129
118 /* Transite to shift state.
119 * \sa jtag_tms()
120 */
121 pin_lo(JTAG_PORT, JTAG_TMS);
122
123 nbits_1 = nbits - 1; 130 nbits_1 = nbits - 1;
124 nbytes = nbits_1 / 8; 131 nbytes = nbits_1 / 8;
125 for(i = 0; i < nbytes; i++) { 132 for(i = 0; i < nbytes; i++) {
126 byte = buf[i]; 133 byte = buf[i];
127 134
128 bit = byte & 0x01; 135 bit = byte & 0x01;
129 SEND_BIT(JTAG_TDI, bit); 136 SEND_BIT(pv, bit, 0);
130 137
131 bit = byte & 0x02; 138 bit = byte & 0x02;
132 SEND_BIT(JTAG_TDI, bit); 139 SEND_BIT(pv, bit, 0);
133 140
134 bit = byte & 0x04; 141 bit = byte & 0x04;
135 SEND_BIT(JTAG_TDI, bit); 142 SEND_BIT(pv, bit, 0);
136 143
137 bit = byte & 0x08; 144 bit = byte & 0x08;
138 SEND_BIT(JTAG_TDI, bit); 145 SEND_BIT(pv, bit, 0);
139 146
140 bit = byte & 0x10; 147 bit = byte & 0x10;
141 SEND_BIT(JTAG_TDI, bit); 148 SEND_BIT(pv, bit, 0);
142 149
143 bit = byte & 0x20; 150 bit = byte & 0x20;
144 SEND_BIT(JTAG_TDI, bit); 151 SEND_BIT(pv, bit, 0);
145 152
146 bit = byte & 0x40; 153 bit = byte & 0x40;
147 SEND_BIT(JTAG_TDI, bit); 154 SEND_BIT(pv, bit, 0);
148 155
149 bit = byte & 0x80; 156 bit = byte & 0x80;
150 SEND_BIT(JTAG_TDI, bit); 157 SEND_BIT(pv, bit, 0);
151 } 158 }
152 159
153 remain = nbits_1 % 8; 160 remain = nbits_1 % 8;
154 byte = buf[i]; 161 byte = buf[i];
155 for(i = 0; i < remain; i++) { 162 for(i = 0; i < remain; i++) {
156 bit = byte & (1 << i); 163 bit = byte & (1 << i);
157 SEND_BIT(JTAG_TDI, bit); 164 SEND_BIT(pv, bit, 0);
158 } 165 }
159 166
160 byte = buf[nbits / 8]; 167 byte = buf[nbits / 8];
161 bit = byte & (1 << (nbits_1 % 8)); 168 bit = byte & (1 << (nbits_1 % 8));
162 169
163 pin_hi(JTAG_PORT, JTAG_TMS); 170 SEND_BIT(pv, bit, 1);
164 SEND_BIT(JTAG_TDI, bit);
165 } 171 }
166 172
167 void jtag_shift_inout(unsigned char *ibuf, unsigned char *obuf, int nbits) { 173 void jtag_shift_inout(unsigned char *ibuf, unsigned char *obuf, int nbits) {
168 int i, j; 174 int i, j;
169 int nbits_1; 175 int nbits_1;
170 int nbytes, byte, obyte; 176 int nbytes;
171 int tdo; 177 int tdo;
172 int byteoff, bitoff; 178 int byteoff, bitoff;
173 int bit, remain; 179 int remain;
180 unsigned char byte, obyte;
181 unsigned char bit;
182 unsigned char pv;
174 183
175 if(nbits == 0) 184 if(nbits == 0)
176 return; 185 return;
177 186
178 /* Transite to shift state.
179 * \sa jtag_tms()
180 */
181 pin_lo(JTAG_PORT, JTAG_TMS);
182
183 nbits_1 = nbits - 1; 187 nbits_1 = nbits - 1;
184 nbytes = nbits_1 / 8; 188 nbytes = nbits_1 / 8;
185 for(i = 0; i < nbytes; i++) { 189 for(i = 0; i < nbytes; i++) {
186 byte = ibuf[i]; 190 byte = ibuf[i];
187 obyte = 0; 191 obyte = 0;
188 192
189 bit = byte & 0x01; 193 bit = byte & 0x01;
190 SEND_GET_BIT(JTAG_TDI, bit, tdo); 194 SEND_GET_BIT(pv, bit, 0, tdo);
191 if(tdo) 195 if(tdo)
192 obyte |= 0x01; 196 obyte |= 0x01;
193 else
194 obyte &= ~0x01;
195 197
196 bit = byte & 0x02; 198 bit = byte & 0x02;
197 SEND_GET_BIT(JTAG_TDI, bit, tdo); 199 SEND_GET_BIT(pv, bit, 0, tdo);
198 tdo = GET_TDO(); 200 tdo = GET_TDO();
199 if(tdo) 201 if(tdo)
200 obyte |= 0x02; 202 obyte |= 0x02;
201 else
202 obyte &= ~0x02;
203 203
204 bit = byte & 0x04; 204 bit = byte & 0x04;
205 SEND_GET_BIT(JTAG_TDI, bit, tdo); 205 SEND_GET_BIT(pv, bit, 0, tdo);
206 if(tdo) 206 if(tdo)
207 obyte |= 0x04; 207 obyte |= 0x04;
208 else
209 obyte &= ~0x04;
210 208
211 bit = byte & 0x08; 209 bit = byte & 0x08;
212 SEND_GET_BIT(JTAG_TDI, bit, tdo); 210 SEND_GET_BIT(pv, bit, 0, tdo);
213 if(tdo) 211 if(tdo)
214 obyte |= 0x08; 212 obyte |= 0x08;
215 else
216 obyte &= ~0x08;
217 213
218 bit = byte & 0x10; 214 bit = byte & 0x10;
219 SEND_GET_BIT(JTAG_TDI, bit, tdo); 215 SEND_GET_BIT(pv, bit, 0, tdo);
220 if(tdo) 216 if(tdo)
221 obyte |= 0x10; 217 obyte |= 0x10;
222 else
223 obyte &= ~0x10;
224 218
225 bit = byte & 0x20; 219 bit = byte & 0x20;
226 SEND_GET_BIT(JTAG_TDI, bit, tdo); 220 SEND_GET_BIT(pv, bit, 0, tdo);
227 if(tdo) 221 if(tdo)
228 obyte |= 0x20; 222 obyte |= 0x20;
229 else
230 obyte &= ~0x20;
231 223
232 bit = byte & 0x40; 224 bit = byte & 0x40;
233 SEND_GET_BIT(JTAG_TDI, bit, tdo); 225 SEND_GET_BIT(pv, bit, 0, tdo);
234 if(tdo) 226 if(tdo)
235 obyte |= 0x40; 227 obyte |= 0x40;
236 else
237 obyte &= ~0x40;
238 228
239 bit = byte & 0x80; 229 bit = byte & 0x80;
240 SEND_GET_BIT(JTAG_TDI, bit, tdo); 230 SEND_GET_BIT(pv, bit, 0, tdo);
241 if(tdo) 231 if(tdo)
242 obyte |= 0x80; 232 obyte |= 0x80;
243 else
244 obyte &= ~0x80;
245 233
246 obuf[i] = obyte; 234 obuf[i] = obyte;
247 } 235 }
248 236
249 remain = nbits_1 % 8; 237 remain = nbits_1 % 8;
250 byte = ibuf[i]; 238 byte = ibuf[i];
251 obyte = 0; 239 obyte = 0;
252 for(j = 0; j < remain; j++) { 240 for(j = 0; j < remain; j++) {
253 bit = byte & (1 << j); 241 bit = byte & (1 << j);
254 SEND_GET_BIT(JTAG_TDI, bit, tdo); 242 SEND_GET_BIT(pv, bit, 0, tdo);
255 if(tdo) 243 if(tdo)
256 obyte |= 1 << j; 244 obyte |= 1 << j;
257 else 245 else
258 obyte &= ~(1 << j); 246 obyte &= ~(1 << j);
259 } 247 }
260 obuf[i] = obyte; 248 obuf[i] = obyte;
261 249
262 byte = ibuf[nbits / 8]; 250 byte = ibuf[nbits / 8];
263 bit = byte & (1 << (nbits_1 % 8)); 251 bit = byte & (1 << (nbits_1 % 8));
264 252
265 pin_hi(JTAG_PORT, JTAG_TMS); 253 SEND_GET_BIT(pv, bit, 1, tdo);
266 SEND_GET_BIT(JTAG_TDI, bit, tdo);
267 if(tdo) 254 if(tdo)
268 obuf[nbits / 8] |= 1 << j; 255 obuf[nbits / 8] |= 1 << j;
269 else 256 else
270 obuf[nbits / 8] &= ~(1 << j); 257 obuf[nbits / 8] &= ~(1 << j);
271 } 258 }