]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - hantek_6022be.c
4dc34f2b4cfefd7d4517fa0a0e2a6734c13f8d3b
[sigrok-firmware-fx2lafw.git] / hantek_6022be.c
1 /*
2  * This file is part of the sigrok-firmware-fx2lafw project.
3  *
4  * Copyright (C) 2009 Ubixum, Inc.
5  * Copyright (C) 2015 Jochen Hoenicke
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <fx2macros.h>
22 #include <fx2ints.h>
23 #include <autovector.h>
24 #include <delay.h>
25 #include <setupdat.h>
26
27 #define SET_ANALOG_MODE()
28
29 /* Toggle the 1kHz calibration pin, only accurate up to ca. 8MHz. */
30 #define TOGGLE_CALIBRATION_PIN() PA7 = !PA7
31
32 /* Change to support as many interfaces as you need. */
33 static BYTE altiface = 0;
34
35 static volatile WORD ledcounter = 0;
36
37 static volatile __bit dosud = FALSE;
38 static volatile __bit dosuspend = FALSE;
39
40 extern __code BYTE highspd_dscr;
41 extern __code BYTE fullspd_dscr;
42
43 void resume_isr(void) __interrupt RESUME_ISR
44 {
45         CLEAR_RESUME();
46 }
47
48 void sudav_isr(void) __interrupt SUDAV_ISR
49 {
50         dosud = TRUE;
51         CLEAR_SUDAV();
52 }
53
54 void usbreset_isr(void) __interrupt USBRESET_ISR
55 {
56         handle_hispeed(FALSE);
57         CLEAR_USBRESET();
58 }
59
60 void hispeed_isr(void) __interrupt HISPEED_ISR
61 {
62         handle_hispeed(TRUE);
63         CLEAR_HISPEED();
64 }
65
66 void suspend_isr(void) __interrupt SUSPEND_ISR
67 {
68         dosuspend = TRUE;
69         CLEAR_SUSPEND();
70 }
71
72 void timer2_isr(void) __interrupt TF2_ISR
73 {
74         TOGGLE_CALIBRATION_PIN();
75
76         if (ledcounter) {
77                 if (--ledcounter == 0) {
78                         /* Clear LED. */
79                         PC0 = 1;
80                         PC1 = 1;
81                 }
82         }
83
84         TF2 = 0;
85 }
86
87 /*
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
91  *
92  * We convert the input values that are strange due to original
93  * firmware code into the value of the three bits as follows:
94  *
95  * val -> bits
96  * 1  -> 010b
97  * 2  -> 001b
98  * 5  -> 000b
99  * 10 -> 011b
100  *
101  * The third bit is always zero since there are only four outputs connected
102  * in the serial selector chip.
103  *
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
106  * requested.
107  */
108 static BOOL set_voltage(BYTE channel, BYTE val)
109 {
110         BYTE bits, mask;
111
112         switch (val) {
113         case 1:
114                 bits = 0x24 * 2;
115                 break;
116         case 2:
117                 bits = 0x24 * 1;
118                 break;
119         case 5:
120                 bits = 0x24 * 0;
121                 break;
122         case 10:
123                 bits = 0x24 * 3;
124                 break;
125         default:
126                 return FALSE;
127         }
128
129         mask = (channel) ? 0xe0 : 0x1c;
130         IOC = (IOC & ~mask) | (bits & mask);
131
132         return TRUE;
133 }
134
135 static BOOL set_numchannels(BYTE numchannels)
136 {
137         if (numchannels == 1 || numchannels == 2) {
138                 BYTE fifocfg = 7 + numchannels;
139                 EP2FIFOCFG = fifocfg;
140                 EP6FIFOCFG = fifocfg;
141                 return TRUE;
142         }
143
144         return FALSE;
145 }
146
147 static void clear_fifo(void)
148 {
149         GPIFABORT = 0xff;
150         SYNCDELAY3;
151         FIFORESET = 0x80;
152         SYNCDELAY3;
153         FIFORESET = 0x82;
154         SYNCDELAY3;
155         FIFORESET = 0x86;
156         SYNCDELAY3;
157         FIFORESET = 0;
158 }
159
160 static void stop_sampling(void)
161 {
162         GPIFABORT = 0xff;
163         SYNCDELAY3;
164         INPKTEND = (altiface == 0) ? 6 : 2;
165 }
166
167 static void start_sampling(void)
168 {
169         int i;
170
171         SET_ANALOG_MODE();
172
173         clear_fifo();
174
175         for (i = 0; i < 1000; i++);
176
177         while (!(GPIFTRIG & 0x80))
178                 ;
179
180         SYNCDELAY3;
181         GPIFTCB1 = 0x28;
182         SYNCDELAY3;
183         GPIFTCB0 = 0;
184         GPIFTRIG = (altiface == 0) ? 6 : 4;
185
186         /* Set green LED, don't clear LED. */
187         ledcounter = 0;
188         PC0 = 1;
189         PC1 = 0;
190 }
191
192 static void select_interface(BYTE alt)
193 {
194         const BYTE *pPacketSize = \
195                 ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
196                 + (9 + (16 * alt) + 9 + 4);
197
198         altiface = alt;
199
200         if (alt == 0) {
201                 /* Bulk on EP6. */
202                 EP2CFG = 0x00;
203                 EP6CFG = 0xe0;
204                 EP6GPIFFLGSEL = 1;
205                 EP6AUTOINLENL = pPacketSize[0];
206                 EP6AUTOINLENH = pPacketSize[1];
207         } else {
208                 /* Iso on EP2. */
209                 EP2CFG = 0xd8;
210                 EP6CFG = 0x00;
211                 EP2GPIFFLGSEL = 1;
212                 EP2AUTOINLENL = pPacketSize[0];
213                 EP2AUTOINLENH = pPacketSize[1] & 0x7;
214                 EP2ISOINPKTS = (pPacketSize[1] >> 3) + 1;
215         }
216 }
217
218 static const struct samplerate_info {
219         BYTE rate;
220         BYTE wait0;
221         BYTE wait1;
222         BYTE opc0;
223         BYTE opc1;
224         BYTE out0;
225         BYTE ifcfg;
226 } samplerates[] = {
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 },
239 };
240
241 static BOOL set_samplerate(BYTE rate)
242 {
243         BYTE i = 0;
244
245         while (samplerates[i].rate != rate) {
246                 i++;
247                 if (i == sizeof(samplerates) / sizeof(samplerates[0]))
248                         return FALSE;
249         }
250
251         IFCONFIG = samplerates[i].ifcfg;
252
253         AUTOPTRSETUP = 7;
254         AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
255         AUTOPTRL2 = 0x00;
256
257         /*
258          * The program for low-speed, e.g. 1 MHz, is:
259          * wait 24, CTLx=0, FIFO
260          * wait 23, CTLx=1
261          * jump 0, CTLx=1
262          *
263          * The program for 24 MHz is:
264          * wait 1, CTLx=0, FIFO
265          * jump 0, CTLx=1
266          *
267          * The program for 30/48 MHz is:
268          * jump 0, CTLx=Z, FIFO, LOOP
269          *
270          * (CTLx is device-dependent, could be e.g. CTL0 or CTL2.)
271          */
272
273         /* LENGTH / BRANCH 0-7 */
274         EXTAUTODAT2 = samplerates[i].wait0;
275         EXTAUTODAT2 = samplerates[i].wait1;
276         EXTAUTODAT2 = 1;
277         EXTAUTODAT2 = 0;
278         EXTAUTODAT2 = 0;
279         EXTAUTODAT2 = 0;
280         EXTAUTODAT2 = 0;
281         EXTAUTODAT2 = 0;
282
283         /* OPCODE 0-7 */
284         EXTAUTODAT2 = samplerates[i].opc0;
285         EXTAUTODAT2 = samplerates[i].opc1;
286         EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
287         EXTAUTODAT2 = 0;
288         EXTAUTODAT2 = 0;
289         EXTAUTODAT2 = 0;
290         EXTAUTODAT2 = 0;
291         EXTAUTODAT2 = 0;
292
293         /* OUTPUT 0-7 */
294         EXTAUTODAT2 = samplerates[i].out0;
295         EXTAUTODAT2 = 0x44; /* OE2=1, CTL2=1 */
296         EXTAUTODAT2 = 0x44; /* OE2=1, CTL2=1 */
297         EXTAUTODAT2 = 0;
298         EXTAUTODAT2 = 0;
299         EXTAUTODAT2 = 0;
300         EXTAUTODAT2 = 0;
301         EXTAUTODAT2 = 0;
302
303         /* LOGIC FUNCTION 0-7 */
304         EXTAUTODAT2 = 0;
305         EXTAUTODAT2 = 0;
306         EXTAUTODAT2 = 0;
307         EXTAUTODAT2 = 0;
308         EXTAUTODAT2 = 0;
309         EXTAUTODAT2 = 0;
310         EXTAUTODAT2 = 0;
311         EXTAUTODAT2 = 0;
312
313         for (i = 0; i < 96; i++)
314                 EXTAUTODAT2 = 0;
315
316         return TRUE;
317 }
318
319 /* Set *alt_ifc to the current alt interface for ifc. */
320 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
321 {
322         (void)ifc;
323
324         *alt_ifc = altiface;
325
326         return TRUE;
327 }
328
329 /*
330  * Return TRUE if you set the interface requested.
331  *
332  * Note: This function should reconfigure and reset the endpoints
333  * according to the interface descriptors you provided.
334  */
335 BOOL handle_set_interface(BYTE ifc,BYTE alt_ifc)
336 {
337         if (ifc == 0)
338                 select_interface(alt_ifc);
339
340         return TRUE;
341 }
342
343 BYTE handle_get_configuration(void)
344 {
345         /* We only support configuration 0. */
346         return 0;
347 }
348
349 BOOL handle_set_configuration(BYTE cfg)
350 {
351         /* We only support configuration 0. */
352         (void)cfg;
353
354         return TRUE;
355 }
356
357 BOOL handle_vendorcommand(BYTE cmd)
358 {
359         stop_sampling();
360
361         /* Set red LED. */
362         PC0 = 0;
363         PC1 = 1;
364         ledcounter = 1000;
365
366         /* Clear EP0BCH/L for each valid command. */
367         if (cmd >= 0xe0 && cmd <= 0xe4) {
368                 EP0BCH = 0;
369                 EP0BCL = 0;
370                 while (EP0CS & bmEPBUSY);
371         }
372
373         switch (cmd) {
374         case 0xe0:
375         case 0xe1:
376                 set_voltage(cmd - 0xe0, EP0BUF[0]);
377                 return TRUE;
378         case 0xe2:
379                 set_samplerate(EP0BUF[0]);
380                 return TRUE;
381         case 0xe3:
382                 if (EP0BUF[0] == 1)
383                         start_sampling();
384                 return TRUE;
385         case 0xe4:
386                 set_numchannels(EP0BUF[0]);
387                 return TRUE;
388         }
389
390         return FALSE; /* Not handled by handlers. */
391 }
392
393 static void init(void)
394 {
395         EP4CFG = 0;
396         EP8CFG = 0;
397
398         SET_ANALOG_MODE();
399
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. */
403         GPIFWFSELECT = 0x00;
404         GPIFREADYSTAT = 0x00;
405
406         stop_sampling();
407
408         set_voltage(0, 1);
409         set_voltage(1, 1);
410         set_samplerate(1);
411         set_numchannels(2);
412         select_interface(0);
413 }
414
415 static void main(void)
416 {
417         /* Save energy. */
418         SETCPUFREQ(CLK_12M);
419
420         init();
421
422         /* Set up interrupts. */
423         USE_USB_INTS();
424
425         ENABLE_SUDAV();
426         ENABLE_USBRESET();
427         ENABLE_HISPEED(); 
428         ENABLE_SUSPEND();
429         ENABLE_RESUME();
430
431         /* Global (8051) interrupt enable. */
432         EA = 1;
433
434         /* Init timer2. */
435         RCAP2L = -500 & 0xff;
436         RCAP2H = (-500 & 0xff00) >> 8;
437         T2CON = 0;
438         ET2 = 1;
439         TR2 = 1;
440
441         RENUMERATE();
442
443         PORTCCFG = 0;
444         PORTACFG = 0;
445         OEC = 0xff;
446         OEA = 0x80;
447
448         while (TRUE) {
449                 if (dosud) {
450                         dosud = FALSE;
451                         handle_setupdata();
452                 }
453
454                 if (dosuspend) {
455                         dosuspend = FALSE;
456                         do {
457                                 /* Make sure ext wakeups are cleared. */
458                                 WAKEUPCS |= bmWU | bmWU2;
459                                 SUSPEND = 1;
460                                 PCON |= 1;
461                                 __asm
462                                 nop
463                                 nop
464                                 nop
465                                 nop
466                                 nop
467                                 nop
468                                 nop
469                                 __endasm;
470                         } while (!remote_wakeup_allowed && REMOTE_WAKEUP());
471
472                         /* Resume (TRM 6.4). */
473                         if (REMOTE_WAKEUP()) {
474                                 delay(5);
475                                 USBCS |= bmSIGRESUME;
476                                 delay(15);
477                                 USBCS &= ~bmSIGRESUME;
478                         }
479                 }
480         }
481 }