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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <fx2macros.h>
24 #include <autovector.h>
28 /* Change to support as many interfaces as you need. */
29 static BYTE altiface = 0;
31 static volatile WORD ledcounter = 0;
33 static volatile __bit dosud = FALSE;
34 static volatile __bit dosuspend = FALSE;
36 extern __code BYTE highspd_dscr;
37 extern __code BYTE fullspd_dscr;
39 void resume_isr(void) __interrupt RESUME_ISR
44 void sudav_isr(void) __interrupt SUDAV_ISR
50 void usbreset_isr(void) __interrupt USBRESET_ISR
52 handle_hispeed(FALSE);
56 void hispeed_isr(void) __interrupt HISPEED_ISR
62 void suspend_isr(void) __interrupt SUSPEND_ISR
68 void timer2_isr(void) __interrupt TF2_ISR
72 if (--ledcounter == 0) {
82 * This sets three bits for each channel, one channel at a time.
83 * For channel 0 we want to set bits 5, 6 & 7
84 * For channel 1 we want to set bits 2, 3 & 4
86 * We convert the input values that are strange due to original
87 * firmware code into the value of the three bits as follows:
95 * The third bit is always zero since there are only four outputs connected
96 * in the serial selector chip.
98 * The multiplication of the converted value by 0x24 sets the relevant bits in
99 * both channels and then we mask it out to only affect the channel currently
102 static BOOL set_voltage(BYTE channel, BYTE val)
123 mask = (channel) ? 0xe0 : 0x1c;
124 IOC = (IOC & ~mask) | (bits & mask);
129 static BOOL set_numchannels(BYTE numchannels)
131 if (numchannels == 1 || numchannels == 2) {
132 BYTE fifocfg = 7 + numchannels;
133 EP2FIFOCFG = fifocfg;
134 EP6FIFOCFG = fifocfg;
141 static void clear_fifo(void)
154 static void stop_sampling(void)
158 INPKTEND = (altiface == 0) ? 6 : 2;
161 static void start_sampling(void)
167 for (i = 0; i < 1000; i++);
169 while (!(GPIFTRIG & 0x80))
176 GPIFTRIG = (altiface == 0) ? 6 : 4;
178 /* Set green LED, don't clear LED. */
184 static void select_interface(BYTE alt)
186 const BYTE *pPacketSize = \
187 ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
188 + (9 + (16 * alt) + 9 + 4);
197 EP6AUTOINLENL = pPacketSize[0];
198 EP6AUTOINLENH = pPacketSize[1];
204 EP2AUTOINLENL = pPacketSize[0];
205 EP2AUTOINLENH = pPacketSize[1] & 0x7;
206 EP2ISOINPKTS = (pPacketSize[1] >> 3) + 1;
210 static const struct samplerate_info {
219 { 48, 0x80, 0, 3, 0, 0x00, 0xea },
220 { 30, 0x80, 0, 3, 0, 0x00, 0xaa },
221 { 24, 1, 0, 2, 1, 0x40, 0xca },
222 { 16, 1, 1, 2, 0, 0x40, 0xca },
223 { 12, 2, 1, 2, 0, 0x40, 0xca },
224 { 8, 3, 2, 2, 0, 0x40, 0xca },
225 { 4, 6, 5, 2, 0, 0x40, 0xca },
226 { 2, 12, 11, 2, 0, 0x40, 0xca },
227 { 1, 24, 23, 2, 0, 0x40, 0xca },
228 { 50, 48, 47, 2, 0, 0x40, 0xca },
229 { 20, 120, 119, 2, 0, 0x40, 0xca },
230 { 10, 240, 239, 2, 0, 0x40, 0xca },
233 static BOOL set_samplerate(BYTE rate)
237 while (samplerates[i].rate != rate) {
239 if (i == sizeof(samplerates) / sizeof(samplerates[0]))
243 IFCONFIG = samplerates[i].ifcfg;
246 AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
250 * The program for low-speed, e.g. 1 MHz, is:
251 * wait 24, CTL2=0, FIFO
255 * The program for 24 MHz is:
256 * wait 1, CTL2=0, FIFO
259 * The program for 30/48 MHz is:
260 * jump 0, CTL2=Z, FIFO, LOOP
263 /* LENGTH / BRANCH 0-7 */
264 EXTAUTODAT2 = samplerates[i].wait0;
265 EXTAUTODAT2 = samplerates[i].wait1;
274 EXTAUTODAT2 = samplerates[i].opc0;
275 EXTAUTODAT2 = samplerates[i].opc1;
276 EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
284 EXTAUTODAT2 = samplerates[i].out0;
285 EXTAUTODAT2 = 0x44; /* OE0=1, CTL0=1 */
286 EXTAUTODAT2 = 0x44; /* OE0=1, CTL0=1 */
293 /* LOGIC FUNCTION 0-7 */
303 for (i = 0; i < 96; i++)
309 /* Set *alt_ifc to the current alt interface for ifc. */
310 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
320 * Return TRUE if you set the interface requested.
322 * Note: This function should reconfigure and reset the endpoints
323 * according to the interface descriptors you provided.
325 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc)
328 select_interface(alt_ifc);
333 BYTE handle_get_configuration(void)
335 /* We only support configuration 0. */
339 BOOL handle_set_configuration(BYTE cfg)
341 /* We only support configuration 0. */
347 BOOL handle_vendorcommand(BYTE cmd)
356 /* Clear EP0BCH/L for each valid command. */
357 if (cmd >= 0xe0 && cmd <= 0xe4) {
360 while (EP0CS & bmEPBUSY);
366 set_voltage(cmd - 0xe0, EP0BUF[0]);
369 set_samplerate(EP0BUF[0]);
376 set_numchannels(EP0BUF[0]);
380 return FALSE; /* Not handled by handlers. */
383 static void init(void)
388 /* In idle mode tristate all outputs. */
389 GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
390 GPIFCTLCFG = 0x80; /* TRICTL=1. CTL0-2: CMOS outputs, tri-statable. */
392 GPIFREADYSTAT = 0x00;
403 static void main(void)
410 /* Set up interrupts. */
419 /* Global (8051) interrupt enable. */
423 RCAP2L = -500 & 0xff;
424 RCAP2H = (-500 & 0xff00) >> 8;
445 /* Make sure ext wakeups are cleared. */
446 WAKEUPCS |= bmWU|bmWU2;
458 } while (!remote_wakeup_allowed && REMOTE_WAKEUP());
460 /* Resume (TRM 6.4). */
461 if (REMOTE_WAKEUP()) {
463 USBCS |= bmSIGRESUME;
465 USBCS &= ~bmSIGRESUME;