/// <summary> /// Read word from device via SMBus on EV2300. /// </summary> /// <param name="SMBusCmd">Device command to read.</param> /// <param name="SMBusWord">Read back device word.</param> /// <param name="targetAddr">Device target address.</param> /// <returns>EV2300 error code.</returns> public EV23KError ReadSMBusWord(short SMBusCmd, out short SMBusWord, short targetAddr) { try { SMBusWord = 0; short nWord = 0; if (!isPresent) { return(EV23KError.NoUSB); } EV23KError err = (EV23KError)EV23KBoard.ReadSMBusWord(SMBusCmd, ref nWord, targetAddr); if (err != EV23KError.NoError) { return(err); } SMBusWord = nWord; return(EV23KError.NoError); } catch (Exception ex) { throw new ArgumentException(ex.Message); } }
/// <summary> /// Initialize when main window was loaded. /// </summary> /// <param name="sender">Not used.</param> /// <param name="e">Not used.</param> private void MainWindow_Loaded(object sender, RoutedEventArgs e) { CfgCycleCellCount.Text = Properties.Settings.Default.CellCount; CfgCycleTaperCurr.Text = Properties.Settings.Default.TaperCurrent; CfgCycleTermVolt.Text = Properties.Settings.Default.TerminationVoltage; CfgCycleChargeRelaxHours.Text = Properties.Settings.Default.ChargeRelaxHours; CfgCycleDischargeRelaxHours.Text = Properties.Settings.Default.DischargeRelaxHours; try { int BrdsNumber = 0; string BrdsName = ""; if (board.Connect(out BrdsNumber, out BrdsName) != 0) { throw new ArgumentException("EV2300 not found."); } LogView.AddEntry("EV2300 connected..."); Console.WriteLine(BrdsName); EV23KError err = board.CheckForError(); gauge = new GaugeInfo(board); UpdateGui(null, null); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Read data block from device via SMBus on EV2300. /// </summary> /// <param name="SMBusCmd">Device command to write.</param> /// <param name="DataBlock">Variable to store data block in.</param> /// <param name="BlockLength">Read length of data block</param> /// <param name="targetAddr">Device target address.</param> /// <returns>EV2300 error code.</returns> public EV23KError ReadSMBusBlock(short SMBusCmd, out object DataBlock, out short BlockLength, short targetAddr) { try { DataBlock = null; BlockLength = 0; short len = 0; object data = null; if (!isPresent) { return(EV23KError.NoUSB); } EV23KError err = (EV23KError)EV23KBoard.ReadSMBusBlock(SMBusCmd, ref data, ref len, targetAddr); if (err != EV23KError.NoError) { return(err); } DataBlock = data; BlockLength = len; return(EV23KError.NoError); } catch (Exception ex) { throw new ArgumentException(ex.Message); } }
/// <summary> /// Write specific device register using methode defined in configuration. /// </summary> /// <param name="caption">Register name to write.</param> /// <returns>EV2300 error code.</returns> /// <remarks>For now only works to set the ManufacturerAccess command.</remarks> private EV23KError WriteDevice(string caption) { EV23KError err = EV23KError.Unknown; object dataBlock = null; short dataLength = 0; SbsCommandItem c = sbsItems.SbsCommands.Find(x => x.Caption == caption); if (c != null) { if (c.WriteStyle == 1) { err = EV23KBoard.WriteSMBusWord(0, c.Command, sbsItems.TargetAdress); } else if (c.WriteStyle == 2) { err = EV23KBoard.ReadManufacturerAccessBlock(sbsItems.TargetMacAdress, c.Command, out dataBlock, out dataLength, sbsItems.TargetAdress); if (c.HasResult && err == EV23KError.NoError) { c.GetRawValueFromDataBlock(dataBlock, dataLength, c.OffsetWithinBlock, c.LengthWithinBlock); c.CalculateDisplayValue(); } } } return(err); }
/// <summary> /// Read specific device register using methode defined in configuration. /// </summary> /// <param name="caption">Register name to read.</param> /// <returns>EV2300 error code.</returns> private EV23KError ReadDevice(string caption) { EV23KError err = EV23KError.Unknown; short dataWord = 0; object dataBlock = null; short dataLength = 0; SbsRegisterItem i = sbsItems.SbsRegister.Find(x => x.Caption == caption); if (i != null) { if (i.ReadStyle == 1) { err = (EV23KError)EV23KBoard.ReadSMBusWord(i.Command, out dataWord, sbsItems.TargetAdress); if (err == EV23KError.NoError) { i.RawValue = dataWord; i.CalculateBitFields(); i.CalculateDisplayValue(); } } else if (i.ReadStyle == 2) { if (!i.IsMac) { err = (EV23KError)EV23KBoard.ReadSMBusBlock(i.Command, out dataBlock, out dataLength, sbsItems.TargetAdress); if (err == EV23KError.NoError) { i.GetRawValueFromDataBlock(dataBlock, dataLength, i.OffsetWithinBlock, i.LengthWithinBlock); i.CalculateBitFields(); i.CalculateDisplayValue(); } } else { err = (EV23KError)EV23KBoard.ReadManufacturerAccessBlock(sbsItems.TargetMacAdress, i.Command, out dataBlock, out dataLength, sbsItems.TargetAdress); if (err == EV23KError.NoError) { i.GetRawValueFromDataBlock(dataBlock, dataLength, i.OffsetWithinBlock, i.LengthWithinBlock); i.CalculateBitFields(); i.CalculateDisplayValue(); } } } else if (i.ReadStyle == 3) { err = (EV23KError)EV23KBoard.ReadManufacturerAccessBlock(sbsItems.TargetMacAdress, i.Command, out dataBlock, out dataLength, sbsItems.TargetAdress); if (err == EV23KError.NoError) { i.GetRawValueFromDataBlock(dataBlock, dataLength, i.OffsetWithinBlock, i.LengthWithinBlock); i.CalculateBitFields(); i.CalculateDisplayValue(); } } } return(err); }
/// <summary> /// Read entire dataflash content into buffer. /// </summary> /// <param name="MacAddr">Manufacturer access command.</param> /// <param name="DataBlock">Flash data block.</param> /// <param name="BlockLength">Flash data block length.</param> /// <param name="targetAddr">Target device address.</param> /// <returns>EV2300 error code.</returns> /// <remarks>For dataflash access see SLUUBT5, chapter 18.1.75</remarks> public EV23KError ReadDataflash(short MacAddr, out object DataBlock, out int BlockLength, short targetAddr) { try { DataBlock = null; BlockLength = 0; short datalen = 32; object data = null; IEnumerable <byte> df = null; if (!isPresent) { return(EV23KError.NoUSB); } byte[] cmd = { 0x00, 0x40 }; EV23KError err = (EV23KError)EV23KBoard.WriteSMBusBlock(MacAddr, cmd, (short)2, (short)(targetAddr - 1)); if (err != EV23KError.NoError) { return(err); } err = (EV23KError)EV23KBoard.ReadSMBusBlock(MacAddr, ref data, ref datalen, targetAddr); if (err == EV23KError.NoError) { df = ((byte[])data).Skip(2); // Skip first two address bytes, copy only flash data } for (int i = 1; i < 103; i++) { data = null; datalen = 0; err = (EV23KError)EV23KBoard.ReadSMBusBlock(MacAddr, ref data, ref datalen, targetAddr); if (err == EV23KError.NoError) { df = df.Concat(((byte[])data).Skip(2)); // Skip frst two address bytes, concat only flash data } else { return(err); } } DataBlock = df.ToArray(); BlockLength = df.Count(); return(EV23KError.NoError); } catch (Exception ex) { throw new ArgumentException(ex.Message); } }
/// <summary> /// Connect to EV2300 board. /// </summary> /// <param name="BrdsNumber">Board numbers found and connected to.</param> /// <param name="BrdsName">Connected board name.</param> /// <returns>EV2300 error code</returns> public EV23KError Connect(out int BrdsNumber, out string BrdsName) { try { BrdsNumber = 0; BrdsName = ""; object obj = null; short len = 0, ver = 0, rev = 0; EV23KBoard.GetFreeBoards(1, ref BrdsNumber, ref BrdsName); if (BrdsNumber <= 0) { return(EV23KError.IncorrectParameter); } BrdsName = BrdsName.Substring(0, BrdsName.Length - 1); EV23KError err = (EV23KError)EV23KBoard.OpenDevice(ref BrdsName); if (err == EV23KError.NoError) { timerCheckStatus.Enabled = true; CheckStatus(null, null); err = (EV23KError)EV23KBoard.GetEV2300Name(ref obj, ref len); if (name != null) { name = Encoding.ASCII.GetString((byte[])obj); int i = name.IndexOf('\0'); if (i >= 0) { name = name.Substring(0, i); } } err = (EV23KError)EV23KBoard.GetEV2300Version(ref ver, ref rev); if (err == EV23KError.NoError) { version = Encoding.ASCII.GetString(new byte[] { (byte)((ver & 0xff00) >> 8), 0x2E, (byte)(ver & 0x00ff), (byte)rev }); } err = (EV23KError)EV23KBoard.GPIOWrite(0x7FF, 0); } return(err); } catch (Exception ex) { throw new ArgumentException(ex.Message); } }
/// <summary> /// Toogle EV2300 GPIO pin. /// </summary> /// <param name="gpio">GPIO pin mask to toggle.</param> /// <returns>EV2300 error code.</returns> public EV23KError GpioToggle(EV23KGpioMask gpio) { if (!isPresent) { return(EV23KError.NoUSB); } short data = 0; EV23KError err = (EV23KError)EV23KBoard.GPIORead((short)gpio, ref data); if (err == EV23KError.NoError) { data = (short)~data; err = (EV23KError)EV23KBoard.GPIOWrite((short)gpio, data); } return(err); }
/// <summary> /// Check present status of EV2300 board. /// </summary> /// <param name="sender">Not used</param> /// <param name="e">Not used</param> private void CheckStatus(object sender, System.EventArgs e) { try { EV23KError err = (EV23KError)EV23KBoard.GPIOMask(0); if (err == EV23KError.NoUSB) { isPresent = false; } else { isPresent = true; } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Read manufacturer access block from device via SMBus on EV2300. /// </summary> /// <param name="MacAddr">Manufacturer Access address/command.</param> /// <param name="Cmd">Manufacturer Access command/register to read.</param> /// <param name="DataBlock">Variable to store data block in.</param> /// <param name="BlockLength">Read length of data block</param> /// <param name="targetAddr">Device target address.</param> /// <returns>EV2300 error code.</returns> public EV23KError ReadManufacturerAccessBlock(short MacAddr, short Cmd, out object DataBlock, out short BlockLength, short targetAddr) { try { DataBlock = null; BlockLength = 0; short len = 0; object data = null; if (!isPresent) { return(EV23KError.NoUSB); } byte[] cmd = { (byte)(Cmd & 0xFF), (byte)((Cmd & 0xFF00) >> 8) }; EV23KError err = (EV23KError)EV23KBoard.WriteSMBusBlock(MacAddr, cmd, (short)2, (short)(targetAddr - 1)); if (err != EV23KError.NoError) { return(err); } err = (EV23KError)EV23KBoard.ReadSMBusBlock(MacAddr, ref data, ref len, targetAddr); if (err != EV23KError.NoError) { return(err); } DataBlock = data; BlockLength = len; return(EV23KError.NoError); } catch (Exception ex) { throw new ArgumentException(ex.Message); } }
/// <summary> /// Read entire data flash from device if not already present. /// </summary> private void ReadDataflash() { if (isDataflashAvail) { return; } EV23KError err = EV23KError.Unknown; object dataBlock = null; int dataLength = 0; if (EV23KBoard.IsPresent) { err = EV23KBoard.ReadDataflash(sbsItems.TargetMacAdress, out dataBlock, out dataLength, sbsItems.TargetAdress); if (err == EV23KError.NoError) { bcfgItems.CreateDataflashModel(dataBlock, dataLength); isDataflashAvail = true; } } return; }