Ejemplo n.º 1
0
 public void ConfigureSpi(SpiSetup setup)
 {
     ConfigureSpi(
         setup,
         CommandCodes.WriteChipParameters,
         SubCommandCodes.SpiPowerUpTransferSettings);
 }
Ejemplo n.º 2
0
        public SpiSetup ReadSpiConfiguration()
        {
            SpiSetup setup = ReadSpiConfiguration(
                CommandCodes.ReadChipParameters,
                SubCommandCodes.SpiPowerUpTransferSettings);

            return(setup);
        }
Ejemplo n.º 3
0
        private static SpiSetup FromPacketToSpiConfiguration(byte[] packet)
        {
            SpiSetup setup = new SpiSetup();

            // read the bitrate
            setup.BitRate = Convert.ToInt64(BitConverter.ToUInt32(packet, 4));

            // read the chip select values
            setup.ActiveChipSelectValues = new bool[Constants.NumberOfGeneralPorpouseLines];
            setup.IdleChipSelectValues   = new bool[Constants.NumberOfGeneralPorpouseLines];

            ushort idle   = BitConverter.ToUInt16(packet, 8);
            ushort active = BitConverter.ToUInt16(packet, 10);

            for (int i = 0; i < Constants.NumberOfGeneralPorpouseLines; i++)
            {
                ushort activeMask = (ushort)Math.Pow(2, i);
                setup.ActiveChipSelectValues[i] = (active & activeMask) == activeMask;

                ushort idleMask = (ushort)Math.Pow(2, i);
                setup.IdleChipSelectValues[i] = (idle & idleMask) == idleMask;
            }

            // read the delays
            setup.ChipSelectToDataDelay = Convert.ToInt32(BitConverter.ToUInt16(packet, 12));
            setup.DataToChipSelectDelay = Convert.ToInt32(BitConverter.ToUInt16(packet, 14));
            setup.BetweenDataDelay      = Convert.ToInt32(BitConverter.ToUInt16(packet, 16));

            // read the bytes to trasnfer
            setup.BytesToTransfer = Convert.ToInt32(BitConverter.ToUInt16(packet, 18));

            // read the spi mode
            switch (packet[20])
            {
            case 0:
                setup.Mode = SpiModes.Spi0;
                break;

            case 1:
                setup.Mode = SpiModes.Spi1;
                break;

            case 2:
                setup.Mode = SpiModes.Spi2;
                break;

            case 3:
                setup.Mode = SpiModes.Spi3;
                break;

            default:
                throw new NotImplementedException();
            }

            return(setup);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This method reads the SPI bus 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 SPI bus configuration.</returns>
        protected SpiSetup ReadSpiConfiguration(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 spi setup
            SpiSetup setup = FromPacketToSpiConfiguration(reply);

            return(setup);
        }
Ejemplo n.º 5
0
        public SpiSetup ReadSpiConfiguration()
        {
            SpiSetup setup = ReadSpiConfiguration(CommandCodes.GetSpiTransferSettings);

            return(setup);
        }
Ejemplo n.º 6
0
 public void ConfigureSpi(SpiSetup setup)
 {
     ConfigureSpi(setup, CommandCodes.SetSpiTransferSettings);
 }
Ejemplo n.º 7
0
        private static void FromSpiConfigurationToPacket(SpiSetup setup, ref byte[] packet)
        {
            // setup bitrate
            byte[] bitrateBytes = BitConverter.GetBytes(Convert.ToUInt32(setup.BitRate));
            packet[4] = bitrateBytes[0];
            packet[5] = bitrateBytes[1];
            packet[6] = bitrateBytes[2];
            packet[7] = bitrateBytes[3];

            // set chip select status
            ushort activeCS = 0;
            ushort idleCS   = 0;

            for (int i = 0; i < Constants.NumberOfGeneralPorpouseLines; i++)
            {
                if (setup.ActiveChipSelectValues[i])
                {
                    activeCS += (ushort)(1 << i);
                }
                if (setup.IdleChipSelectValues[i])
                {
                    idleCS += (ushort)(1 << i);
                }
            }

            byte[] idleCSBytes = BitConverter.GetBytes(Convert.ToUInt16(idleCS));
            packet[8] = idleCSBytes[0];
            packet[9] = idleCSBytes[1];

            byte[] activeCSBytes = BitConverter.GetBytes(Convert.ToUInt16(activeCS));
            packet[10] = activeCSBytes[0];
            packet[11] = activeCSBytes[1];

            // convert delay into 16bits data, considering 100us as the quanta
            byte[] csToDDelayBytes = BitConverter.GetBytes(Convert.ToUInt16(setup.ChipSelectToDataDelay));
            packet[12] = csToDDelayBytes[0];
            packet[13] = csToDDelayBytes[1];

            byte[] dToCsDelayBytes = BitConverter.GetBytes(Convert.ToUInt16(setup.DataToChipSelectDelay));
            packet[14] = dToCsDelayBytes[0];
            packet[15] = dToCsDelayBytes[1];

            byte[] betweenDataDelayBytes = BitConverter.GetBytes(Convert.ToUInt16(setup.BetweenDataDelay));
            packet[16] = betweenDataDelayBytes[0];
            packet[17] = betweenDataDelayBytes[1];

            // set bytes to transfer
            byte[] bytesToTransfBytes = BitConverter.GetBytes(Convert.ToUInt16(setup.BytesToTransfer));
            packet[18] = bytesToTransfBytes[0];
            packet[19] = bytesToTransfBytes[1];

            // set spi mode
            switch (setup.Mode)
            {
            case SpiModes.Spi0:
                packet[20] = 0;
                break;

            case SpiModes.Spi1:
                packet[20] = 1;
                break;

            case SpiModes.Spi2:
                packet[20] = 2;
                break;

            case SpiModes.Spi3:
                packet[20] = 3;
                break;

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// This method configures the SPI bus.
        /// </summary>
        /// <param name="setup">The SPI setup.</param>
        /// <param name="command">The configuration command.</param>
        /// <param name="subcommand">The sub command configuration. If 0, it's not used.</param>
        protected void ConfigureSpi(SpiSetup setup, byte command, byte subcommand = 0)
        {
            // check input argument
            if (setup.ActiveChipSelectValues == null ||
                setup.ActiveChipSelectValues.Length != Constants.NumberOfGeneralPorpouseLines)
            {
                throw new ArgumentException("Expected " + Constants.NumberOfGeneralPorpouseLines + " active CS lines.");
            }

            if (setup.IdleChipSelectValues == null ||
                setup.IdleChipSelectValues.Length != Constants.NumberOfGeneralPorpouseLines)
            {
                throw new ArgumentException("Expected " + Constants.NumberOfGeneralPorpouseLines + " idle CS lines.");
            }

            if (setup.BetweenDataDelay < 0)
            {
                setup.BetweenDataDelay = 0;
            }

            if (setup.DataToChipSelectDelay < 0)
            {
                setup.DataToChipSelectDelay = 0;
            }

            if (setup.ChipSelectToDataDelay < 0)
            {
                setup.ChipSelectToDataDelay = 0;
            }

            if (setup.BitRate < 0)
            {
                setup.BitRate = 0;
            }

            if (setup.BytesToTransfer < 1)
            {
                setup.BytesToTransfer = 12; // default value
            }

            // create the packet
            byte[] packet = new byte[Constants.PacketsSize];
            packet[0] = command;
            packet[1] = subcommand;
            FromSpiConfigurationToPacket(setup, ref packet);

            // 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();
            }

            // check for errors
            switch (reply[1])
            {
            case ReplyStatusCodes.CompletedSuccessfully:
                break;

            case ReplyStatusCodes.BlockedAccess:
                throw new AccessBlockedException();

            case ReplyStatusCodes.UsbTransferInProgress:
                throw new UsbtransferInProgressException();

            default:
                throw new NotImplementedException();
            }
        }