2 * This file is part of the sigrok-firmware-fx2lafw project.
4 * Copyright (C) 2009 Ubixum, Inc.
5 * Copyright (C) 2015 Jochen Hoenicke
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include <fx2macros.h>
23 #include <autovector.h>
27 /* Change to support as many interfaces as you need. */
28 static BYTE altiface = 0;
30 static volatile WORD ledcounter = 0;
32 static volatile __bit dosud = FALSE;
33 static volatile __bit dosuspend = FALSE;
35 extern __code BYTE highspd_dscr;
36 extern __code BYTE fullspd_dscr;
38 void resume_isr(void) __interrupt RESUME_ISR
43 void sudav_isr(void) __interrupt SUDAV_ISR
49 void usbreset_isr(void) __interrupt USBRESET_ISR
51 handle_hispeed(FALSE);
55 void hispeed_isr(void) __interrupt HISPEED_ISR
61 void suspend_isr(void) __interrupt SUSPEND_ISR
67 void timer2_isr(void) __interrupt TF2_ISR
69 /* Toggle the 1kHz calibration pin, only accurate up to ca. 8MHz. */
73 if (--ledcounter == 0) {
84 * This sets three bits for each channel, one channel at a time.
85 * For channel 0 we want to set bits 1, 2 & 3
86 * For channel 1 we want to set bits 4, 5 & 6
88 * We convert the input values that are strange due to original
89 * firmware code into the value of the three bits as follows:
97 * The third bit is always zero since there are only four outputs connected
98 * in the serial selector chip.
100 * The multiplication of the converted value by 0x24 sets the relevant bits in
101 * both channels and then we mask it out to only affect the channel currently
104 static BOOL set_voltage(BYTE channel, BYTE val)
125 bits = bits << (channel ? 1 : 4);
126 mask = (channel) ? 0x70 : 0x0e;
127 IOA = (IOA & ~mask) | (bits & mask);
132 static BOOL set_numchannels(BYTE numchannels)
134 if (numchannels == 1 || numchannels == 2) {
135 BYTE fifocfg = 7 + numchannels;
136 EP2FIFOCFG = fifocfg;
137 EP6FIFOCFG = fifocfg;
144 static void clear_fifo(void)
157 static void stop_sampling(void)
161 INPKTEND = (altiface == 0) ? 6 : 2;
164 static void start_sampling(void)
168 /* Set analog mode. */
173 for (i = 0; i < 1000; i++);
175 while (!(GPIFTRIG & 0x80))
182 GPIFTRIG = (altiface == 0) ? 6 : 4;
184 /* Set green LED, don't clear LED. */
190 static void select_interface(BYTE alt)
192 const BYTE *pPacketSize = \
193 ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
194 + (9 + (16 * alt) + 9 + 4);
203 EP6AUTOINLENL = pPacketSize[0];
204 EP6AUTOINLENH = pPacketSize[1];
210 EP2AUTOINLENL = pPacketSize[0];
211 EP2AUTOINLENH = pPacketSize[1] & 0x7;
212 EP2ISOINPKTS = (pPacketSize[1] >> 3) + 1;
216 static const struct samplerate_info {
225 { 48, 0x80, 0, 3, 0, 0x00, 0xea },
226 { 30, 0x80, 0, 3, 0, 0x00, 0xaa },
227 { 24, 1, 0, 2, 1, 0x10, 0xca },
228 { 16, 1, 1, 2, 0, 0x10, 0xca },
229 { 12, 2, 1, 2, 0, 0x10, 0xca },
230 { 8, 3, 2, 2, 0, 0x10, 0xca },
231 { 4, 6, 5, 2, 0, 0x10, 0xca },
232 { 2, 12, 11, 2, 0, 0x10, 0xca },
233 { 1, 24, 23, 2, 0, 0x10, 0xca },
234 { 50, 48, 47, 2, 0, 0x10, 0xca },
235 { 20, 120, 119, 2, 0, 0x10, 0xca },
236 { 10, 240, 239, 2, 0, 0x10, 0xca },
239 static BOOL set_samplerate(BYTE rate)
243 while (samplerates[i].rate != rate) {
245 if (i == sizeof(samplerates) / sizeof(samplerates[0]))
249 IFCONFIG = samplerates[i].ifcfg;
252 AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
256 * The program for low-speed, e.g. 1 MHz, is:
257 * wait 24, CTLx=0, FIFO
261 * The program for 24 MHz is:
262 * wait 1, CTLx=0, FIFO
265 * The program for 30/48 MHz is:
266 * jump 0, CTLx=Z, FIFO, LOOP
268 * (CTLx is device-dependent, could be e.g. CTL0 or CTL2.)
271 /* LENGTH / BRANCH 0-7 */
272 EXTAUTODAT2 = samplerates[i].wait0;
273 EXTAUTODAT2 = samplerates[i].wait1;
282 EXTAUTODAT2 = samplerates[i].opc0;
283 EXTAUTODAT2 = samplerates[i].opc1;
284 EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
292 EXTAUTODAT2 = samplerates[i].out0;
293 EXTAUTODAT2 = 0x11; /* OE0=1, CTL0=1 */
294 EXTAUTODAT2 = 0x11; /* OE0=1, CTL0=1 */
301 /* LOGIC FUNCTION 0-7 */
311 for (i = 0; i < 96; i++)
317 /* Set *alt_ifc to the current alt interface for ifc. */
318 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
328 * Return TRUE if you set the interface requested.
330 * Note: This function should reconfigure and reset the endpoints
331 * according to the interface descriptors you provided.
333 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc)
336 select_interface(alt_ifc);
341 BYTE handle_get_configuration(void)
343 /* We only support configuration 0. */
347 BOOL handle_set_configuration(BYTE cfg)
349 /* We only support configuration 0. */
355 BOOL handle_vendorcommand(BYTE cmd)
364 /* Clear EP0BCH/L for each valid command. */
365 if (cmd >= 0xe0 && cmd <= 0xe4) {
368 while (EP0CS & bmEPBUSY);
374 set_voltage(cmd - 0xe0, EP0BUF[0]);
377 set_samplerate(EP0BUF[0]);
384 set_numchannels(EP0BUF[0]);
388 return FALSE; /* Not handled by handlers. */
391 static void init(void)
396 /* Set analog mode. */
399 /* In idle mode tristate all outputs. */
400 GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
401 GPIFCTLCFG = 0x80; /* TRICTL=1. CTL0-2: CMOS outputs, tri-statable. */
403 GPIFREADYSTAT = 0x00;
414 static void main(void)
421 /* Set up interrupts. */
430 /* Global (8051) interrupt enable. */
434 RCAP2L = -500 & 0xff;
435 RCAP2H = (-500 & 0xff00) >> 8;
456 /* Make sure ext wakeups are cleared. */
457 WAKEUPCS |= bmWU | bmWU2;
469 } while (!remote_wakeup_allowed && REMOTE_WAKEUP());
471 /* Resume (TRM 6.4). */
472 if (REMOTE_WAKEUP()) {
474 USBCS |= bmSIGRESUME;
476 USBCS &= ~bmSIGRESUME;