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 ccdbg_quarter_clock(struct ccdbg *dbg)
24 usleep(CC_CLOCK_US / 4);
28 ccdbg_open(char *file)
32 dbg = calloc(sizeof (struct ccdbg), 1);
37 dbg->fd = open(file, 2);
48 ccdbg_close(struct ccdbg *dbg)
56 ccdbg_clock_1_0(struct ccdbg *dbg)
58 ccdbg_quarter_clock(dbg);
59 assert(dbg->clock == 1);
60 cccp_write(dbg, CC_CLOCK, 0);
62 ccdbg_quarter_clock(dbg);
66 ccdbg_clock_0_1(struct ccdbg *dbg)
68 ccdbg_quarter_clock(dbg);
69 assert(dbg->clock == 0);
70 cccp_write(dbg, CC_CLOCK, CC_CLOCK);
72 ccdbg_quarter_clock(dbg);
76 * By convention, every macro function is entered with dbg->clock == 1
80 ccdbg_write_bit(struct ccdbg *dbg, uint8_t bit)
83 cccp_write(dbg, CC_DATA, bit ? CC_DATA : 0);
88 ccdbg_write_byte(struct ccdbg *dbg, uint8_t byte)
92 for (bit = 7; bit >= 0; bit--)
93 ccdbg_write_bit(dbg, (byte >> bit) & 1);
97 ccdbg_read_bit(struct ccdbg *dbg)
101 ccdbg_clock_1_0(dbg);
102 data = cccp_read(dbg, CC_DATA);
103 ccdbg_clock_0_1(dbg);
108 ccdbg_read_byte(struct ccdbg *dbg)
113 for (bit = 7; bit >= 0; bit--)
114 byte |= ccdbg_read_bit(dbg) << bit;
119 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
122 ccdbg_write_byte(dbg, cmd);
123 for (i = 0; i < len; i++)
124 ccdbg_write_byte(dbg, data[i]);
128 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
130 ccdbg_cmd_write(dbg, cmd, data, len);
131 return ccdbg_read_byte(dbg);
135 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
137 uint8_t byte1, byte0;
138 ccdbg_cmd_write(dbg, cmd, data, len);
139 byte1 = ccdbg_read_byte(dbg);
140 byte0 = ccdbg_read_byte(dbg);
141 return (byte1 << 8) | byte0;