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_CALIBRATION_PIN();
79 if (ledcounter && (--ledcounter == 0))
86 * Each LSB in the nibble of the byte controls the coupling per channel.
88 * Setting PE3 disables AC coupling capacitor on CH0.
89 * Setting PE0 disables AC coupling capacitor on CH1.
91 static void set_coupling(BYTE coupling_cfg)
93 if (coupling_cfg & 0x01)
98 if (coupling_cfg & 0x10)
104 static BOOL set_numchannels(BYTE numchannels)
106 if (numchannels == 1 || numchannels == 2) {
107 BYTE fifocfg = 7 + numchannels;
108 EP2FIFOCFG = fifocfg;
109 EP6FIFOCFG = fifocfg;
116 static void clear_fifo(void)
129 static void stop_sampling(void)
133 INPKTEND = (altiface == 0) ? 6 : 2;
136 static void start_sampling(void)
144 for (i = 0; i < 1000; i++);
146 while (!(GPIFTRIG & 0x80))
153 GPIFTRIG = (altiface == 0) ? 6 : 4;
155 /* Set green LED, don't clear LED afterwards (ledcounter = 0). */
160 static void select_interface(BYTE alt)
162 const BYTE *pPacketSize = \
163 ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
164 + (9 + (16 * alt) + 9 + 4);
173 EP6AUTOINLENL = pPacketSize[0];
174 EP6AUTOINLENH = pPacketSize[1];
180 EP2AUTOINLENL = pPacketSize[0];
181 EP2AUTOINLENH = pPacketSize[1] & 0x7;
182 EP2ISOINPKTS = (pPacketSize[1] >> 3) + 1;
186 static BOOL set_samplerate(BYTE rate)
190 while (samplerates[i].rate != rate) {
192 if (i == sizeof(samplerates) / sizeof(samplerates[0]))
196 IFCONFIG = samplerates[i].ifcfg;
199 AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
203 * The program for low-speed, e.g. 1 MHz, is:
204 * wait 24, CTLx=0, FIFO
208 * The program for 24 MHz is:
209 * wait 1, CTLx=0, FIFO
212 * The program for 30/48 MHz is:
213 * jump 0, CTLx=Z, FIFO, LOOP
215 * (CTLx is device-dependent, could be e.g. CTL0 or CTL2.)
218 /* LENGTH / BRANCH 0-7 */
219 EXTAUTODAT2 = samplerates[i].wait0;
220 EXTAUTODAT2 = samplerates[i].wait1;
229 EXTAUTODAT2 = samplerates[i].opc0;
230 EXTAUTODAT2 = samplerates[i].opc1;
231 EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
239 EXTAUTODAT2 = samplerates[i].out0;
240 EXTAUTODAT2 = OE_CTL;
241 EXTAUTODAT2 = OE_CTL;
248 /* LOGIC FUNCTION 0-7 */
258 for (i = 0; i < 96; i++)
264 static BOOL set_calibration_pulse(BYTE fs)
268 RCAP2L = -10000 & 0xff;
269 RCAP2H = (-10000 & 0xff00) >> 8;
272 RCAP2L = -1000 & 0xff;
273 RCAP2H = (-1000 & 0xff00) >> 8;
276 RCAP2L = (BYTE)(-100 & 0xff);
280 RCAP2L = (BYTE)(-20 & 0xff);
288 /* Set *alt_ifc to the current alt interface for ifc. */
289 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
299 * Return TRUE if you set the interface requested.
301 * Note: This function should reconfigure and reset the endpoints
302 * according to the interface descriptors you provided.
304 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc)
307 select_interface(alt_ifc);
312 BYTE handle_get_configuration(void)
314 /* We only support configuration 0. */
318 BOOL handle_set_configuration(BYTE cfg)
320 /* We only support configuration 0. */
326 BOOL handle_vendorcommand(BYTE cmd)
330 /* Set red LED, clear after timeout. */
334 /* Clear EP0BCH/L for each valid command. */
335 if (cmd >= 0xe0 && cmd <= 0xe6) {
338 while (EP0CS & bmEPBUSY);
344 set_voltage(cmd - 0xe0, EP0BUF[0]);
347 set_samplerate(EP0BUF[0]);
354 set_numchannels(EP0BUF[0]);
357 SET_COUPLING(EP0BUF[0]);
360 SET_CALIBRATION_PULSE(EP0BUF[0]);
364 return FALSE; /* Not handled by handlers. */
367 static void init(void)
374 /* In idle mode tristate all outputs. */
375 GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
376 GPIFCTLCFG = 0x80; /* TRICTL=1. CTL0-2: CMOS outputs, tri-statable. */
378 GPIFREADYSTAT = 0x00;
389 static void main(void)
396 /* Set up interrupts. */
405 /* Global (8051) interrupt enable. */
409 RCAP2L = -TIMER2_VAL & 0xff;
410 RCAP2H = (-TIMER2_VAL & 0xff00) >> 8;
433 /* Make sure ext wakeups are cleared. */
434 WAKEUPCS |= bmWU | bmWU2;
446 } while (!remote_wakeup_allowed && REMOTE_WAKEUP());
448 /* Resume (TRM 6.4). */
449 if (REMOTE_WAKEUP()) {
451 USBCS |= bmSIGRESUME;
453 USBCS &= ~bmSIGRESUME;