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