5ecea769c6b4f9c6b1340067ba8be4709f211128
[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->cp = cp_usb_open ();
42         if (!dbg->cp) {
43                 free (dbg);
44                 return NULL;
45         }
46         return dbg;
47 }
48
49 void
50 ccdbg_close(struct ccdbg *dbg)
51 {
52         cp_usb_close(dbg->cp);
53         free (dbg);
54 }
55
56 int
57 ccdbg_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
58 {
59         cp_usb_write(dbg->cp, mask, value);
60         return 0;
61 }
62
63 uint8_t
64 ccdbg_read(struct ccdbg *dbg)
65 {
66         return cp_usb_read(dbg->cp);
67 }
68
69 static char
70 is_bit(uint8_t get, uint8_t mask, char on, uint8_t bit)
71 {
72         if (mask&bit) {
73                 if (get&bit)
74                         return on;
75                 else
76                         return '.';
77         } else
78                 return '-';
79 }
80 void
81 ccdbg_print(char *format, uint8_t mask, uint8_t set)
82 {
83         ccdbg_debug (CC_DEBUG_BITBANG, format,
84                      is_bit(set, mask, 'C', CC_CLOCK),
85                      is_bit(set, mask, 'D', CC_DATA),
86                      is_bit(set, mask, 'R', CC_RESET_N));
87 }
88
89 void
90 ccdbg_send(struct ccdbg *dbg, uint8_t mask, uint8_t set)
91 {
92         ccdbg_write(dbg, mask, set);
93         ccdbg_print("%c %c %c\n", mask, set);
94         ccdbg_half_clock(dbg);
95 }
96
97 void
98 ccdbg_send_bit(struct ccdbg *dbg, uint8_t bit)
99 {
100         if (bit) bit = CC_DATA;
101         ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N, CC_CLOCK|bit|CC_RESET_N);
102         ccdbg_send(dbg, CC_CLOCK|CC_DATA|CC_RESET_N,          bit|CC_RESET_N);
103 }
104
105 void
106 ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte)
107 {
108         int bit;
109         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Send Byte 0x%02x\n#\n", byte);
110         for (bit = 7; bit >= 0; bit--) {
111                 ccdbg_send_bit(dbg, (byte >> bit) & 1);
112                 if (bit == 3)
113                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
114         }
115 }
116
117 void
118 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
119 {
120         while (nbytes--)
121                 ccdbg_send_byte(dbg, *bytes++);
122 }
123
124 uint8_t
125 ccdbg_recv_bit(struct ccdbg *dbg, int first)
126 {
127         uint8_t mask = first ? CC_DATA : 0;
128         uint8_t read;
129
130         ccdbg_send(dbg, CC_CLOCK|mask|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N);
131         read = ccdbg_read(dbg);
132         ccdbg_print("#\t%c %c %c\n", CC_DATA, read);
133         ccdbg_send(dbg, CC_CLOCK|     CC_RESET_N,                  CC_RESET_N);
134         return (read & CC_DATA) ? 1 : 0;
135 }
136
137 uint8_t
138 ccdbg_recv_byte(struct ccdbg *dbg, int first)
139 {
140         uint8_t byte = 0;
141         int     bit;
142
143         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv byte\n#\n");
144         for (bit = 0; bit < 8; bit++) {
145                 byte = byte << 1;
146                 byte |= ccdbg_recv_bit(dbg, first);
147                 if (bit == 3)
148                         ccdbg_debug(CC_DEBUG_BITBANG, "\n");
149                 first = 0;
150         }
151         ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv 0x%02x\n#\n", byte);
152         return byte;
153 }
154
155 void
156 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
157 {
158         int first = 1;
159         while (nbytes--) {
160                 *bytes++ = ccdbg_recv_byte(dbg, first);
161                 first = 0;
162         }
163 }
164
165 void
166 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
167 {
168         int     i;
169         ccdbg_send_byte(dbg, cmd);
170         for (i = 0; i < len; i++)
171                 ccdbg_send_byte(dbg, data[i]);
172 }
173
174 uint8_t
175 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
176 {
177         uint8_t byte[1];
178         ccdbg_cmd_write(dbg, cmd, data, len);
179         ccdbg_recv_bytes(dbg, byte, 1);
180         return byte[0];
181 }
182
183 uint16_t
184 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
185 {
186         uint8_t byte[2];
187         ccdbg_cmd_write(dbg, cmd, data, len);
188         ccdbg_recv_bytes(dbg, byte, 2);
189         return (byte[0] << 8) | byte[1];
190 }