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