2947678561d4ac964e99e988fd52d39efe06cbdb
[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
22 void
23 ccdbg_half_clock(struct ccdbg *dbg)
24 {
25         struct timespec req, rem;
26         req.tv_sec = (CC_CLOCK_US / 2) / 1000000;
27         req.tv_nsec = ((CC_CLOCK_US / 2) % 1000000) * 1000;
28         nanosleep(&req, &rem);
29 }
30
31 struct ccdbg *
32 ccdbg_open(void)
33 {
34         struct ccdbg *dbg;
35
36         dbg = calloc(sizeof (struct ccdbg), 1);
37         if (!dbg) {
38                 perror("calloc");
39                 return NULL;
40         }
41         dbg->clock = 1;
42 #ifdef USE_KERNEL
43         dbg->fd = open("/dev/ttyUSB0", 2);
44         if (dbg->fd < 0) {
45                 perror(file);
46                 free(dbg);
47                 return NULL;
48         }
49         cccp_init(dbg);
50         cccp_write(dbg, CC_CLOCK, CC_CLOCK);
51 #else
52         cp_usb_init(dbg);
53 #endif
54         dbg->clock = 1;
55         return dbg;
56 }
57
58 void
59 ccdbg_close(struct ccdbg *dbg)
60 {
61 #if USE_KERNEL
62         cccp_fini(dbg);
63         close (dbg->fd);
64 #else
65         cp_usb_fini(dbg);
66 #endif
67         free (dbg);
68 }
69
70 int
71 ccdbg_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
72 {
73 #if USE_KERNEL
74         return cccp_write(dbg, mask, value);
75 #else
76         cp_usb_write(dbg, mask, value);
77         return 0;
78 #endif
79 }
80
81 uint8_t
82 ccdbg_read(struct ccdbg *dbg)
83 {
84 #if USE_KERNEL
85         return cccp_read_all(dbg);
86 #else
87         return cp_usb_read(dbg);
88 #endif
89 }
90
91 static char
92 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
93 {
94         if (mask&bit) {
95                 if (get&bit)
96                         return on;
97                 else
98                         return '.';
99         } else
100                 return '-';
101 }
102 void
103 ccdbg_print(char *format, uint8_t mask, uint8_t set)
104 {
105         ccdbg_debug (CC_DEBUG_BITBANG, format,
106                      is_bit(set, mask, 'C', CC_CLOCK),
107                      is_bit(set, mask, 'D', CC_DATA),
108                      is_bit(set, mask, 'R', CC_RESET_N));
109 }
110
111 void
112 ccdbg_send(struct ccdbg *dbg, uint8_t mask, uint8_t set)
113 {
114         ccdbg_write(dbg, mask, set);
115         ccdbg_print("%c %c %c\n", mask, set);
116         ccdbg_half_clock(dbg);
117 }
118
119 void
120 ccdbg_send_bit(struct ccdbg *dbg, uint8_t bit)
121 {
122         if (bit) bit = CC_DATA;
123         ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|bit|CC_RESET_N);
124         ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N,          bit|CC_RESET_N);
125 }
126
127 void
128 ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte)
129 {
130         int bit;
131         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Send Byte 0x%02x\n#\n", byte);
132         for (bit = 7; bit >= 0; bit--) {
133                 ccdbg_send_bit(dbg, (byte >> bit) & 1);
134                 if (bit == 3)
135                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
136         }
137 }
138
139 void
140 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
141 {
142         while (nbytes--)
143                 ccdbg_send_byte(dbg, *bytes++);
144 }
145
146 uint8_t
147 ccdbg_recv_bit(struct ccdbg *dbg, int first)
148 {
149         uint8_t mask = first ? CC_DATA : 0;
150         uint8_t read;
151
152         ccdbg_send(dbg, CC_CLOCK|mask|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
153         read = ccdbg_read(dbg);
154         ccdbg_send(dbg, CC_CLOCK|     CC_RESET_N,                  CC_RESET_N);
155         return (read & CC_DATA) ? 1 : 0;
156 }
157
158 uint8_t
159 ccdbg_recv_byte(struct ccdbg *dbg, int first)
160 {
161         uint8_t byte = 0;
162         int     bit;
163
164         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv byte\n#\n");
165         for (bit = 0; bit < 8; bit++) {
166                 byte = byte << 1;
167                 byte |= ccdbg_recv_bit(dbg, first);
168                 if (bit == 3)
169                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
170                 first = 0;
171         }
172         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv 0x%02x\n#\n", byte);
173         return byte;
174 }
175
176 void
177 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
178 {
179         int first = 1;
180         while (nbytes--) {
181                 *bytes++ = ccdbg_recv_byte(dbg, first);
182                 first = 0;
183         }
184 }
185
186 void
187 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
188 {
189         int     i;
190         ccdbg_send_byte(dbg, cmd);
191         for (i = 0; i < len; i++)
192                 ccdbg_send_byte(dbg, data[i]);
193 }
194
195 uint8_t
196 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
197 {
198         uint8_t byte[1];
199         ccdbg_cmd_write(dbg, cmd, data, len);
200         ccdbg_recv_bytes(dbg, byte, 1);
201         return byte[0];
202 }
203
204 uint16_t
205 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
206 {
207         uint8_t byte[2];
208         ccdbg_cmd_write(dbg, cmd, data, len);
209         ccdbg_recv_bytes(dbg, byte, 2);
210         return (byte[0] << 8) | byte[1];
211 }