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_clock_1_0(struct ccdbg *dbg)
50 ccdbg_quarter_clock(dbg);
51 assert(dbg->clock == 1);
52 cccp_write(dbg, CC_CLOCK, 0);
54 ccdbg_quarter_clock(dbg);
58 ccdbg_clock_0_1(struct ccdbg *dbg)
60 ccdbg_quarter_clock(dbg);
61 assert(dbg->clock == 0);
62 cccp_write(dbg, CC_CLOCK, CC_CLOCK);
64 ccdbg_quarter_clock(dbg);
68 * By convention, every macro function is entered with dbg->clock == 1
72 ccdbg_write_bit(struct ccdbg *dbg, uint8_t bit)
75 cccp_write(dbg, CC_DATA, bit ? CC_DATA : 0);
80 ccdbg_write_byte(struct ccdbg *dbg, uint8_t byte)
84 for (bit = 7; bit >= 0; bit--)
85 ccdbg_write_bit(dbg, (byte >> bit) & 1);
89 ccdbg_read_bit(struct ccdbg *dbg)
94 data = cccp_read(dbg, CC_DATA);
100 ccdbg_read_byte(struct ccdbg *dbg)
105 for (bit = 7; bit >= 0; bit--)
106 byte |= ccdbg_read_bit(dbg) << bit;
111 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
114 ccdbg_write_byte(dbg, cmd);
115 for (i = 0; i < len; i++)
116 ccdbg_write_byte(dbg, data[i]);
120 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
122 ccdbg_cmd_write(dbg, cmd, data, len);
123 return ccdbg_read_byte(dbg);
127 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
129 uint8_t byte1, byte0;
130 ccdbg_cmd_write(dbg, cmd, data, len);
131 byte1 = ccdbg_read_byte(dbg);
132 byte0 = ccdbg_read_byte(dbg);
133 return (byte1 << 8) | byte0;