annotate src/avr_jtag.c @ 2:abf221bf3ce4

AVR JTAG server.
author Thinker K.F. Li <thinker@branda.to>
date Sat, 21 Feb 2009 23:06:50 +0800
parents
children e410832c3280
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>
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include "avriotools.h"
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include "cmd_proto.h"
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include "jtag.h"
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 #define BAUD_RATE 420000
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 static char client_buf[255 + CP_CMD_OVERHEAD];
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 static
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 void ack(int seq) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 int i;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 int sz;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 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
18
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 for(i = 0; i < sz; i++)
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 uart_putc(client_buf[i]);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 }
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 static
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 void nak(int seq, const char *msg) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 int i;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 int sz;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 memcpy(client_buf + CP_CMD_HEAD_SZ, msg, strlen(msg));
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 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
30
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 for(i = 0; i < sz; i++)
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 uart_putc(client_buf[i]);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 }
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 static
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 void send_client(const char *buf, int bsz) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 int i;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 for(i = 0; i < bsz; i++)
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 uart_putc(buf[i]);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 }
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 #define GET_DATA_BITS(data) ((data)[0] | ((data)[1] << 8))
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 int main(int argc, char * const argv[]) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 cp_cmd_t *cmd;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 cmd_proto_t *cp;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 int c, nbits;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 int bsz;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 char buf[16];
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 jtag_init();
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 uart_init(BAUD_RATE);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 cp = cmd_proto_new();
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 while(1) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 uart_getc(c);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 cmd = cmd_proto_rcv(cp, c);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 if(cmd == &BAD_CMD) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 nak(cmd->seq, "BAD CMD");
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 continue;
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
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 if(cmd == &CSUM_ERR_CMD) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 nak(cmd->seq, "CSUM ERR");
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 continue;
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
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 switch(cmd->code) {
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 case CPCMD_PING:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 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
74 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 CPCMD_PONG, cmd->data_sz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 send_client(client_buf, bsz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 break;
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 case CPCMD_SHIFT_TDI:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 jtag_shift(cmd->data + 2, nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 ack(cmd->seq);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 case CPCMD_SHIFT_TMS:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 jtag_tms(cmd->data + 2, nbits);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 ack(cmd->seq);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 case CPCMD_SHIFT_TDI_TDO:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 nbits = GET_DATA_BITS(cmd->data);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 jtag_shift_inout(cmd->data + 2,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 client_buf + CP_CMD_HEAD_SZ + 2,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 nbits);
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 client_buf[CP_CMD_HEAD_SZ] = nbits & 0xff;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 client_buf[CP_CMD_HEAD_SZ + 1] = nbits >> 8;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 bsz = cmd_proto_cmd_fill(client_buf, cmd->seq,
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 CPCMD_DATA, (nbits + 23) / 8);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 send_client(client_buf, bsz);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 break;
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 default:
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 sprintf(buf, "CODE 0x%02x", cmd->code);
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 nak(cmd->seq, buf);
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 }
abf221bf3ce4 AVR JTAG server.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 }