public void ConfigureChip(ChipSettings settings) { ConfigureChip( settings, CommandCodes.WriteChipParameters, SubCommandCodes.ChipSettingsPowerUpDefault); }
public ChipSettings ReadChipConfiguration() { ChipSettings settings = ReadChipConfiguration( CommandCodes.ReadChipParameters, SubCommandCodes.ChipSettingsPowerUpDefault); return(settings); }
/// <summary> /// This method reads the chip configuration. /// </summary> /// <param name="command">The configuration command.</param> /// <param name="subcommand">The sub command configuration. If 0, it's not used.</param> /// <returns>The chip settings.</returns> protected ChipSettings ReadChipConfiguration(byte command, byte subcommand = 0) { // create the packet byte[] packet = new byte[Constants.PacketsSize]; packet[0] = command; packet[1] = subcommand; // write the packet byte[] reply = _hidHandler.WriteData(packet); // check if package format is faulty if (reply[0] != command || (subcommand != 0 && reply[2] != subcommand)) { throw new PacketReplyFormatException(); } // create the current settings instance ChipSettings settings = FromPacketToChipSettings(reply); return(settings); }
public ChipSettings ReadChipConfiguration() { ChipSettings settings = ReadChipConfiguration(CommandCodes.GetGpioCurrentChipSettings); return(settings); }
public void ConfigureChip(ChipSettings settings) { ConfigureChip(settings, CommandCodes.SetGpioCurrentChipSettings); }
/// <summary> /// This method configures the chip. /// </summary> /// <param name="settings">The chip settings.</param> /// <param name="command">The writing configuration command.</param> /// <param name="subcommand">The sub command configuration. If 0, it's not used.</param> protected void ConfigureChip(ChipSettings settings, byte command, byte subcommand = 0) { // check if the input argument has 9 length array configuration if (settings.PinDirections == null || settings.PinDirections.Length != Constants.NumberOfGeneralPorpouseLines) { throw new ArgumentException("Expected non null and " + Constants.NumberOfGeneralPorpouseLines + " length pins directions array."); } if (settings.PinModes == null || settings.PinModes.Length != Constants.NumberOfGeneralPorpouseLines) { throw new ArgumentException("Expected non null and " + Constants.NumberOfGeneralPorpouseLines + " length pins modes array."); } if (settings.DefaultOutput == null || settings.DefaultOutput.Length != Constants.NumberOfGeneralPorpouseLines) { throw new ArgumentException("Expected non null and " + Constants.NumberOfGeneralPorpouseLines + " length pins default output array."); } // create the packet byte[] packet = new byte[Constants.PacketsSize]; packet[0] = command; packet[1] = subcommand; FromChipSettingstoPacket(settings, ref packet); // set the password according with the access control byte (the 18th byte) string password = settings.Password; if (_nonVolatileRam && settings.AccessControl != NramChipAccessControl.NotProtected) { if (string.IsNullOrEmpty(password)) { // reset password for (int i = 0; i < Constants.MaximumPasswordLength; i++) { packet[i + 19] = 0; } } else { // setup password string subPassword = ""; if (password.Length > Constants.MaximumPasswordLength) { subPassword = password.Substring(0, Constants.MaximumPasswordLength); } else { subPassword = password; } for (int i = 0; i < Constants.MaximumPasswordLength; i++) { if (password.Length > i) { packet[i + 19] = BitConverter.GetBytes(password[i])[0]; } else { packet[i + 19] = 0x00; } } } } // write the new setup and read the reply byte[] settingsReply = _hidHandler.WriteData(packet); // check if package format is faulty if (settingsReply[0] != command || (subcommand != 0 && settingsReply[2] != subcommand)) { throw new PacketReplyFormatException(); } // check for errors switch (settingsReply[1]) { case ReplyStatusCodes.CompletedSuccessfully: break; case ReplyStatusCodes.BlockedAccess: throw new AccessBlockedException(); default: throw new NotImplementedException(); } }
private static void FromChipSettingstoPacket(ChipSettings settings, ref byte[] packet) { ushort gpioOut = 0; ushort gpioDir = 0; for (int i = 0; i < Constants.NumberOfGeneralPorpouseLines; i++) { PinMode mode = settings.PinModes[i]; PinDirection direction = settings.PinDirections[i]; // setup pin mode byte mbyte = 0; switch (mode) { case PinMode.GPIO: mbyte = 0; break; case PinMode.ChipSelects: mbyte = 1; break; case PinMode.DedicatedFunction: mbyte = 2; break; default: throw new NotImplementedException(); } packet[i + 4] = mbyte; // create the GPIO configuration byte dirbyte = (byte)(direction == PinDirection.Output ? 0 : 1); ushort bit = (ushort)(dirbyte << i); gpioDir |= bit; // set the GPIO configuration if (settings.DefaultOutput[i]) { gpioOut |= (ushort)(1 << i); } else { gpioOut &= (ushort)~(1 << i); } } byte[] gpioOutBytes = BitConverter.GetBytes(gpioOut); packet[13] = gpioOutBytes[0]; packet[14] = gpioOutBytes[1]; byte[] gpioDirBytes = BitConverter.GetBytes(gpioDir); packet[15] = gpioDirBytes[0]; packet[16] = gpioDirBytes[1]; // set the other chip functionalities byte otherChipSettingsByte = 0x00; otherChipSettingsByte |= (byte)(settings.SpiBusReleaseEnable ? 0 : 1); // 0000 0001 otherChipSettingsByte |= (byte)(settings.RemoteWakeUpEnabled ? 16 : 0); // 0000 1000 switch (settings.InterruptBitMode) { case DedicatedFunction.CountFallingEdges: otherChipSettingsByte |= 2; // 001 break; case DedicatedFunction.CountRisingEdges: otherChipSettingsByte |= 3; // 010 break; case DedicatedFunction.CountLowPulses: otherChipSettingsByte |= 4; // 011 break; case DedicatedFunction.CountHighPulses: otherChipSettingsByte |= 5; // 100 break; case DedicatedFunction.NoInterruptCounting: default: otherChipSettingsByte |= 0x00; // 000 break; } packet[17] = otherChipSettingsByte; // set the Nvram chip parameters access control switch (settings.AccessControl) { case NramChipAccessControl.NotProtected: packet[18] = 0x00; break; case NramChipAccessControl.PasswordProtected: packet[18] = 0x40; break; case NramChipAccessControl.PermanentlyLocked: packet[18] = 0x80; break; } }
private static ChipSettings FromPacketToChipSettings(byte[] packet) { // create the current settings instance ChipSettings settings = new ChipSettings(); settings.PinDirections = new PinDirection[Constants.NumberOfGeneralPorpouseLines]; settings.PinModes = new PinMode[Constants.NumberOfGeneralPorpouseLines]; settings.DefaultOutput = new bool[Constants.NumberOfGeneralPorpouseLines]; ushort gpioOutput = BitConverter.ToUInt16(packet, 13); ushort gpioDirection = BitConverter.ToUInt16(packet, 15); for (int i = 0; i < Constants.NumberOfGeneralPorpouseLines; i++) { // set pin modes switch (packet[4 + i]) { case 0: settings.PinModes[i] = PinMode.GPIO; break; case 1: settings.PinModes[i] = PinMode.ChipSelects; break; case 2: settings.PinModes[i] = PinMode.DedicatedFunction; break; default: throw new NotImplementedException(); } // compute the current bit mask int bitMask = (int)Math.Pow(2, i); int pinStatus = 0; // set pin directions pinStatus = gpioDirection & bitMask; settings.PinDirections[i] = pinStatus == bitMask ? PinDirection.Output : // 0 PinDirection.Input; // 1 // set the default outputs pinStatus = gpioOutput & bitMask; settings.DefaultOutput[i] = pinStatus == bitMask; } // set other chip functionalities byte otherChipSettingByte = packet[17]; settings.SpiBusReleaseEnable = (otherChipSettingByte & 1) == 0; settings.RemoteWakeUpEnabled = (otherChipSettingByte & 8) == 8; int dedfunctIndex = (otherChipSettingByte >> 1) & 7; // 0000 0111 mask switch (dedfunctIndex) { case 0: settings.InterruptBitMode = DedicatedFunction.NoInterruptCounting; break; case 1: settings.InterruptBitMode = DedicatedFunction.CountFallingEdges; break; case 2: settings.InterruptBitMode = DedicatedFunction.CountRisingEdges; break; case 3: settings.InterruptBitMode = DedicatedFunction.CountLowPulses; break; case 4: settings.InterruptBitMode = DedicatedFunction.CountHighPulses; break; default: throw new NotImplementedException(); } // set nvram access control switch (packet[18]) { case 0x00: settings.AccessControl = NramChipAccessControl.NotProtected; break; case 0x40: settings.AccessControl = NramChipAccessControl.PasswordProtected; break; case 0x80: settings.AccessControl = NramChipAccessControl.PermanentlyLocked; break; } return(settings); }