annotate src/avr_jtag.c @ 8:074e860d7d31

jtagdev is a desktop client to communicate with jtag device through Arduino.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 22 Feb 2009 22:30:20 +0800
parents 61f27549de57
children cc106f278d7d
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
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
11 static 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
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 void nak(int seq, const char *msg) {
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
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 void send_client(const char *buf, int bsz) {
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
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
44 void flash_init(void) {
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
45 pin_mode(&PORTB, PINB4, PM_OUTPUT);
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
46 pin_mode(&PORTB, PINB5, PM_OUTPUT);
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
47 pin_lo(PORTB, PINB4);
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
48 pin_lo(PORTB, PINB5);
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
49 }
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
50
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
51 void flash_led(void) {
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
52 pin_hi(PORTB, PINB4);
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
53 _delay_ms(50);
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
54 pin_lo(PORTB, PINB4);
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
55 }
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
56
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 #define GET_DATA_BITS(data) ((data)[0] | ((data)[1] << 8))
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 int main(int argc, char * const argv[]) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 cp_cmd_t *cmd;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 cmd_proto_t *cp;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 int c, nbits;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 int bsz;
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
64 static char buf[16];
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
66 flash_init();
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
67 flash_led();
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
68
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 uart_init(BAUD_RATE);
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
70 jtag_init();
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 cp = cmd_proto_new();
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
72
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 while(1) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 uart_getc(c);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 cmd = cmd_proto_rcv(cp, c);
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
76 if(cmd == NULL)
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
77 continue;
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 if(cmd == &BAD_CMD) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 nak(cmd->seq, "BAD CMD");
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 continue;
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
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 if(cmd == &CSUM_ERR_CMD) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 nak(cmd->seq, "CSUM ERR");
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 continue;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 }
3
e410832c3280 Issue of avr-ld.
Thinker K.F. Li <thinker@branda.to>
parents: 2
diff changeset
88
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 switch(cmd->code) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 case CPCMD_PING:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 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
92 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 CPCMD_PONG, cmd->data_sz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 send_client(client_buf, bsz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 case CPCMD_SHIFT_TDI:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 jtag_shift(cmd->data + 2, nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 ack(cmd->seq);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 case CPCMD_SHIFT_TMS:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 jtag_tms(cmd->data + 2, nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 ack(cmd->seq);
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
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 case CPCMD_SHIFT_TDI_TDO:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 jtag_shift_inout(cmd->data + 2,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 client_buf + CP_CMD_HEAD_SZ + 2,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 client_buf[CP_CMD_HEAD_SZ] = nbits & 0xff;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 client_buf[CP_CMD_HEAD_SZ + 1] = nbits >> 8;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 CPCMD_DATA, (nbits + 23) / 8);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 send_client(client_buf, bsz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122
7
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
123 case CPCMD_TRST:
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
124 jtag_trst();
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
125 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
126 CPCMD_ACK, 0);
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
127 send_client(client_buf, bsz);
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
128 break;
61f27549de57 Support TRST.
Thinker K.F. Li <thinker@branda.to>
parents: 3
diff changeset
129
2
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130 default:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 sprintf(buf, "CODE 0x%02x", cmd->code);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132 nak(cmd->seq, buf);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135 }