]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blobdiff - hantek_6022be.c
fx2lafw: Blink LED on pin PA1 during acquisition.
[sigrok-firmware-fx2lafw.git] / hantek_6022be.c
index 1f58a608271f1fb1926377a9b7d45529faa850e4..91a959e24439cd7705e21e2f4ad62bf3e8a70d2d 100644 (file)
@@ -15,8 +15,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <fx2macros.h>
@@ -184,7 +183,7 @@ static void start_sampling(void)
 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;
@@ -216,18 +215,18 @@ static 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 },
 };
 
 static BOOL set_samplerate(BYTE rate)
@@ -243,7 +242,7 @@ static BOOL set_samplerate(BYTE rate)
        IFCONFIG = samplerates[i].ifcfg;
 
        AUTOPTRSETUP = 7;
-       AUTOPTRH2 = 0xE4;
+       AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
        AUTOPTRL2 = 0x00;
 
        /*
@@ -260,6 +259,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;
@@ -269,24 +269,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; /* OE2=1, CTL2=1 */
+       EXTAUTODAT2 = 0x44; /* OE2=1, CTL2=1 */
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
+       EXTAUTODAT2 = 0;
 
+       /* LOGIC FUNCTION 0-7 */
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
        EXTAUTODAT2 = 0;
@@ -349,31 +352,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;
        }
@@ -387,8 +385,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;
 
@@ -422,7 +420,7 @@ static void main(void)
 
        /* Init timer2. */
        RCAP2L = -500 & 0xff;
-       RCAP2H = (-500 >> 8) & 0xff;
+       RCAP2H = (-500 & 0xff00) >> 8;
        T2CON = 0;
        ET2 = 1;
        TR2 = 1;