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