Beispiel #1
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Enables the SPI slave device which uses a GPIO pin as a slave select.
        ///
        /// NOTE on how this works. When using a GPIO pin as a slave select line
        ///   we still need to open a SPIDev device because we need a device to
        ///   send the data to. The way SPIDev works each spidev device is
        ///   fundamentally associated with a particular slave select line and
        ///   this cannot be changed. The GPIO line will be used as a separate
        ///   slave select but the spidev device specific slave select will also
        ///   be asserted whenever the device is writtent to.
        ///
        ///   In order to use a GPIO as a slave select you must ignore and not
        ///   electrically attach anything to the slave select pin the SPIDEV
        ///   device uses as it will be asserted on each write.
        ///
        ///   In other words, the SPIDev device is needed to shift the data in
        ///   and out, however you must ignore its internal slave select line
        ///   entirely if you wish to use GPIO based slave selects. Otherwise
        ///   any device attached to it will be receive every write to the
        ///   SPIPort no matter which GPIO slave select is also asserted.
        ///
        /// </summary>
        /// <returns>ssHandle - the handle for the Slave Device or null for fail</returns>
        /// <param name="spiSlaveDeviceIn">The GPIO of the pin we use as the slave select</param>
        /// <history>
        ///    21 Dec 14  Cynic - Originally written
        /// </history>
        public SPISlaveDeviceHandle EnableSPIGPIOSlaveDevice(GpioEnum gpioEnum)
        {
            // get first slave device. We need to check we have one
            SPISlaveDeviceHandle ssHandle = GetFirstSlaveDeviceWithFD();

            // sanity check
            if (ssHandle == null)
            {
                throw new Exception("At least one non GPIO Slave Device must be enabled first.");
            }
            // create a new output port
            OutputPortMM gpioSlaveSelect = new OutputPortMM(gpioEnum);

            // set this high by default, most modes have slave selects high and go low to activate
            gpioSlaveSelect.Write(true);

            // create a new slave device handle
            SPISlaveDeviceHandle outHandle = new SPISlaveDeviceHandle(SPISlaveDeviceEnum.SPI_SLAVEDEVICE_GPIO, gpioSlaveSelect);

            if (outHandle == null)
            {
                throw new Exception("Could not create GPIO based slave select device");
            }

            //Console.WriteLine("SPIPort GPIO Slave Device Enabled: "+ gpioEnum.ToString());

            // record that we opened this slave device (so we can close it later)
            openSlaveDevices.Add(outHandle);
            // return the slave device handle
            return(outHandle);
        }
Beispiel #2
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>
 ///    21 Dec 14  Cynic - Originally written
 /// </history>
 public SPISlaveDeviceHandle(SPISlaveDeviceEnum spiSlaveDeviceIn, OutputPortMM gpioSlaveSelectIn)
 {
     spiSlaveDevice  = spiSlaveDeviceIn;
     gpioSlaveSelect = gpioSlaveSelectIn;
 }