53ea758342c6f22f055c42549bc2fc3c403c8336
[fw/altos] / lib / ccdbg-io.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "ccdbg.h"
20 #include <time.h>
21 #ifdef CP_USB_ASYNC
22 #include "cp-usb-async.h"
23 #else
24 #include "cp-usb.h"
25 #endif
26
27 void
28 ccdbg_half_clock(struct ccdbg *dbg)
29 {
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);
34 }
35
36 struct ccdbg *
37 ccdbg_open(void)
38 {
39         struct ccdbg *dbg;
40
41         dbg = calloc(sizeof (struct ccdbg), 1);
42         if (!dbg) {
43                 perror("calloc");
44                 return NULL;
45         }
46 #ifdef CP_USB_ASYNC
47         dbg->cp_async = cp_usb_async_open();
48         if (!dbg->cp_async) {
49                 free (dbg);
50                 return NULL;
51         }
52 #else
53         dbg->cp = cp_usb_open ();
54         if (!dbg->cp) {
55                 free (dbg);
56                 return NULL;
57         }
58 #endif
59         return dbg;
60 }
61
62 void
63 ccdbg_close(struct ccdbg *dbg)
64 {
65 #ifdef CP_USB_ASYNC
66         cp_usb_async_close(dbg->cp_async);
67 #else
68         cp_usb_close(dbg->cp);
69 #endif
70         free (dbg);
71 }
72
73 int
74 ccdbg_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
75 {
76 #ifdef CP_USB_ASYNC
77         cp_usb_async_write(dbg->cp_async, mask, value);
78 #else
79         cp_usb_write(dbg->cp, mask, value);
80 #endif
81         return 0;
82 }
83
84 void
85 ccdbg_read(struct ccdbg *dbg, uint8_t *valuep)
86 {
87 #ifdef CP_USB_ASYNC
88         cp_usb_async_read(dbg->cp_async, valuep);
89 #else
90         *valuep = cp_usb_read(dbg->cp);
91 #endif
92 }
93
94 void
95 ccdbg_sync_io(struct ccdbg *dbg)
96 {
97 #ifdef CP_USB_ASYNC
98         cp_usb_async_sync(dbg->cp_async);
99 #endif
100 }
101
102 static char
103 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
104 {
105         if (mask&bit) {
106                 if (get&bit)
107                         return on;
108                 else
109                         return '.';
110         } else
111                 return '-';
112 }
113 void
114 ccdbg_print(char *format, uint8_t mask, uint8_t set)
115 {
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));
120 }
121
122 void
123 ccdbg_send(struct ccdbg *dbg, uint8_t mask, uint8_t set)
124 {
125         ccdbg_write(dbg, mask, set);
126         ccdbg_print("%c %c %c\n", mask, set);
127         ccdbg_half_clock(dbg);
128 }
129
130 void
131 ccdbg_send_bit(struct ccdbg *dbg, uint8_t bit)
132 {
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);
136 }
137
138 void
139 ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte)
140 {
141         int bit;
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);
145                 if (bit == 3)
146                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
147         }
148         ccdbg_sync_io(dbg);
149 }
150
151 void
152 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
153 {
154         while (nbytes--)
155                 ccdbg_send_byte(dbg, *bytes++);
156 }
157
158 void
159 ccdbg_recv_bit(struct ccdbg *dbg, int first, uint8_t *bit)
160 {
161         uint8_t mask = first ? CC_DATA : 0;
162
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);
166 }
167
168 void
169 ccdbg_recv_byte(struct ccdbg *dbg, int first, uint8_t *bytep)
170 {
171         uint8_t byte = 0;
172         uint8_t bits[8];
173         int     bit;
174
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]);
178                 first = 0;
179         }
180         ccdbg_sync_io(dbg);
181         for (bit = 0; bit < 8; bit++) {
182                 byte = byte << 1;
183                 byte |= (bits[bit] & CC_DATA) ? 1 : 0;
184                 ccdbg_print("#\t%c %c %c\n", CC_DATA, bits[bit]);
185                 if (bit == 3)
186                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
187         }
188         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv 0x%02x\n#\n", byte);
189         *bytep = byte;
190 }
191
192 void
193 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
194 {
195         int i;
196         int first = 1;
197         for (i = 0; i < nbytes; i++) {
198                 ccdbg_recv_byte(dbg, first, &bytes[i]);
199                 first = 0;
200         }
201 }
202
203 void
204 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
205 {
206         int     i;
207         ccdbg_send_byte(dbg, cmd);
208         for (i = 0; i < len; i++)
209                 ccdbg_send_byte(dbg, data[i]);
210 }
211
212 uint8_t
213 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
214 {
215         uint8_t byte[1];
216         ccdbg_cmd_write(dbg, cmd, data, len);
217         ccdbg_recv_bytes(dbg, byte, 1);
218         return byte[0];
219 }
220
221 uint16_t
222 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
223 {
224         uint8_t byte[2];
225         ccdbg_cmd_write(dbg, cmd, data, len);
226         ccdbg_recv_bytes(dbg, byte, 2);
227         return (byte[0] << 8) | byte[1];
228 }