forked from sigrokproject/sigrok-firmware-fx2lafw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fx2lafw.c
335 lines (281 loc) · 6.96 KB
/
fx2lafw.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* This file is part of the sigrok-firmware-fx2lafw project.
*
* Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/*
* fx2lafw is an open-source firmware for Cypress FX2 based logic analyzers.
*
* It is written in C, using fx2lib as helper library, and sdcc as compiler.
* The code is licensed under the terms of the GNU GPL, version 2 or later.
*
* Technical notes:
*
* - We use the FX2 in GPIF mode to sample the data (asynchronously).
* - We use the internal 48MHz clock for GPIF.
* - The 8 channels/pins we sample (the GPIF data bus) are PB0-PB7,
* or PB0-PB7 + PD0-PD7 for 16-channel sampling.
* - Endpoint 2 (quad-buffered) is used for data transfers from FX2 to host.
*
* Documentation:
*
* - See http://sigrok.org/wiki/Fx2lafw
*/
#include <fx2regs.h>
#include <fx2macros.h>
#include <fx2ints.h>
#include <delay.h>
#include <setupdat.h>
#include <eputils.h>
#include <gpif.h>
#include <command.h>
#include <fx2lafw.h>
#include <gpif-acquisition.h>
/* ... */
volatile __bit got_sud;
BYTE vendor_command;
volatile WORD ledcounter = 0;
static void setup_endpoints(void)
{
/* Setup EP2 (IN). */
EP2CFG = (1u << 7) | /* EP is valid/activated */
(1u << 6) | /* EP direction: IN */
(1u << 5) | (0u << 4) | /* EP Type: bulk */
(1u << 3) | /* EP buffer size: 1024 */
(0u << 2) | /* Reserved. */
(0u << 1) | (0u << 0); /* EP buffering: quad buffering */
SYNCDELAY();
/* Disable all other EPs (EP1, EP4, EP6, and EP8). */
EP1INCFG &= ~bmVALID;
SYNCDELAY();
EP1OUTCFG &= ~bmVALID;
SYNCDELAY();
EP4CFG &= ~bmVALID;
SYNCDELAY();
EP6CFG &= ~bmVALID;
SYNCDELAY();
EP8CFG &= ~bmVALID;
SYNCDELAY();
/* EP2: Reset the FIFOs. */
/* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
RESETFIFO(0x02);
/* EP2: Enable AUTOIN mode. Set FIFO width to 8bits. */
EP2FIFOCFG = bmAUTOIN;
SYNCDELAY();
/* EP2: Auto-commit 512 (0x200) byte packets (due to AUTOIN = 1). */
EP2AUTOINLENH = 0x02;
SYNCDELAY();
EP2AUTOINLENL = 0x00;
SYNCDELAY();
/* EP2: Set the GPIF flag to 'full'. */
EP2GPIFFLGSEL = (1 << 1) | (0 << 1);
SYNCDELAY();
}
static void send_fw_version(void)
{
/* Populate the buffer. */
struct version_info *const vi = (struct version_info *)EP0BUF;
vi->major = FX2LAFW_VERSION_MAJOR;
vi->minor = FX2LAFW_VERSION_MINOR;
/* Send the message. */
EP0BCH = 0;
SYNCDELAY();
EP0BCL = sizeof(struct version_info);
SYNCDELAY();
}
static void send_revid_version(void)
{
uint8_t *p;
/* Populate the buffer. */
p = (uint8_t *)EP0BUF;
*p = REVID;
/* Send the message. */
EP0BCH = 0;
SYNCDELAY();
EP0BCL = 1;
SYNCDELAY();
}
BOOL handle_vendorcommand(BYTE cmd)
{
/* Protocol implementation */
switch (cmd) {
case CMD_START:
/* Tell hardware we are ready to receive data. */
vendor_command = cmd;
EP0BCL = 0;
SYNCDELAY();
return TRUE;
case CMD_GET_FW_VERSION:
send_fw_version();
return TRUE;
case CMD_GET_REVID_VERSION:
send_revid_version();
return TRUE;
}
return FALSE;
}
BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
{
/* We only support interface 0, alternate interface 0. */
if (ifc != 0)
return FALSE;
*alt_ifc = 0;
return TRUE;
}
BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc)
{
/* We only support interface 0, alternate interface 0. */
if (ifc != 0 || alt_ifc != 0)
return FALSE;
/* Perform procedure from TRM, section 2.3.7: */
/* (1) TODO. */
/* (2) Reset data toggles of the EPs in the interface. */
/* Note: RESETTOGGLE() gets the EP number WITH bit 7 set/cleared. */
RESETTOGGLE(0x82);
/* (3) Restore EPs to their default conditions. */
/* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
RESETFIFO(0x02);
/* TODO */
/* (4) Clear the HSNAK bit. Not needed, fx2lib does this. */
return TRUE;
}
BYTE handle_get_configuration(void)
{
/* We only support configuration 1. */
return 1;
}
BOOL handle_set_configuration(BYTE cfg)
{
/* We only support configuration 1. */
return (cfg == 1) ? TRUE : FALSE;
}
void sudav_isr(void) __interrupt SUDAV_ISR
{
got_sud = TRUE;
CLEAR_SUDAV();
}
/* IN BULK NAK - the host started requesting data. */
void ibn_isr(void) __interrupt IBN_ISR
{
/*
* If the IBN interrupt is not disabled, clearing
* does not work. See AN78446, 6.2.
*/
BYTE ibnsave = IBNIE;
IBNIE = 0;
CLEAR_USBINT();
/*
* If the host sent the START command, start the GPIF
* engine. The host will repeat the BULK IN in the next
* microframe.
*/
if ((IBNIRQ & bmEP2IBN) && (gpif_acquiring == PREPARED)) {
ledcounter = 1;
LED_OFF();
gpif_acquisition_start();
}
/* Clear IBN flags for all EPs. */
IBNIRQ = 0xff;
NAKIRQ = bmIBN;
SYNCDELAY();
IBNIE = ibnsave;
SYNCDELAY();
}
void usbreset_isr(void) __interrupt USBRESET_ISR
{
handle_hispeed(FALSE);
CLEAR_USBRESET();
}
void hispeed_isr(void) __interrupt HISPEED_ISR
{
handle_hispeed(TRUE);
CLEAR_HISPEED();
}
void timer2_isr(void) __interrupt TF2_ISR
{
/* Blink LED during acquisition, keep it on otherwise. */
if (gpif_acquiring == RUNNING) {
if (--ledcounter == 0) {
LED_TOGGLE();
ledcounter = 1000;
}
} else if (gpif_acquiring == STOPPED) {
LED_ON();
}
TF2 = 0;
}
void fx2lafw_init(void)
{
/* Set DYN_OUT and ENH_PKT bits, as recommended by the TRM. */
REVCTL = bmNOAUTOARM | bmSKIPCOMMIT;
got_sud = FALSE;
vendor_command = 0;
/* Renumerate. */
RENUMERATE_UNCOND();
SETCPUFREQ(CLK_48M);
USE_USB_INTS();
/* TODO: Does the order of the following lines matter? */
ENABLE_SUDAV();
ENABLE_EP2IBN();
ENABLE_HISPEED();
ENABLE_USBRESET();
LED_INIT();
LED_ON();
/* Init timer2. */
RCAP2L = -500 & 0xff;
RCAP2H = (-500 & 0xff00) >> 8;
T2CON = 0;
ET2 = 1;
TR2 = 1;
/* Global (8051) interrupt enable. */
EA = 1;
/* Setup the endpoints. */
setup_endpoints();
/* Put the FX2 into GPIF master mode and setup the GPIF. */
gpif_init_la();
}
void fx2lafw_poll(void)
{
if (got_sud) {
handle_setupdata();
got_sud = FALSE;
}
if (vendor_command) {
switch (vendor_command) {
case CMD_START:
if ((EP0CS & bmEPBUSY) != 0)
break;
if (EP0BCL == sizeof(struct cmd_start_acquisition)) {
gpif_acquisition_prepare(
(const struct cmd_start_acquisition *)EP0BUF);
}
/* Acknowledge the vendor command. */
vendor_command = 0;
break;
default:
/* Unimplemented command. */
vendor_command = 0;
break;
}
}
gpif_poll();
}
void main(void)
{
fx2lafw_init();
while (1)
fx2lafw_poll();
}