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 #define OE_CTL (((1 << CTL_BIT) << 4) | (1 << CTL_BIT)) /* OEx = CTLx = 1 */
23 static BOOL set_voltage(BYTE channel, BYTE val);
25 struct samplerate_info {
35 /* Change to support as many interfaces as you need. */
36 static BYTE altiface = 0;
38 static volatile WORD ledcounter = 0;
40 static volatile __bit dosud = FALSE;
41 static volatile __bit dosuspend = FALSE;
43 extern __code BYTE highspd_dscr;
44 extern __code BYTE fullspd_dscr;
46 void resume_isr(void) __interrupt(RESUME_ISR)
51 void sudav_isr(void) __interrupt(SUDAV_ISR)
57 void usbreset_isr(void) __interrupt(USBRESET_ISR)
59 handle_hispeed(FALSE);
63 void hispeed_isr(void) __interrupt(HISPEED_ISR)
69 void suspend_isr(void) __interrupt(SUSPEND_ISR)
75 void timer2_isr(void) __interrupt(TF2_ISR)
77 /* Toggle the probe calibration pin, only accurate up to ca. 8MHz. */
78 TOGGLE_CALIBRATION_PIN();
80 if (ledcounter && (--ledcounter == 0))
87 * Each LSB in the nibble of the byte controls the coupling per channel.
89 * Setting PE3 disables AC coupling capacitor on CH0.
90 * Setting PE0 disables AC coupling capacitor on CH1.
92 static void set_coupling(BYTE coupling_cfg)
94 if (coupling_cfg & 0x01)
99 if (coupling_cfg & 0x10)
105 static BOOL set_numchannels(BYTE numchannels)
107 if (numchannels == 1 || numchannels == 2) {
108 BYTE fifocfg = 7 + numchannels;
109 EP2FIFOCFG = fifocfg;
110 EP6FIFOCFG = fifocfg;
117 static void clear_fifo(void)
130 static void stop_sampling(void)
134 INPKTEND = (altiface == 0) ? 6 : 2;
137 static void start_sampling(void)
145 for (i = 0; i < 1000; i++);
147 while (!(GPIFTRIG & 0x80))
154 GPIFTRIG = (altiface == 0) ? 6 : 4;
156 /* Set green LED, don't clear LED afterwards (ledcounter = 0). */
161 static void select_interface(BYTE alt)
163 const BYTE *pPacketSize = \
164 ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
165 + (9 + (16 * alt) + 9 + 4);
174 EP6AUTOINLENL = pPacketSize[0];
175 EP6AUTOINLENH = pPacketSize[1];
181 EP2AUTOINLENL = pPacketSize[0];
182 EP2AUTOINLENH = pPacketSize[1] & 0x7;
183 EP2ISOINPKTS = (pPacketSize[1] >> 3) + 1;
187 static BOOL set_samplerate(BYTE rate)
191 while (samplerates[i].rate != rate) {
193 if (i == sizeof(samplerates) / sizeof(samplerates[0]))
197 IFCONFIG = samplerates[i].ifcfg;
200 AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
204 * The program for low-speed, e.g. 1 MHz, is:
205 * wait 24, CTLx=0, FIFO
209 * The program for 24 MHz is:
210 * wait 1, CTLx=0, FIFO
213 * The program for 30/48 MHz is:
214 * jump 0, CTLx=Z, FIFO, LOOP
216 * (CTLx is device-dependent, could be e.g. CTL0 or CTL2.)
219 /* LENGTH / BRANCH 0-7 */
220 EXTAUTODAT2 = samplerates[i].wait0;
221 EXTAUTODAT2 = samplerates[i].wait1;
230 EXTAUTODAT2 = samplerates[i].opc0;
231 EXTAUTODAT2 = samplerates[i].opc1;
232 EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
240 EXTAUTODAT2 = samplerates[i].out0;
241 EXTAUTODAT2 = OE_CTL;
242 EXTAUTODAT2 = OE_CTL;
249 /* LOGIC FUNCTION 0-7 */
259 for (i = 0; i < 96; i++)
265 static BOOL set_calibration_pulse(BYTE fs)
269 RCAP2L = -10000 & 0xff;
270 RCAP2H = (-10000 & 0xff00) >> 8;
273 RCAP2L = -1000 & 0xff;
274 RCAP2H = (-1000 & 0xff00) >> 8;
277 RCAP2L = (BYTE)(-100 & 0xff);
281 RCAP2L = (BYTE)(-20 & 0xff);
289 /* Set *alt_ifc to the current alt interface for ifc. */
290 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
300 * Return TRUE if you set the interface requested.
302 * Note: This function should reconfigure and reset the endpoints
303 * according to the interface descriptors you provided.
305 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc)
308 select_interface(alt_ifc);
313 BYTE handle_get_configuration(void)
315 /* We only support configuration 0. */
319 BOOL handle_set_configuration(BYTE cfg)
321 /* We only support configuration 0. */
327 BOOL handle_vendorcommand(BYTE cmd)
331 /* Set red LED, clear after timeout. */
335 /* Clear EP0BCH/L for each valid command. */
336 if (cmd >= 0xe0 && cmd <= 0xe6) {
339 while (EP0CS & bmEPBUSY);
345 set_voltage(cmd - 0xe0, EP0BUF[0]);
348 set_samplerate(EP0BUF[0]);
355 set_numchannels(EP0BUF[0]);
358 SET_COUPLING(EP0BUF[0]);
361 SET_CALIBRATION_PULSE(EP0BUF[0]);
365 return FALSE; /* Not handled by handlers. */
368 static void init(void)
375 /* In idle mode tristate all outputs. */
376 GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
377 GPIFCTLCFG = 0x80; /* TRICTL=1. CTL0-2: CMOS outputs, tri-statable. */
379 GPIFREADYSTAT = 0x00;
390 static void main(void)
397 /* Set up interrupts. */
406 /* Global (8051) interrupt enable. */
410 RCAP2L = -TIMER2_VAL & 0xff;
411 RCAP2H = (-TIMER2_VAL & 0xff00) >> 8;
434 /* Make sure ext wakeups are cleared. */
435 WAKEUPCS |= bmWU | bmWU2;
447 } while (!remote_wakeup_allowed && REMOTE_WAKEUP());
449 /* Resume (TRM 6.4). */
450 if (REMOTE_WAKEUP()) {
452 USBCS |= bmSIGRESUME;
454 USBCS &= ~bmSIGRESUME;