]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - fx2lafw.c
Moved gpif code out to separate source file
[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
UH
51/* Protocol commands */
52#define CMD_SET_SAMPLERATE 0xb0
53#define CMD_START 0xb1
54#define CMD_STOP 0xb2
55#define CMD_GET_FW_VERSION 0xb3
d5f5ea73
UH
56
57/* ... */
58volatile bit got_sud;
59
d5f5ea73
UH
60static void setup_endpoints(void)
61{
d5f5ea73
UH
62 /* Setup EP2 (IN). */
63 EP2CFG = (1 << 7) | /* EP is valid/activated */
64 (1 << 6) | /* EP direction: IN */
65 (1 << 5) | (0 << 4) | /* EP Type: bulk */
66 (0 << 3) | /* EP buffer size: 512 */
67 (0 << 2) | /* Reserved. */
68 (0 << 1) | (0 << 0); /* EP buffering: quad buffering */
69 SYNCDELAY();
70
c430e296
JH
71 /* Setup EP6 (IN) in the debug build. */
72#ifdef DEBUG
73 EP6CFG = (1 << 7) | /* EP is valid/activated */
74 (1 << 6) | /* EP direction: IN */
75 (1 << 5) | (0 << 4) | /* EP Type: bulk */
76 (0 << 3) | /* EP buffer size: 512 */
77 (0 << 2) | /* Reserved */
78 (1 << 1) | (0 << 0); /* EP buffering: double buffering */
79#else
80 EP6CFG &= ~bmVALID;
81#endif
82 SYNCDELAY();
83
106ee45c 84 /* Disable all other EPs (EP1, EP4, and EP8). */
4ad20a4c
UH
85 EP1INCFG &= ~bmVALID;
86 SYNCDELAY();
87 EP1OUTCFG &= ~bmVALID;
88 SYNCDELAY();
d5f5ea73
UH
89 EP4CFG &= ~bmVALID;
90 SYNCDELAY();
d5f5ea73
UH
91 EP8CFG &= ~bmVALID;
92 SYNCDELAY();
93
4ad20a4c 94 /* EP2: Reset the FIFOs. */
d5f5ea73 95 /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
d5f5ea73 96 RESETFIFO(0x02)
c430e296
JH
97#ifdef DEBUG
98 /* Reset the FIFOs of EP6 when in debug mode. */
99 RESETFIFO(0x06)
100#endif
d5f5ea73 101
dc7ac8bf
UH
102 /* EP2: Enable AUTOIN mode. Set FIFO width to 8bits. */
103 EP2FIFOCFG = bmAUTOIN | ~bmWORDWIDE;
104 SYNCDELAY();
105
106 /* EP2: Auto-commit 512 (0x200) byte packets (due to AUTOIN = 1). */
107 EP2AUTOINLENH = 0x02;
108 SYNCDELAY();
109 EP2AUTOINLENL = 0x00;
110 SYNCDELAY();
111
4ad20a4c 112 /* EP2: Set the GPIF flag to 'full'. */
fb0b6d28 113 EP2GPIFFLGSEL = (1 << 1) | (0 << 1);
d5f5ea73
UH
114 SYNCDELAY();
115}
116
117BOOL handle_vendorcommand(BYTE cmd)
118{
c7283c28
UH
119 /* Protocol implementation */
120
121 switch (cmd) {
122 case CMD_SET_SAMPLERATE:
123 /* TODO */
124 break;
125 case CMD_START:
e41576ec
JH
126 gpif_acquisition_start();
127 return TRUE;
c7283c28
UH
128 case CMD_STOP:
129 GPIFABORT = 0xff;
130 /* TODO */
131 return TRUE;
132 break;
133 case CMD_GET_FW_VERSION:
134 /* TODO */
135 break;
136 default:
137 /* Unimplemented command. */
138 break;
139 }
4ad20a4c 140
d5f5ea73
UH
141 return FALSE;
142}
143
144BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
145{
146 /* We only support interface 0, alternate interface 0. */
147 if (ifc != 0)
148 return FALSE;
149
150 *alt_ifc = 0;
151 return TRUE;
152}
153
154BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc)
155{
156 /* We only support interface 0, alternate interface 0. */
157 if (ifc != 0 || alt_ifc != 0)
158 return FALSE;
159
160 /* Perform procedure from TRM, section 2.3.7: */
161
162 /* (1) TODO. */
163
164 /* (2) Reset data toggles of the EPs in the interface. */
165 /* Note: RESETTOGGLE() gets the EP number WITH bit 7 set/cleared. */
d5f5ea73 166 RESETTOGGLE(0x82);
106ee45c
UH
167#ifdef DEBUG
168 RESETTOGGLE(0x86);
169#endif
d5f5ea73
UH
170
171 /* (3) Restore EPs to their default conditions. */
172 /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
d5f5ea73
UH
173 RESETFIFO(0x02);
174 /* TODO */
106ee45c 175#ifdef DEBUG
c430e296 176 RESETFIFO(0x06);
106ee45c 177#endif
c430e296 178
d5f5ea73
UH
179 /* (4) Clear the HSNAK bit. Not needed, fx2lib does this. */
180
181 return TRUE;
182}
183
184BYTE handle_get_configuration(void)
185{
186 /* We only support configuration 1. */
187 return 1;
188}
189
190BOOL handle_set_configuration(BYTE cfg)
191{
192 /* We only support configuration 1. */
193 return (cfg == 1) ? TRUE : FALSE;
194}
195
196void sudav_isr(void) interrupt SUDAV_ISR
197{
198 got_sud = TRUE;
199 CLEAR_SUDAV();
200}
201
202void sof_isr(void) interrupt SOF_ISR using 1
203{
204 CLEAR_SOF();
205}
206
207void usbreset_isr(void) interrupt USBRESET_ISR
208{
209 handle_hispeed(FALSE);
210 CLEAR_USBRESET();
211}
212
213void hispeed_isr(void) interrupt HISPEED_ISR
214{
215 handle_hispeed(TRUE);
216 CLEAR_HISPEED();
217}
218
1cbff47d 219void fx2lafw_init(void)
d5f5ea73
UH
220{
221 /* Set DYN_OUT and ENH_PKT bits, as recommended by the TRM. */
e7434142 222 REVCTL = bmNOAUTOARM | bmSKIPCOMMIT;
d5f5ea73
UH
223
224 got_sud = FALSE;
225
226 /* Renumerate. */
227 RENUMERATE_UNCOND();
228
229 SETCPUFREQ(CLK_48M);
230
231 USE_USB_INTS();
232
233 /* TODO: Does the order of the following lines matter? */
234 ENABLE_SUDAV();
235 ENABLE_SOF();
236 ENABLE_HISPEED();
237 ENABLE_USBRESET();
238
239 /* Global (8051) interrupt enable. */
240 EA = 1;
241
242 /* Setup the endpoints. */
243 setup_endpoints();
244
245 /* Put the FX2 into GPIF master mode and setup the GPIF. */
246 gpif_init_la();
1cbff47d 247}
d5f5ea73 248
1cbff47d
JH
249void fx2lafw_run(void)
250{
251 if (got_sud) {
252 handle_setupdata();
253 got_sud = FALSE;
d5f5ea73
UH
254 }
255}