]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - fx2lafw.c
Support wide sampling
[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 BYTE vendor_command;
55
56 static void setup_endpoints(void)
57 {
58         /* Setup EP2 (IN). */
59         EP2CFG = (1 << 7) |               /* EP is valid/activated */
60                  (1 << 6) |               /* EP direction: IN */
61                  (1 << 5) | (0 << 4) |    /* EP Type: bulk */
62                  (1 << 3) |               /* EP buffer size: 1024 */
63                  (0 << 2) |               /* Reserved. */
64                  (0 << 1) | (0 << 0);     /* EP buffering: quad buffering */
65         SYNCDELAY();
66
67         /* Disable all other EPs (EP1, EP4, EP6, and EP8). */
68         EP1INCFG &= ~bmVALID;
69         SYNCDELAY();
70         EP1OUTCFG &= ~bmVALID;
71         SYNCDELAY();
72         EP4CFG &= ~bmVALID;
73         SYNCDELAY();
74         EP6CFG &= ~bmVALID;
75         SYNCDELAY();
76         EP8CFG &= ~bmVALID;
77         SYNCDELAY();
78
79         /* EP2: Reset the FIFOs. */
80         /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
81         RESETFIFO(0x02)
82
83         /* EP2: Enable AUTOIN mode. Set FIFO width to 8bits. */
84         EP2FIFOCFG = bmAUTOIN;
85         SYNCDELAY();
86
87         /* EP2: Auto-commit 512 (0x200) byte packets (due to AUTOIN = 1). */
88         EP2AUTOINLENH = 0x02;
89         SYNCDELAY();
90         EP2AUTOINLENL = 0x00;
91         SYNCDELAY();
92
93         /* EP2: Set the GPIF flag to 'full'. */
94         EP2GPIFFLGSEL = (1 << 1) | (0 << 1);
95         SYNCDELAY();
96 }
97
98 static void send_fw_version(void)
99 {
100         /* Populate the buffer. */
101         struct version_info *const vi = (struct version_info *)EP0BUF;
102         vi->major = FX2LAFW_VERSION_MAJOR;
103         vi->minor = FX2LAFW_VERSION_MINOR;
104
105         /* Send the message. */
106         EP0BCH = 0;
107         EP0BCL = sizeof(struct version_info);
108 }
109
110 static void send_revid_version(void)
111 {
112         uint8_t *p;
113
114         /* Populate the buffer. */
115         p = (uint8_t *)EP0BUF;
116         *p = REVID;
117
118         /* Send the message. */
119         EP0BCH = 0;
120         EP0BCL = 1;
121 }
122
123 BOOL handle_vendorcommand(BYTE cmd)
124 {
125         /* Protocol implementation */
126         switch (cmd) {
127         case CMD_START:
128                 vendor_command = cmd;
129                 EP0BCL = 0;
130                 return TRUE;
131                 break;
132         case CMD_GET_FW_VERSION:
133                 send_fw_version();
134                 return TRUE;
135                 break;
136         case CMD_GET_REVID_VERSION:
137                 send_revid_version();
138                 return TRUE;
139                 break;
140         }
141
142         return FALSE;
143 }
144
145 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
146 {
147         /* We only support interface 0, alternate interface 0. */
148         if (ifc != 0)
149                 return FALSE;
150
151         *alt_ifc = 0;
152         return TRUE;
153 }
154
155 BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc)
156 {
157         /* We only support interface 0, alternate interface 0. */
158         if (ifc != 0 || alt_ifc != 0)
159                 return FALSE;
160         
161         /* Perform procedure from TRM, section 2.3.7: */
162
163         /* (1) TODO. */
164
165         /* (2) Reset data toggles of the EPs in the interface. */
166         /* Note: RESETTOGGLE() gets the EP number WITH bit 7 set/cleared. */
167         RESETTOGGLE(0x82);
168 #ifdef DEBUG
169         RESETTOGGLE(0x86);
170 #endif
171
172         /* (3) Restore EPs to their default conditions. */
173         /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
174         RESETFIFO(0x02);
175         /* TODO */
176 #ifdef DEBUG
177         RESETFIFO(0x06);
178 #endif
179
180         /* (4) Clear the HSNAK bit. Not needed, fx2lib does this. */
181
182         return TRUE;
183 }
184
185 BYTE handle_get_configuration(void)
186 {
187         /* We only support configuration 1. */
188         return 1;
189 }
190
191 BOOL handle_set_configuration(BYTE cfg)
192 {
193         /* We only support configuration 1. */
194         return (cfg == 1) ? TRUE : FALSE;
195 }
196
197 void sudav_isr(void) interrupt SUDAV_ISR
198 {
199         got_sud = TRUE;
200         CLEAR_SUDAV();
201 }
202
203 void sof_isr(void) interrupt SOF_ISR using 1
204 {
205         CLEAR_SOF();
206 }
207
208 void usbreset_isr(void) interrupt USBRESET_ISR
209 {
210         handle_hispeed(FALSE);
211         CLEAR_USBRESET();
212 }
213
214 void hispeed_isr(void) interrupt HISPEED_ISR
215 {
216         handle_hispeed(TRUE);
217         CLEAR_HISPEED();
218 }
219
220 void fx2lafw_init(void)
221 {
222         /* Set DYN_OUT and ENH_PKT bits, as recommended by the TRM. */
223         REVCTL = bmNOAUTOARM | bmSKIPCOMMIT;
224
225         got_sud = FALSE;
226         vendor_command = 0;
227
228         /* Renumerate. */
229         RENUMERATE_UNCOND();
230
231         SETCPUFREQ(CLK_48M);
232
233         USE_USB_INTS();
234
235         /* TODO: Does the order of the following lines matter? */
236         ENABLE_SUDAV();
237         ENABLE_SOF();
238         ENABLE_HISPEED();
239         ENABLE_USBRESET();
240
241         /* Global (8051) interrupt enable. */
242         EA = 1;
243
244         /* Setup the endpoints. */
245         setup_endpoints();
246
247         /* Put the FX2 into GPIF master mode and setup the GPIF. */
248         gpif_init_la();
249 }
250
251 void fx2lafw_poll(void)
252 {
253         if (got_sud) {
254                 handle_setupdata();
255                 got_sud = FALSE;
256         }
257
258         if (vendor_command) {
259                 switch (vendor_command) {
260                 case CMD_START:
261                         if ((EP0CS & bmEPBUSY) != 0)
262                                 break;
263
264                         if (EP0BCL == sizeof(struct cmd_start_acquisition)) {
265                                 gpif_acquisition_start(
266                                  (const struct cmd_start_acquisition *)EP0BUF);
267                         }
268
269                         /* Acknowledge the vendor command. */
270                         vendor_command = 0;
271                         break;
272                 default:
273                         /* Unimplemented command. */
274                         vendor_command = 0;
275                         break;
276                 }
277         }
278
279         gpif_poll();
280 }
281
282 void main(void)
283 {
284         fx2lafw_init();
285         while (1)
286                 fx2lafw_poll();
287 }