Ejemplo n.º 1
0
    public static void i2cdump(int max_bytes, int num_packets)
    {
        // Get the size of the timing information for a transaction of
        // max_bytes length
        int timing_size = BeagleApi.bg_bit_timing_size(
            BeagleProtocol.BG_PROTOCOL_I2C, max_bytes);

        ushort[] data_in = new ushort[max_bytes];
        uint[]   timing  = new uint[timing_size];

        // Get the current sampling rate
        int samplerate_khz = BeagleApi.bg_samplerate(beagle, 0);

        int i;

        // Start the capture
        if (BeagleApi.bg_enable(beagle, BeagleProtocol.BG_PROTOCOL_I2C) !=
            (int)BeagleStatus.BG_OK)
        {
            Console.Write("error: could not enable I2C capture; exiting...\n");
            Environment.Exit(1);
        }

        Console.Write("index,time(ns),I2C,status,<addr:r/w>" +
                      "(*),data0 ... dataN(*)\n");
        Console.Out.Flush();

        // Capture and print each transaction
        for (i = 0; i < num_packets || num_packets == 0; ++i)
        {
            uint  status = 0;
            ulong time_sop = 0, time_sop_ns = 0;
            ulong time_duration   = 0;
            uint  time_dataoffset = 0;

            // Read transaction with bit timing data
            int count = BeagleApi.bg_i2c_read_bit_timing(
                beagle, ref status, ref time_sop,
                ref time_duration, ref time_dataoffset,
                max_bytes, data_in, timing_size, timing);

            // Translate timestamp to ns
            time_sop_ns = TIMESTAMP_TO_NS(time_sop, samplerate_khz);

            Console.Write("{0:d},{1:d}", i, time_sop_ns);
            Console.Write(",I2C,(");

            if (count < 0)
            {
                Console.Write("error={0:d},", count);
            }

            print_general_status(status);
            print_i2c_status(status);
            Console.Write(")");

            // Check for errors
            if (count <= 0)
            {
                Console.Write("\n");
                Console.Out.Flush();

                if (count < 0)
                {
                    break;
                }

                // If zero data captured, continue
                continue;
            }


            // Print the address and read/write
            Console.Write(",");
            int offset = 0;
            if ((status & BeagleApi.BG_READ_ERR_MIDDLE_OF_PACKET) == 0)
            {
                // Display the start condition
                Console.Write("[S] ");

                if (count >= 1)
                {
                    // Determine if byte was NACKed
                    int nack = (data_in[0] & BeagleApi.BG_I2C_MONITOR_NACK);

                    // Determine if this is a 10-bit address
                    if (count == 1 || (data_in[0] & 0xf9) != 0xf0 ||
                        (nack != 0))
                    {
                        // Display 7-bit address
                        Console.Write("<{0:x2}:{1:s}>{2:s} ",
                                      (data_in[0] & 0xff) >> 1,
                                      ((data_in[0] & 0x01) != 0) ? 'r' : 'w',
                                      (nack != 0) ? "*" : "");
                        offset = 1;
                    }
                    else
                    {
                        // Display 10-bit address
                        Console.Write("<{0:x3}:{1:s}>{2:s} ",
                                      ((data_in[0] << 7) & 0x300) |
                                      (data_in[1] & 0xff),
                                      ((data_in[0] & 0x01) != 0) ? 'r' : 'w',
                                      (nack != 0) ? "*" : "");
                        offset = 2;
                    }
                }
            }

            // Display rest of transaction
            count = count - offset;
            for (int n = 0; n < count; ++n)
            {
                // Determine if byte was NACKed
                int nack = (data_in[offset] & BeagleApi.BG_I2C_MONITOR_NACK);

                Console.Write("{0:x2}{1:s} ",
                              data_in[offset] & 0xff, (nack != 0) ? "*" : "");
                ++offset;
            }

            // Display the stop condition
            if ((status & BeagleApi.BG_READ_I2C_NO_STOP) == 0)
            {
                Console.Write("[P]");
            }

            Console.Write("\n");
            Console.Out.Flush();
        }

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