Ejemplo n.º 1
0
 /// <summary>
 /// Writes a byte to the SPI device.
 /// </summary>
 /// <param name="value">The byte to be written to the SPI device.</param>
 public override void WriteByte(byte value)
 {
     _winDevice.Write(new[] { value });
 }
 // Wrapper to send SPI commands two bytes at a time (register -> data)
 private void sendCommand(SpiDevice dev, byte register, byte data)
 {
     dev.Write(new byte[] { register, data });
     
 }
        private async Task StartScenarioAsync()
        {
            String spiDeviceSelector = SpiDevice.GetDeviceSelector();
            IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);

            // 0 = Chip select line to use.
            var ADXL345_Settings = new SpiConnectionSettings(0);

            // 5MHz is the rated speed of the ADXL345 accelerometer.
            ADXL345_Settings.ClockFrequency = 5000000;

            // The accelerometer expects an idle-high clock polarity.
            // We use Mode3 to set the clock polarity and phase to: CPOL = 1, CPHA = 1.
            ADXL345_Settings.Mode = SpiMode.Mode3;

            // If this next line crashes with an ArgumentOutOfRangeException,
            // then the problem is that no SPI devices were found.
            //
            // If the next line crashes with Access Denied, then the problem is
            // that access to the SPI device (ADXL345) is denied.
            //
            // The call to FromIdAsync will also crash if the settings are invalid.
            //
            // FromIdAsync produces null if there is a sharing violation on the device.
            // This will result in a NullReferenceException a few lines later.
            adxl345Sensor = await SpiDevice.FromIdAsync(devices[0].Id, ADXL345_Settings);

            // 
            // Initialize the accelerometer:
            //
            // For this device, we create 2-byte write buffers:
            // The first byte is the register address we want to write to.
            // The second byte is the contents that we want to write to the register. 
            //

            // 0x31 is address of data format register, 0x01 sets range to +- 4Gs.
            byte[] WriteBuf_DataFormat = new byte[] { 0x31, 0x01 };

            // 0x2D is address of power control register, 0x08 puts the accelerometer into measurement mode.
            byte[] WriteBuf_PowerControl = new byte[] { 0x2D, 0x08 };

            // Write the register settings.
            //
            // If this next line crashes with a NullReferenceException, then
            // there was a sharing violation on the device.
            // (See comment earlier in this function.)
            //
            // If this next line crashes for some other reason, then there was
            // an error communicating with the device.

            adxl345Sensor.Write(WriteBuf_DataFormat);
            adxl345Sensor.Write(WriteBuf_PowerControl);

            // Start the polling timer.
            timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) };
            timer.Tick += Timer_Tick;
            timer.Start();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Writes a byte to the SPI device.
 /// </summary>
 /// <param name="data">The byte to be written to the SPI device.</param>
 public override void WriteByte(byte data)
 {
     _winDevice.Write(new[] { data });
 }
Ejemplo n.º 5
0
        /* Initialization for SPI accelerometer */
        private async void InitSPIAccel()
        {
            try {
                var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
                settings.ClockFrequency = 5000000;                              /* 5MHz is the rated speed of the ADXL345 accelerometer                     */
                settings.Mode = SpiMode.Mode3;                                  /* The accelerometer expects an idle-high clock polarity, we use Mode3    
                                                                                 * to set the clock polarity and phase to: CPOL = 1, CPHA = 1         
                                                                                 */

                string aqs = SpiDevice.GetDeviceSelector();                     /* Get a selector string that will return all SPI controllers on the system */
                var dis = await DeviceInformation.FindAllAsync(aqs);            /* Find the SPI bus controller devices with our selector string             */
                SPIAccel = await SpiDevice.FromIdAsync(dis[0].Id, settings);    /* Create an SpiDevice with our bus controller and SPI settings             */
                if (SPIAccel == null)
                {
                    Text_Status.Text = string.Format(
                        "SPI Controller {0} is currently in use by " +
                        "another application. Please ensure that no other applications are using SPI.",
                        dis[0].Id);
                    return;
                }
            }
            catch (Exception ex)
            {
                Text_Status.Text = "SPI Initialization failed. Exception: " + ex.Message;
                return;
            }
                
            /* 
             * Initialize the accelerometer:
             *
             * For this device, we create 2-byte write buffers:
             * The first byte is the register address we want to write to.
             * The second byte is the contents that we want to write to the register. 
             */
            byte[] WriteBuf_DataFormat = new byte[] { ACCEL_REG_DATA_FORMAT, 0x01 };        /* 0x01 sets range to +- 4Gs                         */
            byte[] WriteBuf_PowerControl = new byte[] { ACCEL_REG_POWER_CONTROL, 0x08 };    /* 0x08 puts the accelerometer into measurement mode */

            /* Write the register settings */
            try
            {  
                SPIAccel.Write(WriteBuf_DataFormat);
                SPIAccel.Write(WriteBuf_PowerControl);
            }
            /* If the write fails display the error and stop running */
            catch (Exception ex)
            {
                Text_Status.Text = "Failed to communicate with device: " + ex.Message;
                return;
            }

            /* Now that everything is initialized, create a timer so we read data every 100mS */
            periodicTimer = new Timer(this.TimerCallback, null, 0, 100);
        }
