Add ccdump
[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 #include "cc-usb.h"
22 #include "cc-bitbang.h"
23
24
25 struct ccdbg *
26 ccdbg_open(void)
27 {
28         struct ccdbg *dbg;
29
30         dbg = calloc(sizeof (struct ccdbg), 1);
31         if (!dbg) {
32                 perror("calloc");
33                 return NULL;
34         }
35         dbg->usb = cc_usb_open();
36         if (!dbg->usb) {
37                 dbg->bb = cc_bitbang_open();
38                 if (!dbg->bb) {
39                         free(dbg);
40                         return NULL;
41                 }
42         }
43         return dbg;
44 }
45
46 void
47 ccdbg_close(struct ccdbg *dbg)
48 {
49         if (dbg->usb)
50                 cc_usb_close(dbg->usb);
51         if (dbg->bb)
52                 cc_bitbang_close(dbg->bb);
53         free (dbg);
54 }
55
56 void
57 ccdbg_debug_mode(struct ccdbg *dbg)
58 {
59         if (dbg->usb)
60                 cc_usb_debug_mode(dbg->usb);
61         else if (dbg->bb)
62                 cc_bitbang_debug_mode(dbg->bb);
63 }
64
65 void
66 ccdbg_reset(struct ccdbg *dbg)
67 {
68         if (dbg->usb)
69                 cc_usb_reset(dbg->usb);
70         else if (dbg->bb)
71                 cc_bitbang_reset(dbg->bb);
72 }
73
74 void
75 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
76 {
77         if (dbg->usb)
78                 cc_usb_send_bytes(dbg->usb, bytes, nbytes);
79         else if (dbg->bb)
80                 cc_bitbang_send_bytes(dbg->bb, bytes, nbytes);
81 }
82
83 void
84 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
85 {
86         if (dbg->usb)
87                 cc_usb_recv_bytes(dbg->usb, bytes, nbytes);
88         else if (dbg->bb)
89                 cc_bitbang_recv_bytes(dbg->bb, bytes, nbytes);
90 }
91
92 void
93 ccdbg_sync(struct ccdbg *dbg)
94 {
95         if (dbg->usb)
96                 cc_usb_sync(dbg->usb);
97         else if (dbg->bb)
98                 cc_bitbang_sync(dbg->bb);
99 }
100
101 void
102 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
103 {
104         ccdbg_send_bytes(dbg, &cmd, 1);
105         ccdbg_send_bytes(dbg, data, len);
106 }
107
108 uint8_t
109 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
110 {
111         uint8_t byte[1];
112         ccdbg_cmd_write(dbg, cmd, data, len);
113         ccdbg_recv_bytes(dbg, byte, 1);
114         ccdbg_sync(dbg);
115         return byte[0];
116 }
117
118 uint16_t
119 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
120 {
121         uint8_t byte[2];
122         ccdbg_cmd_write(dbg, cmd, data, len);
123         ccdbg_recv_bytes(dbg, byte, 2);
124         ccdbg_sync(dbg);
125         return (byte[0] << 8) | byte[1];
126 }
127
128 void
129 ccdbg_cmd_write_queue8(struct ccdbg *dbg, uint8_t cmd,
130                        uint8_t *data, int len,
131                        uint8_t *reply)
132 {
133         ccdbg_cmd_write(dbg, cmd, data, len);
134         ccdbg_recv_bytes(dbg, reply, 1);
135 }