Beispiel #1
0
    static void usbDump(int numPackets, int timeoutMS)
    {
        long start, elapsedTime;

        // Set up variables
        byte[] packet    = new byte[1024];
        int    packetnum = 0;

        samplerateKhz = BeagleApi.bg_samplerate(beagle, 0);

        // Configure Beagle 480 for delayed-download capture
        BeagleApi.bg_usb480_capture_configure(beagle,
                                              BeagleUsb480CaptureMode.BG_USB480_CAPTURE_DELAYED_DOWNLOAD,
                                              BeagleUsb2TargetSpeed.BG_USB2_AUTO_SPEED_DETECT);

        // Enable the hardware filtering.  This will filter out packets with
        // the same device address as the Beagle analyzer and also filter
        // the PID packet groups listed below.
        BeagleApi.bg_usb480_hw_filter_config(beagle,
                                             BeagleApi.BG_USB2_HW_FILTER_SELF |
                                             BeagleApi.BG_USB2_HW_FILTER_PID_SOF |
                                             BeagleApi.BG_USB2_HW_FILTER_PID_IN |
                                             BeagleApi.BG_USB2_HW_FILTER_PID_PING |
                                             BeagleApi.BG_USB2_HW_FILTER_PID_PRE |
                                             BeagleApi.BG_USB2_HW_FILTER_PID_SPLIT);

        // Start the capture portion of the delayed-download capture
        if (BeagleApi.bg_enable(beagle, BeagleProtocol.BG_PROTOCOL_USB) !=
            (int)BeagleStatus.BG_OK)
        {
            Console.Write("error: could not enable USB capture; exiting...\n");
            Environment.Exit(1);
        }

        // Wait until timeout period elapses or the hardware buffer on
        // the Beagle USB 480 fills
        Console.Write("Hardware buffer usage:\n");
        start = timeMicroseconds();

        while (true)
        {
            uint bufferSize  = 0;
            uint bufferUsage = 0;
            byte bufferFull  = 0;

            // Poll the hardware buffer status
            BeagleApi.bg_usb480_hw_buffer_stats(beagle, ref bufferSize,
                                                ref bufferUsage,
                                                ref bufferFull);

            // Print out the progress
            elapsedTime = (timeMicroseconds() - start) / 1000;

            printProgress(bufferUsage / (bufferSize / 100),
                          ((double)elapsedTime) / 1000,
                          bufferUsage, bufferSize);

            // If timed out or buffer is full, exit loop
            if (bufferFull != 0 ||
                (timeoutMS != 0 && elapsedTime > timeoutMS))
            {
                break;
            }

            // Sleep for 150 milliseconds
            BeagleApi.bg_sleep_ms(150);
        }

        // Start the download portion of the delayed-download capture
        //
        // Output the header...
        Console.Write("\nindex,time(ns),USB,status,pid,data0 ... dataN(*)\n");
        Console.Out.Flush();

        // ...then start decoding packets
        while (packetnum < numPackets || (numPackets == 0))
        {
            uint  status         = 0;
            uint  events         = 0;
            ulong timeSop        = 0;
            ulong timeSopNS      = 0;
            ulong timeDuration   = 0;
            uint  timeDataOffset = 0;

            // Calling bg_usb480_read will automatically stop the
            // capture portion of the delayed-download capture and
            // will begin downloading the capture results.
            int length = BeagleApi.bg_usb480_read(
                beagle, ref status, ref events,
                ref timeSop, ref timeDuration,
                ref timeDataOffset, 1024, packet);

            timeSopNS = TIMESTAMP_TO_NS(timeSop, samplerateKhz);

            // Check for invalid packet or Beagle error
            if (length < 0)
            {
                String errorStatus = "";
                errorStatus += String.Format("error={0:d}", length);
                usbPrintPacket(packetnum, timeSopNS, status, events,
                               errorStatus, null);
                break;
            }

            // Output the current transaction
            if (length > 0 || events != 0 ||
                (status != 0 && status != BeagleApi.BG_READ_TIMEOUT))
            {
                String packetData = usbPrintDataPacket(ref packet,
                                                       length);
                usbPrintPacket(packetnum, timeSopNS, status,
                               events, null, packetData);
                ++packetnum;
            }

            // Exit if observe end of capture
            if ((status & BeagleApi.BG_READ_USB_END_OF_CAPTURE) != 0)
            {
                break;
            }
        }

        // Stop the capture
        BeagleApi.bg_disable(beagle);
    }