static (SerialPort, SerialPort) tryGetSerialFlasher(string port) { if (port == null) { Console.WriteLine("Port cannot be null here!"); Environment.Exit(1); } SerialPort flash = new SerialPort( portName: port, parity: Parity.None, baudRate: 9600, stopBits: StopBits.One, dataBits: 8 ); tryWakeGenericDevice(flash); // Reset var device = tryGetICEDevice(IceSerialInfo.tryFindDevicePort()); // Wait for serial ports to reset Thread.Sleep(2000); Console.WriteLine("Resetting serial devices..."); // Reset FPGA IceSerial.resetFpga(device); IceSerial.runFpga(device); // Simply wait Console.WriteLine("Waiting for CPU to wake..."); Thread.Sleep(500); return(flash, device); }
public static void doMemtest(SerialPort flash, SerialPort device) { Console.WriteLine("Beginning tests!"); sbuf[3] = (byte)((len >> 0) & 0xff); sbuf[2] = (byte)((len >> 8) & 0xff); sbuf[1] = (byte)((len >> 16) & 0xff); sbuf[0] = (byte)((len >> 24) & 0xff); for (byte i = 0; i <= 0xFF; i++) { flash.DiscardInBuffer(); for (int j = 0; j < len; j++) { sbuf[j + 4] = i; } transmit(flash, len + 8); bool fail = false; for (int j = 0; j < len; j++) { byte a = (byte)flash.ReadByte(); if (a != i) { Console.WriteLine("Test 0x{0:X2} failed. Got 0x{1:X2} instead.", i, a); fail = true; break; } } if (!fail) { Console.WriteLine("Test 0x{0:X2} ok.", i); } IceSerial.resetFpga(device); IceSerial.runFpga(device); Thread.Sleep(500); flash.DiscardInBuffer(); } }
static SerialPort tryGetICEDevice(string foundPort) { if (foundPort == null) { Console.WriteLine($"Error: IceWerx device not found."); Environment.Exit(1); } // Flash SerialPort device = new SerialPort( portName: foundPort, parity: Parity.None, baudRate: 19200, stopBits: StopBits.Two, dataBits: 8 ); tryWakeGenericDevice(device); IceSerial.getVersion(device); return(device); }
static void Main(string[] args) { var argport = new Option <string>( aliases: new string[] { "--port", "-p" }, description: "Set device name (can be null)", getDefaultValue: () => { return(null); }); var argfile = new Argument <FileInfo>( name: "file", description: "Binary file to upload", getDefaultValue: () => null); var argverify = new Option <bool>( aliases: new string[] { "--verify", "-v" }, description: "Verify after upload"); var argtty = new Option <bool>( aliases: new string[] { "--tty", "-i" }, description: "Enter tty mode after flashing"); var cmdlist = new Command("list", "Enumerate serial port devices"); var cmdrun = new Command("run", "Run the FPGA device"); cmdrun.AddOption(argport); var cmdreset = new Command("reset", "Reset (not erase) the FPGA device"); cmdreset.AddOption(argport); var cmdprogram = new Command("program", "Upload binary bitstream to the FPGA"); cmdprogram.AddOption(argport); cmdprogram.AddOption(argverify); cmdprogram.AddArgument(argfile); var cmdflashboot = new Command("flash", "Upload program to bootstrap using the serial port of the uploader"); cmdflashboot.AddOption(argport); cmdflashboot.AddOption(argverify); cmdflashboot.AddOption(argtty); cmdflashboot.AddArgument(argfile); var cmdmemtest = new Command("memtest", "Runs the UART memtest"); cmdmemtest.AddOption(argport); var root = new RootCommand { cmdlist, cmdrun, cmdreset, cmdprogram, cmdflashboot, cmdmemtest }; cmdlist.Handler = CommandHandler.Create <string, FileInfo>( (port, file) => { IceSerialInfo.enumerate(); }); cmdrun.Handler = CommandHandler.Create <string>( (port) => { var device = tryGetICEDevice(port == null ? IceSerialInfo.tryFindDevicePort() : port); IceSerial.runFpga(device); Console.WriteLine("Running..."); }); cmdreset.Handler = CommandHandler.Create <string>( (port) => { var device = tryGetICEDevice(port == null ? IceSerialInfo.tryFindDevicePort() : port); IceSerial.resetFpga(device); Console.WriteLine("Done."); }); cmdprogram.Handler = CommandHandler.Create <bool, string, FileInfo>( (verify, port, file) => { var device = tryGetICEDevice(port == null ? IceSerialInfo.tryFindDevicePort() : port); IceSerial.doUpload(device, file, verify); }); cmdflashboot.Handler = CommandHandler.Create <bool, string, FileInfo, bool>( (verify, port, file, tty) => { var(flash, device) = tryGetSerialFlasher(port); BootSerial.doUpload(flash, file, verify); if (tty) { enterTtyMode(flash); } flash.Close(); device.Close(); }); cmdmemtest.Handler = CommandHandler.Create <string>( (port) => { var(flash, device) = tryGetSerialFlasher(port); BootSerial.doMemtest(flash, device); flash.Close(); device.Close(); }); root.Invoke(args); Environment.Exit(0); }