// 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 FpgaMeasurementInstrumentsLib; using NLog; using Raytheon.Common; namespace Raytheon.Instruments { /// /// A class that implements the Teradyne HSS Sub System Card with the HSI Library /// public unsafe class CommFpgaHssubCardSsHsi : IFpgaComm { #region PrivateClassMembers private enum Mode { PRIMARY_CONTROL, SECONDARY_CONTROL }; private readonly string _cardAddress; private readonly uint _startingOffset; private readonly string _cardFirmwareFile; private uint _cardHandle; private static object _syncObj = new Object(); private readonly Mode __mode; private SelfTestResult _selfTestResult; private State _state; private string _name; private readonly ILogger _logger; private readonly IConfigurationManager _configurationManager; private readonly IConfiguration _configuration; #endregion #region PrivateFuctions /// /// The finalizer. /// ~CommFpgaHssubCardSsHsi() { Dispose(false); } /// /// /// private void LoadFirmware() { // lock up the FPGA resource lock (_syncObj) { ushort customerId = 0; ushort appId = 0; uint revId = 0; int ret = HssubNativeMethods.terHsi_Firmware_Load(_cardHandle, HssubNativeMethods.TERHSI_FPGA_TEST_DEFINED, _cardFirmwareFile, ref customerId, ref appId, ref revId); if ((uint)ret == 0xbffa4442) { // expected, load still seems to work } else if (ret != 0) { string errorStr = HssUtilSs.BuildErrorString(_cardHandle, ret, _name); throw new Exception("terHsi_Firmware_Load() returned an error(" + ret + ")" + ": " + errorStr + " on card " + _name); } } } /// /// /// private void SelfTest() { // loop through each card and command self test /*short testResults = -1; StringBuilder errorStrTemp = new StringBuilder(512); int ret = HssubNativeMethods.terHss_self_test(_chassisHandle, ref testResults, errorStrTemp); if (ret != 0) { string errorStr = HssUtilTs.BuildErrorString(_chassisHandle, ret, _chassisLogicalName); throw new Exception("terHss_self_test returned an error(" + ret + ")" + ": " + errorStr); } else if (testResults != 0) { throw new Exception("HSSub Self Test returned an error: " + testResults.ToString() + ": " + errorStrTemp.ToString()); }*/ } /// /// Dispose of this object's resources. /// /// True = currently disposing, False = not disposing. protected virtual void Dispose(bool disposing) { if (disposing) { lock (_syncObj) { if (_state == State.Ready) { int ret = HssubNativeMethods.terHsi_close(_cardHandle); _state = State.Uninitialized; } } } } #endregion #region PublicFuctions /// /// CommFpgaHssubCardSsHsi factory constructor /// /// /// public CommFpgaHssubCardSsHsi(string deviceName, IConfigurationManager configurationManager) { Name = deviceName; _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}"); _configurationManager = configurationManager; _configuration = _configurationManager.GetConfiguration(Name); _cardAddress = _configuration.GetConfigurationValue("CommFpgaHssubCardSsHsi", "CardAddress", "127.0.0.1"); _startingOffset = _configuration.GetConfigurationValue("CommFpgaHssubCardSsHsi", "StartingOffset", 0); _cardFirmwareFile = _configuration.GetConfigurationValue("CommFpgaHssubCardSsHsi", "CardFirmwareFile", ""); _cardHandle = 0; _selfTestResult = SelfTestResult.Unknown; _state = State.Uninitialized; __mode = Mode.PRIMARY_CONTROL; } /// /// /// /// /// /// /// /// public CommFpgaHssubCardSsHsi(string deviceName, string cardAddress, uint startingOffset, string cardFirmwareFile) { _name = deviceName; _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}"); _cardAddress = cardAddress; _startingOffset = startingOffset; _cardFirmwareFile = cardFirmwareFile; _cardHandle = 0; _selfTestResult = SelfTestResult.Unknown; _state = State.Uninitialized; __mode = Mode.PRIMARY_CONTROL; } /// /// Overloaded constructor for a secondary hsi control. (Firmware load done elsewhere, device reset done elsewhere) /// Using this constructor will allow the hsot access to the HSS, but it will not load firmware or reset the device /// /// /// /// public CommFpgaHssubCardSsHsi(string deviceName, string cardAddress, uint startingOffset) { _name = deviceName; _logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}"); _cardAddress = cardAddress; _startingOffset = startingOffset; _cardFirmwareFile = null; _cardHandle = 0; _selfTestResult = SelfTestResult.Unknown; _state = State.Uninitialized; __mode = Mode.SECONDARY_CONTROL; } /// /// /// /// public bool ClearErrors() { return false; } /// /// /// public string DetailedStatus { get { return "This is a FPGA HSS HSI called " + _name; } } /// /// /// public bool DisplayEnabled { get { return false; } set { throw new NotImplementedException(); } } /// /// Dispose of this object's resources. /// public void Dispose() { // lock up the FPGA resource lock (_syncObj) { Dispose(true); GC.SuppressFinalize(this); } } /// /// /// 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 fpgaName) { lock (_syncObj) { if (_state == State.Uninitialized) { int ret = HssubNativeMethods.terHsi_init(_cardAddress, 0, 0, ref _cardHandle); if (ret != 0) { string errorStr = HssUtilSs.BuildErrorString(0, ret, _name); throw new Exception("terHsi_init returned an error(" + ret + ")" + ": " + errorStr + " on card " + _name + " on card " + _name); } // reset it and load firmware if this is the primary controller of the HSS if (__mode == Mode.PRIMARY_CONTROL) { Reset(); LoadFirmware(); } _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 fpgaName, uint address) { // lock up the FPGA resource lock (_syncObj) { uint dataRead = 0; int ret = HssubNativeMethods.terHsi_LB_Read32(_cardHandle, _startingOffset + address, ref dataRead); if (ret != 0) { string errorStr = HssUtilSs.BuildErrorString(_cardHandle, ret, _name); throw new Exception("terHsi_LB_Read32() returned an error(" + ret + ")" + ": " + errorStr + " on card " + _name); } return dataRead; } } /// /// /// /// /// /// /// public void ReadBlock(string fpgaName, uint address, uint numberOfWordsToRead, bool shallWeIncrementAddress, ref uint[] dataRead) { // lock up the FPGA resource lock (_syncObj) { throw new NotImplementedException(); } } /// /// /// public void Reset() { lock (_syncObj) { int ret = HssubNativeMethods.terHsi_reset(_cardHandle); if (ret != 0) { string errorStr = HssUtilSs.BuildErrorString(_cardHandle, ret, _name); throw new Exception("terHsi_reset() returned an error(" + ret + ")" + ": " + errorStr + " on card " + _name); } } } /// /// /// public SelfTestResult SelfTestResult { get { return _selfTestResult; } } /// /// /// public void Shutdown() { // lock up the FPGA resource lock (_syncObj) { string errorMsg = ""; bool wasThereAnError = false; if (_state == State.Ready) { int ret = 0; // reset it if this is the primary controller of the HSS if (__mode == Mode.PRIMARY_CONTROL) { ret = HssubNativeMethods.terHsi_reset(_cardHandle); if (ret != 0) { wasThereAnError = true; errorMsg += "terHsi_reset returned error code: " + ret.ToString() + ". "; } } ret = HssubNativeMethods.terHsi_close(_cardHandle); if (ret != 0) { wasThereAnError = true; errorMsg += "terHsi_close returned error code: " + ret.ToString(); } _state = State.Uninitialized; if (wasThereAnError == true) { throw new Exception(errorMsg); } } } } /// /// /// public State Status { get { return _state; } } /// /// /// /// /// /// public void Write(string fpgaName, uint address, uint value) { // lock up the FPGA resource lock (_syncObj) { int ret = HssubNativeMethods.terHsi_LB_Write32(_cardHandle, _startingOffset + address, value); if (ret != 0) { string errorStr = HssUtilSs.BuildErrorString(_cardHandle, ret, _name); throw new Exception("terHsi_LB_Write32() returned an error(" + ret + ")" + ": " + errorStr + " on card " + _name); } } } /// /// /// /// /// /// /// /// public void WriteBlock(string fpgaName, uint address, uint numberOfWordsToWrite, uint[] data, bool shallWeIncrementAddress) { // lock up the FPGA resource lock (_syncObj) { if (shallWeIncrementAddress == false) { throw new Exception("terHsi_LB_WriteBlock32() does not support shallWeIncrementAddress = false command on card " + _name); } else { int ret = HssubNativeMethods.terHsi_LB_WriteBlock32(_cardHandle, _startingOffset + address, numberOfWordsToWrite, data); if (ret != 0) { string errorStr = HssUtilSs.BuildErrorString(_cardHandle, ret, _name); throw new Exception("terHsi_LB_WriteBlock32() returned an error(" + ret + ")" + ": " + errorStr + " on card " + _name); } } } } /// /// Loads firmware /// /// public void LoadFirmware(string fpgaName) { LoadFirmware(); } #endregion } }