public void DistributeLockShouldTimeout() { ILockFactory processLockFactory = Init("Distribute").GetRequiredService <ILockFactory>(); string lockName = "ProcessLockSameLockNameTest"; bool getLock = true; List <Task> tasks = new List <Task>(); for (int i = 0; i < 2; i++) { Task t = Task.Run(() => { using (ILock pLock = processLockFactory.GetLock(lockName)) { getLock = pLock.Wait(TimeSpan.FromSeconds(1)); if (getLock) { System.Threading.Thread.Sleep(3000); } } }); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); Assert.IsFalse(getLock); using (ILock pLock = processLockFactory.GetLock(lockName)) { getLock = pLock.Wait(TimeSpan.FromSeconds(3)); } Assert.IsTrue(getLock); }
public void SetDacChannelDataSourceAllChannels(DacChannelDataSource source) { using (_lockFactory.GetLock(LockType.CommandLock)) { switch (source) { case DacChannelDataSource.DataValueA: _sendSpecialFunctionCommand.SendSpecialFunction(SpecialFunctionCode.BlockWriteABSelectRegisters, (byte)ABSelectRegisterBits.AllChannelsA); _evalBoard.DeviceState.ABSelectRegisters[0] = ABSelectRegisterBits.AllChannelsA; _evalBoard.DeviceState.ABSelectRegisters[1] = ABSelectRegisterBits.AllChannelsA; _evalBoard.DeviceState.ABSelectRegisters[2] = ABSelectRegisterBits.AllChannelsA; _evalBoard.DeviceState.ABSelectRegisters[3] = ABSelectRegisterBits.AllChannelsA; _evalBoard.DeviceState.ABSelectRegisters[4] = ABSelectRegisterBits.AllChannelsA; break; case DacChannelDataSource.DataValueB: _sendSpecialFunctionCommand.SendSpecialFunction(SpecialFunctionCode.BlockWriteABSelectRegisters, (byte)ABSelectRegisterBits.AllChannelsB); _evalBoard.DeviceState.ABSelectRegisters[0] = ABSelectRegisterBits.AllChannelsB; _evalBoard.DeviceState.ABSelectRegisters[1] = ABSelectRegisterBits.AllChannelsB; _evalBoard.DeviceState.ABSelectRegisters[2] = ABSelectRegisterBits.AllChannelsB; _evalBoard.DeviceState.ABSelectRegisters[3] = ABSelectRegisterBits.AllChannelsB; _evalBoard.DeviceState.ABSelectRegisters[4] = ABSelectRegisterBits.AllChannelsB; break; default: throw new ArgumentOutOfRangeException(nameof(source)); } } }
public void PulseLDACPin() { using (_lockFactory.GetLock(LockType.CommandLock)) { _sendDeviceCommandCommand.SendDeviceCommand(DeviceCommand.PulseLDACPin, 0); } }
public void SetCLRPinLow() { using (_lockFactory.GetLock(LockType.CommandLock)) { _sendDeviceCommandCommand.SendDeviceCommand(DeviceCommand.SetCLRPinLow, 0); } }
public void SetDacChannelGain(ChannelAddress channelAddress, ushort value) { if (channelAddress < ChannelAddress.Dac0 || channelAddress > ChannelAddress.Dac39) { throw new ArgumentOutOfRangeException(nameof(channelAddress)); } var val = _evalBoard.DeviceState.Precision == DacPrecision.SixteenBit ? value : (ushort)(value & (ushort)BasicMasks.HighFourteenBits); if (!_evalBoard.DeviceState.UseRegisterCache || val != _evalBoard.DeviceState.MRegisters[channelAddress.ToChannelNumber()]) { using (_lockFactory.GetLock(LockType.CommandLock)) { _setCLRPinLowCommand.SetCLRPinLow(); _sendSPICommand.SendSPI((uint)SerialInterfaceModeBits.WriteToDACGainRegisterM | (uint)(((byte)channelAddress & (byte)BasicMasks.SixBits) << 16) | val); _evalBoard.DeviceState.MRegisters[channelAddress.ToChannelNumber()] = val; _setCLRPinHighCommand.SetCLRPinHigh(); } } }
public void SetDacChannelDataValueA(ChannelAddress channelAddress, ushort value) { if (channelAddress < ChannelAddress.Dac0 || channelAddress > ChannelAddress.Dac39) { throw new ArgumentOutOfRangeException(nameof(channelAddress)); } using (_lockFactory.GetLock(LockType.CommandLock)) { var val = _evalBoard.DeviceState.Precision == DacPrecision.SixteenBit ? value : (ushort)(value & (ushort)BasicMasks.HighFourteenBits); if (_evalBoard.DeviceState.UseRegisterCache && val == _evalBoard.DeviceState.X1ARegisters[channelAddress.ToChannelNumber()]) { return; } var controlRegisterBits = _readbackControlRegisterCommand.ReadbackControlRegister() & ControlRegisterBits.WritableBits; controlRegisterBits &= ~ControlRegisterBits .InputRegisterSelect; //set control register bit F2 =0 to select register X1A for input _writeControlRegisterCommand.WriteControlRegister(controlRegisterBits); _sendSPICommand.SendSPI((uint)SerialInterfaceModeBits.WriteToDACInputDataRegisterX | (uint)(((byte)channelAddress & (byte)BasicMasks.SixBits) << 16) | val); _evalBoard.DeviceState.X1ARegisters[channelAddress.ToChannelNumber()] = val; } }
public void SetLDACPinHigh() { using (_lockFactory.GetLock(LockType.CommandLock)) { _sendDeviceCommandCommand.SendDeviceCommand(DeviceCommand.SetLDACPinHigh, 0); } }
public ushort ReadbackX1BRegister(ChannelAddress channelAddress) { using (_lockFactory.GetLock(LockType.CommandLock)) { if (channelAddress < ChannelAddress.Dac0 || channelAddress > ChannelAddress.Dac39) { throw new ArgumentOutOfRangeException(nameof(channelAddress)); } if (_evalBoard.DeviceState.UseRegisterCache && _evalBoard.DeviceState.X1BRegisters[channelAddress.ToChannelNumber()].HasValue) { var deviceStateX1BRegister = _evalBoard.DeviceState.X1BRegisters[channelAddress.ToChannelNumber()]; if (deviceStateX1BRegister != null) { return(deviceStateX1BRegister.Value); } } _sendSpecialFunctionCommand.SendSpecialFunction(SpecialFunctionCode.SelectRegisterForReadback, (ushort)((ushort)AddressCodesForDataReadback.X1BRegister | (ushort)(((byte)channelAddress & (byte)BasicMasks.SixBits) << 7))); var val = _readSPICommand.ReadSPI(); if (_evalBoard.DeviceState.Precision == DacPrecision.FourteenBit) { val &= (ushort)BasicMasks.HighFourteenBits; } _evalBoard.DeviceState.X1BRegisters[channelAddress.ToChannelNumber()] = val; return(val); } }
public void DistributeLockShouldReleaseOnce() { ILockFactory processLockFactory = Init("Distribute").GetRequiredService <ILockFactory>(); string lockName = "ProcessLockSameLockNameTest"; bool getLock = true; List <Task> tasks = new List <Task>(); int sum = 0; for (int i = 0; i < 100; i++) { Task t = Task.Run(() => { using (ILock pLock = processLockFactory.GetLock(lockName)) { getLock = pLock.Wait(); if (getLock) { sum++; } pLock.Release(); } }); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); Assert.AreEqual(100, sum); }
public async Task ProcessLockSameLockNameTestAsync() { ILockFactory processLockFactory = Init("Process").GetRequiredService <ILockFactory>(); List <Task> tasks = new List <Task>(); int sum = 0; string lockName = "ProcessLockSameLockNameTest"; for (int i = 0; i < 10000; i++) { Task t = Task.Run(async() => { using (ILock pLock = processLockFactory.GetLock(lockName)) { if (await pLock.WaitAsync()) { sum++; } } }); tasks.Add(t); } await Task.WhenAll(tasks.ToArray()); Assert.AreEqual(10000, sum); }
public async Task DistributeLockDifferentLockNameTestAsync() { ILockFactory processLockFactory = Init("Distribute").GetRequiredService <ILockFactory>(); List <Task> tasks = new List <Task>(); int sum = 0; for (int i = 0; i < 10000; i++) { Task t = Task.Run(async() => { using (ILock pLock = processLockFactory.GetLock(Guid.NewGuid().ToString())) { if (await pLock.WaitAsync()) { sum++; } } }); tasks.Add(t); } await Task.WhenAll(tasks.ToArray()); Assert.AreNotEqual(10000, sum); }
public void SendSPI(uint data) { using (_lockFactory.GetLock(LockType.CommandLock)) { _sendDeviceCommandCommand.SendDeviceCommand(DeviceCommand.SendSPI, data); } }
public string GetDeviceSymbolicName() { using (_lockFactory.GetLock(LockType.CommandLock)) { return(_evalBoard.UsbDevice?.SymbolicName); } }
public void InitializeSPIPins() { using (_lockFactory.GetLock(LockType.CommandLock)) { _sendDeviceCommandCommand.SendDeviceCommand(DeviceCommand.InitializeSPIPins, 0); _evalBoard.DeviceState.SPI_Initialized = true; } }
public void SendSpecialFunction(SpecialFunctionCode specialFunctionCode, ushort data) { using (_lockFactory.GetLock(LockType.CommandLock)) { var value = (uint)((((byte)specialFunctionCode & (byte)BasicMasks.SixBits) << 16) | data); _sendSPICommand.SendSPI(value); } }
public void UsbControlTransfer(byte requestType, byte request, int value, int index, byte[] buffer = null, int?length = null) { using (_lockFactory.GetLock(LockType.CommandLock)) { _evalBoard.UsbDevice.ControlTransfer(requestType, request, value, index, buffer, length); } }
public bool GetThermalShutdownEnabled() { using (_lockFactory.GetLock(LockType.CommandLock)) { var controlRegisterBits = _readbackControlRegisterCommand.ReadbackControlRegister(); return((controlRegisterBits & ControlRegisterBits.ThermalShutdownEnabled) == ControlRegisterBits.ThermalShutdownEnabled); //if bit 1=1, thermal shutdown is enabled } }
public bool GetIsOverTemperature() { using (_lockFactory.GetLock(LockType.CommandLock)) { var controlRegisterBits = _readbackControlRegisterCommand.ReadbackControlRegister(); return((controlRegisterBits & ControlRegisterBits.OverTemperature) == ControlRegisterBits.OverTemperature); } }
public bool GetPacketErrorCheckErrorOccurredStatus() { using (_lockFactory.GetLock(LockType.CommandLock)) { var controlRegisterBits = _readbackControlRegisterCommand.ReadbackControlRegister(); return((controlRegisterBits & ControlRegisterBits.PacketErrorCheckErrorOccurred) == ControlRegisterBits.PacketErrorCheckErrorOccurred); } }
public void ToggleReset() { using (_lockFactory.GetLock(LockType.CommandLock)) { _evalBoard.DeviceState.ClearRegisterCache(); _setRESETPinHighCommand.SetRESETPinHigh(); Thread.Sleep(1000); _setRESETPinLowCommand.SetRESETPinLow(); } }
public ushort GetDacChannelDataValueB(ChannelAddress channelAddress) { using (_lockFactory.GetLock(LockType.CommandLock)) { if (channelAddress >= ChannelAddress.Dac0 && channelAddress <= ChannelAddress.Dac39) { return(_readbackX1BRegisterCommand.ReadbackX1BRegister(channelAddress)); } throw new ArgumentOutOfRangeException(nameof(channelAddress)); } }
public void PerformSoftPowerDown() { using (_lockFactory.GetLock(LockType.CommandLock)) { _evalBoard.DeviceState.ClearRegisterCache(); var controlRegisterBits = _readbackControlRegisterCommand.ReadbackControlRegister() & ControlRegisterBits.WritableBits; controlRegisterBits |= ControlRegisterBits.SoftPowerDown; //set bit F0=1 to perform soft power-down _writeControlRegisterCommand.WriteControlRegister(controlRegisterBits); } }
public void WriteOFS0Register(ushort value) { using (_lockFactory.GetLock(LockType.CommandLock)) { value &= (ushort)BasicMasks.FourteenBits; if (!_evalBoard.DeviceState.UseRegisterCache || value != _evalBoard.DeviceState.OFS0Register) { _sendSpecialFunctionCommand.SendSpecialFunction(SpecialFunctionCode.WriteOFS0Register, value); _evalBoard.DeviceState.OFS0Register = value; } } }
public void ClearRegisterCache() { using (_lockFactory.GetLock(LockType.CommandLock)) { ABSelectRegisters = new ABSelectRegisterBits?[5]; ControlRegister = null; CRegisters = new ushort?[40]; MRegisters = new ushort?[40]; X1ARegisters = new ushort?[40]; X1BRegisters = new ushort?[40]; OFS0Register = null; OFS1Register = null; SPI_Initialized = false; } }
public void SetOffsetDAC0(ushort value) { value &= (ushort)BasicMasks.FourteenBits; if (!_evalBoard.DeviceState.UseRegisterCache || value != _evalBoard.DeviceState.OFS0Register) { using (_lockFactory.GetLock(LockType.CommandLock)) { _setCLRPinLowCommand.SetCLRPinLow(); _writeOFS0RegisterCommand.WriteOFS0Register(value); _setCLRPinHighCommand.SetCLRPinHigh(); } } }
public void ResetDevice(bool r) { using (_lockFactory.GetLock(LockType.CommandLock)) { _evalBoard.DeviceState.ClearRegisterCache(); byte[] buffer = { (byte)(r ? 1 : 0) }; _usbControlTransferCommand.UsbControlTransfer( (byte)UsbRequestType.TypeVendor, 0xA0, 0xE600, 0, buffer, buffer.Length); Thread.Sleep(r ? 50 : 400); // give the firmware some time for initialization } }
public ControlRegisterBits ReadbackControlRegister() { using (_lockFactory.GetLock(LockType.CommandLock)) { if (_evalBoard.DeviceState.UseRegisterCache && _evalBoard.DeviceState.ControlRegister.HasValue) { return((ControlRegisterBits)((ushort)_evalBoard.DeviceState.ControlRegister.Value & (ushort)ControlRegisterBits.ReadableBits)); } _sendSpecialFunctionCommand.SendSpecialFunction(SpecialFunctionCode.SelectRegisterForReadback, (ushort)AddressCodesForDataReadback.ControlRegister); var val = (ControlRegisterBits)(_readSPICommand.ReadSPI() & (ushort)ControlRegisterBits.ReadableBits); _evalBoard.DeviceState.ControlRegister = val; return(val); } }
public DacChannelDataSource GetDacChannelDataSource(ChannelAddress channelAddress) { using (_lockFactory.GetLock(LockType.CommandLock)) { if (channelAddress < ChannelAddress.Dac0 || channelAddress > ChannelAddress.Dac39) { throw new ArgumentOutOfRangeException(nameof(channelAddress)); } var channelNum = (byte)((byte)channelAddress - 8); var currentSourceSelections = _readbackABSelectRegisterCommand.ReadbackABSelectRegister(channelNum / 8); var channelOffset = (byte)(channelNum % 8); var channelMask = (ABSelectRegisterBits)(1 << channelOffset); var sourceIsDataValueB = (currentSourceSelections & channelMask) == channelMask; return(sourceIsDataValueB ? DacChannelDataSource.DataValueB : DacChannelDataSource.DataValueA); } }
public ushort ReadbackOFS1Register() { using (_lockFactory.GetLock(LockType.CommandLock)) { if (_evalBoard.DeviceState.UseRegisterCache && _evalBoard.DeviceState.OFS1Register.HasValue) { return(_evalBoard.DeviceState.OFS1Register.Value); } _sendSpecialFunctionCommand.SendSpecialFunction(SpecialFunctionCode.SelectRegisterForReadback, (ushort)AddressCodesForDataReadback.OFS1Register); var spi = _readSPICommand.ReadSPI(); var val = (ushort)(spi & (ushort)BasicMasks.FourteenBits); _evalBoard.DeviceState.OFS1Register = val; return(val); } }
public void SetThermalShutdownEnabled(bool value) { using (_lockFactory.GetLock(LockType.CommandLock)) { var controlRegisterBits = _readbackControlRegisterCommand.ReadbackControlRegister() & ControlRegisterBits.WritableBits; if (value) { controlRegisterBits |= ControlRegisterBits.ThermalShutdownEnabled; } else { controlRegisterBits &= ~ControlRegisterBits.ThermalShutdownEnabled; } _writeControlRegisterCommand.WriteControlRegister(controlRegisterBits); } }