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