9586faa38bfcf0b7ed96d2784529a719f069ceb2
[fw/altos] / 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
21 void
22 ccdbg_quarter_clock(struct ccdbg *dbg)
23 {
24         usleep(CC_CLOCK_US / 4);
25 }
26
27 void
28 ccdbg_half_clock(struct ccdbg *dbg)
29 {
30         usleep(CC_CLOCK_US / 2);
31 }
32
33 struct ccdbg *
34 ccdbg_open(char *file)
35 {
36         struct ccdbg *dbg;
37
38         dbg = calloc(sizeof (struct ccdbg), 1);
39         if (!dbg) {
40                 perror("calloc");
41                 return NULL;
42         }
43         dbg->fd = open(file, 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         dbg->clock = 1;
52         return dbg;
53 }
54
55 void
56 ccdbg_close(struct ccdbg *dbg)
57 {
58         cccp_fini(dbg);
59         close (dbg->fd);
60         free (dbg);
61 }
62
63 void
64 ccdbg_clock_1_0(struct ccdbg *dbg)
65 {
66         ccdbg_quarter_clock(dbg);
67         assert(dbg->clock == 1);
68         cccp_write(dbg, CC_CLOCK, 0);
69         dbg->clock = 0;
70         ccdbg_quarter_clock(dbg);
71 }
72
73 void
74 ccdbg_clock_0_1(struct ccdbg *dbg)
75 {
76         ccdbg_quarter_clock(dbg);
77         assert(dbg->clock == 0);
78         cccp_write(dbg, CC_CLOCK, CC_CLOCK);
79         dbg->clock = 1;
80         ccdbg_quarter_clock(dbg);
81 }
82
83 /*
84  * By convention, every macro function is entered with dbg->clock == 1
85  */
86
87 void
88 ccdbg_write_bit(struct ccdbg *dbg, uint8_t bit)
89 {
90         uint8_t data;
91
92         assert(dbg->clock == 1);
93         data = CC_CLOCK;
94         if (bit)
95                 data |= CC_DATA;
96         ccdbg_half_clock(dbg);
97         cccp_write(dbg, CC_DATA|CC_CLOCK, data);
98         ccdbg_half_clock(dbg);
99         cccp_write(dbg, CC_CLOCK, 0);
100 //      printf ("%d", bit);
101 }
102
103 void
104 ccdbg_write_byte(struct ccdbg *dbg, uint8_t byte)
105 {
106         int     bit;
107
108         for (bit = 7; bit >= 0; bit--)
109                 ccdbg_write_bit(dbg, (byte >> bit) & 1);
110 }
111
112 uint8_t
113 ccdbg_read_bit(struct ccdbg *dbg)
114 {
115         uint8_t data;
116
117         ccdbg_half_clock(dbg);
118         cccp_write(dbg, CC_CLOCK, CC_CLOCK);
119         ccdbg_half_clock(dbg);
120         cccp_write(dbg, CC_CLOCK, 0);
121         data = cccp_read(dbg, CC_DATA);
122         return data ? 1 : 0;
123 }
124
125 uint8_t
126 ccdbg_read_byte(struct ccdbg *dbg)
127 {
128         int     bit;
129         uint8_t byte = 0;
130
131         for (bit = 7; bit >= 0; bit--)
132                 byte |= ccdbg_read_bit(dbg) << bit;
133         return byte;
134 }
135
136 void
137 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
138 {
139         int     i;
140         ccdbg_write_byte(dbg, cmd);
141         for (i = 0; i < len; i++)
142                 ccdbg_write_byte(dbg, data[i]);
143 }
144
145 uint8_t
146 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
147 {
148         uint8_t ret;
149         ccdbg_cmd_write(dbg, cmd, data, len);
150         ret = ccdbg_read_byte(dbg);
151         return ret;
152 }
153
154 uint16_t
155 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
156 {
157         uint8_t byte1, byte0;
158         int     i;
159         ccdbg_cmd_write(dbg, cmd, data, len);
160         byte1 = ccdbg_read_byte(dbg); 
161         byte0 = ccdbg_read_byte(dbg);
162         for (i = 0; i < 4; i++)
163                 (void) ccdbg_read_byte(dbg);
164         return (byte1 << 8) | byte0;
165 }
166