1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright © 2008 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "ccdbg.h"
#include <time.h>
#include "cc-usb.h"
#include "cc-bitbang.h"
struct ccdbg *
ccdbg_open(void)
{
struct ccdbg *dbg;
char *tty;
dbg = calloc(sizeof (struct ccdbg), 1);
if (!dbg) {
perror("calloc");
return NULL;
}
tty = getenv("CCDBG_TTY");
if (!tty || tty[0] == '/')
dbg->usb = cc_usb_open(tty);
if (!dbg->usb) {
dbg->bb = cc_bitbang_open();
if (!dbg->bb) {
free(dbg);
return NULL;
}
}
return dbg;
}
void
ccdbg_close(struct ccdbg *dbg)
{
if (dbg->usb)
cc_usb_close(dbg->usb);
if (dbg->bb)
cc_bitbang_close(dbg->bb);
free (dbg);
}
void
ccdbg_debug_mode(struct ccdbg *dbg)
{
if (dbg->usb)
cc_usb_debug_mode(dbg->usb);
else if (dbg->bb)
cc_bitbang_debug_mode(dbg->bb);
}
void
ccdbg_reset(struct ccdbg *dbg)
{
if (dbg->usb)
cc_usb_reset(dbg->usb);
else if (dbg->bb)
cc_bitbang_reset(dbg->bb);
}
void
ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
{
if (dbg->usb)
cc_usb_send_bytes(dbg->usb, bytes, nbytes);
else if (dbg->bb)
cc_bitbang_send_bytes(dbg->bb, bytes, nbytes);
}
void
ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes)
{
if (dbg->usb)
cc_usb_recv_bytes(dbg->usb, bytes, nbytes);
else if (dbg->bb)
cc_bitbang_recv_bytes(dbg->bb, bytes, nbytes);
}
void
ccdbg_sync(struct ccdbg *dbg)
{
if (dbg->usb)
cc_usb_sync(dbg->usb);
else if (dbg->bb)
cc_bitbang_sync(dbg->bb);
}
void
ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
{
ccdbg_send_bytes(dbg, &cmd, 1);
ccdbg_send_bytes(dbg, data, len);
}
uint8_t
ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
{
uint8_t byte[1];
ccdbg_cmd_write(dbg, cmd, data, len);
ccdbg_recv_bytes(dbg, byte, 1);
ccdbg_sync(dbg);
return byte[0];
}
uint16_t
ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len)
{
uint8_t byte[2];
ccdbg_cmd_write(dbg, cmd, data, len);
ccdbg_recv_bytes(dbg, byte, 2);
ccdbg_sync(dbg);
return (byte[0] << 8) | byte[1];
}
void
ccdbg_cmd_write_queue8(struct ccdbg *dbg, uint8_t cmd,
uint8_t *data, int len,
uint8_t *reply)
{
ccdbg_cmd_write(dbg, cmd, data, len);
ccdbg_recv_bytes(dbg, reply, 1);
}
|