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