]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blobdiff - hantek_6022bl.c
scopes: Initialize PORTA/C/E consistently for all devices.
[sigrok-firmware-fx2lafw.git] / hantek_6022bl.c
index 740a95920fda88267ae0b82b301d5e82aab7803f..fa595b4a25acd45cef7d61c5a66ed1f292df4a25 100644 (file)
 #include <delay.h>
 #include <setupdat.h>
 
+#define SET_ANALOG_MODE() PA7 = 1
+
+/* Toggle the 1kHz calibration pin, only accurate up to ca. 8MHz. */
+#define TOGGLE_CALIBRATION_PIN() PC2 = !PC2
+
+#define LED_CLEAR() PC0 = 1; PC1 = 1;
+#define LED_GREEN() PC0 = 1; PC1 = 0;
+#define LED_RED()   PC0 = 0; PC1 = 1;
+
 /* Change to support as many interfaces as you need. */
 static BYTE altiface = 0;
 
@@ -66,23 +75,18 @@ void suspend_isr(void) __interrupt SUSPEND_ISR
 
 void timer2_isr(void) __interrupt TF2_ISR
 {
-       /* Toggle the 1kHz calibration pin, only accurate up to ca 8MHz. */
-       PC2 = !PC2;
-
-       if (ledcounter) {
-               if (--ledcounter == 0) {
-                       /* Clear LED. */
-                       PC0 = 1;
-                       PC1 = 1;
-               }
-       }
+       TOGGLE_CALIBRATION_PIN();
+
+       if (ledcounter && (--ledcounter == 0))
+               LED_CLEAR();
+
        TF2 = 0;
 }
 
 /*
  * This sets three bits for each channel, one channel at a time.
- * For channel 0 we want to set bits 5, 6 & 7
- * For channel 1 we want to set bits 2, 3 & 4
+ * For channel 0 we want to set bits 1, 2 & 3
+ * For channel 1 we want to set bits 4, 5 & 6
  *
  * We convert the input values that are strange due to original
  * firmware code into the value of the three bits as follows:
@@ -106,23 +110,24 @@ static BOOL set_voltage(BYTE channel, BYTE val)
 
        switch (val) {
        case 1:
-               bits = 0x24 * 2;
+               bits = 0x02;
                break;
        case 2:
-               bits = 0x24 * 1;
+               bits = 0x01;
                break;
        case 5:
-               bits = 0x24 * 0;
+               bits = 0x00;
                break;
        case 10:
-               bits = 0x24 * 3;
+               bits = 0x03;
                break;
        default:
                return FALSE;
        }
 
-       mask = (channel) ? 0xe0 : 0x1c;
-       IOC = (IOC & ~mask) | (bits & mask);
+       bits = bits << (channel ? 1 : 4);
+       mask = (channel) ? 0x70 : 0x0e;
+       IOA = (IOA & ~mask) | (bits & mask);
 
        return TRUE;
 }
@@ -163,6 +168,8 @@ static void start_sampling(void)
 {
        int i;
 
+       SET_ANALOG_MODE();
+
        clear_fifo();
 
        for (i = 0; i < 1000; i++);
@@ -176,10 +183,9 @@ static void start_sampling(void)
        GPIFTCB0 = 0;
        GPIFTRIG = (altiface == 0) ? 6 : 4;
 
-       /* Set green LED, don't clear LED. */
+       /* Set green LED, don't clear LED afterwards (ledcounter = 0). */
+       LED_GREEN();
        ledcounter = 0;
-       PC0 = 1;
-       PC1 = 0;
 }
 
 static void select_interface(BYTE alt)
