2 * This file is part of the fx2lafw project.
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
22 * fx2lafw is an open-source firmware for Cypress FX2 based logic analyzers.
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.
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.
38 * - See http://sigrok.org/wiki/Fx2lafw
42 #include <fx2macros.h>
44 #include <autovector.h>
49 #define SYNCDELAY() SYNCDELAY4
54 /* GPIF terminology: DP = decision point, NDP = non-decision-point */
59 * See section "10.3.4 State Instructions" in the TRM for details.
61 static const BYTE wavedata[128] = {
65 * This is the basic algorithm implemented in our GPIF state machine:
67 * State 0: NDP: Sample the FIFO data bus.
68 * State 1: DP: If EP2 is full, go to state 7 (the IDLE state), i.e.,
69 * end the current waveform. Otherwise, go to state 0 again,
70 * i.e., sample data until EP2 is full.
78 /* S0-S6: LENGTH/BRANCH */
80 * For NDPs (LENGTH): Number of IFCLK cycles to stay in this state.
81 * For DPs (BRANCH): [7] ReExec, [5:3]: BRANCHON1, [2:0]: BRANCHON0.
83 * 0x01: Stay one IFCLK cycle in this state.
84 * 0x38: No Re-execution, BRANCHON1 = state 7, BRANCHON0 = state 0.
86 0x01, 0x38, 0x01, 0x01, 0x01, 0x01, 0x01,
87 /* TRM says "reserved", but GPIF designer always puts a 0x07 here. */
92 * 0x02: NDP, sample the FIFO data bus.
93 * 0x01: DP, don't sample the FIFO data bus.
95 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
100 /* Unused, we don't output anything, we only sample the pins. */
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 /* S0-S6: LOGIC FUNCTION (not used for NDPs) */
107 * 0x36: LFUNC = "A AND B", A = FIFO flag, B = FIFO flag.
108 * The FIFO flag (FF == full flag, in our case) is configured via
111 * So: If the EP2 FIFO is full and the EP2 FIFO is full, go to
112 * the state specified by BRANCHON1 (state 7), otherwise BRANCHON0
113 * (state 0). See the LENGTH/BRANCH value above for details.
115 0x00, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
116 /* TRM says "reserved", but GPIF designer always puts a 0x3f here. */
119 /* TODO: Must unused waveforms be "valid"? */
121 /* Waveform 1 (unused): */
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 /* Waveform 2 (unused): */
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 /* Waveform 3 (unused): */
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 static void gpif_setup_registers(void)
142 /* TODO. Value probably irrelevant, as we don't use RDY* signals? */
146 * Set TRICTL = 0, thus CTL0-CTL5 are CMOS outputs.
147 * TODO: Probably irrelevant, as we don't use CTL0-CTL5?
151 /* When GPIF is idle, tri-state the data bus. */
152 /* Bit 7: DONE, bit 0: IDLEDRV. TODO: Set/clear DONE bit? */
153 GPIFIDLECS = (1 << 0);
155 /* When GPIF is idle, set CTL0-CTL5 to 0. */
159 * Map index 0 in wavedata[] to FIFORD. The rest is assigned too,
160 * but not used by us.
162 * GPIFWFSELECT: [7:6] = SINGLEWR index, [5:4] = SINGLERD index,
163 * [3:2] = FIFOWR index, [1:0] = FIFORD index
165 GPIFWFSELECT = (0x3 << 6) | (0x2 << 4) | (0x1 << 2) | (0x0 << 0);
167 /* Contains RDY* pin values. Read-only according to TRM. */
171 static void gpif_write_waveforms(void)
176 * Write the four waveforms into the respective WAVEDATA register
177 * locations (0xe400 - 0xe47f) using the FX2's autopointer feature.
179 AUTOPTRSETUP = 0x07; /* Increment autopointers 1 & 2. */
180 AUTOPTRH1 = MSB((WORD)wavedata); /* Source is the 'wavedata' array. */
181 AUTOPTRL1 = LSB((WORD)wavedata);
182 AUTOPTRH2 = 0xe4; /* Dest is WAVEDATA (0xe400). */
184 for (i = 0; i < 128; i++)
185 EXTAUTODAT2 = EXTAUTODAT1;
188 static void gpif_init_addr_pins(void)
191 * Configure the 9 GPIF address pins (GPIFADR[8:0], which consist of
192 * PORTC[7:0] and PORTE[7]), and output an initial address (zero).
193 * TODO: Probably irrelevant, the 56pin FX2 has no ports C and E.
195 PORTCCFG = 0xff; /* Set PORTC[7:0] as alt. func. (GPIFADR[7:0]). */
196 OEC = 0xff; /* Configure PORTC[7:0] as outputs. */
197 PORTECFG |= 0x80; /* Set PORTE[7] as alt. func. (GPIFADR[8]). */
198 OEE |= 0x80; /* Configure PORTE[7] as output. */
200 GPIFADRL = 0x00; /* Clear GPIFADR[7:0]. */
202 GPIFADRH = 0x00; /* Clear GPIFADR[8]. */
205 static void gpif_init_flowstates(void)
207 /* Clear all flowstate registers, we don't use this functionality. */
218 static void gpif_init_la(void)
221 * Setup the FX2 in GPIF master mode, using the internal clock
222 * (non-inverted) at 48MHz, and using async sampling.
226 /* Abort currently executing GPIF waveform (if any). */
229 /* Setup the GPIF registers. */
230 gpif_setup_registers();
232 /* Write the four GPIF waveforms into the WAVEDATA register. */
233 gpif_write_waveforms();
235 /* Initialize GPIF address pins, output initial values. */
236 gpif_init_addr_pins();
238 /* Initialize flowstate registers (not used by us). */
239 gpif_init_flowstates();
242 static void setup_endpoints(void)
244 /* Setup EP1 (OUT). */
245 EP1OUTCFG = (1 << 7) | /* EP is valid/activated */
246 (0 << 6) | /* Reserved */
247 (1 << 5) | (0 << 4) | /* EP Type: bulk */
248 (0 << 3) | /* Reserved */
249 (0 << 2) | /* Reserved */
250 (0 << 1) | (0 << 0); /* Reserved */
253 /* Setup EP2 (IN). */
254 EP2CFG = (1 << 7) | /* EP is valid/activated */
255 (1 << 6) | /* EP direction: IN */
256 (1 << 5) | (0 << 4) | /* EP Type: bulk */
257 (0 << 3) | /* EP buffer size: 512 */
258 (0 << 2) | /* Reserved. */
259 (0 << 1) | (0 << 0); /* EP buffering: quad buffering */
262 /* Disable all other EPs (EP4, EP6, and EP8). */
270 /* Reset the FIFOs of EP1 and EP2. */
271 /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
275 /* Set the GPIF flag for EP2 to 'full'. */
276 EP2GPIFFLGSEL = (1 << 1) | (0 << 1);
280 BOOL handle_vendorcommand(BYTE cmd)
286 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
288 /* We only support interface 0, alternate interface 0. */
296 BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc)
298 /* We only support interface 0, alternate interface 0. */
299 if (ifc != 0 || alt_ifc != 0)
302 /* Perform procedure from TRM, section 2.3.7: */
306 /* (2) Reset data toggles of the EPs in the interface. */
307 /* Note: RESETTOGGLE() gets the EP number WITH bit 7 set/cleared. */
311 /* (3) Restore EPs to their default conditions. */
312 /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */
318 /* (4) Clear the HSNAK bit. Not needed, fx2lib does this. */
323 BYTE handle_get_configuration(void)
325 /* We only support configuration 1. */
329 BOOL handle_set_configuration(BYTE cfg)
331 /* We only support configuration 1. */
332 return (cfg == 1) ? TRUE : FALSE;
335 void sudav_isr(void) interrupt SUDAV_ISR
341 void sof_isr(void) interrupt SOF_ISR using 1
346 void usbreset_isr(void) interrupt USBRESET_ISR
348 handle_hispeed(FALSE);
352 void hispeed_isr(void) interrupt HISPEED_ISR
354 handle_hispeed(TRUE);
360 /* Set DYN_OUT and ENH_PKT bits, as recommended by the TRM. */
361 REVCTL = (1 << 1) | (1 << 0);
372 /* TODO: Does the order of the following lines matter? */
378 /* Global (8051) interrupt enable. */
381 /* Setup the endpoints. */
384 /* Put the FX2 into GPIF master mode and setup the GPIF. */
388 /* Initiate a GPIF read. */
391 /* TODO: This seems to hang? */
392 gpif_fifo_read(GPIF_EP2);