forked from djmuhlestein/fx2lib
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdebugdev.c
More file actions
236 lines (193 loc) · 4.14 KB
/
debugdev.c
File metadata and controls
236 lines (193 loc) · 4.14 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <stdio.h>
#include <fx2regs.h>
#include <fx2macros.h>
#include <serial.h>
#include <delay.h>
#include <autovector.h>
#include <lights.h>
#include <setupdat.h>
#include <eputils.h>
#define SYNCDELAY SYNCDELAY4
#define REARMVAL 0x80
#define REARM() EP2BCL=REARMVAL
volatile WORD bytes;
volatile __bit gotbuf;
volatile BYTE icount;
volatile __bit got_sud;
DWORD lcount;
__bit on;
extern WORD debug_dscr;
extern void _transchar(char c);
void main()
{
REVCTL=0; // not using advanced endpoint controls
d2off();
on=0;
lcount=0;
got_sud=FALSE;
icount=0;
gotbuf=FALSE;
bytes=0;
// renumerate
RENUMERATE_UNCOND();
SETCPUFREQ(CLK_48M);
SETIF48MHZ();
sio0_init(115200);
USE_USB_INTS();
ENABLE_SUDAV();
ENABLE_SOF();
ENABLE_HISPEED();
ENABLE_USBRESET();
// only valid endpoints are 2/6
EP2CFG = 0xA2; // 10100010
SYNCDELAY;
EP6CFG = 0xE2; // 11100010
SYNCDELAY;
EP1INCFG &= ~bmVALID;
SYNCDELAY;
EP1OUTCFG &= ~bmVALID;
SYNCDELAY;
EP4CFG &= ~bmVALID;
SYNCDELAY;
EP8CFG &= ~bmVALID;
SYNCDELAY;
// arm ep2
EP2BCL = 0x80; // write once
SYNCDELAY;
EP2BCL = 0x80; // do it again
// make it so we enumberate
EA=1; // global __interrupt enable
printf ( "USB DEBUG: Done initializing stuff\n" );
d1off();
while(TRUE) {
if ( got_sud ) {
handle_setupdata();
got_sud=FALSE;
}
if ( !(EP2468STAT & bmEP2EMPTY) ) {
if ( !(EP2468STAT & bmEP6FULL) ) { // wait for at least one empty in buffer
WORD i;
bytes = MAKEWORD(EP2BCH,EP2BCL);
for (i=0;i<bytes;++i)
_transchar(EP2FIFOBUF[i]);
REARM();
}
}
}
}
// copied routines from setupdat.h
// value (low byte) = ep
#define VC_EPSTAT 0xB1
BOOL handle_vendorcommand(BYTE cmd)
{
__xdata BYTE* pep;
switch ( cmd ) {
case 6:
return TRUE;
case VC_EPSTAT:
pep = ep_addr(SETUPDAT[2]);
if (pep) {
EP0BUF[0] = *pep;
EP0BCH=0;
EP0BCL=1;
return TRUE;
}
default:
printf ( "Need to implement vendor command: %02x\n", cmd );
}
return FALSE;
}
// this firmware only supports 0,0
BOOL handle_get_interface(BYTE ifc, BYTE* alt_ifc)
{
// printf ( "Get Interface\n" );
if (ifc==0) {
*alt_ifc=0;
return TRUE;
} else {
return FALSE;
}
}
BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc)
{
if (ifc==0&&alt_ifc==0) {
// SEE TRM 2.3.7
// reset toggles
RESETTOGGLE(0x02);
RESETTOGGLE(0x86);
// restore endpoints to default condition
RESETFIFO(0x02);
EP2BCL=0x80;
SYNCDELAY;
EP2BCL=0X80;
SYNCDELAY;
RESETFIFO(0x86);
return TRUE;
} else
return FALSE;
}
// get/set configuration
BYTE handle_get_configuration()
{
return 1;
}
BOOL handle_get_descriptor()
{
BYTE desc = SETUPDAT[3];
if (desc != DSCR_DEBUG_TYPE)
return FALSE;
SUDPTRH = MSB((WORD)&debug_dscr);
SUDPTRL = LSB((WORD)&debug_dscr);
return TRUE;
}
BOOL handle_set_configuration(BYTE cfg)
{
return cfg==1 ? TRUE : FALSE; // we only handle cfg 1
}
// copied usb jt routines from usbjt.h
void sudav_isr() __interrupt SUDAV_ISR {
got_sud=TRUE;
CLEAR_SUDAV();
}
__bit on2;
__xdata WORD sofct=0;
void sof_isr () __interrupt SOF_ISR __using 1
{
if(++sofct==8000) { // about 8000 sof __interrupts per second at high speed
on2 = !on2;
if (on2) {
d2on();
} else {
d2off();
}
sofct=0;
}
CLEAR_SOF();
}
void usbreset_isr() __interrupt USBRESET_ISR
{
handle_hispeed(FALSE);
CLEAR_USBRESET();
}
void hispeed_isr() __interrupt HISPEED_ISR
{
handle_hispeed(TRUE);
CLEAR_HISPEED();
}