Esempio n. 1
0
        private byte[] TwoPhaseCommunication(NeuronGroup neuronGroup, byte[] messagePhaseOne, byte[] messagePhaseTwo)
        {
            var returnMessagePhaseOne = new byte[messagePhaseOne.Length];

            messagePhaseOne.CopyTo(returnMessagePhaseOne, 0);

            var returnMessagePhaseTwo = new byte[messagePhaseTwo.Length];

            messagePhaseTwo.CopyTo(returnMessagePhaseTwo, 0);

            byte[] correctByte = null;

            SpiDevice device;

            if (!_devices.TryGetValue(neuronGroup, out device) || device == null)
            {
                _driverLogger.LogError(this, "Device not found!");
                return(null);
            }

            var crcCheck   = false;
            var tryCounter = 10;

            while (!crcCheck)
            {
                try
                {
                    if (tryCounter-- < 0)
                    {
                        throw new Exception("the spi communication failed!");
                    }

                    lock (_spiDeviceLocker)
                    {
                        PrepareSpiCommunication(device);

                        device.TransferFullDuplex(messagePhaseOne, returnMessagePhaseOne);


                        if (returnMessagePhaseOne[0] == 0)
                        {
                            correctByte = new byte[1];
                            device.Read(correctByte);
                        }

                        device.TransferFullDuplex(messagePhaseTwo, returnMessagePhaseTwo);
                    }

                    if (correctByte != null)
                    {
                        for (var i = 1; i < returnMessagePhaseOne.Length; i++)
                        {
                            returnMessagePhaseOne[i - 1] = returnMessagePhaseOne[i];
                        }

                        returnMessagePhaseOne[returnMessagePhaseOne.Length - 1] = correctByte[0];
                    }

                    var firstCrc = CreateCrc(returnMessagePhaseOne, returnMessagePhaseOne.Length - 2);


                    var crc = CreateCrc(returnMessagePhaseTwo, returnMessagePhaseTwo.Length - 2, BitConverter.ToUInt16(firstCrc, 0));

                    if (crc != null &&
                        crc[0] == returnMessagePhaseTwo[returnMessagePhaseTwo.Length - 2] &&
                        crc[1] == returnMessagePhaseTwo[returnMessagePhaseTwo.Length - 1])
                    {
                        crcCheck = true;
                    }
                    else
                    {
                        _driverLogger.LogError(this, "CRC Check failed for answer " + string.Join("-", returnMessagePhaseTwo));
                        Task.Delay(100)?.Wait();
                    }
                }

                catch (Exception e)
                {
                    _driverLogger.LogException(this, e);

                    _driverLogger.LogError(this, "#1 PI->Board " + string.Join("-", messagePhaseOne));
                    _driverLogger.LogError(this, "#1 PI<-Board " + string.Join("-", returnMessagePhaseOne));

                    _driverLogger.LogError(this, "#2 PI->Board " + string.Join("-", messagePhaseTwo));
                    _driverLogger.LogError(this, "#2 PI<-Board " + string.Join("-", returnMessagePhaseTwo));
                    if (tryCounter < 0)
                    {
                        throw;
                    }

                    Task.Delay(10)?.Wait();
                }
            }

            return(returnMessagePhaseTwo);
        }
Esempio n. 2
0
        internal static BoardInformation GetBoardInfoFromRegisters(SpiRegisterSet registerSet, DriverLogger logger, NeuronGroup neuronGroup)
        {
            try
            {
                if (registerSet == null || registerSet.Count != 5)
                {
                    return(null);
                }


                if (!registerSet.ContainsRegister(1000) ||
                    !registerSet.ContainsRegister(1001) ||
                    !registerSet.ContainsRegister(1002) ||
                    !registerSet.ContainsRegister(1003) ||
                    !registerSet.ContainsRegister(1004))
                {
                    logger.LogError(null, "i got the wrong registerSet to create the system information!");
                    return(null);
                }

                var configRegisters = registerSet.ToRegisterValueArray();

                int swSubver;
                int hwVersion;
                int hwSubver;

                var swVersion = configRegisters[0] >> 8;
                var diCount   = configRegisters[1] >> 8;
                var doCount   = configRegisters[1] & 0xff;
                var aiCount   = configRegisters[2] >> 8;
                var aoCount   = (configRegisters[2] & 0xff) >> 4;

                var uledCount = 0;

                if (swVersion < 4)
                {
                    swSubver  = 0;
                    hwVersion = (configRegisters[0] & 0xff) >> 4;
                    hwSubver  = configRegisters[0] & 0xf;
                }
                else
                {
                    swSubver  = configRegisters[0] & 0xff;
                    hwVersion = configRegisters[3] >> 8;
                    hwSubver  = configRegisters[3] & 0xff;

                    if (hwSubver < 3)
                    {
                    }
                    if (hwVersion == 0)
                    {
                        if (configRegisters[0] != 0x0400)
                        {
                            uledCount = 4;
                        }
                    }
                }


                var firmwareVersion    = new Version(swVersion, swSubver);
                var digitalInputCount  = diCount;
                var digitalOutputCount = doCount;
                var analogInputCount   = aiCount;
                var analogOutputCount  = aoCount;
                var userLedCount       = uledCount;
                var hardwareVersion    = new Version(hwVersion, hwSubver);


                return(new BoardInformation(firmwareVersion, hardwareVersion, digitalInputCount, digitalOutputCount, analogInputCount, analogOutputCount, userLedCount, hwVersion, neuronGroup));
            }
            catch (Exception e)
            {
                logger.LogException(null, e);
                return(null);
            }
        }
Esempio n. 3
0
        internal async Task Initialize()
        {
            try
            {
                _logger.LogDebug(this, "Initializing...");

                var deviceSelector = I2cDevice.GetDeviceSelector("I2C1");

                if (deviceSelector == null)
                {
                    throw new Exception("No deviceSelector for I2C found!");
                }

                var devices = DeviceInformation.FindAllAsync(deviceSelector);

                if (devices == null)
                {
                    throw new Exception("Find All Async failed");
                }

                var unipiI2CDevices = Enum.GetValues(typeof(UnipiI2CDevice));

                if (unipiI2CDevices == null)
                {
                    throw new Exception("No I2C Devices in Configuration Enumn");
                }

                foreach (UnipiI2CDevice unipiI2CDevice in unipiI2CDevices)
                {
                    var found = false;

                    await devices;

                    var deviceInformationCollection = devices.GetResults();

                    if (deviceInformationCollection == null)
                    {
                        throw new Exception("No devices found!");
                    }

                    foreach (var deviceInformation in deviceInformationCollection.Where(deviceInformation => deviceInformation != null))
                    {
                        _logger.LogDebug(this, "searching for device " + unipiI2CDevice + " on device " + deviceInformation.Id);

                        var i2CSettings = new I2cConnectionSettings((int)unipiI2CDevice)
                        {
                            BusSpeed = I2cBusSpeed.FastMode
                        };

                        var device = I2cDevice.FromIdAsync(deviceInformation.Id, i2CSettings);

                        if (device == null)
                        {
                            throw new Exception("Find All Async failed");
                        }

                        await device;

                        _logger.LogDebug(this, "device " + unipiI2CDevice + " found");

                        lock (_busLock)
                        {
                            _devices.Add(unipiI2CDevice, device.GetResults());
                        }


                        found = true;

                        break;
                    }

                    if (!found)
                    {
                        _logger.LogError(this, "device " + unipiI2CDevice + " not found!");
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.LogException(this, exception);
            }
        }