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 BYTE altiface = 0; // alt interface
31 volatile WORD ledcounter = 0;
33 volatile __bit dosud=FALSE;
34 volatile __bit dosuspend=FALSE;
36 extern void main_init();
40 SETCPUFREQ(CLK_12M); // save energy
57 RCAP2H = (-500 >> 8) & 0xff;
62 // iic files (c2 load) don't need to renumerate/delay
85 WAKEUPCS |= bmWU|bmWU2; // make sure ext wakeups are cleared
97 } while ( !remote_wakeup_allowed && REMOTE_WAKEUP());
101 if ( REMOTE_WAKEUP() ) {
103 USBCS |= bmSIGRESUME;
105 USBCS &= ~bmSIGRESUME;
111 void resume_isr() __interrupt RESUME_ISR {
115 void sudav_isr() __interrupt SUDAV_ISR {
119 void usbreset_isr() __interrupt USBRESET_ISR {
120 handle_hispeed(FALSE);
123 void hispeed_isr() __interrupt HISPEED_ISR {
124 handle_hispeed(TRUE);
128 void suspend_isr() __interrupt SUSPEND_ISR {
133 void timer2_isr() __interrupt TF2_ISR {
136 if (--ledcounter == 0) {
145 /* This sets three bits for each channel, one channel at a time.
146 * For channel 0 we want to set bits 5, 6 & 7
147 * For channel 1 we want to set bits 2, 3 & 4
149 * We convert the input values that are strange due to original firmware code into the value of the three bits as follows:
156 * The third bit is always zero since there are only four outputs connected in the serial selector chip.
158 * The multiplication of the converted value by 0x24 sets the relevant bits in
159 * both channels and then we mask it out to only affect the channel currently
162 BOOL set_voltage(BYTE channel, BYTE val)
182 mask = channel ? 0xe0 : 0x1c;
183 IOC = (IOC & ~mask) | (bits & mask);
187 BOOL set_numchannels(BYTE numchannels)
189 if (numchannels == 1 || numchannels == 2) {
190 BYTE fifocfg = 7 + numchannels;
191 EP2FIFOCFG = fifocfg;
192 EP6FIFOCFG = fifocfg;
222 void start_sampling()
227 for (i = 0; i < 1000; i++);
228 while (!(GPIFTRIG & 0x80)) {
247 extern __code BYTE highspd_dscr;
248 extern __code BYTE fullspd_dscr;
249 void select_interface(BYTE alt)
251 const BYTE *pPacketSize = (USBCS & bmHSM ? &highspd_dscr : &fullspd_dscr)
252 + (9 + 16*alt + 9 + 4);
260 EP6AUTOINLENL = pPacketSize[0];
261 EP6AUTOINLENH = pPacketSize[1];
268 EP2AUTOINLENL = pPacketSize[0];
269 EP2AUTOINLENH = pPacketSize[1] & 0x7;
270 EP2ISOINPKTS = (pPacketSize[1] >> 3) + 1;
274 const struct samplerate_info {
283 { 48,0x80, 0, 3, 0, 0x00, 0xea },
284 { 30,0x80, 0, 3, 0, 0x00, 0xaa },
285 { 24, 1, 0, 2, 1, 0x40, 0xca },
286 { 16, 1, 1, 2, 0, 0x40, 0xca },
287 { 12, 2, 1, 2, 0, 0x40, 0xca },
288 { 8, 3, 2, 2, 0, 0x40, 0xca },
289 { 4, 6, 5, 2, 0, 0x40, 0xca },
290 { 2, 12, 11, 2, 0, 0x40, 0xca },
291 { 1, 24, 23, 2, 0, 0x40, 0xca },
292 { 50, 48, 47, 2, 0, 0x40, 0xca },
293 { 20, 120, 119, 2, 0, 0x40, 0xca },
294 { 10, 240, 239, 2, 0, 0x40, 0xca }
297 BOOL set_samplerate(BYTE rate)
300 while (samplerates[i].rate != rate) {
302 if (i == sizeof(samplerates)/sizeof(samplerates[0]))
306 IFCONFIG = samplerates[i].ifcfg;
312 /* The program for low-speed, e.g. 1 MHz, is
313 * wait 24, CTL2=0, FIFO
317 * The program for 24 MHz is
318 * wait 1, CTL2=0, FIFO
321 * The program for 30/48 MHz is:
322 * jump 0, CTL2=Z, FIFO, LOOP
325 EXTAUTODAT2 = samplerates[i].wait0;
326 EXTAUTODAT2 = samplerates[i].wait1;
334 EXTAUTODAT2 = samplerates[i].opc0;
335 EXTAUTODAT2 = samplerates[i].opc1;
343 EXTAUTODAT2 = samplerates[i].out0;
361 for (i = 0; i < 96; i++)
366 // set *alt_ifc to the current alt interface for ifc
367 BOOL handle_get_interface(BYTE ifc, BYTE* alt_ifc) {
368 (void) ifc; // ignore unused parameter
372 // return TRUE if you set the interface requested
373 // NOTE this function should reconfigure and reset the endpoints
374 // according to the interface descriptors you provided.
375 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc) {
377 select_interface(alt_ifc);
382 // handle getting and setting the configuration
383 // 1 is the default. We don't support multiple configurations.
384 BYTE handle_get_configuration() {
388 BOOL handle_set_configuration(BYTE cfg) {
389 (void) cfg; // ignore unused parameter
393 BOOL handle_vendorcommand(BYTE cmd) {
404 while (EP0CS & bmEPBUSY);
405 set_voltage(cmd - 0xe0, EP0BUF[0]);
410 while (EP0CS & bmEPBUSY);
411 set_samplerate(EP0BUF[0]);
416 while (EP0CS & bmEPBUSY);
423 while (EP0CS & bmEPBUSY);
424 set_numchannels(EP0BUF[0]);
427 return FALSE; // not handled by handlers
434 // in idle mode tristate all outputs
438 GPIFREADYSTAT = 0x00;