6462739a56b08747d55fe74aa6a5337c1c8a05a6
[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 void
22 get_bit(char *line, int i, char on, uint8_t bit, uint8_t *bits, uint8_t *masks)
23 {
24         if (line[i] == on) {
25                 *bits |= bit;
26                 *masks |= bit;
27                 return;
28         }
29         if (line[i] == '.') {
30                 *masks |= bit;
31                 return;
32         }
33         if (line[i] == '-') {
34                 return;
35         }
36         fprintf(stderr, "bad line %s\n", line);
37         exit (1);
38 }
39
40 static char
41 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
42 {
43         if (mask&bit) {
44                 if (get&bit)
45                         return on;
46                 else
47                         return '.';
48         } else
49                 return '-';
50 }
51
52 static uint8_t
53 ccdbg_write_read(struct ccdbg *dbg, uint8_t set, uint8_t mask)
54 {
55         uint8_t get = set;
56
57         if (mask != (CC_DATA|CC_CLOCK|CC_RESET_N))
58                 get = ccdbg_read(dbg);
59         ccdbg_write(dbg, mask, set);
60         printf ("%c %c %c",
61                 is_bit(set, mask, 'C', CC_CLOCK),
62                 is_bit(set, mask, 'D', CC_DATA),
63                 is_bit(set, mask, 'R', CC_RESET_N));
64         if (mask != (CC_DATA|CC_CLOCK|CC_RESET_N))
65                 printf(" -> %c %c %c",
66                        is_bit(get, 0xf, 'C', CC_CLOCK),
67                        is_bit(get, 0xf, 'D', CC_DATA),
68                        is_bit(get, 0xf, 'R', CC_RESET_N));
69         printf("\n");
70         ccdbg_half_clock(dbg);
71         return get;
72 }
73
74 static void
75 _ccdbg_debug_mode(struct ccdbg *dbg)
76 {
77         printf ("#\n");
78         printf ("# Debug mode\n");
79         printf ("#\n");
80         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
81         ccdbg_write_read(dbg,          CC_DATA           , CC_CLOCK|CC_DATA|CC_RESET_N);
82         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           , CC_CLOCK|CC_DATA|CC_RESET_N);
83         ccdbg_write_read(dbg,          CC_DATA           , CC_CLOCK|CC_DATA|CC_RESET_N);
84         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           , CC_CLOCK|CC_DATA|CC_RESET_N);
85         ccdbg_write_read(dbg,          CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
86 }
87
88 static void
89 _ccdbg_reset(struct ccdbg *dbg)
90 {
91         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
92         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           , CC_CLOCK|CC_DATA|CC_RESET_N);
93         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA           , CC_CLOCK|CC_DATA|CC_RESET_N);
94         ccdbg_write_read(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
95 }
96
97 static void
98 _ccdbg_send_bit(struct ccdbg *dbg, uint8_t bit)
99 {
100         if (bit) bit = CC_DATA;
101         ccdbg_write_read(dbg, CC_CLOCK|bit|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
102         ccdbg_write_read(dbg,          bit|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
103 }
104
105 static void
106 _ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte)
107 {
108         int bit;
109         printf ("#\n");
110         printf ("# Send Byte 0x%02x\n", byte);
111         printf ("#\n");
112         for (bit = 7; bit >= 0; bit--) {
113                 _ccdbg_send_bit(dbg, (byte >> bit) & 1);
114                 if (bit == 3)
115                         printf ("\n");
116         }
117 }
118
119 static void
120 _ccdbg_send_bits(struct ccdbg *dbg, int n, uint32_t bits)
121 {
122         int bit;
123         printf ("#\n");
124         printf ("# Send %d bits 0x%08x\n", n, bits);
125         printf ("#\n");
126         for (bit = n - 1; bit >= 0; bit--) {
127                 _ccdbg_send_bit(dbg, (bits >> bit) & 1);
128                 if ((bit & 3) == 3)
129                         printf ("\n");
130         }
131 }
132
133 static void
134 _ccdbg_print_bits(int n, uint32_t bits)
135 {
136         int     bit;
137
138         for (bit = n - 1; bit >= 0; bit--)
139                 printf ("%d", (bits >> bit) & 1);
140 }
141
142 static uint32_t
143 _ccdbg_read_bits(struct ccdbg *dbg, int bits)
144 {
145         int             bit;
146         uint32_t        val = 0;
147         uint8_t         get;
148
149         printf ("#\n");
150         printf ("# Read %d bits\n", bits);
151         printf ("#\n");
152         for (bit = 0; bit < bits; bit++) {
153                       ccdbg_write_read(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_RESET_N);
154                 get = ccdbg_write_read(dbg,          CC_DATA|CC_RESET_N, CC_CLOCK|CC_RESET_N);
155                 val <<= 1;
156                 if (get & CC_DATA)
157                         val |= 1;
158                 if ((bit & 3) == 3)
159                         printf ("\n");
160         }
161         printf ("#\n");
162         printf ("# Read "); _ccdbg_print_bits(bits, val); printf ("\n");
163         printf ("#\n");
164         return val;
165 }
166
167 static int
168 _ccdbg_check_bits(uint32_t bits, uint8_t match)
169 {
170         int     bit;
171
172         for (bit = 0; bit < 24; bit++)
173                 if (((bits >> bit) & 0xff) == match)
174                         return 1;
175         return 0;
176 }
177
178 static uint32_t
179 _ccdbg_play(struct ccdbg *dbg, int num_sync, uint32_t sync)
180 {
181         uint32_t        bits;
182         _ccdbg_debug_mode(dbg);
183         _ccdbg_send_bits(dbg, num_sync, sync);
184         _ccdbg_send_byte(dbg, CC_GET_CHIP_ID);
185         bits = _ccdbg_read_bits(dbg, 16);
186         _ccdbg_send_byte(dbg, CC_GET_CHIP_ID);
187         bits = _ccdbg_read_bits(dbg, 16);
188 //      _ccdbg_send_byte(dbg, CC_READ_STATUS);
189         _ccdbg_reset(dbg);
190         if (_ccdbg_check_bits(bits, 0x11)) {
191                 printf("#\n#match with %d bits 0x%08x\n#\n", num_sync, sync);
192                 return 1;
193         }
194         return 0;
195 }
196
197 static int
198 _ccdbg_play_num(struct ccdbg *dbg, int num)
199 {
200         uint32_t        sync;
201         uint32_t        max;
202
203         printf ("#\n#play %d\n#\n", num);
204         max = (1 << num);
205         for (sync = 0; sync < max; sync++)
206                 if (_ccdbg_play(dbg, num, sync))
207                         return 1;
208         return 0;
209 }
210
211 static int
212 _ccdbg_play_many(struct ccdbg *dbg, int max)
213 {
214         int     num;
215
216         for (num = 0; num < max; num++)
217                 if (_ccdbg_play_num(dbg, num))
218                         return 1;
219         return 0;
220 }
221
222 static void
223 ccdbg_manual(struct ccdbg *dbg, FILE *input)
224 {
225         char    line[80];
226         uint8_t set, mask;
227
228         while (fgets(line, sizeof line, input)) {
229                 if (line[0] == '#' || line[0] == '\n') {
230                         printf ("%s", line);
231                         continue;
232                 }
233                 set = 0;
234                 mask = 0;
235                 get_bit(line, 0, 'C', CC_CLOCK, &set, &mask);
236                 get_bit(line, 2, 'D', CC_DATA, &set, &mask);
237                 get_bit(line, 4, 'R', CC_RESET_N, &set, &mask);
238                 ccdbg_write_read(dbg, set, mask);
239         }
240 }
241
242 int
243 main (int argc, char **argv)
244 {
245         struct ccdbg    *dbg;
246         uint8_t         status;
247         uint16_t        chip_id;
248
249         dbg = ccdbg_open("/dev/ttyUSB0");
250         if (!dbg)
251                 exit (1);
252 #if 0   
253         _ccdbg_play(dbg, 0, 0);
254         _ccdbg_play_many(dbg, 8);
255 #endif
256         ccdbg_manual(dbg, stdin);
257 #if 0
258         ccdbg_debug_mode(dbg);
259         status = ccdbg_read_status(dbg);
260         printf("Status: 0x%02x\n", status);
261         chip_id = ccdbg_get_chip_id(dbg);
262         printf("Chip id: 0x%04x\n", chip_id);
263         _ccdbg_reset(dbg);
264 #endif
265         ccdbg_close(dbg);
266         exit (0);
267 }