2 * Copyright © 2009 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 "ccdbg-debug.h"
23 #include "cc-bitbang.h"
25 #include "cp-usb-async.h"
28 struct cp_usb_async *cp_async;
31 static uint32_t cc_clock_us = CC_CLOCK_US;
32 static uint32_t cc_reset_us = CC_RESET_US;
35 cc_bitbang_set_clock(uint32_t us)
41 cc_bitbang_half_clock(struct cc_bitbang *bb)
43 struct timespec req, rem;
44 req.tv_sec = (cc_clock_us / 2) / 1000000;
45 req.tv_nsec = ((cc_clock_us / 2) % 1000000) * 1000;
46 nanosleep(&req, &rem);
50 cc_bitbang_wait_reset(struct cc_bitbang *bb)
52 struct timespec req, rem;
55 req.tv_sec = (cc_reset_us) / 1000000;
56 req.tv_nsec = ((cc_reset_us) % 1000000) * 1000;
57 nanosleep(&req, &rem);
63 struct cc_bitbang *bb;
65 bb = calloc(sizeof (struct cc_bitbang), 1);
70 bb->cp_async = cp_usb_async_open();
79 cc_bitbang_close(struct cc_bitbang *bb)
81 cp_usb_async_close(bb->cp_async);
86 cc_bitbang_debug_mode(struct cc_bitbang *bb)
88 /* force two rising clocks while holding RESET_N low */
89 ccdbg_debug(CC_DEBUG_COMMAND, "#\n");
90 ccdbg_debug(CC_DEBUG_COMMAND, "# Debug mode\n");
91 ccdbg_debug(CC_DEBUG_COMMAND, "#\n");
92 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
93 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_DATA );
94 cc_bitbang_wait_reset(bb);
95 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA );
96 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_DATA );
97 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA );
98 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_DATA|CC_RESET_N);
99 cc_bitbang_wait_reset(bb);
103 cc_bitbang_reset(struct cc_bitbang *bb)
105 ccdbg_debug(CC_DEBUG_COMMAND, "#\n");
106 ccdbg_debug(CC_DEBUG_COMMAND, "# Reset\n");
107 ccdbg_debug(CC_DEBUG_COMMAND, "#\n");
108 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
109 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA );
110 cc_bitbang_wait_reset(bb);
111 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA );
112 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA );
113 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA );
114 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
115 cc_bitbang_wait_reset(bb);
119 cc_bitbang_write(struct cc_bitbang *bb, uint8_t mask, uint8_t value)
121 cp_usb_async_write(bb->cp_async, mask, value);
126 cc_bitbang_read(struct cc_bitbang *bb, uint8_t *valuep)
128 cp_usb_async_read(bb->cp_async, valuep);
132 cc_bitbang_sync(struct cc_bitbang *bb)
134 cp_usb_async_sync(bb->cp_async);
138 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
150 cc_bitbang_print(char *format, uint8_t mask, uint8_t set)
152 ccdbg_debug (CC_DEBUG_BITBANG, format,
153 is_bit(set, mask, 'C', CC_CLOCK),
154 is_bit(set, mask, 'D', CC_DATA),
155 is_bit(set, mask, 'R', CC_RESET_N));
159 cc_bitbang_send(struct cc_bitbang *bb, uint8_t mask, uint8_t set)
161 cc_bitbang_write(bb, mask, set);
162 cc_bitbang_print("%c %c %c\n", mask, set);
163 cc_bitbang_half_clock(bb);
167 cc_bitbang_send_bit(struct cc_bitbang *bb, uint8_t bit)
169 if (bit) bit = CC_DATA;
170 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|bit|CC_RESET_N);
171 cc_bitbang_send(bb, CC_CLOCK|CC_DATA|CC_RESET_N, bit|CC_RESET_N);
175 cc_bitbang_send_byte(struct cc_bitbang *bb, uint8_t byte)
178 ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Send Byte 0x%02x\n#\n", byte);
179 for (bit = 7; bit >= 0; bit--) {
180 cc_bitbang_send_bit(bb, (byte >> bit) & 1);
182 ccdbg_debug(CC_DEBUG_BITBANG, "\n");
188 cc_bitbang_send_bytes(struct cc_bitbang *bb, uint8_t *bytes, int nbytes)
191 cc_bitbang_send_byte(bb, *bytes++);
195 cc_bitbang_recv_bit(struct cc_bitbang *bb, int first, uint8_t *bit)
197 uint8_t mask = first ? CC_DATA : 0;
199 cc_bitbang_send(bb, CC_CLOCK|mask|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
200 cc_bitbang_read(bb, bit);
201 cc_bitbang_send(bb, CC_CLOCK| CC_RESET_N, CC_RESET_N);
205 cc_bitbang_recv_byte(struct cc_bitbang *bb, int first, uint8_t *bytep)
211 ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv byte\n#\n");
212 for (bit = 0; bit < 8; bit++) {
213 cc_bitbang_recv_bit(bb, first, &bits[bit]);
217 for (bit = 0; bit < 8; bit++) {
219 byte |= (bits[bit] & CC_DATA) ? 1 : 0;
220 cc_bitbang_print("#\t%c %c %c\n", CC_DATA, bits[bit]);
222 ccdbg_debug(CC_DEBUG_BITBANG, "\n");
224 ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv 0x%02x\n#\n", byte);
229 cc_bitbang_recv_bytes(struct cc_bitbang *bb, uint8_t *bytes, int nbytes)
233 for (i = 0; i < nbytes; i++) {
234 cc_bitbang_recv_byte(bb, first, &bytes[i]);