private void setup_i2c() //set timing bit size, sample rate, and timeout { // Get the size of the timing information for a transaction of // max_bytes length timing_size = BeagleApi.bg_bit_timing_size(BeagleProtocol.BG_PROTOCOL_I2C, max_bytes); timing = new uint[timing_size]; sample_rate = BeagleApi.bg_samplerate(handle, 200); //sampling rate in khz if (BeagleApi.bg_timeout(handle, 500) != (int)BeagleStatus.BG_OK) //set the timeout to 1s { Console.WriteLine("error: Could not set Beagle timeout; exiting...\n"); //throw new InvalidOperationException("Could not set Beagle timeout; exiting..."); } if (BeagleApi.bg_enable(handle, BeagleProtocol.BG_PROTOCOL_I2C) != (int)BeagleStatus.BG_OK) //start polling bus { Console.WriteLine("error: could not enable I2C capture; exiting...\n"); //throw new InvalidOperationException("error: could not enable I2C capture; exiting..."); } }
static void usbDump(int numPackets) { // Set up variables byte[] packet = new byte[1024]; int timingSize = BeagleApi.bg_bit_timing_size(BeagleProtocol.BG_PROTOCOL_USB, 1024); uint[] timing = new uint[timingSize]; byte[] savedIn = new byte[64]; uint[] savedInTiming = new uint[8 * 64]; ulong savedInSop = 0; int savedInLength = 0; uint savedInStatus = 0; uint savedInEvents = 0; ulong countSop = 0; int sofCount = 0; int preCount = 0; int inAckCount = 0; int inNakCount = 0; byte pid = 0; int syncErrors = 0; int packetnum = 0; samplerateKhz = BeagleApi.bg_samplerate(beagle, 0); int idleSamples = IDLE_THRESHOLD * samplerateKhz; // Open the connection to the Beagle. Default to port 0. 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); } // Output the header... Console.Write("index,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; int length = BeagleApi.bg_usb12_read_bit_timing( beagle, ref status, ref events, ref timeSop, ref timeDuration, ref timeDataOffset, 1024, packet, timingSize, timing); 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; } // Check for USB error if (status == BeagleApi.BG_READ_USB_ERR_BAD_SYNC) { ++syncErrors; } if (length > 0) { pid = packet[0]; } else { pid = 0; } // Check the PID and collapse appropriately: // SOF* PRE* (IN (ACK|NAK))* // If we have saved summary information, and we have // hit an error, received a non-summary packet, or // have exceeded the idle time, then dump out the // summary information before continuing if (status != BeagleApi.BG_READ_OK || usbTrigger(pid) || ((int)(timeSop - countSop) >= idleSamples)) { int offset = usbPrintSummaryPacket(packetnum, countSop, sofCount, preCount, inAckCount, inNakCount, syncErrors); sofCount = 0; preCount = 0; inAckCount = 0; inNakCount = 0; syncErrors = 0; countSop = timeSop; // Adjust the packet index if any events were printed by // usbPrintSummaryPacket. packetnum += offset; } // Now handle the current packet based on its packet ID switch (pid) { case BeagleApi.BG_USB_PID_SOF: // Increment the SOF counter ++sofCount; break; case BeagleApi.BG_USB_PID_PRE: // Increment the PRE counter ++preCount; break; case BeagleApi.BG_USB_PID_IN: // If the transaction is an IN, don't display it yet and // save the transaction. // If the following transaction is an ACK or NAK, // increment the appropriate IN/ACK or IN/NAK counter. // If the next transaction is not an ACK or NAK, // display the saved IN transaction . System.Array.Copy(packet, 0, savedIn, 0, length); System.Array.Copy(timing, 0, savedInTiming, 0, length * 8); savedInSop = timeSop; savedInLength = length; savedInStatus = status; savedInEvents = events; break; case BeagleApi.BG_USB_PID_NAK: goto case BeagleApi.BG_USB_PID_ACK; case BeagleApi.BG_USB_PID_ACK: // If the last transaction was IN, increment the appropriate // counter and don't display the transaction. if (savedInLength > 0) { savedInLength = 0; if (pid == BeagleApi.BG_USB_PID_ACK) { ++inAckCount; } else { ++inNakCount; } break; } goto default; default: // If the last transaction was IN, output it if (savedInLength > 0) { ulong saved_in_sop_ns = TIMESTAMP_TO_NS(savedInSop, samplerateKhz); String packetData = usbPrintDataPacket (ref savedIn, savedInLength); usbPrintPacket(packetnum, saved_in_sop_ns, savedInStatus, savedInEvents, null, packetData); ++packetnum; savedInLength = 0; } // 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; } countSop = timeSop + timeDuration; break; } } // Stop the capture BeagleApi.bg_disable(beagle); }
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); }
public static void spidump(int max_bytes, int num_packets) { // Get the size of timing information for each transaction of size // max_bytes int timing_size = BeagleApi.bg_bit_timing_size( BeagleProtocol.BG_PROTOCOL_SPI, max_bytes); byte[] data_mosi = new byte[max_bytes]; byte[] data_miso = new byte[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_SPI) != (int)BeagleStatus.BG_OK) { Console.Write("error: could not enable SPI capture; exiting...\n"); Environment.Exit(1); } Console.Write("index,time(ns),SPI,status,mosi0/miso0 ... " + "mosiN/misoN\n"); Console.Out.Flush(); // Capture and print information for each transaction for (i = 0; i < num_packets || num_packets == 0; ++i) { uint status = 0; ulong time_sop = 0; ulong time_sop_ns = 0; ulong time_duration = 0; uint time_dataoffset = 0; int n = 0; // Read transaction with bit timing data int count = BeagleApi.bg_spi_read_bit_timing(beagle, ref status, ref time_sop, ref time_duration, ref time_dataoffset, max_bytes, data_mosi, max_bytes, data_miso, timing_size, timing); // Translate timestamp to ns time_sop_ns = TIMESTAMP_TO_NS(time_sop, samplerate_khz); Console.Write("{0:d},{1:d},SPI,(", i, time_sop_ns); if (count < 0) { Console.Write("error={0:d},", count); } print_general_status(status); print_spi_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; } // Display the data for (n = 0; n < count; ++n) { if (n != 0) { Console.Write(", "); } if ((n & 0xf) == 0) { Console.Write("\n "); } Console.Write("{0:x2}/{1:x2}", data_mosi[n], data_miso[n]); } Console.Write("\n"); Console.Out.Flush(); } // Stop the capture BeagleApi.bg_disable(beagle); }
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); }