// UNCLASSIFIED /*------------------------------------------------------------------------- RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON COMPANY. THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. GOVERNMENT. UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. -------------------------------------------------------------------------*/ using System; using System.IO.Ports; using System.Threading; using NLog; using Raytheon.Common; namespace Raytheon.Instruments { /// /// A class the provides an interface for read/write to registers on an FPGA that implement PC Node communication /// public class CommFpgaPcNode2x : IFpgaComm, IDisposable { #region PrivateClassMembers private SerialPort _serialPort; private readonly uint _pcNodeAddress; private byte[] _readBuf; private static object _syncObj = new Object(); private uint _delayBeforeReadMs; private SelfTestResult _selfTestResult; private State _state; private string _name; /// /// NLog logger /// private readonly ILogger _logger; /// /// Raytheon configuration /// private readonly IConfigurationManager _configurationManager; private readonly IConfiguration _configuration; #endregion #region PrivateFuctions /// /// The Finalizer /// ~CommFpgaPcNode2x() { Dispose(false); } /// /// Builds a command in the PC node format /// /// The address to write to /// The data to write /// True is the command is to perform a read, false to perform a write /// The formatted command private byte[] BuildCommand(uint address, uint data, bool isForRead) { byte[] command = new byte[9]; byte type = 0; // 2 bit shift per the spec, regardless of its a read or write address = address >> 2; if (isForRead == true) { type = 0xf1; // 2 bit shift per the spec for the read (this would be the PC node address) data = data >> 2; } else { type = 0xf0; } command[0] = type; byte[] addressBytes = BitConverter.GetBytes(address); Array.Copy(addressBytes, 0, command, 1, addressBytes.Length); byte[] dataBytes = BitConverter.GetBytes(data); Array.Copy(dataBytes, 0, command, 5, dataBytes.Length); return command; } /// /// Reads the serial port until it is empty /// private void ClearBuffer() { lock (_syncObj) { bool isClearComplete = false; while (isClearComplete == false) { try { int numBytesRead = _serialPort.Read(_readBuf, 0, _readBuf.Length); if (numBytesRead < _readBuf.Length) { isClearComplete = true; } } catch (Exception) { //expected if buffer is already cleared isClearComplete = true; } } } } /// /// Dispose of this object /// /// protected virtual void Dispose(bool disposing) { try { if (disposing) { if (_state == State.Ready) { _serialPort.Dispose(); _state = State.Uninitialized; } } } catch (Exception) { try { //ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace); } catch (Exception) { //Do not rethrow. Exception from error logger that has already been garbage collected } } } #endregion #region PublicFuctions /// /// CommFpgaPcNode2x factory constructor /// /// /// public CommFpgaPcNode2x(string deviceName, IConfigurationManager configurationManager, ILogger logger) { Name = deviceName; _logger = logger; _configurationManager = configurationManager; _configuration = _configurationManager.GetConfiguration(Name); _pcNodeAddress = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "PCNodeAddress", 0); string comPortName = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "ComPortName", "COM1"); int baudRate = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "BaudRate", 115200); Parity parity = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "Parity", Parity.None); int dataBits = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "DataBits", 8); StopBits stopBits = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "StopBits", StopBits.None); _delayBeforeReadMs = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "DelayBeforeReadMs", 0); _pcNodeAddress = _configuration.GetConfigurationValue("CommFpgaPcNode2x", "PcNodeAddress", 0); const int READ_BUF_SIZE = 100; try { _serialPort = new SerialPort(comPortName, baudRate, parity, dataBits, stopBits) { ReadTimeout = 100 }; } catch (Exception ex) { _logger.Error(ex); if (_serialPort.IsOpen == true) { _serialPort.Close(); } throw; } _readBuf = new byte[READ_BUF_SIZE]; _selfTestResult = SelfTestResult.Unknown; _state = State.Uninitialized; } /// /// The constructor which opens up a serial port /// /// The address of the PC node /// The port name. "Com1' for example /// The num of ms to wait before a read /// The baud rate /// The parity /// Number of data bits /// Number of Stop Bits public CommFpgaPcNode2x(string name, uint pcNodeAddress, string comPortName, uint delayBeforeReadMs, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One) { const int READ_BUF_SIZE = 100; try { _name = name; _logger = LogManager.GetCurrentClassLogger(); _serialPort = new SerialPort(comPortName, baudRate, parity, dataBits, stopBits); _serialPort.ReadTimeout = 100; _delayBeforeReadMs = delayBeforeReadMs; _pcNodeAddress = pcNodeAddress; _readBuf = new byte[READ_BUF_SIZE]; _selfTestResult = SelfTestResult.Unknown; _state = State.Uninitialized; } catch (Exception) { if (_serialPort.IsOpen == true) { _serialPort.Close(); } throw; } } /// /// /// /// public bool ClearErrors() { return false; } /// /// /// public string DetailedStatus { get { return "This is a FPGA PC Node called " + _name; } } /// /// /// public bool DisplayEnabled { get { return false; } set { throw new NotImplementedException(); } } /// /// Dispose of this object /// public void Dispose() { try { Dispose(true); GC.SuppressFinalize(this); } catch (Exception) { try { //ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace); } catch (Exception) { //Do not rethrow. Exception from error logger that has already been garbage collected } } } /// /// /// public bool FrontPanelEnabled { get { return false; } set { throw new NotImplementedException(); } } /// /// /// public InstrumentMetadata Info { get { throw new NotImplementedException(); } } /// /// /// public void Initialize() { Initialize(string.Empty); } /// /// /// /// public void Initialize(string fpga) { lock (_syncObj) { if (_state == State.Uninitialized) { if (_serialPort.IsOpen == false) { _serialPort.Open(); } ClearBuffer(); _state = State.Ready; } else { throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString() + " on card " + _name); } } } /// /// /// public string Name { get { return _name; } set { _name = value; } } /// /// /// /// public SelfTestResult PerformSelfTest() { _selfTestResult = SelfTestResult.Unknown; return _selfTestResult; } /// /// /// /// /// /// public uint Read(string fpga, uint address) { const int NUM_BYTES_EXPECTED_ON_READ = 9; int numBytesRead = 0; // lock up the FPGA resource lock (_syncObj) { byte[] command = BuildCommand(address, _pcNodeAddress, true); for (uint numTries = 0; numTries < 2; numTries++) { _serialPort.Write(command, 0, command.Length); Array.Clear(_readBuf, 0, _readBuf.Length); Thread.Sleep((int)_delayBeforeReadMs); numBytesRead = _serialPort.Read(_readBuf, 0, _readBuf.Length); int numBytesExpectedToRead = NUM_BYTES_EXPECTED_ON_READ; if (numBytesRead != numBytesExpectedToRead) { //put out diagnostic byte[] errorDiag = new byte[numBytesRead]; Array.Copy(_readBuf, errorDiag, numBytesRead); string errMessage = Util.ByteArrayToHexString(errorDiag); //ErrorLogger.Instance().Write("Read got wrong size. expected to read: " + numBytesExpectedToRead.ToString() + ", read: " + numBytesRead.ToString() + " The data received is: " + errMessage); if (numTries != 0) { //second time - not a good read throw new Exception("expected to read: " + numBytesExpectedToRead.ToString() + ", read: " + numBytesRead.ToString()); } } else { break;//continue processing } } // Grab the last 4 bytes uint data = BitConverter.ToUInt32(_readBuf, numBytesRead - 4); return data; } } /// /// /// /// /// /// /// /// public void ReadBlock(string fpgaName, uint address, uint numberOfWordsToRead, bool shallWeIncrementAddress, ref uint[] dataRead) { // lock up the FPGA resource lock (_syncObj) { throw new Exception("Not Implemented"); } } /// /// /// public void Reset() { // lock up the FPGA resource lock (_syncObj) { ClearBuffer(); } } /// /// /// public SelfTestResult SelfTestResult { get { return _selfTestResult; } } /// /// /// public void Shutdown() { // lock up the FPGA resource lock (_syncObj) { if (_state == State.Ready) { _serialPort.Dispose(); _state = State.Uninitialized; } } } /// /// /// public State Status { get { return _state; } } /// /// /// /// /// /// public void Write(string fpga, uint address, uint data) { byte[] command = BuildCommand(address, data, false); // lock up the FPGA resource lock (_syncObj) { _serialPort.Write(command, 0, command.Length); } } /// /// /// /// /// /// /// /// public void WriteBlock(string fpgaName, uint address, uint numberOfWordsToWrite, uint[] data, bool shallWeIncrementAddress) { // lock up the FPGA resource lock (_syncObj) { throw new Exception("Not Implemented"); } } /// /// Loads firmware /// /// public void LoadFirmware(string fpgaName) { Initialize(fpgaName); } #endregion } }