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