]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - fx2lafw.c
Removed CMD_SET_SAMPLRATE
[sigrok-firmware-fx2lafw.git] / fx2lafw.c
CommitLineData
d5f5ea73
UH
1/*
2 * This file is part of the fx2lafw project.
3 *
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * fx2lafw is an open-source firmware for Cypress FX2 based logic analyzers.
23 *
24 * It is written in C, using fx2lib as helper library, and sdcc as compiler.
25 * The code is licensed under the terms of the GNU GPL, version 2 or later.
26 *
27 * Technical notes:
28 *
29 * - We use the FX2 in GPIF mode to sample the data (asynchronously).
30 * - We use the internal 48MHz clock for GPIF.
31 * - The 8 channels/pins we sample (the GPIF data bus) are PB0-PB7.
32 * Support for 16 channels is not yet included, but might be added later.
33 * - Endpoint 2 is used for data transfers from FX2 to host.
34 * - The endpoint is quad-buffered.
35 *
36 * Documentation:
37 *
38 * - See http://sigrok.org/wiki/Fx2lafw
39 */
40
41#include <fx2regs.h>
42#include <fx2macros.h>
43#include <delay.h>
d5f5ea73
UH
44#include <setupdat.h>
45#include <eputils.h>
46#include <gpif.h>
e41576ec 47
8f4a701f 48#include <fx2lafw.h>
e41576ec 49#include <gpif-acquisition.h>
d5f5ea73 50
c7283c28 51/* Protocol commands */
8f5f7854
JH
52#define CMD_START 0xb0
53#define CMD_STOP 0xb1
54#define CMD_GET_FW_VERSION 0xb2
d5f5ea73
UH
55
56/* ... */
57volatile bit got_sud;
58
d5f5ea73
UH
59static void setup_endpoints(void)
60{
d5f5ea73
UH
61 /* Setup EP2 (IN). */
62 EP2CFG = (1 << 7) | /* EP is valid/activated */
63 (1 << 6) | /* EP direction: IN */
64 (1 << 5) | (0 << 4) | /* EP Type: bulk */
65 (0 << 3) | /* EP buffer size: 512 */
66 (0 << 2) | /* Reserved. */
67 (0 << 1) | (0 << 0); /* EP buffering: quad buffering */
68 SYNCDELAY();
69
c430e296
JH
70 /* Setup EP6 (IN) in the debug build. */
71#ifdef DEBUG
72 EP6CFG = (1 << 7) | /* EP is valid/activated */
73 (1 << 6) | /* EP direction: IN */
74 (1 << 5) | (0 << 4) | /* EP Type: bulk */
75 (0 << 3) | /* EP buffer size: 512 */
76 (0 << 2) | /* Reserved */
77 (1 << 1) | (0 << 0); /* EP buffering: double buffering */
78#else
79 EP6CFG &= ~bmVALID;
80#endif
81 SYNCDELAY();
82
106ee45c 83 /* Disable all other EPs (EP1, EP4, and EP8). */
4ad20a4c
UH
84 EP1INCFG &= ~bmVALID;
85 SYNCDELAY();
86 EP1OUTCFG &= ~bmVALID;
87 SYNCDELAY();
d5f5ea73
UH
88 EP4CFG &= ~bmVALID;
89 SYNCDELAY();
d5f5ea73
UH
90 EP8CFG &= ~bmVALID;
91 SYNCDELAY();
92
4ad20a4c 93 /* EP2: Reset the FIFOs. */
d5f5ea73 94 /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
d5f5ea73 95 RESETFIFO(0x02)
c430e296
JH
96#ifdef DEBUG
97 /* Reset the FIFOs of EP6 when in debug mode. */
98 RESETFIFO(0x06)
99#endif
d5f5ea73 100
dc7ac8bf
UH
101 /* EP2: Enable AUTOIN mode. Set FIFO width to 8bits. */
102 EP2FIFOCFG = bmAUTOIN | ~bmWORDWIDE;
103 SYNCDELAY();
104
105 /* EP2: Auto-commit 512 (0x200) byte packets (due to AUTOIN = 1). */
106 EP2AUTOINLENH = 0x02;
107 SYNCDELAY();
108 EP2AUTOINLENL = 0x00;
109 SYNCDELAY();
110
4ad20a4c 111 /* EP2: Set the GPIF flag to 'full'. */
fb0b6d28 112 EP2GPIFFLGSEL = (1 << 1) | (0 << 1);
d5f5ea73
UH
113 SYNCDELAY();
114}
115
116BOOL handle_vendorcommand(BYTE cmd)
117{
c7283c28
UH
118 /* Protocol implementation */
119
120 switch (cmd) {
c7283c28 121 case CMD_START:
e41576ec
JH
122 gpif_acquisition_start();
123 return TRUE;
c7283c28
UH
124 case CMD_STOP:
125 GPIFABORT = 0xff;
126 /* TODO */
127 return TRUE;
128 break;
129 case CMD_GET_FW_VERSION:
130 /* TODO */
131 break;
132 default:
133 /* Unimplemented command. */
134 break;
135 }
4ad20a4c 136
d5f5ea73
UH
137 return FALSE;
138}
139
140BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
141{
142 /* We only support interface 0, alternate interface 0. */
143 if (ifc != 0)
144 return FALSE;
145
146 *alt_ifc = 0;
147 return TRUE;
148}
149
150BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc)
151{
152 /* We only support interface 0, alternate interface 0. */
153 if (ifc != 0 || alt_ifc != 0)
154 return FALSE;
155
156 /* Perform procedure from TRM, section 2.3.7: */
157
158 /* (1) TODO. */
159
160 /* (2) Reset data toggles of the EPs in the interface. */
161 /* Note: RESETTOGGLE() gets the EP number WITH bit 7 set/cleared. */
d5f5ea73 162 RESETTOGGLE(0x82);
106ee45c
UH
163#ifdef DEBUG
164 RESETTOGGLE(0x86);
165#endif
d5f5ea73
UH
166
167 /* (3) Restore EPs to their default conditions. */
168 /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
d5f5ea73
UH
169 RESETFIFO(0x02);
170 /* TODO */
106ee45c 171#ifdef DEBUG
c430e296 172 RESETFIFO(0x06);
106ee45c 173#endif
c430e296 174
d5f5ea73
UH
175 /* (4) Clear the HSNAK bit. Not needed, fx2lib does this. */
176
177 return TRUE;
178}
179
180BYTE handle_get_configuration(void)
181{
182 /* We only support configuration 1. */
183 return 1;
184}
185
186BOOL handle_set_configuration(BYTE cfg)
187{
188 /* We only support configuration 1. */
189 return (cfg == 1) ? TRUE : FALSE;
190}
191
192void sudav_isr(void) interrupt SUDAV_ISR
193{
194 got_sud = TRUE;
195 CLEAR_SUDAV();
196}
197
198void sof_isr(void) interrupt SOF_ISR using 1
199{
200 CLEAR_SOF();
201}
202
203void usbreset_isr(void) interrupt USBRESET_ISR
204{
205 handle_hispeed(FALSE);
206 CLEAR_USBRESET();
207}
208
209void hispeed_isr(void) interrupt HISPEED_ISR
210{
211 handle_hispeed(TRUE);
212 CLEAR_HISPEED();
213}
214
1cbff47d 215void fx2lafw_init(void)
d5f5ea73
UH
216{
217 /* Set DYN_OUT and ENH_PKT bits, as recommended by the TRM. */
e7434142 218 REVCTL = bmNOAUTOARM | bmSKIPCOMMIT;
d5f5ea73
UH
219
220 got_sud = FALSE;
221
222 /* Renumerate. */
223 RENUMERATE_UNCOND();
224
225 SETCPUFREQ(CLK_48M);
226
227 USE_USB_INTS();
228
229 /* TODO: Does the order of the following lines matter? */
230 ENABLE_SUDAV();
231 ENABLE_SOF();
232 ENABLE_HISPEED();
233 ENABLE_USBRESET();
234
235 /* Global (8051) interrupt enable. */
236 EA = 1;
237
238 /* Setup the endpoints. */
239 setup_endpoints();
240
241 /* Put the FX2 into GPIF master mode and setup the GPIF. */
242 gpif_init_la();
1cbff47d 243}
d5f5ea73 244
1cbff47d
JH
245void fx2lafw_run(void)
246{
247 if (got_sud) {
248 handle_setupdata();
249 got_sud = FALSE;
d5f5ea73
UH
250 }
251}