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 #include "cp-usb-async.h"
28 ccdbg_half_clock(struct ccdbg *dbg)
30 struct timespec req, rem;
31 req.tv_sec = (CC_CLOCK_US / 2) / 1000000;
32 req.tv_nsec = ((CC_CLOCK_US / 2) % 1000000) * 1000;
33 nanosleep(&req, &rem);
41 dbg = calloc(sizeof (struct ccdbg), 1);
47 dbg->cp_async = cp_usb_async_open();
53 dbg->cp = cp_usb_open ();
63 ccdbg_close(struct ccdbg *dbg)
66 cp_usb_async_close(dbg->cp_async);
68 cp_usb_close(dbg->cp);
74 ccdbg_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
77 cp_usb_async_write(dbg->cp_async, mask, value);
79 cp_usb_write(dbg->cp, mask, value);
85 ccdbg_read(struct ccdbg *dbg, uint8_t *valuep)
88 cp_usb_async_read(dbg->cp_async, valuep);
90 *valuep = cp_usb_read(dbg->cp);
95 ccdbg_sync_io(struct ccdbg *dbg)
98 cp_usb_async_sync(dbg->cp_async);
103 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
114 ccdbg_print(char *format, uint8_t mask, uint8_t set)
116 ccdbg_debug (CC_DEBUG_BITBANG, format,
117 is_bit(set, mask, 'C', CC_CLOCK),
118 is_bit(set, mask, 'D', CC_DATA),
119 is_bit(set, mask, 'R', CC_RESET_N));
123 ccdbg_send(struct ccdbg *dbg, uint8_t mask, uint8_t set)
125 ccdbg_write(dbg, mask, set);
126 ccdbg_print("%c %c %c\n", mask, set);
127 ccdbg_half_clock(dbg);
131 ccdbg_send_bit(struct ccdbg *dbg, uint8_t bit)
133 if (bit) bit = CC_DATA;
134 ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|bit|CC_RESET_N);
135 ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, bit|CC_RESET_N);
139 ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte)
142 ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Send Byte 0x%02x\n#\n", byte);
143 for (bit = 7; bit >= 0; bit--) {
144 ccdbg_send_bit(dbg, (byte >> bit) & 1);
146 ccdbg_debug(CC_DEBUG_BITBANG, "\n");
152 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
155 ccdbg_send_byte(dbg, *bytes++);
159 ccdbg_recv_bit(struct ccdbg *dbg, int first, uint8_t *bit)
161 uint8_t mask = first ? CC_DATA : 0;
163 ccdbg_send(dbg, CC_CLOCK|mask|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
164 ccdbg_read(dbg, bit);
165 ccdbg_send(dbg, CC_CLOCK| CC_RESET_N, CC_RESET_N);
169 ccdbg_recv_byte(struct ccdbg *dbg, int first, uint8_t *bytep)
175 ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv byte\n#\n");
176 for (bit = 0; bit < 8; bit++) {
177 ccdbg_recv_bit(dbg, first, &bits[bit]);
181 for (bit = 0; bit < 8; bit++) {
183 byte |= (bits[bit] & CC_DATA) ? 1 : 0;
184 ccdbg_print("#\t%c %c %c\n", CC_DATA, bits[bit]);
186 ccdbg_debug(CC_DEBUG_BITBANG, "\n");
188 ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv 0x%02x\n#\n", byte);
193 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
197 for (i = 0; i < nbytes; i++) {
198 ccdbg_recv_byte(dbg, first, &bytes[i]);
204 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
207 ccdbg_send_byte(dbg, cmd);
208 for (i = 0; i < len; i++)
209 ccdbg_send_byte(dbg, data[i]);
213 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
216 ccdbg_cmd_write(dbg, cmd, data, len);
217 ccdbg_recv_bytes(dbg, byte, 1);
222 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
225 ccdbg_cmd_write(dbg, cmd, data, len);
226 ccdbg_recv_bytes(dbg, byte, 2);
227 return (byte[0] << 8) | byte[1];