annotate src/avr_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
rev   line source
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <stdlib.h>
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <string.h>
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
4 #include <util/delay.h>
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include "avriotools.h"
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include "cmd_proto.h"
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 #include "jtag.h"
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 #define BAUD_RATE 420000
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
11 static unsigned char client_buf[256 + CP_CMD_OVERHEAD];
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 static
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 void ack(int seq) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 int i;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 int sz;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 sz = cmd_proto_cmd_fill(client_buf, seq, CPCMD_ACK, 0);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 for(i = 0; i < sz; i++)
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 uart_putc(client_buf[i]);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 static
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
25 void nak(int seq, const unsigned char *msg) {
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 int i;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 int sz;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 memcpy(client_buf + CP_CMD_HEAD_SZ, msg, strlen(msg));
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 sz = cmd_proto_cmd_fill(client_buf, seq, CPCMD_NAK, 7);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 for(i = 0; i < sz; i++)
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 uart_putc(client_buf[i]);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 static
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
37 void send_client(const unsigned char *buf, int bsz) {
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 int i;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 for(i = 0; i < bsz; i++)
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 uart_putc(buf[i]);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
44 #define GET_DATA_BITS(data) (((unsigned int)(data)[0]) | \
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
45 ((unsigned int)(data)[1] << 8))
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 int main(int argc, char * const argv[]) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 cp_cmd_t *cmd;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 cmd_proto_t *cp;
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
50 int c;
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
51 unsigned int nbits;
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 int bsz;
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 7
diff changeset
53 static unsigned char buf[16];
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
54
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 uart_init(BAUD_RATE);
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
56 jtag_init();
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 cp = cmd_proto_new();
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
58
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 while(1) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 uart_getc(c);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 cmd = cmd_proto_rcv(cp, c);
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
62 if(cmd == NULL)
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
63 continue;
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 if(cmd == &BAD_CMD) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 nak(cmd->seq, "BAD CMD");
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 continue;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 if(cmd == &CSUM_ERR_CMD) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 nak(cmd->seq, "CSUM ERR");
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 continue;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 }
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
74
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 switch(cmd->code) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 case CPCMD_PING:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 memcpy(client_buf + CP_CMD_HEAD_SZ, cmd->data, cmd->data_sz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 CPCMD_PONG, cmd->data_sz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 send_client(client_buf, bsz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 case CPCMD_SHIFT_TDI:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 jtag_shift(cmd->data + 2, nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 ack(cmd->seq);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 case CPCMD_SHIFT_TMS:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 jtag_tms(cmd->data + 2, nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 ack(cmd->seq);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 case CPCMD_SHIFT_TDI_TDO:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 jtag_shift_inout(cmd->data + 2,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 client_buf + CP_CMD_HEAD_SZ + 2,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 client_buf[CP_CMD_HEAD_SZ] = nbits & 0xff;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 client_buf[CP_CMD_HEAD_SZ + 1] = nbits >> 8;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 CPCMD_DATA, (nbits + 23) / 8);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 send_client(client_buf, bsz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108
7
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
109 case CPCMD_TRST:
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
110 jtag_trst();
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
111 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
112 CPCMD_ACK, 0);
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
113 send_client(client_buf, bsz);
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
114 break;
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
115
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 default:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 sprintf(buf, "CODE 0x%02x", cmd->code);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 nak(cmd->seq, buf);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 }