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_half_clock(struct ccdbg *dbg)
30 usleep(CC_CLOCK_US / 2);
34 ccdbg_open(char *file)
38 dbg = calloc(sizeof (struct ccdbg), 1);
43 dbg->fd = open(file, 2);
50 cccp_write(dbg, CC_CLOCK, CC_CLOCK);
56 ccdbg_close(struct ccdbg *dbg)
64 ccdbg_clock_1_0(struct ccdbg *dbg)
66 ccdbg_quarter_clock(dbg);
67 assert(dbg->clock == 1);
68 cccp_write(dbg, CC_CLOCK, 0);
70 ccdbg_quarter_clock(dbg);
74 ccdbg_clock_0_1(struct ccdbg *dbg)
76 ccdbg_quarter_clock(dbg);
77 assert(dbg->clock == 0);
78 cccp_write(dbg, CC_CLOCK, CC_CLOCK);
80 ccdbg_quarter_clock(dbg);
84 * By convention, every macro function is entered with dbg->clock == 1
88 ccdbg_write_bit(struct ccdbg *dbg, uint8_t bit)
92 assert(dbg->clock == 1);
96 ccdbg_half_clock(dbg);
97 cccp_write(dbg, CC_DATA|CC_CLOCK, data);
98 ccdbg_half_clock(dbg);
99 cccp_write(dbg, CC_CLOCK, 0);
100 // printf ("%d", bit);
104 ccdbg_write_byte(struct ccdbg *dbg, uint8_t byte)
108 for (bit = 7; bit >= 0; bit--)
109 ccdbg_write_bit(dbg, (byte >> bit) & 1);
113 ccdbg_read_bit(struct ccdbg *dbg)
117 ccdbg_half_clock(dbg);
118 cccp_write(dbg, CC_CLOCK, CC_CLOCK);
119 ccdbg_half_clock(dbg);
120 cccp_write(dbg, CC_CLOCK, 0);
121 data = cccp_read(dbg, CC_DATA);
126 ccdbg_read_byte(struct ccdbg *dbg)
131 for (bit = 7; bit >= 0; bit--)
132 byte |= ccdbg_read_bit(dbg) << bit;
137 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
140 ccdbg_write_byte(dbg, cmd);
141 for (i = 0; i < len; i++)
142 ccdbg_write_byte(dbg, data[i]);
146 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
149 ccdbg_cmd_write(dbg, cmd, data, len);
150 ret = ccdbg_read_byte(dbg);
155 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
157 uint8_t byte1, byte0;
159 ccdbg_cmd_write(dbg, cmd, data, len);
160 byte1 = ccdbg_read_byte(dbg);
161 byte0 = ccdbg_read_byte(dbg);
162 for (i = 0; i < 4; i++)
163 (void) ccdbg_read_byte(dbg);
164 return (byte1 << 8) | byte0;