comparison src/jtag.c @ 4:6b1594fb668f

Improve performance of jtag.c and test it with Python scripts. - cp_tditdo.py send bits to TDI and hope it will feed back from TDO. - Expand loops in jtag.c to improve performance.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 22 Feb 2009 12:54:45 +0800
parents abf221bf3ce4
children eb14cac68cbb
comparison
equal deleted inserted replaced
3:e410832c3280 4:6b1594fb668f
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() do { _delay_ms(0.0004); } while(0) 7 #define CLK_DELAY()
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);
19 #define TCK_LO() pin_lo(JTAG_PORT, JTAG_TCK) 19 #define TCK_LO() pin_lo(JTAG_PORT, JTAG_TCK)
20 #define TCK_HI() pin_hi(JTAG_PORT, JTAG_TCK) 20 #define TCK_HI() pin_hi(JTAG_PORT, JTAG_TCK)
21 #define SEND_BIT(pin, bit) \ 21 #define SEND_BIT(pin, bit) \
22 do { \ 22 do { \
23 if(bit) \ 23 if(bit) \
24 pin_hi(JTAG_PORT, JTAG_TDI); \ 24 pin_hi(JTAG_PORT, pin); \
25 else \ 25 else \
26 pin_lo(JTAG_PORT, JTAG_TDI); \ 26 pin_lo(JTAG_PORT, pin); \
27 CLK_DELAY(); \ 27 CLK_DELAY(); \
28 TCK_HI(); \ 28 TCK_HI(); \
29 CLK_DELAY(); \ 29 CLK_DELAY(); \
30 } while(0) 30 } while(0)
31 #define GET_TDO() (JTAG_PIN & _BV(JTAG_TDO)) 31 #define GET_TDO() (JTAG_PIN & _BV(JTAG_TDO))
32 32
33 void jtag_tms(char *buf, int nbits) { 33 void jtag_tms(char *buf, int nbits) {
34 int i; 34 int i;
35 int nbytes, byte;
35 int byteoff, bitoff; 36 int byteoff, bitoff;
36 int bit; 37 int bit;
37 38
39 nbytes = nbits / 8;
40 for(i = 0; i < nbytes; i++) {
41 byte = buf[i];
42
43 bit = byte & 0x01;
44 SEND_BIT(JTAG_TMS, bit);
45 TCK_LO();
46
47 bit = byte & 0x02;
48 SEND_BIT(JTAG_TMS, bit);
49 TCK_LO();
50
51 bit = byte & 0x04;
52 SEND_BIT(JTAG_TMS, bit);
53 TCK_LO();
54
55 bit = byte & 0x08;
56 SEND_BIT(JTAG_TMS, bit);
57 TCK_LO();
58
59 bit = byte & 0x10;
60 SEND_BIT(JTAG_TMS, bit);
61 TCK_LO();
62
63 bit = byte & 0x20;
64 SEND_BIT(JTAG_TMS, bit);
65 TCK_LO();
66
67 bit = byte & 0x40;
68 SEND_BIT(JTAG_TMS, bit);
69 TCK_LO();
70
71 bit = byte & 0x80;
72 SEND_BIT(JTAG_TMS, bit);
73 TCK_LO();
74 }
75
76 byte = buf[i];
77 nbits %= 8;
38 for(i = 0; i < nbits; i++) { 78 for(i = 0; i < nbits; i++) {
39 byteoff = i / 8; 79 bit = byte & (1 << i);
40 bitoff = i % 8;
41 bit = buf[byteoff] & (1 << bitoff);
42 SEND_BIT(JTAG_TMS, bit); 80 SEND_BIT(JTAG_TMS, bit);
43 TCK_LO(); 81 TCK_LO();
44 } 82 }
45 } 83 }
46 84
47 void jtag_shift(char *buf, int nbits) { 85 void jtag_shift(char *buf, int nbits) {
48 int i; 86 int i;
87 int nbytes, byte;
49 int byteoff, bitoff; 88 int byteoff, bitoff;
50 int bit; 89 int bit;
51 90
52 for(i = 0; i < nbits; i++) { 91 nbytes = nbits / 8;
53 byteoff = i / 8; 92 for(i = 0; i < nbytes; i++) {
54 bitoff = i % 8; 93 byte = buf[i];
55 bit = buf[byteoff] & (1 << bitoff); 94
56 SEND_BIT(JTAG_TDI, bit); 95 bit = byte & 0x01;
57 TCK_LO(); 96 SEND_BIT(JTAG_TDI, bit);
97 TCK_LO();
98
99 bit = byte & 0x02;
100 SEND_BIT(JTAG_TDI, bit);
101 TCK_LO();
102
103 bit = byte & 0x04;
104 SEND_BIT(JTAG_TDI, bit);
105 TCK_LO();
106
107 bit = byte & 0x08;
108 SEND_BIT(JTAG_TDI, bit);
109 TCK_LO();
110
111 bit = byte & 0x10;
112 SEND_BIT(JTAG_TDI, bit);
113 TCK_LO();
114
115 bit = byte & 0x20;
116 SEND_BIT(JTAG_TDI, bit);
117 TCK_LO();
118
119 bit = byte & 0x40;
120 SEND_BIT(JTAG_TDI, bit);
121 TCK_LO();
122
123 bit = byte & 0x80;
124 SEND_BIT(JTAG_TDI, bit);
125 TCK_LO();
126 }
127
128 nbits %= 8;
129 if(nbits) {
130 byte = buf[i];
131 for(i = 0; i < nbits; i++) {
132 bit = byte & (1 << i);
133 SEND_BIT(JTAG_TDI, bit);
134 TCK_LO();
135 }
58 } 136 }
59 } 137 }
60 138
61 void jtag_shift_inout(char *ibuf, char *obuf, int nbits) { 139 void jtag_shift_inout(char *ibuf, char *obuf, int nbits) {
62 int i; 140 int i, j;
141 int nbytes, byte, obyte;
142 int tdo;
63 int byteoff, bitoff; 143 int byteoff, bitoff;
64 int bit, tdo, bitmask; 144 int bit;
65 145
66 for(i = 0; i < nbits; i++) { 146 nbytes = nbits / 8;
67 byteoff = i / 8; 147 for(i = 0; i < nbytes; i++) {
68 bitoff = i % 8; 148 byte = ibuf[i];
69 bitmask = 1 << bitoff; 149 obyte = 0;
70 150
71 bit = ibuf[byteoff] & bitmask; 151 bit = byte & 0x01;
72 SEND_BIT(JTAG_TDI, bit); 152 SEND_BIT(JTAG_TDI, bit);
73 tdo = GET_TDO(); 153 tdo = GET_TDO();
74 TCK_LO(); 154 if(tdo)
75 if(tdo) 155 obyte |= 0x01;
76 obuf[byteoff] |= bitmask; 156 else
77 else 157 obyte &= ~0x01;
78 obuf[byteoff] &= ~(bitmask); 158 TCK_LO();
79 } 159
80 } 160 bit = byte & 0x02;
161 SEND_BIT(JTAG_TDI, bit);
162 tdo = GET_TDO();
163 if(tdo)
164 obyte |= 0x02;
165 else
166 obyte &= ~0x02;
167 TCK_LO();
168
169 bit = byte & 0x04;
170 SEND_BIT(JTAG_TDI, bit);
171 tdo = GET_TDO();
172 if(tdo)
173 obyte |= 0x04;
174 else
175 obyte &= ~0x04;
176 TCK_LO();
177
178 bit = byte & 0x08;
179 SEND_BIT(JTAG_TDI, bit);
180 tdo = GET_TDO();
181 if(tdo)
182 obyte |= 0x08;
183 else
184 obyte &= ~0x08;
185 TCK_LO();
186
187 bit = byte & 0x10;
188 SEND_BIT(JTAG_TDI, bit);
189 tdo = GET_TDO();
190 if(tdo)
191 obyte |= 0x10;
192 else
193 obyte &= ~0x10;
194 TCK_LO();
195
196 bit = byte & 0x20;
197 SEND_BIT(JTAG_TDI, bit);
198 tdo = GET_TDO();
199 if(tdo)
200 obyte |= 0x20;
201 else
202 obyte &= ~0x20;
203 TCK_LO();
204
205 bit = byte & 0x40;
206 SEND_BIT(JTAG_TDI, bit);
207 tdo = GET_TDO();
208 if(tdo)
209 obyte |= 0x40;
210 else
211 obyte &= ~0x40;
212 TCK_LO();
213
214 bit = byte & 0x80;
215 SEND_BIT(JTAG_TDI, bit);
216 tdo = GET_TDO();
217 if(tdo)
218 obyte |= 0x80;
219 else
220 obyte &= ~0x80;
221 TCK_LO();
222
223 obuf[i] = obyte;
224 }
225
226 nbits %= 8;
227 if(nbits) {
228 byte = ibuf[i];
229 obyte = 0;
230 for(j = 0; j < nbits; j++) {
231 bit = byte & (1 << j);
232 SEND_BIT(JTAG_TDI, bit);
233 tdo = GET_TDO();
234 if(tdo)
235 obyte |= 1 << j;
236 else
237 obyte &= ~(1 << j);
238 TCK_LO();
239 }
240 obuf[i] = obyte;
241 }
242 }