Esempio n. 1
0
    public static void mdiodump(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_MDIO, 0);

        uint[] timing = new uint[timing_size];

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

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

        Console.Write("index,time(ns),MDIO,status,<clause:opcode>," +
                      "<addr1>,<addr2>,data\n");
        Console.Out.Flush();

        // Capture and print each transaction
        int i;

        for (i = 0; i < num_packets || num_packets == 0; ++i)
        {
            uint  packet = 0;
            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_mdio_read_bit_timing(
                beagle, ref status, ref time_sop,
                ref time_duration, ref time_dataoffset,
                ref packet, timing_size, timing);

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

            // Check for errors
            if (count < 0)
            {
                Console.Write("{0,4:d},{1,13:d},MDIO,( error={2:d},",
                              i, time_sop_ns, count);
                print_general_status(status);
                print_mdio_status(status);
                Console.Write(")\n");
                Console.Out.Flush();
                continue;
            }

            // Parse the MDIO frame
            byte   clause = 0;
            byte   opcode = 0;
            byte   addr1  = 0;
            byte   addr2  = 0;
            ushort data   = 0;
            int    ret    = BeagleApi.bg_mdio_parse(packet, ref clause,
                                                    ref opcode, ref addr1, ref addr2, ref data);

            Console.Write("{0:d},{1:d},MDIO,(", i, time_sop_ns);
            print_general_status(status);
            if ((status & BeagleApi.BG_READ_TIMEOUT) == 0)
            {
                print_mdio_status((uint)ret);
            }
            Console.Write(")");

            // If zero data captured, continue
            if (count == 0)
            {
                Console.Write("\n");
                Console.Out.Flush();
                continue;
            }

            // Print the clause and opcode
            Console.Write(",");
            if ((status & BeagleApi.BG_READ_ERR_MIDDLE_OF_PACKET) == 0)
            {
                if (clause == (byte)BeagleMdioClause.BG_MDIO_CLAUSE_22)
                {
                    Console.Write("<22:");
                    switch (opcode)
                    {
                    case BeagleApi.BG_MDIO_OPCODE22_WRITE:
                        Console.Write("W");
                        break;

                    case BeagleApi.BG_MDIO_OPCODE22_READ:
                        Console.Write("R");
                        break;

                    case BeagleApi.BG_MDIO_OPCODE22_ERROR:
                        Console.Write("?");
                        break;
                    }
                }
                else if (clause == (byte)BeagleMdioClause.BG_MDIO_CLAUSE_45)
                {
                    Console.Write("<45:");
                    switch (opcode)
                    {
                    case BeagleApi.BG_MDIO_OPCODE45_ADDR:
                        Console.Write("A");
                        break;

                    case BeagleApi.BG_MDIO_OPCODE45_WRITE:
                        Console.Write("W");
                        break;

                    case BeagleApi.BG_MDIO_OPCODE45_READ_POSTINC:
                        Console.Write("RI");
                        break;

                    case BeagleApi.BG_MDIO_OPCODE45_READ:
                        Console.Write("R");
                        break;
                    }
                }
                else
                {
                    Console.Write("<?:?");
                }

                // Recall that for Clause 22:
                //     PHY  Addr = addr1, Reg Addr = addr2
                // and for Clause 45:
                //     Port Addr = addr1, Dev Addr = addr2
                Console.Write(">,<{0:X2}>,<{1:X2}>,{2:X4}\n",
                              addr1, addr2, data);
            }
            Console.Out.Flush();
        }

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