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