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