@@ -219,16 +225,16 @@ static const struct samplerate_info {
 } samplerates[] = {
        { 48, 0x80,   0, 3, 0, 0x00, 0xea },
        { 30, 0x80,   0, 3, 0, 0x00, 0xaa },
-       { 24,    1,   0, 2, 1, 0x40, 0xca },
-       { 16,    1,   1, 2, 0, 0x40, 0xca },
-       { 12,    2,   1, 2, 0, 0x40, 0xca },
-       {  8,    3,   2, 2, 0, 0x40, 0xca },
-       {  4,    6,   5, 2, 0, 0x40, 0xca },
-       {  2,   12,  11, 2, 0, 0x40, 0xca },
-       {  1,   24,  23, 2, 0, 0x40, 0xca },
-       { 50,   48,  47, 2, 0, 0x40, 0xca },
-       { 20,  120, 119, 2, 0, 0x40, 0xca },
-       { 10,  240, 239, 2, 0, 0x40, 0xca },
+       { 24,    1,   0, 2, 1, 0x10, 0xca },
+       { 16,    1,   1, 2, 0, 0x10, 0xca },
+       { 12,    2,   1, 2, 0, 0x10, 0xca },
+       {  8,    3,   2, 2, 0, 0x10, 0xca },
+       {  4,    6,   5, 2, 0, 0x10, 0xca },
+       {  2,   12,  11, 2, 0, 0x10, 0xca },
+       {  1,   24,  23, 2, 0, 0x10, 0xca },
+       { 50,   48,  47, 2, 0, 0x10, 0xca },
+       { 20,  120, 119, 2, 0, 0x10, 0xca },
+       { 10,  240, 239, 2, 0, 0x10, 0xca },
 };
 
 static BOOL set_samplerate(BYTE rate)
@@ -249,16 +255,18 @@ static BOOL set_samplerate(BYTE rate)
 
        /*
         * The program for low-speed, e.g. 1 MHz, is:
-        * wait 24, CTL2=0, FIFO
-        * wait 23, CTL2=1
-        * jump 0, CTL2=1
+        * wait 24, CTLx=0, FIFO
+        * wait 23, CTLx=1
+        * jump 0, CTLx=1
         *
         * The program for 24 MHz is:
-        * wait 1, CTL2=0, FIFO
-        * jump 0, CTL2=1
+        * wait 1, CTLx=0, FIFO
+        * jump 0, CTLx=1
         *
         * The program for 30/48 MHz is:
-        * jump 0, CTL2=Z, FIFO, LOOP
+        * jump 0, CTLx=Z, FIFO, LOOP
+        *
+        * (CTLx is device-dependent, could be e.g. CTL0 or CTL2.)
         */
 
        /* LENGTH / BRANCH 0-7 */
@@ -283,8 +291,8 @@ static BOOL set_samplerate(BYTE rate)
 
        /* OUTPUT 0-7 */
        EXTAUTODAT2 = samplerates[i].out0;
-       EXTAUTODAT2 = 0x44; /* OE0=1, CTL0=1 */
-       EXTAUTODAT2 = 0x44; /* OE0=1, CTL0=1 */
+       EXTAUTODAT2 = 0x11; /* OE0=1, CTL0=1 */
+       EXTAUTODAT2 = 0x11; /* OE0=1, CTL0=1 */
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
@@ -349,9 +357,8 @@ BOOL handle_vendorcommand(BYTE cmd)
 {
        stop_sampling();
 
-       /* Set red LED. */
-       PC0 = 0;
-       PC1 = 1;
+       /* Set red LED, clear after timeout. */
+       LED_RED();
        ledcounter = 1000;
 
        /* Clear EP0BCH/L for each valid command. */
@@ -386,8 +393,7 @@ static void init(void)
        EP4CFG = 0;
        EP8CFG = 0;
 
-       /* Set analog mode */
-       PA7 = 1;
+       SET_ANALOG_MODE();
 
        /* In idle mode tristate all outputs. */
        GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
@@ -432,10 +438,12 @@ static void main(void)
 
        RENUMERATE();
 
+       PORTECFG = 0;
        PORTCCFG = 0;
        PORTACFG = 0;
+       OEE = 0xff;
        OEC = 0xff;
-       OEA = 0x80;
+       OEA = 0xff;
 
        while (TRUE) {
                if (dosud) {
@@ -447,7 +455,7 @@ static void main(void)
                        dosuspend = FALSE;
                        do {
                                /* Make sure ext wakeups are cleared. */
-                               WAKEUPCS |= bmWU|bmWU2;
+                               WAKEUPCS |= bmWU | bmWU2;
                                SUSPEND = 1;
                                PCON |= 1;
                                __asm