64cb768f4447f1fbb2e798005739489de590b565
[fw/altos] / ccdbg.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ccdbg.h"
20
21 static uint8_t
22 get_bit(char *line, int i, char on, uint8_t bit)
23 {
24         if (line[i] == on)
25                 return bit;
26         if (line[i] == '.')
27                 return 0;
28         fprintf(stderr, "bad line %s\n", line);
29         exit (1);
30 }
31
32 static char
33 is_bit(uint8_t get, char on, uint8_t bit)
34 {
35         if (get&bit)
36                 return on;
37         else
38                 return '.';
39 }
40
41 static uint8_t
42 ccdbg_write_read(struct ccdbg *dbg, uint8_t set)
43 {
44         uint8_t get;
45
46         cccp_write(dbg, CC_DATA|CC_CLOCK|CC_RESET_N, set);
47         get = cccp_read_all(dbg);
48         printf("%c %c %c -> %c %c %c\n",
49                is_bit(set, 'C', CC_CLOCK),
50                is_bit(set, 'D', CC_DATA),
51                is_bit(set, 'R', CC_RESET_N),
52                is_bit(get, 'C', CC_CLOCK),
53                is_bit(get, 'D', CC_DATA),
54                is_bit(get, 'R', CC_RESET_N));
55         ccdbg_half_clock(dbg);
56         return get;
57 }
58
59 static void
60 _ccdbg_debug_mode(struct ccdbg *dbg)
61 {
62         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA|CC_RESET_N);
63         ccdbg_write_read(dbg,          CC_DATA           );
64         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           );
65         ccdbg_write_read(dbg,          CC_DATA           );
66         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           );
67         ccdbg_write_read(dbg,          CC_DATA|CC_RESET_N);
68 }
69
70 static void
71 _ccdbg_reset(struct ccdbg *dbg)
72 {
73         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA|CC_RESET_N);
74         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           );
75         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           );
76         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA|CC_RESET_N);
77 }
78
79 static void
80 ccdbg_manual(struct ccdbg *dbg, FILE *input)
81 {
82         char    line[80];
83         uint8_t set;
84
85         while (fgets(line, sizeof line, input)) {
86                 if (line[0] == '#' || line[0] == '\n') {
87                         printf ("%s", line);
88                         continue;
89                 }
90                 set = 0;
91                 set |= get_bit(line, 0, 'C', CC_CLOCK);
92                 set |= get_bit(line, 2, 'D', CC_DATA);
93                 set |= get_bit(line, 4, 'R', CC_RESET_N);
94                 ccdbg_write_read(dbg, set);
95         }
96 }
97
98 int
99 main (int argc, char **argv)
100 {
101         struct ccdbg    *dbg;
102         uint8_t         status;
103         uint16_t        chip_id;
104
105         dbg = ccdbg_open("/dev/ttyUSB0");
106         if (!dbg)
107                 exit (1);
108         ccdbg_manual(dbg, stdin);
109 #if 0   
110         ccdbg_debug_mode(dbg);
111         status = ccdbg_read_status(dbg);
112         printf("Status: 0x%02x\n", status);
113         chip_id = ccdbg_get_chip_id(dbg);
114         printf("Chip id: 0x%04x\n", chip_id);
115 #endif
116         _ccdbg_reset(dbg);
117         ccdbg_close(dbg);
118         exit (0);
119 }