]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - fx2lafw.c
Removed CMD_STOP and renumbered commands
[sigrok-firmware-fx2lafw.git] / fx2lafw.c
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>
44 #include <setupdat.h>
45 #include <eputils.h>
46 #include <gpif.h>
47
48 #include <command.h>
49 #include <fx2lafw.h>
50 #include <gpif-acquisition.h>
51
52 /* ... */
53 volatile bit got_sud;
54
55 static void setup_endpoints(void)
56 {
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
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
79         /* Disable all other EPs (EP1, EP4, and EP8). */
80         EP1INCFG &= ~bmVALID;
81         SYNCDELAY();
82         EP1OUTCFG &= ~bmVALID;
83         SYNCDELAY();
84         EP4CFG &= ~bmVALID;
85         SYNCDELAY();
86         EP8CFG &= ~bmVALID;
87         SYNCDELAY();
88
89         /* EP2: Reset the FIFOs. */
90         /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
91         RESETFIFO(0x02)
92 #ifdef DEBUG
93         /* Reset the FIFOs of EP6 when in debug mode. */
94         RESETFIFO(0x06)
95 #endif
96
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
107         /* EP2: Set the GPIF flag to 'full'. */
108         EP2GPIFFLGSEL = (1 << 1) | (0 << 1);
109         SYNCDELAY();
110 }
111
112 BOOL handle_vendorcommand(BYTE cmd)
113 {
114         /* Protocol implementation */
115
116         switch (cmd) {
117         case CMD_GET_FW_VERSION:
118                 /* TODO */
119                 break;
120         case CMD_START:
121                 gpif_acquisition_start();
122                 return TRUE;
123         default:
124                 /* Unimplemented command. */
125                 break;
126         }
127
128         return FALSE;
129 }
130
131 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
132 {
133         /* We only support interface 0, alternate interface 0. */
134         if (ifc != 0)
135                 return FALSE;
136
137         *alt_ifc = 0;
138         return TRUE;
139 }
140
141 BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc)
142 {
143         /* We only support interface 0, alternate interface 0. */
144         if (ifc != 0 || alt_ifc != 0)
145                 return FALSE;
146         
147         /* Perform procedure from TRM, section 2.3.7: */
148
149         /* (1) TODO. */
150
151         /* (2) Reset data toggles of the EPs in the interface. */
152         /* Note: RESETTOGGLE() gets the EP number WITH bit 7 set/cleared. */
153         RESETTOGGLE(0x82);
154 #ifdef DEBUG
155         RESETTOGGLE(0x86);
156 #endif
157
158         /* (3) Restore EPs to their default conditions. */
159         /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
160         RESETFIFO(0x02);
161         /* TODO */
162 #ifdef DEBUG
163         RESETFIFO(0x06);
164 #endif
165
166         /* (4) Clear the HSNAK bit. Not needed, fx2lib does this. */
167
168         return TRUE;
169 }
170
171 BYTE handle_get_configuration(void)
172 {
173         /* We only support configuration 1. */
174         return 1;
175 }
176
177 BOOL handle_set_configuration(BYTE cfg)
178 {
179         /* We only support configuration 1. */
180         return (cfg == 1) ? TRUE : FALSE;
181 }
182
183 void sudav_isr(void) interrupt SUDAV_ISR
184 {
185         got_sud = TRUE;
186         CLEAR_SUDAV();
187 }
188
189 void sof_isr(void) interrupt SOF_ISR using 1
190 {
191         CLEAR_SOF();
192 }
193
194 void usbreset_isr(void) interrupt USBRESET_ISR
195 {
196         handle_hispeed(FALSE);
197         CLEAR_USBRESET();
198 }
199
200 void hispeed_isr(void) interrupt HISPEED_ISR
201 {
202         handle_hispeed(TRUE);
203         CLEAR_HISPEED();
204 }
205
206 void fx2lafw_init(void)
207 {
208         /* Set DYN_OUT and ENH_PKT bits, as recommended by the TRM. */
209         REVCTL = bmNOAUTOARM | bmSKIPCOMMIT;
210
211         got_sud = FALSE;
212
213         /* Renumerate. */
214         RENUMERATE_UNCOND();
215
216         SETCPUFREQ(CLK_48M);
217
218         USE_USB_INTS();
219
220         /* TODO: Does the order of the following lines matter? */
221         ENABLE_SUDAV();
222         ENABLE_SOF();
223         ENABLE_HISPEED();
224         ENABLE_USBRESET();
225
226         /* Global (8051) interrupt enable. */
227         EA = 1;
228
229         /* Setup the endpoints. */
230         setup_endpoints();
231
232         /* Put the FX2 into GPIF master mode and setup the GPIF. */
233         gpif_init_la();
234 }
235
236 void fx2lafw_run(void)
237 {
238         if (got_sud) {
239                 handle_setupdata();
240                 got_sud = FALSE;
241         }
242 }