/*====================================================================== | 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(); }
//suppose this is the block erase function,sector size is 4k ,block size is 32k/64k //command d8 is block erase command static int _erase(int handle, int sector, int num) { byte[] noresult = new byte[1]; // Reset the state of the bus. CheetahApi.ch_spi_queue_clear(handle); CheetahApi.ch_spi_queue_ss(handle, 0); CheetahApi.ch_spi_queue_oe(handle, 0); CheetahApi.ch_spi_batch_shift(handle, 0, noresult); int eraseAll = 0; if (sector == 0 && num == 2048) { eraseAll = 1; } String str; if (eraseAll != 0) { str = "Bulk"; } else { str = "Block"; } while (num != 0) { // Make sure the sector is a valid one. if (sector < 0 || sector > 2047) { break; } int addr = sector << 12; if (eraseAll == 0) { Console.Write("Erasing sector {0:d2} (bytes 0x{1:x6} " + "to 0x{2:x6})...\n", sector, addr, addr | 0xfff); Console.Out.Flush(); } else { Console.Write("Erasing entire device...\n"); Console.Out.Flush(); } // Start the erase sequence. CheetahApi.ch_spi_queue_clear(handle); CheetahApi.ch_spi_queue_oe(handle, 1); // Queue the write enable instruction for the flash. CheetahApi.ch_spi_queue_ss(handle, 0x1); CheetahApi.ch_spi_queue_byte(handle, 1, 0x06); CheetahApi.ch_spi_queue_ss(handle, 0); CheetahApi.ch_spi_queue_ss(handle, 0x1); if (eraseAll == 0) { // Queue the sector erase command. CheetahApi.ch_spi_queue_byte(handle, 1, 0x20); CheetahApi.ch_spi_queue_byte(handle, 1, (byte)((addr >> 16) & 0xff)); CheetahApi.ch_spi_queue_byte(handle, 1, (byte)((addr >> 8) & 0xff)); CheetahApi.ch_spi_queue_byte(handle, 1, (byte)((addr >> 0) & 0xff)); } else { // Queue the chip erase command. CheetahApi.ch_spi_queue_byte(handle, 1, 0xc7); } CheetahApi.ch_spi_queue_ss(handle, 0); // Shift the queued commands. (Don't need the data back.) int batch = CheetahApi.ch_spi_batch_length(handle); int count = CheetahApi.ch_spi_batch_shift(handle, 0, noresult); if (count != batch) { Console.Write("Expected {0:d} bytes but only received " + "{1:d} bytes\n", batch, count); return(1); } ulong start = _timeMicroseconds(); CheetahApi.ch_spi_queue_clear(handle); CheetahApi.ch_spi_queue_ss(handle, 0x1); CheetahApi.ch_spi_queue_byte(handle, 1, 0x05); CheetahApi.ch_spi_queue_byte(handle, 1, 0x00); CheetahApi.ch_spi_queue_ss(handle, 0); while (true) { CheetahApi.ch_sleep_ms(10); byte[] status_in = new byte[2]; CheetahApi.ch_spi_batch_shift(handle, 2, status_in); if ((status_in[1] & 0x01) == 0) { break; } } ulong end = _timeMicroseconds(); Console.Write("{0:s} erase took {1:f3} seconds\n", str, (double)(end - start) / 1000000); Console.Out.Flush(); if (eraseAll == 0) { ++sector; --num; } else { sector += 64; num = 0; } } // Reset the state of the bus. CheetahApi.ch_spi_queue_clear(handle); CheetahApi.ch_spi_queue_oe(handle, 0); CheetahApi.ch_spi_batch_shift(handle, 0, noresult); return(0); }
/*========================================================================= | 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; }