]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blobdiff - hantek_6022be.c
hantek_6022be.c: Simplify handle_vendorcommand().
[sigrok-firmware-fx2lafw.git] / hantek_6022be.c
index 6c0c4aaf5610b0fa2abd8d0d14424deb0c2ac12e..190ba89e713d3fbc9d89204c88be6383f25c1355 100644 (file)
 #include <setupdat.h>
 
 /* Change to support as many interfaces as you need. */
-BYTE altiface = 0;
+static BYTE altiface = 0;
 
-volatile WORD ledcounter = 0;
+static volatile WORD ledcounter = 0;
 
-volatile __bit dosud = FALSE;
-volatile __bit dosuspend = FALSE;
+static volatile __bit dosud = FALSE;
+static volatile __bit dosuspend = FALSE;
 
 extern __code BYTE highspd_dscr;
 extern __code BYTE fullspd_dscr;
@@ -99,7 +99,7 @@ void timer2_isr(void) __interrupt TF2_ISR
  * both channels and then we mask it out to only affect the channel currently
  * requested.
  */
-BOOL set_voltage(BYTE channel, BYTE val)
+static BOOL set_voltage(BYTE channel, BYTE val)
 {
        BYTE bits, mask;
 
@@ -126,7 +126,7 @@ BOOL set_voltage(BYTE channel, BYTE val)
        return TRUE;
 }
 
-BOOL set_numchannels(BYTE numchannels)
+static BOOL set_numchannels(BYTE numchannels)
 {
        if (numchannels == 1 || numchannels == 2) {
                BYTE fifocfg = 7 + numchannels;
@@ -138,7 +138,7 @@ BOOL set_numchannels(BYTE numchannels)
        return FALSE;
 }
 
-void clear_fifo(void)
+static void clear_fifo(void)
 {
        GPIFABORT = 0xff;
        SYNCDELAY3;
@@ -151,14 +151,14 @@ void clear_fifo(void)
        FIFORESET = 0;
 }
 
-void stop_sampling(void)
+static void stop_sampling(void)
 {
        GPIFABORT = 0xff;
        SYNCDELAY3;
        INPKTEND = (altiface == 0) ? 6 : 2;
 }
 
-void start_sampling(void)
+static void start_sampling(void)
 {
        int i;
 
@@ -181,10 +181,10 @@ void start_sampling(void)
        PC1 = 0;
 }
 
-void select_interface(BYTE alt)
+static void select_interface(BYTE alt)
 {
        const BYTE *pPacketSize = \
-               (USBCS & bmHSM ? &highspd_dscr : &fullspd_dscr)
+               ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
                + (9 + (16 * alt) + 9 + 4);
 
        altiface = alt;
@@ -207,7 +207,7 @@ void select_interface(BYTE alt)
        }
 }
 
-const struct samplerate_info {
+static const struct samplerate_info {
        BYTE rate;
        BYTE wait0;
        BYTE wait1;
@@ -216,21 +216,21 @@ const struct samplerate_info {
        BYTE out0;
        BYTE ifcfg;
 } 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 },
+       { 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 },
 };
 
-BOOL set_samplerate(BYTE rate)
+static BOOL set_samplerate(BYTE rate)
 {
        BYTE i = 0;
 
@@ -349,31 +349,26 @@ BOOL handle_vendorcommand(BYTE cmd)
        PC1 = 1;
        ledcounter = 1000;
 
-       switch (cmd) {
-       case 0xe0:
-       case 0xe1:
+       /* Clear EP0BCH/L for each valid command. */
+       if (cmd >= 0xe0 && cmd <= 0xe4) {
                EP0BCH = 0;
                EP0BCL = 0;
                while (EP0CS & bmEPBUSY);
+       }
+
+       switch (cmd) {
+       case 0xe0:
+       case 0xe1:
                set_voltage(cmd - 0xe0, EP0BUF[0]);
                return TRUE;
        case 0xe2:
-               EP0BCH = 0;
-               EP0BCL = 0;
-               while (EP0CS & bmEPBUSY);
                set_samplerate(EP0BUF[0]);
                return TRUE;
        case 0xe3:
-               EP0BCH = 0;
-               EP0BCL = 0;
-               while (EP0CS & bmEPBUSY);
                if (EP0BUF[0] == 1)
                        start_sampling();
                return TRUE;
        case 0xe4:
-               EP0BCH = 0;
-               EP0BCL = 0;
-               while (EP0CS & bmEPBUSY);
                set_numchannels(EP0BUF[0]);
                return TRUE;
        }
@@ -381,7 +376,7 @@ BOOL handle_vendorcommand(BYTE cmd)
        return FALSE; /* Not handled by handlers. */
 }
 
-void init(void)
+static void init(void)
 {
        EP4CFG = 0;
        EP8CFG = 0;
@@ -401,7 +396,7 @@ void init(void)
        select_interface(0);
 }
 
-void main(void)
+static void main(void)
 {
        /* Save energy. */
        SETCPUFREQ(CLK_12M);