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