Exemple #1
0
            public void Read(byte slaveSelectPin, byte clockPin, byte mosiPin, byte misoPin, byte mode, SpiFrequency frequency,
                             byte[] data = null, bool lsbFirst = true, bool useNativePins = true)
            {
                SpiParameterBuilder builder = new SpiParameterBuilder((byte)((spiByteArray.attributes.length() - 1) | (id << 4)));

                builder.slaveSelectPin(slaveSelectPin)
                .clockPin(clockPin)
                .mosiPin(mosiPin)
                .misoPin(misoPin)
                .mode(mode)
                .frequency(frequency);

                if (lsbFirst)
                {
                    builder.lsbFirst();
                }

                if (useNativePins)
                {
                    builder.useNativePins();
                }

                if (data != null)
                {
                    builder.data(data);
                }

                byte[] parameters = builder.build();
                byte[] command    = new byte[spiByteArray.eventConfig.Length - 1 + parameters.Length];
                Array.Copy(spiByteArray.eventConfig, 0, command, 0, 2);
                Array.Copy(parameters, 0, command, 2, parameters.Length);

                bridge.sendCommand(command);
            }
Exemple #2
0
        public void WriteSPI(byte slaveSelectPin, byte clockPin, byte mosiPin, byte misoPin, byte mode, SpiFrequency frequency,
                             byte[] data, bool lsbFirst = true, bool useNativePins = true)
        {
            if (bridge.lookupModuleInfo(SERIAL_PASSTHROUGH).revision >= SPI_REVISION)
            {
                SpiParameterBuilder builder = new SpiParameterBuilder();
                builder.slaveSelectPin(slaveSelectPin)
                .clockPin(clockPin)
                .mosiPin(mosiPin)
                .misoPin(misoPin)
                .mode(mode)
                .frequency(frequency);

                if (lsbFirst)
                {
                    builder.lsbFirst();
                }

                if (useNativePins)
                {
                    builder.useNativePins();
                }

                if (data != null)
                {
                    builder.data(data);
                }

                bridge.sendCommand(SERIAL_PASSTHROUGH, SPI_RW, builder.build());
            }
        }
Exemple #3
0
        public Task <byte[]> ReadSPIAsync(byte length, byte slaveSelectPin, byte clockPin, byte mosiPin, byte misoPin, byte mode, SpiFrequency frequency,
                                          byte[] data = null, bool lsbFirst = true, bool useNativePins = true)
        {
            spiReadTask    = new TaskCompletionSource <byte[]>();
            spiReadTimeout = new Timer(e => spiReadTask.SetException(new TimeoutException("Reading spi data timed out")), null, MetaWearBoard.COMMS_TIMEOUT, Timeout.Infinite);

            SpiParameterBuilder builder = new SpiParameterBuilder((byte)((length - 1) | (DIRECT_SPI_READ_ID << 4)));

            builder.slaveSelectPin(slaveSelectPin)
            .clockPin(clockPin)
            .mosiPin(mosiPin)
            .misoPin(misoPin)
            .mode(mode)
            .frequency(frequency);

            if (lsbFirst)
            {
                builder.lsbFirst();
            }
            if (useNativePins)
            {
                builder.useNativePins();
            }
            if (data != null)
            {
                builder.data(data);
            }

            bridge.sendCommand(SERIAL_PASSTHROUGH, Util.setRead(SPI_RW), builder.build());

            return(spiReadTask.Task);
        }
Exemple #4
0
        public async Task <byte[]> ReadSPIAsync(byte length, byte slaveSelectPin, byte clockPin, byte mosiPin, byte misoPin, byte mode, SpiFrequency frequency,
                                                byte[] data = null, bool lsbFirst = true, bool useNativePins = true)
        {
            SpiParameterBuilder builder = new SpiParameterBuilder((byte)((length - 1) | (DIRECT_SPI_READ_ID << 4)));

            builder.slaveSelectPin(slaveSelectPin)
            .clockPin(clockPin)
            .mosiPin(mosiPin)
            .misoPin(misoPin)
            .mode(mode)
            .frequency(frequency);

            if (lsbFirst)
            {
                builder.lsbFirst();
            }
            if (useNativePins)
            {
                builder.useNativePins();
            }
            if (data != null)
            {
                builder.data(data);
            }

            var response = await spiReadTask.Execute("Did not received SPI data within {0}ms", bridge.TimeForResponse,
                                                     () => bridge.sendCommand(SERIAL_PASSTHROUGH, Util.setRead(SPI_RW), builder.build()));

            if (response.Length > 3)
            {
                byte[] dataInner = new byte[response.Length - 3];
                Array.Copy(response, 3, dataInner, 0, dataInner.Length);
                return(dataInner);
            }
            else
            {
                throw new InvalidOperationException("Error reading SPI data from device or register address.  Response: " + Util.arrayToHexString(response));
            }
        }