2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 get_bit(char *line, int i, char on, uint8_t bit, uint8_t *bits, uint8_t *masks)
36 fprintf(stderr, "bad line %s\n", line);
41 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
53 ccdbg_write_read(struct ccdbg *dbg, uint8_t set, uint8_t mask)
57 if (mask != (CC_DATA|CC_CLOCK|CC_RESET_N))
58 get = ccdbg_read(dbg);
59 ccdbg_write(dbg, mask, set);
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));
70 ccdbg_half_clock(dbg);
75 _ccdbg_debug_mode(struct ccdbg *dbg)
78 printf ("# Debug mode\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);
89 _ccdbg_reset(struct ccdbg *dbg)
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);
98 _ccdbg_send_bit(struct ccdbg *dbg, uint8_t bit)
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);
106 _ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte)
110 printf ("# Send Byte 0x%02x\n", byte);
112 for (bit = 7; bit >= 0; bit--) {
113 _ccdbg_send_bit(dbg, (byte >> bit) & 1);
120 _ccdbg_send_bits(struct ccdbg *dbg, int n, uint32_t bits)
124 printf ("# Send %d bits 0x%08x\n", n, bits);
126 for (bit = n - 1; bit >= 0; bit--) {
127 _ccdbg_send_bit(dbg, (bits >> bit) & 1);
134 _ccdbg_print_bits(int n, uint32_t bits)
138 for (bit = n - 1; bit >= 0; bit--)
139 printf ("%d", (bits >> bit) & 1);
143 _ccdbg_read_bits(struct ccdbg *dbg, int bits)
150 printf ("# Read %d bits\n", bits);
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);
162 printf ("# Read "); _ccdbg_print_bits(bits, val); printf ("\n");
168 _ccdbg_check_bits(uint32_t bits, uint8_t match)
172 for (bit = 0; bit < 24; bit++)
173 if (((bits >> bit) & 0xff) == match)
179 _ccdbg_play(struct ccdbg *dbg, int num_sync, uint32_t sync)
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);
190 if (_ccdbg_check_bits(bits, 0x11)) {
191 printf("#\n#match with %d bits 0x%08x\n#\n", num_sync, sync);
198 _ccdbg_play_num(struct ccdbg *dbg, int num)
203 printf ("#\n#play %d\n#\n", num);
205 for (sync = 0; sync < max; sync++)
206 if (_ccdbg_play(dbg, num, sync))
212 _ccdbg_play_many(struct ccdbg *dbg, int max)
216 for (num = 0; num < max; num++)
217 if (_ccdbg_play_num(dbg, num))
223 ccdbg_manual(struct ccdbg *dbg, FILE *input)
228 while (fgets(line, sizeof line, input)) {
229 if (line[0] == '#' || line[0] == '\n') {
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);
243 main (int argc, char **argv)
249 dbg = ccdbg_open("/dev/ttyUSB0");
253 _ccdbg_play(dbg, 0, 0);
254 _ccdbg_play_many(dbg, 8);
256 ccdbg_manual(dbg, stdin);
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);