]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blobdiff - sainsmart_dds120.c
Use the same VID/PID for Hantek 6022BE and SainSmart DDS120.
[sigrok-firmware-fx2lafw.git] / sainsmart_dds120.c
index aa1190ae4a4ff951ff3b6a2bef52bbc4be30808f..9f06af32ae318bf0bd13915951563b7fe7e88f47 100644 (file)
@@ -150,6 +150,26 @@ static BOOL set_voltage(BYTE channel, BYTE val)
        return TRUE;
 }
 
+
+/**
+ * Each LSB in the nibble of the byte controls the coupling per channel.
+ *
+ * Setting PE3 disables AC coupling capacitor on CH0.
+ * Setting PE0 disables AC coupling capacitor on CH1.
+ */
+static void set_coupling(BYTE coupling_cfg)
+{
+       if (coupling_cfg & 0x01)
+               IOE |= 0x08;
+       else
+               IOE &= ~0x08;
+
+       if (coupling_cfg & 0x10)
+               IOE |= 0x01;
+       else
+               IOE &= ~0x01;
+}
+
 static BOOL set_numchannels(BYTE numchannels)
 {
        if (numchannels == 1 || numchannels == 2) {
@@ -240,9 +260,14 @@ static const struct samplerate_info {
        { 30, 0x80,   0, 3, 0, 0x00, 0xaa },
        { 24,    1,   0, 2, 1, 0x40, 0xea },
        { 16,    1,   1, 2, 0, 0x40, 0xea },
+       { 15,    1,   0, 2, 1, 0x40, 0xaa },
        { 12,    2,   1, 2, 0, 0x40, 0xea },
+       { 11,    1,   1, 2, 0, 0x40, 0xaa },
        {  8,    3,   2, 2, 0, 0x40, 0xea },
+       {  6,    2,   2, 2, 0, 0x40, 0xaa },
+       {  5,    3,   2, 2, 0, 0x40, 0xaa },
        {  4,    6,   5, 2, 0, 0x40, 0xea },
+       {  3,    5,   4, 2, 0, 0x40, 0xaa },
        {  2,   12,  11, 2, 0, 0x40, 0xea },
        {  1,   24,  23, 2, 0, 0x40, 0xea },
        { 50,   48,  47, 2, 0, 0x40, 0xea },
@@ -263,7 +288,7 @@ static BOOL set_samplerate(BYTE rate)
        IFCONFIG = samplerates[i].ifcfg;
 
        AUTOPTRSETUP = 7;
-       AUTOPTRH2 = 0xE4;
+       AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
        AUTOPTRL2 = 0x00;
 
        /*
@@ -280,6 +305,7 @@ static BOOL set_samplerate(BYTE rate)
         * jump 0, CTL2=Z, FIFO, LOOP
         */
 
+       /* LENGTH / BRANCH 0-7 */
        EXTAUTODAT2 = samplerates[i].wait0;
        EXTAUTODAT2 = samplerates[i].wait1;
        EXTAUTODAT2 = 1;
@@ -289,24 +315,27 @@ static BOOL set_samplerate(BYTE rate)
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
 
+       /* OPCODE 0-7 */
        EXTAUTODAT2 = samplerates[i].opc0;
        EXTAUTODAT2 = samplerates[i].opc1;
-       EXTAUTODAT2 = 1;
+       EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
 
+       /* OUTPUT 0-7 */
        EXTAUTODAT2 = samplerates[i].out0;
-       EXTAUTODAT2 = 0x44;
-       EXTAUTODAT2 = 0x44;
-       EXTAUTODAT2 = 0x00;
-       EXTAUTODAT2 = 0x00;
-       EXTAUTODAT2 = 0x00;
-       EXTAUTODAT2 = 0x00;
-       EXTAUTODAT2 = 0x00;
+       EXTAUTODAT2 = 0x44; /* OE0=1, CTL0=1 */
+       EXTAUTODAT2 = 0x44; /* OE0=1, CTL0=1 */
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
 
+       /* LOGIC FUNCTION 0-7 */
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
@@ -322,6 +351,30 @@ static BOOL set_samplerate(BYTE rate)
        return TRUE;
 }
 
+static BOOL set_calibration_pulse(BYTE fs)
+{
+       switch (fs) {
+       case 0:         // 100Hz
+               RCAP2L = -10000 & 0xff;
+               RCAP2H = (-10000 & 0xff00) >> 8;
+               return TRUE;
+       case 1:         // 1kHz
+               RCAP2L = -1000 & 0xff;
+               RCAP2H = (-1000 & 0xff00) >> 8;
+               return TRUE;
+       case 10:        // 1kHz
+               RCAP2L = (BYTE)(-100 & 0xff);
+               RCAP2H = 0xff;
+               return TRUE;
+       case 50:        // 50kHz
+               RCAP2L = (BYTE)(-20 & 0xff);
+               RCAP2H = 0xff;
+               return TRUE;
+       default:
+               return FALSE;
+       }
+}
+
 /* Set *alt_ifc to the current alt interface for ifc. */
 BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
 {
@@ -365,7 +418,7 @@ BOOL handle_vendorcommand(BYTE cmd)
        stop_sampling();
 
        /* Clear EP0BCH/L for each valid command. */
-       if (cmd >= 0xe0 && cmd <= 0xe4) {
+       if (cmd >= 0xe0 && cmd <= 0xe6) {
                EP0BCH = 0;
                EP0BCL = 0;
                while (EP0CS & bmEPBUSY);
@@ -386,6 +439,12 @@ BOOL handle_vendorcommand(BYTE cmd)
        case 0xe4:
                set_numchannels(EP0BUF[0]);
                return TRUE;
+       case 0xe5:
+               set_coupling(EP0BUF[0]);
+               return TRUE;
+       case 0xe6:
+               set_calibration_pulse(EP0BUF[0]);
+               return TRUE;
        }
 
        return FALSE; /* Not handled by handlers. */
@@ -397,8 +456,8 @@ static void init(void)
        EP8CFG = 0;
 
        /* In idle mode tristate all outputs. */
-       GPIFIDLECTL = 0x00;
-       GPIFCTLCFG = 0x80;
+       GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
+       GPIFCTLCFG = 0x80; /* TRICTL=1. CTL0-2: CMOS outputs, tri-statable. */
        GPIFWFSELECT = 0x00;
        GPIFREADYSTAT = 0x00;
 
@@ -432,7 +491,7 @@ static void main(void)
 
        /* Init timer2. */
        RCAP2L = -1000 & 0xff;
-       RCAP2H = (-1000 >> 8) & 0xff;
+       RCAP2H = (-1000 & 0xff00) >> 8;
        T2CON = 0;
        ET2 = 1;
        TR2 = 1;