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 #define SET_ANALOG_MODE()
29 /* Toggle the 1kHz calibration pin, only accurate up to ca. 8MHz. */
30 #define TOGGLE_CALIBRATION_PIN() PA7 = !PA7
32 /* Change to support as many interfaces as you need. */
33 static BYTE altiface = 0;
35 static volatile WORD ledcounter = 0;
37 static volatile __bit dosud = FALSE;
38 static volatile __bit dosuspend = FALSE;
40 extern __code BYTE highspd_dscr;
41 extern __code BYTE fullspd_dscr;
43 void resume_isr(void) __interrupt RESUME_ISR
48 void sudav_isr(void) __interrupt SUDAV_ISR
54 void usbreset_isr(void) __interrupt USBRESET_ISR
56 handle_hispeed(FALSE);
60 void hispeed_isr(void) __interrupt HISPEED_ISR
66 void suspend_isr(void) __interrupt SUSPEND_ISR
72 void timer2_isr(void) __interrupt TF2_ISR
74 TOGGLE_CALIBRATION_PIN();
77 if (--ledcounter == 0) {
88 * This sets three bits for each channel, one channel at a time.
89 * For channel 0 we want to set bits 5, 6 & 7
90 * For channel 1 we want to set bits 2, 3 & 4
92 * We convert the input values that are strange due to original
93 * firmware code into the value of the three bits as follows:
101 * The third bit is always zero since there are only four outputs connected
102 * in the serial selector chip.
104 * The multiplication of the converted value by 0x24 sets the relevant bits in
105 * both channels and then we mask it out to only affect the channel currently
108 static BOOL set_voltage(BYTE channel, BYTE val)
129 mask = (channel) ? 0xe0 : 0x1c;
130 IOC = (IOC & ~mask) | (bits & mask);
135 static BOOL set_numchannels(BYTE numchannels)
137 if (numchannels == 1 || numchannels == 2) {
138 BYTE fifocfg = 7 + numchannels;
139 EP2FIFOCFG = fifocfg;
140 EP6FIFOCFG = fifocfg;
147 static void clear_fifo(void)
160 static void stop_sampling(void)
164 INPKTEND = (altiface == 0) ? 6 : 2;
167 static void start_sampling(void)
175 for (i = 0; i < 1000; i++);
177 while (!(GPIFTRIG & 0x80))
184 GPIFTRIG = (altiface == 0) ? 6 : 4;
186 /* Set green LED, don't clear LED. */
192 static void select_interface(BYTE alt)
194 const BYTE *pPacketSize = \
195 ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
196 + (9 + (16 * alt) + 9 + 4);
205 EP6AUTOINLENL = pPacketSize[0];
206 EP6AUTOINLENH = pPacketSize[1];
212 EP2AUTOINLENL = pPacketSize[0];
213 EP2AUTOINLENH = pPacketSize[1] & 0x7;
214 EP2ISOINPKTS = (pPacketSize[1] >> 3) + 1;
218 static const struct samplerate_info {
227 { 48, 0x80, 0, 3, 0, 0x00, 0xea },
228 { 30, 0x80, 0, 3, 0, 0x00, 0xaa },
229 { 24, 1, 0, 2, 1, 0x40, 0xca },
230 { 16, 1, 1, 2, 0, 0x40, 0xca },
231 { 12, 2, 1, 2, 0, 0x40, 0xca },
232 { 8, 3, 2, 2, 0, 0x40, 0xca },
233 { 4, 6, 5, 2, 0, 0x40, 0xca },
234 { 2, 12, 11, 2, 0, 0x40, 0xca },
235 { 1, 24, 23, 2, 0, 0x40, 0xca },
236 { 50, 48, 47, 2, 0, 0x40, 0xca },
237 { 20, 120, 119, 2, 0, 0x40, 0xca },
238 { 10, 240, 239, 2, 0, 0x40, 0xca },
241 static BOOL set_samplerate(BYTE rate)
245 while (samplerates[i].rate != rate) {
247 if (i == sizeof(samplerates) / sizeof(samplerates[0]))
251 IFCONFIG = samplerates[i].ifcfg;
254 AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
258 * The program for low-speed, e.g. 1 MHz, is:
259 * wait 24, CTLx=0, FIFO
263 * The program for 24 MHz is:
264 * wait 1, CTLx=0, FIFO
267 * The program for 30/48 MHz is:
268 * jump 0, CTLx=Z, FIFO, LOOP
270 * (CTLx is device-dependent, could be e.g. CTL0 or CTL2.)
273 /* LENGTH / BRANCH 0-7 */
274 EXTAUTODAT2 = samplerates[i].wait0;
275 EXTAUTODAT2 = samplerates[i].wait1;
284 EXTAUTODAT2 = samplerates[i].opc0;
285 EXTAUTODAT2 = samplerates[i].opc1;
286 EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
294 EXTAUTODAT2 = samplerates[i].out0;
295 EXTAUTODAT2 = 0x44; /* OE2=1, CTL2=1 */
296 EXTAUTODAT2 = 0x44; /* OE2=1, CTL2=1 */
303 /* LOGIC FUNCTION 0-7 */
313 for (i = 0; i < 96; i++)
319 /* Set *alt_ifc to the current alt interface for ifc. */
320 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
330 * Return TRUE if you set the interface requested.
332 * Note: This function should reconfigure and reset the endpoints
333 * according to the interface descriptors you provided.
335 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc)
338 select_interface(alt_ifc);
343 BYTE handle_get_configuration(void)
345 /* We only support configuration 0. */
349 BOOL handle_set_configuration(BYTE cfg)
351 /* We only support configuration 0. */
357 BOOL handle_vendorcommand(BYTE cmd)
366 /* Clear EP0BCH/L for each valid command. */
367 if (cmd >= 0xe0 && cmd <= 0xe4) {
370 while (EP0CS & bmEPBUSY);
376 set_voltage(cmd - 0xe0, EP0BUF[0]);
379 set_samplerate(EP0BUF[0]);
386 set_numchannels(EP0BUF[0]);
390 return FALSE; /* Not handled by handlers. */
393 static void init(void)
400 /* In idle mode tristate all outputs. */
401 GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
402 GPIFCTLCFG = 0x80; /* TRICTL=1. CTL0-2: CMOS outputs, tri-statable. */
404 GPIFREADYSTAT = 0x00;
415 static void main(void)
422 /* Set up interrupts. */
431 /* Global (8051) interrupt enable. */
435 RCAP2L = -500 & 0xff;
436 RCAP2H = (-500 & 0xff00) >> 8;
457 /* Make sure ext wakeups are cleared. */
458 WAKEUPCS |= bmWU | bmWU2;
470 } while (!remote_wakeup_allowed && REMOTE_WAKEUP());
472 /* Resume (TRM 6.4). */
473 if (REMOTE_WAKEUP()) {
475 USBCS |= bmSIGRESUME;
477 USBCS &= ~bmSIGRESUME;