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