Command line register writer for CyUSB PLL evaluation boards by rbrennan
The attached command line application takes a hex register value as an argument, connects to an ADF4xxx evaluation board, writes the register values, disconnects from the evaluation board, and ends.
All the files in the zip archive must be keep together.
The application is called from a command line by:
>adf4xxx_com ABcd1234
where, ABcd1234 is the hex register value to be written. Must be less than or equal to FFFFFFFF.
RE: Command line register writer for CyUSB PLL evaluation boards by rbrennan:
using System;
using System.Text;
using CyUSB;
namespace adf4xxx_com
{
class Program
{
static void Main(string[] args)
{
if (args[0] != null)
{
USBDeviceList usbDevices;
CyFX2Device connectedDevice;
bool FirmwareLoaded;
try
{
uint reg = Convert.ToUInt32(args[0], 16);
#region USB stuff
FirmwareLoaded = false;
int PID = 0xB40D;
int PID2 = 0xB403;
usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
connectedDevice = usbDevices[0x0456, PID] as CyFX2Device;
if (connectedDevice != null)
FirmwareLoaded = connectedDevice.LoadExternalRam("adf4xxx_usb_fw_2_0.hex");
else
{
connectedDevice = usbDevices[0x0456, PID2] as CyFX2Device;
if (connectedDevice != null)
FirmwareLoaded = connectedDevice.LoadExternalRam("adf4xxx_usb_fw_1_0.hex");
}
if (FirmwareLoaded)
{
connectedDevice.ControlEndPt.Target = CyConst.TGT_DEVICE;
connectedDevice.ControlEndPt.ReqType = CyConst.REQ_VENDOR;
connectedDevice.ControlEndPt.Direction = CyConst.DIR_TO_DEVICE;
connectedDevice.ControlEndPt.ReqCode = 0xDD; // DD references the function in the firmware ADF_uwave_2.hex to write to the chip
connectedDevice.ControlEndPt.Value = 0;
connectedDevice.ControlEndPt.Index = 0;
Console.WriteLine("Working...");
}
else
{
Console.WriteLine("Firmware failed to load.");
Console.ReadKey();
}
#endregion
Console.WriteLine("Writing 0x{0:X}...", reg);
WriteToDevice(reg, connectedDevice);
if (connectedDevice != null)
{
connectedDevice.Reset();
connectedDevice.Dispose();
}
}
catch
{
Console.WriteLine("Failure.");
Console.WriteLine("Argument must be hex value <= FFFFFFFF.");
Console.WriteLine("Ensure correct USB device is connected. Possbily disconnect other USB devices.");
}
Console.WriteLine("Copyright 2013 - Analog Devices - adf4xxx_com v1");
}
else
{
Console.WriteLine("There was no argument. This application must be passed one hex argument <= FFFFFFFF.");
Console.ReadKey();
}
}
static private void WriteToDevice(uint data, CyFX2Device connectedDevice)
{
uint[] toWrite = new uint[1];
int buffer_length = 5;
byte[] buffer = new byte[5]; // (Bits per register / 8) + 1
if (connectedDevice != null)
{
for (int i = 0; i < 4; i++)
buffer[i] = (byte)(data >> (i * 8));
buffer[4] = 32;
buffer_length = 5;
if (connectedDevice.ControlEndPt.XferData(ref buffer, ref buffer_length))
Console.WriteLine("Success.");
else
Console.WriteLine("XferData failed.");
}
else
{
Console.WriteLine("Writing failed. No device connected.");
Console.ReadKey();
}
}
}
}