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