コード例 #1
0
ファイル: SPIPortFS.cs プロジェクト: valoni/BBBCSIO
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Enables the SPI slave device. Will throw exceptions if it cannot open the
        /// device
        /// </summary>
        /// <returns>ssHandle - the handle for the Slave Device or null for fail</returns>
        /// <param name="spiSlaveDeviceIn">The SPI Slave Device we are configuring</param>
        /// <history>
        ///    21 Dec 14  Cynic - Originally written
        /// </history>
        public SPISlaveDeviceHandle EnableSPISlaveDevice(SPISlaveDeviceEnum spiSlaveDeviceIn)
        {
            string deviceFileName;

            // set up now
            deviceFileName = BBBDefinitions.SPIDEV_FILENAME;

            // set up the spi device number, this is based off the port
            // NOTE that SPI port 0 goes to /dev/spidev1.x and SPI port 1
            //      goes to /dev/spidev2.x. That is just the way it is
            if (SPIPort == SPIPortEnum.SPIPORT_0)
            {
                deviceFileName = deviceFileName.Replace("%device%", "1");
            }
            else if (SPIPort == SPIPortEnum.SPIPORT_1)
            {
                deviceFileName = deviceFileName.Replace("%device%", "2");
            }
            else
            {
                // should never happen
                throw new Exception("Unknown SPI Port:" + SPIPort.ToString());
            }

            // set up the spi slave number
            if (spiSlaveDeviceIn == SPISlaveDeviceEnum.SPI_SLAVEDEVICE_CS0)
            {
                deviceFileName = deviceFileName.Replace("%slave%", "0");
            }
            else if (spiSlaveDeviceIn == SPISlaveDeviceEnum.SPI_SLAVEDEVICE_CS1)
            {
                deviceFileName = deviceFileName.Replace("%slave%", "1");
            }
            else
            {
                // should never happen
                throw new Exception("Unknown SPI Slave Device:" + spiSlaveDeviceIn.ToString());
            }

            // we open the file. We have to have an open file descriptor
            // note this is an external call. It has to be because the
            // ioctl needs an open file descriptor it can use
            int fd = ExternalFileOpen(deviceFileName, O_RDWR | O_NONBLOCK);

            if (fd <= 0)
            {
                throw new Exception("Could not open spidev file:" + deviceFileName);
            }

            //Console.WriteLine("SPIPort Slave Device Enabled: "+ deviceFileName);

            // create a new slave device handle
            SPISlaveDeviceHandle outHandle = new SPISlaveDeviceHandle(spiSlaveDeviceIn, fd);

            // record that we opened this slave device (so we can close it later)
            openSlaveDevices.Add(outHandle);
            // return the slave device handle
            return(outHandle);
        }
コード例 #2
0
 /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 /// <summary>
 /// Resets the object to the default state
 /// </summary>
 /// <history>
 ///    01 Dec 16  Cynic - Originally written
 /// </history>
 public void Reset()
 {
     spiSlaveDevice       = SPISlaveDeviceEnum.SPI_SLAVEDEVICE_NONE;
     spiDevFileDescriptor = -1;
     GpioSlaveSelect      = null;
     speedInHz            = 0;
     delay_usecs          = 0;
     bits_per_word        = 0;
     cs_change            = 0;
 }
コード例 #3
0
 /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="spiSlaveDeviceIn">The SPI slave device</param>
 /// <param name="spiDevFileDescriptor">The SPI file descriptor</param>
 /// <history>
 ///    01 Dec 16  Cynic - Originally written
 /// </history>
 public SPISlaveDeviceHandle(SPISlaveDeviceEnum spiSlaveDeviceIn, int spiDevFileDescriptorIn)
 {
     spiSlaveDevice       = spiSlaveDeviceIn;
     spiDevFileDescriptor = spiDevFileDescriptorIn;
 }
コード例 #4
0
 /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="spiSlaveDeviceIn">The SPI slave device</param>
 /// <param name="gpioSlaveSelectIn">The GPIO output port acting as the slave select line</param>
 /// <history>
 ///    01 Dec 16  Cynic - Originally written
 /// </history>
 public SPISlaveDeviceHandle(SPISlaveDeviceEnum spiSlaveDeviceIn, OutputPortMM gpioSlaveSelectIn)
 {
     spiSlaveDevice  = spiSlaveDeviceIn;
     gpioSlaveSelect = gpioSlaveSelectIn;
 }