Make serial test simpler
[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 static uint32_t cc_clock_us = CC_CLOCK_US;
28 static uint32_t cc_reset_us = CC_RESET_US;
29
30 void
31 ccdbg_set_clock(uint32_t us)
32 {
33         cc_clock_us = us;
34 }
35
36 void
37 ccdbg_half_clock(struct ccdbg *dbg)
38 {
39         struct timespec req, rem;
40         req.tv_sec = (cc_clock_us / 2) / 1000000;
41         req.tv_nsec = ((cc_clock_us / 2) % 1000000) * 1000;
42         nanosleep(&req, &rem);
43 }
44
45 void
46 ccdbg_wait_reset(struct ccdbg *dbg)
47 {
48         struct timespec req, rem;
49         
50         ccdbg_sync_io(dbg);
51         req.tv_sec = (cc_reset_us) / 1000000;
52         req.tv_nsec = ((cc_reset_us) % 1000000) * 1000;
53         nanosleep(&req, &rem);
54 }
55         
56 struct ccdbg *
57 ccdbg_open(void)
58 {
59         struct ccdbg *dbg;
60
61         dbg = calloc(sizeof (struct ccdbg), 1);
62         if (!dbg) {
63                 perror("calloc");
64                 return NULL;
65         }
66 #ifdef CP_USB_ASYNC
67         dbg->cp_async = cp_usb_async_open();
68         if (!dbg->cp_async) {
69                 free (dbg);
70                 return NULL;
71         }
72 #else
73         dbg->cp = cp_usb_open ();
74         if (!dbg->cp) {
75                 free (dbg);
76                 return NULL;
77         }
78 #endif
79         return dbg;
80 }
81
82 void
83 ccdbg_close(struct ccdbg *dbg)
84 {
85 #ifdef CP_USB_ASYNC
86         cp_usb_async_close(dbg->cp_async);
87 #else
88         cp_usb_close(dbg->cp);
89 #endif
90         free (dbg);
91 }
92
93 int
94 ccdbg_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
95 {
96 #ifdef CP_USB_ASYNC
97         cp_usb_async_write(dbg->cp_async, mask, value);
98 #else
99         cp_usb_write(dbg->cp, mask, value);
100 #endif
101         return 0;
102 }
103
104 void
105 ccdbg_read(struct ccdbg *dbg, uint8_t *valuep)
106 {
107 #ifdef CP_USB_ASYNC
108         cp_usb_async_read(dbg->cp_async, valuep);
109 #else
110         *valuep = cp_usb_read(dbg->cp);
111 #endif
112 }
113
114 void
115 ccdbg_sync_io(struct ccdbg *dbg)
116 {
117 #ifdef CP_USB_ASYNC
118         cp_usb_async_sync(dbg->cp_async);
119 #endif
120 }
121
122 static char
123 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
124 {
125         if (mask&bit) {
126                 if (get&bit)
127                         return on;
128                 else
129                         return '.';
130         } else
131                 return '-';
132 }
133 void
134 ccdbg_print(char *format, uint8_t mask, uint8_t set)
135 {
136         ccdbg_debug (CC_DEBUG_BITBANG, format,
137                      is_bit(set, mask, 'C', CC_CLOCK),
138                      is_bit(set, mask, 'D', CC_DATA),
139                      is_bit(set, mask, 'R', CC_RESET_N));
140 }
141
142 void
143 ccdbg_send(struct ccdbg *dbg, uint8_t mask, uint8_t set)
144 {
145         ccdbg_write(dbg, mask, set);
146         ccdbg_print("%c %c %c\n", mask, set);
147         ccdbg_half_clock(dbg);
148 }
149
150 void
151 ccdbg_send_bit(struct ccdbg *dbg, uint8_t bit)
152 {
153         if (bit) bit = CC_DATA;
154         ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|bit|CC_RESET_N);
155         ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N,          bit|CC_RESET_N);
156 }
157
158 void
159 ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte)
160 {
161         int bit;
162         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Send Byte 0x%02x\n#\n", byte);
163         for (bit = 7; bit >= 0; bit--) {
164                 ccdbg_send_bit(dbg, (byte >> bit) & 1);
165                 if (bit == 3)
166                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
167         }
168         ccdbg_sync_io(dbg);
169 }
170
171 void
172 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
173 {
174         while (nbytes--)
175                 ccdbg_send_byte(dbg, *bytes++);
176 }
177
178 void
179 ccdbg_recv_bit(struct ccdbg *dbg, int first, uint8_t *bit)
180 {
181         uint8_t mask = first ? CC_DATA : 0;
182
183         ccdbg_send(dbg, CC_CLOCK|mask|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
184         ccdbg_read(dbg, bit);
185         ccdbg_send(dbg, CC_CLOCK|     CC_RESET_N,                  CC_RESET_N);
186 }
187
188 void
189 ccdbg_recv_byte(struct ccdbg *dbg, int first, uint8_t *bytep)
190 {
191         uint8_t byte = 0;
192         uint8_t bits[8];
193         int     bit;
194
195         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv byte\n#\n");
196         for (bit = 0; bit < 8; bit++) {
197                 ccdbg_recv_bit(dbg, first, &bits[bit]);
198                 first = 0;
199         }
200         ccdbg_sync_io(dbg);
201         for (bit = 0; bit < 8; bit++) {
202                 byte = byte << 1;
203                 byte |= (bits[bit] & CC_DATA) ? 1 : 0;
204                 ccdbg_print("#\t%c %c %c\n", CC_DATA, bits[bit]);
205                 if (bit == 3)
206                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
207         }
208         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv 0x%02x\n#\n", byte);
209         *bytep = byte;
210 }
211
212 void
213 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
214 {
215         int i;
216         int first = 1;
217         for (i = 0; i < nbytes; i++) {
218                 ccdbg_recv_byte(dbg, first, &bytes[i]);
219                 first = 0;
220         }
221 }
222
223 void
224 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
225 {
226         int     i;
227         ccdbg_send_byte(dbg, cmd);
228         for (i = 0; i < len; i++)
229                 ccdbg_send_byte(dbg, data[i]);
230 }
231
232 uint8_t
233 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
234 {
235         uint8_t byte[1];
236         ccdbg_cmd_write(dbg, cmd, data, len);
237         ccdbg_recv_bytes(dbg, byte, 1);
238         return byte[0];
239 }
240
241 uint16_t
242 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
243 {
244         uint8_t byte[2];
245         ccdbg_cmd_write(dbg, cmd, data, len);
246         ccdbg_recv_bytes(dbg, byte, 2);
247         return (byte[0] << 8) | byte[1];
248 }