static void _readMemory(int handle, ushort addr, ushort length) { int count; int i; byte[] data_in = new byte[length + 3]; for (int j = 0; j < length + 3; j++) { data_in[j] = 0; } CheetahApi.ch_spi_queue_clear(handle); CheetahApi.ch_spi_queue_oe(handle, 1); // Queue the read command, address, and data CheetahApi.ch_spi_queue_ss(handle, 0x1); CheetahApi.ch_spi_queue_byte(handle, 1, 0x03); CheetahApi.ch_spi_queue_byte(handle, 1, (byte)((addr >> 8) & 0xff)); CheetahApi.ch_spi_queue_byte(handle, 1, (byte)((addr >> 0) & 0xff)); CheetahApi.ch_spi_queue_byte(handle, length, 0x00); CheetahApi.ch_spi_queue_ss(handle, 0); count = CheetahApi.ch_spi_batch_shift(handle, length + 3, data_in); if (count < 0) { Console.Write("error: {0:s}\n", CheetahApi.ch_status_string(count)); } else if (count != length + 3) { Console.Write("error: read {0:d} bytes (expected {1:d})\n", count - 3, length); } // Dump the data to the screen Console.Write("\nData read from device:"); for (i = 0; i < length; ++i) { if ((i & 0x0f) == 0) { Console.Write("\n{0:x4}: ", addr + i); } Console.Write("{0:x2} ", data_in[i + 3] & 0xff); if (((i + 1) & 0x07) == 0) { Console.Write(" "); } } Console.Write("\n"); Console.Out.Flush(); }
/*====================================================================== | MAIN PROGRAM | =====================================================================*/ public static void Main(String[] args) { int handle = 0; int port = 0; // open port 0 by default int bitrate = 0; int mode = 0; int bitorder = 0; int length = 0; if (args.Length < 5) { print_usage(); Environment.Exit(1); } port = Convert.ToInt32(args[0]); bitrate = Convert.ToInt32(args[1]); mode = Convert.ToInt32(args[2]); bitorder = Convert.ToInt32(args[3]); length = Convert.ToInt32(args[4]); handle = CheetahApi.ch_open(port); if (handle <= 0) { Console.Error.Write( "Unable to open Cheetah device on port {0:d}\n", port); Console.Error.Write("Error code = {0:d} ({1:s})\n", handle, CheetahApi.ch_status_string(handle)); Environment.Exit(1); } Console.Write("Opened Cheetah device on port {0:d}\n", port); Console.Write("Host interface is {0:s}\n", ((CheetahApi.ch_host_ifce_speed(handle)) != 0) ? "high speed" : "full speed"); // Ensure that the SPI subsystem is configured. // Make sure that the bitorder parameter is valid, defaulting to LSB CheetahSpiBitorder spi_bitorder = (bitorder == (int)CheetahSpiBitorder.CH_SPI_BITORDER_MSB) ? CheetahSpiBitorder.CH_SPI_BITORDER_MSB : CheetahSpiBitorder.CH_SPI_BITORDER_LSB; CheetahApi.ch_spi_configure( handle, (CheetahSpiPolarity)(mode >> 1), (CheetahSpiPhase)(mode & 1), spi_bitorder, 0x0); Console.Write( "SPI configuration set to mode {0:d}, {1:s} shift, " + "SS[2:0] active low\n", mode, (spi_bitorder == CheetahSpiBitorder.CH_SPI_BITORDER_MSB) ? "MSB" : "LSB"); Console.Out.Flush(); // Power the target using the Cheetah adapter's power supply. CheetahApi.ch_target_power(handle, CheetahApi.CH_TARGET_POWER_ON); CheetahApi.ch_sleep_ms(100); // Set the bitrate. bitrate = CheetahApi.ch_spi_bitrate(handle, bitrate); Console.Write("Bitrate set to {0:d} kHz\n", bitrate); Console.Out.Flush(); _blast(handle, length); // Close and exit. CheetahApi.ch_close(handle); return; }
/*====================================================================== | MAIN PROGRAM | =====================================================================*/ public static void Main(String[] args) { int handle = 0; int port = 0; // open port 0 by default int bitrate = 0; int mode = 0; ushort arg5 = 0; ushort arg6 = 0; String command = ""; String binfilepath = ""; Byte[] data; if (args.Length < 6) { print_usage(); Environment.Exit(1); } port = Convert.ToInt32(args[0]); bitrate = Convert.ToInt32(args[1]); mode = Convert.ToInt32(args[2]); command = args[3]; arg5 = Convert.ToUInt16(args[4]); arg6 = Convert.ToUInt16(args[5]); if (args.Length > 6) { binfilepath = args[6]; } handle = CheetahApi.ch_open(port); if (handle <= 0) { Console.Error.Write( "Unable to open Cheetah device on port {0:d}\n", port); Console.Error.Write("Error code = {0:d} ({1:s})\n", handle, CheetahApi.ch_status_string(handle)); Environment.Exit(1); } Console.Write("Opened Cheetah device on port {0:d}\n", port); Console.Write("Host interface is {0:s}\n", ((CheetahApi.ch_host_ifce_speed(handle)) != 0) ? "high speed" : "full speed"); // Ensure that the SPI subsystem is configured. CheetahApi.ch_spi_configure( handle, (TotalPhase.CheetahSpiPolarity)(mode >> 1), (TotalPhase.CheetahSpiPhase)(mode & 1), CheetahSpiBitorder.CH_SPI_BITORDER_MSB, 0x0); Console.Write("SPI configuration set to mode {0:d}, {1:s} shift, " + "SS[2:0] active low\n", mode, "MSB"); Console.Out.Flush(); // Power off the target using the Cheetah adapter's power supply. CheetahApi.ch_target_power(handle, CheetahApi.CH_TARGET_POWER_OFF); CheetahApi.ch_sleep_ms(100); // Power the target using the Cheetah adapter's power supply. CheetahApi.ch_target_power(handle, CheetahApi.CH_TARGET_POWER_ON); CheetahApi.ch_sleep_ms(100); // Set the bitrate. bitrate = CheetahApi.ch_spi_bitrate(handle, bitrate); Console.Write("Bitrate set to {0:d} kHz\n", bitrate); Console.Out.Flush(); // Determine which command transaction type was requested. int commandID = -1; if (command == "read") { commandID = COMMAND_READ; } else if (command == "write") { commandID = COMMAND_WRITE; } else if (command == "erase") { commandID = COMMAND_ERASE; } else if (command == "verify") { commandID = COMMAND_VERIFY; } else { Console.Write("Unknown option: {0:s}\n", command); Console.Write("Valid options are: read, write, erase, " + "and verify\n\n"); print_usage(); Environment.Exit(1); } // Execute the appropriate command. switch (commandID) { case COMMAND_READ: _read(handle, arg5, arg6, out data); break; case COMMAND_WRITE: _write(handle, arg5, binfilepath); break; case COMMAND_ERASE: _erase(handle, arg5, arg6); break; case COMMAND_VERIFY: _verify(handle, arg5 * 1024, arg6 * 1024); break; } // Power off the target using the Cheetah adapter's power supply. CheetahApi.ch_target_power(handle, CheetahApi.CH_TARGET_POWER_OFF); // Close the device. CheetahApi.ch_close(handle); return; }
/*====================================================================== | FUNCTIONS | =====================================================================*/ static void _blast_async(int handle, int txnlen, int iter) { double elapsed = 0; byte[] noresult = new byte[1]; // Make a simple queue to just assert OE. CheetahApi.ch_spi_queue_clear(handle); CheetahApi.ch_spi_queue_oe(handle, (byte)1); CheetahApi.ch_spi_batch_shift(handle, 0, noresult); // Queue the batch which is a sequence of SPI packets // (back-to-back) each of length 4. CheetahApi.ch_spi_queue_clear(handle); int i; int count = 0; byte[] data_out = new byte[4]; for (i = 0; i < txnlen; ++i) { CheetahApi.ch_spi_queue_ss(handle, 0x1); data_out[0] = (byte)((count >> 24) & 0xff); data_out[1] = (byte)((count >> 16) & 0xff); data_out[2] = (byte)((count >> 8) & 0xff); data_out[3] = (byte)((count >> 0) & 0xff); ++count; CheetahApi.ch_spi_queue_array(handle, 4, data_out); CheetahApi.ch_spi_queue_ss(handle, 0x0); } ulong start = _timeMillis(); // First, submit first batch CheetahApi.ch_spi_async_submit(handle); int n, ret; for (n = 0; n < iter - 1; ++n) { // Submit another batch, while the previous one is in // progress. The application may even clear the current // batch queue and queue a different set of SPI // transactions before submitting this batch // asynchronously. CheetahApi.ch_spi_async_submit(handle); // The application can now perform some other functions // while the Cheetah is both finishing the previous batch // and shifting the current batch as well. In order to // keep the Cheetah's pipe full, this entire loop must // complete AND another batch must be submitted // before the current batch completes. CheetahApi.ch_sleep_ms(25); // Collect the previous batch ret = CheetahApi.ch_spi_async_collect(handle, 0, noresult); elapsed = ((double)(_timeMillis() - start)) / 1000; Console.Write("collected batch #{0:d3} in {1:f2} seconds\n", n + 1, elapsed); if (ret < 0) { Console.Write("status error: {0:s}\n", CheetahApi.ch_status_string(ret)); } Console.Out.Flush(); start = _timeMillis(); // The current batch is now shifting out on the SPI // interface. The application can again do some more tasks // here but this entire loop must finish so that a new // batch is armed before the current batch completes. CheetahApi.ch_sleep_ms(25); } // Collect batch the last batch ret = CheetahApi.ch_spi_async_collect(handle, 0, noresult); elapsed = ((double)(_timeMillis() - start)) / 1000; Console.Write("collected batch #{0:d3} in {1:f2} seconds\n", n + 1, elapsed); if (ret < 0) { Console.Write("status error: {0:s}\n", CheetahApi.ch_status_string(ret)); } Console.Out.Flush(); }
/*========================================================================= | MAIN PROGRAM | ========================================================================*/ public static void Main(String[] args) { int handle = 0; int port = 0; // open port 0 by default int bitrate = 0; int mode = 0; ushort addr = 0; ushort length = 0; String command = ""; if (args.Length < 6) { print_usage(); Environment.Exit(1); } port = Convert.ToInt32(args[0]); bitrate = Convert.ToInt32(args[1]); mode = Convert.ToInt32(args[2]); command = args[3]; addr = Convert.ToUInt16(args[4]); length = Convert.ToUInt16(args[5]); // Open the device handle = CheetahApi.ch_open(port); if (handle <= 0) { Console.Error.Write( "Unable to open Cheetah device on port {0:d}\n", port); Console.Error.Write("Error code = {0:d} ({1:s})\n", handle, CheetahApi.ch_status_string(handle)); Environment.Exit(1); } Console.Write("Opened Cheetah device on port {0:d}\n", port); Console.Write("Host interface is {0:s}\n", ((CheetahApi.ch_host_ifce_speed(handle)) != 0) ? "high speed" : "full speed"); // Ensure that the SPI subsystem is configured CheetahApi.ch_spi_configure( handle, (TotalPhase.CheetahSpiPolarity)(mode >> 1), (TotalPhase.CheetahSpiPhase)(mode & 1), CheetahSpiBitorder.CH_SPI_BITORDER_MSB, 0x0); Console.Write("SPI configuration set to mode {0:d}, {1:s} shift, " + "SS[2:0] active low\n", mode, "MSB"); Console.Out.Flush(); // Power the target using the Cheetah adapter's power supply CheetahApi.ch_target_power(handle, CheetahApi.CH_TARGET_POWER_ON); CheetahApi.ch_sleep_ms(100); // Set the bitrate bitrate = CheetahApi.ch_spi_bitrate(handle, bitrate); Console.Write("Bitrate set to {0:d} kHz\n", bitrate); Console.Out.Flush(); byte[] noresult = new byte[1]; // Shift a dummy byte to clear the EEPROM state CheetahApi.ch_spi_queue_clear(handle); CheetahApi.ch_spi_queue_oe(handle, 1); CheetahApi.ch_spi_queue_ss(handle, 0x1); CheetahApi.ch_spi_queue_byte(handle, 1, 0x00); CheetahApi.ch_spi_queue_ss(handle, 0); CheetahApi.ch_spi_batch_shift(handle, 0, noresult); // Perform the requested operation if (command == "write") { _writeMemory(handle, addr, length, 0); Console.Write("Wrote to EEPROM\n"); } else if (command == "read") { _readMemory(handle, addr, length); } else if (command == "zero") { _writeMemory(handle, addr, length, 1); Console.Write("Zeroed EEPROM\n"); } else { Console.Write("unknown command: {0:s}\n", command); } // Close and exit CheetahApi.ch_close(handle); return; }