Ejemplo n.º 6
0
        private async Task Init()
        {
            var deviceSelector = I2cDevice.GetDeviceSelector();
            var controllers = await DeviceInformation.FindAllAsync(deviceSelector);

            _mcp3425 = await I2cDevice.FromIdAsync(controllers[0].Id, new I2cConnectionSettings(0x68) { BusSpeed = I2cBusSpeed.FastMode });
            _mcp3425.Write(new byte[] { 0x98 }); // 16bit

            _tsl2561 = await I2cDevice.FromIdAsync(controllers[0].Id, new I2cConnectionSettings(0x39) { BusSpeed = I2cBusSpeed.FastMode });
            _tsl2561.Write(new byte[] { 0x80, 0x03 }); // power on :)

            // spi stuff
            var settings = new SpiConnectionSettings(0);
            settings.ClockFrequency = 10000000;
            settings.Mode = SpiMode.Mode0;

            string spiAqs = SpiDevice.GetDeviceSelector("SPI0");
            var deviceInformation = await DeviceInformation.FindAllAsync(spiAqs);
            _spiDevice = await SpiDevice.FromIdAsync(deviceInformation[0].Id, settings);
            endFrame = new byte[(int)Math.Ceiling(leds / 16d)];
            for (var i = 0; i < endFrame.Length; i++)
            {
                endFrame[i] = 0x0;
            }

            // start
            _spiDevice.Write(new byte[4]);

            // all off
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });
            _spiDevice.Write(new byte[] { 0xE0, 0x0, 0x0, 0x0 });

            // end
            _spiDevice.Write(endFrame);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Writes a byte to the SPI device.
 /// </summary>
 /// <param name="value">The byte to be written to the SPI device.</param>
 public override void WriteByte(byte value)
 {
     _winDevice.Write(new[] { _isInverted?ReverseByte(value) : value });
 }
Ejemplo n.º 8
0
        internal async Task Initialize(byte frequency, byte nodeId, byte networkId)
        {
            var settings = new SpiConnectionSettings(0);
            settings.ClockFrequency = 5000000;                              /* 5MHz is the rated speed of the ADXL345 accelerometer                     */
            settings.Mode = SpiMode.Mode3;

            string aqs = SpiDevice.GetDeviceSelector();                     /* Get a selector string that will return all SPI controllers on the system */
            var dis = await DeviceInformation.FindAllAsync(aqs);            /* Find the SPI bus controller devices with our selector string             */
            _spiDevice = await SpiDevice.FromIdAsync(dis[0].Id, settings);    /* Create an SpiDevice with our bus controller and SPI settings             */

            _spiDevice.ConnectionSettings.Mode = SpiMode.Mode0;
            _spiDevice.ConnectionSettings.ClockFrequency = 5000;

            _gpioController = GpioController.GetDefault();

            _selectpin = _gpioController.OpenPin(22);
            _selectpin.SetDriveMode(GpioPinDriveMode.Output);

            byte[][] config =
            {
                new byte[] { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY },
                new byte[] { REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 },
                new byte[] { REG_BITRATEMSB, RF_BITRATEMSB_55555},
                new byte[] { REG_BITRATELSB, RF_BITRATELSB_55555},
                new byte[] { REG_FDEVMSB, RF_FDEVMSB_50000},
                new byte[] { REG_FDEVLSB, RF_FDEVLSB_50000},
                new byte[] { REG_FRFMSB, RF_FRFMSB_915 },
    /* 0x08 */ new byte[] { REG_FRFMID, RF_FRFMID_915 },
    /* 0x09 */ new byte[] { REG_FRFLSB, RF_FRFLSB_915 },
                new byte[] { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2 }, 
                new byte[] { REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01 },
                new byte[] { REG_DIOMAPPING2, RF_DIOMAPPING2_CLKOUT_OFF },
                new byte[]{ REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN },
                new byte[]{ REG_RSSITHRESH, 220 },
                new byte[] { REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0 },
                new byte[]{ REG_SYNCVALUE1, 0x2D },      // attempt to make this compatible with sync1 byte of RFM12B lib
    /* 0x30 */ new byte[]{ REG_SYNCVALUE2, networkId }, // NETWORK ID
    /* 0x37 */ new byte[]{ REG_PACKETCONFIG1, RF_PACKET1_FORMAT_VARIABLE | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_ON | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF },
    /* 0x38 */ new byte[]{ REG_PAYLOADLENGTH, 66 }, // in variable length mode: the max frame size, not used in TX
    //* 0x39 */ { REG_NODEADRS, nodeID }, // turned off because we're not using address filtering
    /* 0x3C */ new byte[]{ REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | RF_FIFOTHRESH_VALUE }, // TX on FIFO not empty
    /* 0x3D */ new byte[]{ REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_2BITS | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, // RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
    //for BR-19200: /* 0x3D */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, // RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent)
    /* 0x6F */ new byte[]{ REG_TESTDAGC, RF_DAGC_IMPROVED_LOWBETA0 }, // run DAGC continuously in RX mode for Fading Margin Improvement, recommended default for AfcLowBetaOn=0
                
            };

            foreach (var configValue in config)
            {
                _spiDevice.Write(configValue);
            }

            SetHighPower();


        }