Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OLEDCClick"/> class.
        /// </summary>
        /// <param name="socket">The socket on which the OLEDc Click board is inserted on MikroBus.Net</param>
        /// <exception cref="PinInUseException">If some pins are already used by another driver, then the exception is thrown.</exception>
        /// <example>Example usage:
        /// <code language = "C#">
        ///	var _oled = new OLEDCClick(Hardware.SocketFour);
        /// </code>
        /// <code language = "VB">
        ///	Dim _oled as OLEDCClick = New OLEDCClick(Hardware.SocketFour)
        /// </code>
        /// </example>
        public OLEDCClick(Hardware.Socket socket)
        {
            try
            {
                Hardware.CheckPins(socket, socket.Miso, socket.Cs, socket.Sck, socket.Rst, socket.An, socket.Pwm);

                _spiConfig = new SPI.Configuration(socket.Cs, false, 0, 0, true, true, 40000, socket.SpiModule);

                if (Hardware.SPIBus == null)
                {
                    Hardware.SPIBus = new SPI(_spiConfig);
                }

                _resetPin = new OutputPort(socket.Rst, true);
                _dcPin    = new OutputPort(socket.Pwm, true);
                _rwPin    = new OutputPort(socket.An, true);

                _canvas = new MikroBitmap(_canvasWidth, _canvasHeight);

                InitilizeOLED();
            }
            catch (PinInUseException ex)
            {
                throw new PinInUseException(ex.Message);
            }
        }
Пример #2
0
 /// <summary>
 /// Constructor intializes read and write at 10mhz
 /// </summary>
 /// <param name="ChipSelectPin">CS pin</param>
 /// <param name="HoldPin">HOLD pin</param>
 public RamStream(Cpu.Pin ChipSelectPin, Cpu.Pin HoldPin)
 {
     hold_port = new OutputPort(HoldPin, true);
     hold_port.Write(true);
     SPI.Configuration config = new SPI.Configuration(ChipSelectPin, false, 0, 0, false, true, 10000, SPI.SPI_module.SPI1);
     spi_driver = new SPI(config);
 }
Пример #3
0
        /// <summary>Constructs a new instance.</summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public DisplayN18(int socketNumber)
            : base(WpfMode.Separate)
        {
            this.byteArray  = new byte[1];
            this.shortArray = new ushort[2];
            this.isBgr      = true;

            this.socket = Socket.GetSocket(socketNumber, true, this, null);
            this.socket.EnsureTypeIsSupported('S', this);

            this.resetPin     = GTI.DigitalOutputFactory.Create(this.socket, Socket.Pin.Three, false, this);
            this.backlightPin = GTI.DigitalOutputFactory.Create(this.socket, Socket.Pin.Four, true, this);
            this.rsPin        = GTI.DigitalOutputFactory.Create(this.socket, Socket.Pin.Five, false, this);

            this.spiConfig      = new GTI.SpiConfiguration(false, 0, 0, false, true, 12000);
            this.netMFSpiConfig = new SPI.Configuration(this.socket.CpuPins[6], this.spiConfig.IsChipSelectActiveHigh, this.spiConfig.ChipSelectSetupTime, this.spiConfig.ChipSelectHoldTime, this.spiConfig.IsClockIdleHigh, this.spiConfig.IsClockSamplingEdgeRising, this.spiConfig.ClockRateKHz, this.socket.SPIModule);
            this.spi            = GTI.SpiFactory.Create(this.socket, this.spiConfig, GTI.SpiSharing.Shared, this.socket, Socket.Pin.Six, this);

            this.Reset();

            this.ConfigureDisplay();

            base.OnDisplayConnected("Display N18", 128, 160, DisplayOrientation.Normal, null);

            this.Clear();
        }
Пример #4
0
        public bool InitCAN(enBaudRate baudrate, UInt16 filter, UInt16 mask)
        {
            // Configure SPI
            var configSPI = new SPI.Configuration(Pins.GPIO_NONE, LOW, 0, 0, HIGH, HIGH, 10000, SPI.SPI_module.SPI1);  //a0 D10

            spi = new SPI(configSPI);

            // Write reset to the CAN transceiver.
            //           spi.Write(new byte[] { RESET });
            //Read mode and make sure it is config
            Thread.Sleep(100);
            // BECKER WHAT IS HAPPENING HERE.
//            byte mode = (byte)(ReadRegister(CANSTAT));  // >> 5);
//            if (mode != 0x04)
//            {
//                return false;
//            }
//            else

            //  Must set the filters and masks while the 2515 is in reset state.

            //SetMask(mask, filter);
            //SetCANBaud(baudrate);
            return(true);
        }
Пример #5
0
        public static void Main()
        {
            SerialPort UART = new SerialPort("COM1", 9600);

            SPI.Configuration config = new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di10, false, 0, 0, true, true, 250, SPI.SPI_module.SPI1);
            SPI Spi = new SPI(config);

            UART.Open();
            string Datoreaded = "";

            byte[] datain  = new byte[1];
            byte[] dataout = new byte[1];
            while (true)
            {
                // Sleep for 500 milliseconds
                Thread.Sleep(500);

                dataout[0] = (byte)0x55;
                datain[0]  = (byte)0;
                Spi.WriteRead(dataout, datain);
                Datoreaded  = datain[0].ToString();
                Datoreaded += "\n\r";
                byte[] buffer = Encoding.UTF8.GetBytes(Datoreaded);
                UART.Write(buffer, 0, buffer.Length);
            }
        }
Пример #6
0
        /// <summary>
        /// Creates a new NeoPixelSPI with a given chip-select pin, using a given SPI module
        /// </summary>
        /// <param name="chipSelectPin">chip-select pin</param>
        /// <param name="spiModule">SPI module</param>
        public NeoPixelSPI(Cpu.Pin chipSelectPin, SPI.SPI_module spiModule)
        {
            uint speed = 6666; // 6.666 MHz

            SPI.Configuration spiConfig = new SPI.Configuration(chipSelectPin, false, 0, 0, false, true, speed, spiModule);
            this.spi = new SPI(spiConfig);
        }
Пример #7
0
        public static void Main()
        {
            SPI.Configuration spiConfig = new SPI.Configuration(
                ShieldConfiguration.CurrentConfiguration.SpiChipSelectPin,
                false,
                100,
                100,
                false,
                true,
                1000,
                ShieldConfiguration.CurrentConfiguration.SpiModule
                );
            var spi          = new SPI(spiConfig);
            var statusBuffer = new byte[2];

            // Watch the LEDs on UberShield. If they are showing the bootloader
            // flashing pattern, there's no SPI connectivity. If the lights
            // alternate off / red / green / redgreen then you're quad-winning.
            // If they're off, you're not in bootloader mode.

            while (true)
            {
                statusBuffer[0] = 0x01;
                for (byte counter = 0; counter <= 3; ++counter)
                {
                    statusBuffer[1] = (byte)((counter << 2) | 0x03);
                    spi.Write(statusBuffer);
                    Thread.Sleep(500);
                }
            }
        }
Пример #8
0
        public AdaFruitLPD8806(int width, int height, Cpu.Pin chipSelect, SPI.SPI_module spiModule = SPI.SPI_module.SPI1, uint speedKHz = 10000)
        {
            Width          = width;
            Height         = height;
            PixelCount     = Width * Height;
            PixelBufferEnd = (PixelCount - 1) * BytesPerPixel;
            FrameSize      = Width * Height * BytesPerPixel;

            var spiConfig = new SPI.Configuration(
                SPI_mod: spiModule,
                ChipSelect_Port: chipSelect,
                ChipSelect_ActiveState: false,
                ChipSelect_SetupTime: 0,
                ChipSelect_HoldTime: 0,
                Clock_IdleState: false,
                Clock_Edge: true,
                Clock_RateKHz: speedKHz
                );

            spi = new SPI(spiConfig);

            pixelBuffer = new byte[PixelCount * BytesPerPixel];

            SetBackgroundColor(0, 0, 0);
        }
Пример #9
0
        private void NativeBitmapSpi(Bitmap bitmap, SPI.Configuration config, int xSrc, int ySrc, int width, int height, GT.Mainboard.BPP bpp)
        {
            if (bpp != GT.Mainboard.BPP.BPP16_BGR_BE)
            {
                throw new ArgumentOutOfRangeException("bpp", "Only BPP16_BGR_BE supported");
            }

            if (!this.configSet)
            {
                Display.BitmapFormat     = GHI.Utilities.Bitmaps.Format.Bpp16BgrBe;
                Display.CurrentRotation  = Display.Rotation.Normal;
                Display.Type             = Display.DisplayType.Spi;
                Display.ControlPin       = Cpu.Pin.GPIO_NONE;
                Display.BacklightPin     = Cpu.Pin.GPIO_NONE;
                Display.ResetPin         = Cpu.Pin.GPIO_NONE;
                Display.SpiConfiguration = config;
                Display.Width            = bitmap.Width;
                Display.Height           = bitmap.Height;

                Display.Save();

                this.configSet = true;
            }

            bitmap.Flush(xSrc, ySrc, width, height);
        }
Пример #10
0
        public EPDBase(Cpu.Pin chipSelectPin, Cpu.Pin dcPin, Cpu.Pin resetPin, Cpu.Pin busyPin, SPI.SPI_module spiModule = SPI.SPI_module.SPI1, uint speedKHz = (uint)9500)
        {
            dataCommandPort = new OutputPort(dcPin, false);
            resetPort       = new OutputPort(resetPin, true);
            busyPort        = new InputPort(busyPin, true, Port.ResistorMode.Disabled);

            var spiConfig = new SPI.Configuration(
                SPI_mod: spiModule,
                ChipSelect_Port: chipSelectPin,
                ChipSelect_ActiveState: false,
                ChipSelect_SetupTime: 0,
                ChipSelect_HoldTime: 0,
                Clock_IdleState: false,
                Clock_Edge: true,
                Clock_RateKHz: speedKHz);

            spi = new SPI(spiConfig);

            imageBuffer = new byte[Width * Height / 8];

            for (int i = 0; i < Width * Height / 8; i++)
            {
                imageBuffer[i] = 0xff;
            }

            Initialize();
        }
Пример #11
0
        /// <summary>Initialize the CAN transceiver.</summary>
        /// <param name="baudrate">The selected baud rate.</param>
        /// <returns>True if configuration was successful.</returns>
        /// <remarks>Transceiver needs to be set to normal mode before starting TX/RX operations.</remarks>
        public bool InitCAN(SPI.SPI_module spiModule, Cpu.Pin chipSelect, byte[] baudrate)
        {
            if (baudrate.Length != 3)
            {
                throw new Exception("Baudrate settings should be 3-byte array of CNF1,2,3 registers.");
            }

            // Configure SPI
            var configSPI = new SPI.Configuration(chipSelect, LOW, 0, 0, HIGH, HIGH, 10000, spiModule);

            spi = new SPI(configSPI);

            // Write reset to the CAN transceiver.
            spi.Write(new byte[] { RESET });
            //Read mode and make sure it is config
            Thread.Sleep(100);

            byte mode = (byte)(ReadRegister(CANSTAT) >> 5);

            if (mode != 0x04)
            {
                return(false);
            }
            else
            {
                SetCANBaud(baudrate);
                return(true);
            }
        }
Пример #12
0
        /// <summary>
        /// Main program loop.
        /// </summary>
        public static void Main()
        {
            SPI.Configuration spiConfig = new SPI.Configuration(
                ChipSelect_Port: Pins.GPIO_PIN_D7,                  // Chip select is digital IO 7.
                ChipSelect_ActiveState: false,                      // Chip select is active low.
                ChipSelect_SetupTime: 0,                            // Amount of time between selection and the clock starting
                ChipSelect_HoldTime: 0,                             // Amount of time the device must be active after the data has been read.
                Clock_Edge: false,                                  // Sample on the falling edge.
                Clock_IdleState: false,                             // Clock is idle when low.
                Clock_RateKHz: 10,                                  // 10KHz clock speed.
                SPI_mod: SPI_Devices.SPI1                           // Use SPI1
                );

            SPI spi   = new SPI(spiConfig);
            int count = 0;

            while (true)
            {
                byte[] dataOut = new byte[] { 0 };
                byte[] dataIn  = new byte[REG_SIZE];
                spi.WriteRead(dataOut, dataIn, 1);
                count++;
                string message = "Read " + count.ToString() + ":";
                for (int index = 0; index < dataIn.Length; index++)
                {
                    message += " " + Hexadecimal(dataIn[index]);
                }
                Debug.Print(message);
                Debug.Print(DecodeTime(dataIn));
                Thread.Sleep(2000);
            }
        }
Пример #13
0
        public NRFC(Hardware.Socket socket)
        {
            Hardware.CheckPins(socket, socket.Miso, socket.Mosi, socket.Cs, socket.Sck, socket.Rst, socket.Int);

            _spiConfig = new SPI.Configuration(socket.Cs, false, 0, 0, false, true, 2000, socket.SpiModule);
            if (Hardware.SPIBus == null)
            {
                Hardware.SPIBus = new SPI(_spiConfig);
            }

            // Initialize IRQ Port
            _irqPin              = new InterruptPort(socket.Int, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            _irqPin.OnInterrupt += HandleInterrupt;

            // Initialize Chip Enable Port
            _cePin = new OutputPort(socket.Rst, false);

            // Module reset time
            Thread.Sleep(100);

            _initialized = true;

            _transmitSuccessFlag = new ManualResetEvent(false);
            _transmitFailedFlag  = new ManualResetEvent(false);
        }
Пример #14
0
        public PCD8544(Cpu.Pin chipSelectPin, Cpu.Pin dcPin, Cpu.Pin resetPin,
                       SPI.SPI_module spiModule = SPI.SPI_module.SPI1,
                       uint speedKHz            = 4000)
        {
            spiBuffer = new byte[Width * Height / 8];

            dataCommandPort = new OutputPort(dcPin, true);
            resetPort       = new OutputPort(resetPin, true);

            /*  var spiConfig = new SPI.Configuration(
             *    chipSelectPin,          // chip select port
             *    false,                  // IC is accessed when chip select is low
             *    0,                      // setup time 1 ms
             *    0,                      // hold chip select 1 ms after transfer
             *    false,                  // clock line is low if device is not selected
             *    true,                   // data is sampled at leading edge of clock
             *    speedKHz,               // clockrate is 4 kHz
             *    spiModule               // use first SPI bus
             *    );*/

            var spiConfig = new SPI.Configuration(
                SPI_mod: spiModule,
                ChipSelect_Port: chipSelectPin,
                ChipSelect_ActiveState: false,
                ChipSelect_SetupTime: 0,
                ChipSelect_HoldTime: 0,
                Clock_IdleState: false,
                Clock_Edge: true,
                Clock_RateKHz: speedKHz);

            spi = new SPI(spiConfig);

            Initialize();
        }
Пример #15
0
        public DisplayTFTSPIBase(Cpu.Pin chipSelectPin, Cpu.Pin dcPin, Cpu.Pin resetPin,
                                 uint width, uint height,
                                 SPI.SPI_module spiModule = SPI.SPI_module.SPI1,
                                 uint speedKHz            = 9500, bool idleClockState = false)
        {
            _width  = width;
            _height = height;

            spiBuffer = new byte[_width * _height * sizeof(ushort)];

            dataCommandPort = new OutputPort(dcPin, false);
            resetPort       = new OutputPort(resetPin, true);

            var spiConfig = new SPI.Configuration(
                SPI_mod: spiModule,
                ChipSelect_Port: chipSelectPin,
                ChipSelect_ActiveState: false,
                ChipSelect_SetupTime: 0,
                ChipSelect_HoldTime: 0,
                Clock_IdleState: idleClockState,
                Clock_Edge: true,
                Clock_RateKHz: speedKHz);

            spi = new SPI(spiConfig);
        }
Пример #16
0
        void Initialize(GoSocket socket)
        {
            // now try to bind to the socket (and verify our module's uniqueId)
            if (!base.BindSocket(socket, _moduleGuid))
            {
                throw new ArgumentException();
            }

            // get socket's physical pins and SPI bus
            Cpu.Pin        socketGpioPin;
            SPI.SPI_module socketSpiModule;
            Cpu.Pin        socketSpiSlaveSelectPin;
            //
            socket.GetPhysicalResources(out socketGpioPin, out socketSpiModule, out socketSpiSlaveSelectPin);

            _spiConfig = new SPI.Configuration(socketSpiSlaveSelectPin, false, 0, 0, false, false, 500, socketSpiModule);
            _spi       = new SPI(_spiConfig);

            // wire up event handlers
            _interruptPort              = new InterruptPort((Cpu.Pin)socketGpioPin, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            _interruptPort.OnInterrupt += _interruptPort_OnInterrupt;

            // read initial button state
            _isPressed = !_interruptPort.Read();
        }
Пример #17
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="csPin">CS pin for SPI interface</param>
 /// <param name="spiModule">SPI module</param>
 public LIS302DL(Cpu.Pin csPin, SPI.SPI_module spiModule)
 {
     //The 302DL is a mode 3 device
     SPI.Configuration spiConfig = new SPI.Configuration(csPin, false, 0, 0, true, true, 10000, spiModule);
     _spi = new SPI(spiConfig);
     Init();
 }
Пример #18
0
        public static void Main()
        {
            var config = new SPI.Configuration(SPI_mod: SPI_Devices.SPI1,
                                               ChipSelect_Port: Pins.GPIO_PIN_D8,
                                               ChipSelect_ActiveState: false,
                                               ChipSelect_SetupTime: 0,
                                               ChipSelect_HoldTime: 0,
                                               Clock_IdleState: true,
                                               Clock_Edge: true,
                                               Clock_RateKHz: 10);

            var shiftRegister = new x74595(8, config);

            DigitalOutputPort relayPort = shiftRegister.CreateOutputPort(0, false);

            var relay = new Relay(relayPort);

            while (true)
            {
                // toggle the relay
                relay.Toggle();

                Debug.Print("Relay on: " + relay.IsOn.ToString());

                // wait for 5 seconds
                Thread.Sleep(1000);
            }
        }
Пример #19
0
        public Adafruit1306OledDriver(
            Cpu.Pin dc,
            Cpu.Pin reset,
            Cpu.Pin chipSelect,
            SPI.SPI_module spiModule = SPI.SPI_module.SPI1,
            uint speedKHz            = 10000)
        {
            AutoRefreshScreen = true;

            var spiConfig = new SPI.Configuration(
                SPI_mod: spiModule,
                ChipSelect_Port: chipSelect,
                ChipSelect_ActiveState: false,
                ChipSelect_SetupTime: 0,
                ChipSelect_HoldTime: 0,
                Clock_IdleState: false,
                Clock_Edge: true,
                Clock_RateKHz: speedKHz
                );

            this.Spi = new SPI(spiConfig);

            this.DcPin    = new OutputPort(dc, false);
            this.ResetPin = new OutputPort(reset, false);
        }
Пример #20
0
        /// <summary>
        ///     Default constructor for the Current Click board.
        /// </summary>
        /// <param name="socket">The MBN Socket that the Current click is inserted into.</param>
        /// <param name="shuntResistorResistorValue">The value in Ohms of the resistor in the ShuntResistor Terminal Block</param>
        /// <param name="userDefinedShuntResistor">
        ///     Optional User Defined value of the ShuntResistor Resistor.
        ///     To use a User Defined ShuntResistor Resistor, set the value of the <see cref="ShuntResistor"/> to SR_CUSTOM
        /// </param>
        public CurrentClick(Hardware.Socket socket, ShuntResistor shuntResistorResistorValue, float userDefinedShuntResistor = 1f)
        {
            try
            {
                Hardware.CheckPins(socket, socket.Miso, socket.Mosi, socket.Cs, socket.Sck);

                _spiConfig = new SPI.Configuration(socket.Cs, false, 0, 0, true, true, 1000, socket.SpiModule);

                if (Hardware.SPIBus == null)
                {
                    Hardware.SPIBus = new SPI(_spiConfig);
                }

                if (shuntResistorResistorValue == ShuntResistor.SR_CUSTOM)
                {
                    _userdefinedShuntResistorValue = userDefinedShuntResistor;
                }
                else
                {
                    _shuntResistorResistorValue = shuntResistorResistorValue;
                }
            }
            // Catch only the PinInUse exception, so that program will halt on other exceptions
            // Send it directly to caller
            catch (PinInUseException ex)
            {
                throw new PinInUseException(ex.Message);
            }
        }
Пример #21
0
        private byte _currentResistance;                     // Resistance

        /// <summary>
        /// Main class for the MikroE Digipot Click board driver
        /// </summary>
        /// <param name="socket">The socket on which the Digipot Click board is plugged on MikroBus.Net</param>
        /// <param name="initialResistance">The initial resistance the Digipot should be initialized with.</param>
        /// <exception cref="System.InvalidOperationException">Thrown if some pins are already in use by another board on the same socket</exception>
        public DigiPotClick(Hardware.Socket socket, byte initialResistance)
        {
            try
            {
                // Checks if needed SPI pins are available
                Hardware.CheckPins(socket, socket.Cs, socket.Sck, socket.Mosi, socket.Miso);

                // Initialize SPI
                _spiConfig = new SPI.Configuration(
                    socket.Cs,                              //   Cpu.Pin ChipSelect_Port,
                    false,                                  //   bool ChipSelect_ActiveState,
                    10,                                     //   uint ChipSelect_SetupTime,
                    20,                                     //   uint ChipSelect_HoldTime,
                    true,                                   //   bool Clock_IdleState,
                    false,                                  //   bool Clock_Edge,
                    10000,                                  //   uint Clock_RateKHz,
                    socket.SpiModule);
                if (Hardware.SPIBus == null)
                {
                    Hardware.SPIBus = new SPI(_spiConfig);
                }

                _currentResistance = initialResistance;
            }
            // Catch only the PinInUse exception, so that program will halt on other exceptions
            // Send it directly to caller
            catch (PinInUseException) { throw new PinInUseException(); }
        }
Пример #22
0
    /// <summary>
    /// c'tor
    /// </summary>
    /// <param name="socket">the Gadgeteer socket that the strip is on</param>
    /// <param name="numLeds">the number of LEDs in the strip</param>
    public LedStripLPD8806(GT.Socket socket, int numLeds)
    {
        var spiConfig = new SPI.Configuration(Cpu.Pin.GPIO_NONE,
                                                            false, // chip select active state
                                                            0, // chip select setup time
                                                            0, // chip select hold time
                                                            false, // clock idle state
                                                            true, // clock edge (true = rising)
                                                            2000,   // 2mhz
                                                            SPI.SPI_module.SPI1
                                                            );

        // the protocol seems to be that we need to write 1 + (1 per 64 LEDs) bytes
        // at the end of each update (I've only tested this on a 32-LED strip)
        int latchBytes = ((numLeds + 63) / 64) * 3;
        mLedByteCount = numLeds * 3;

        mData = new byte[mLedByteCount + latchBytes];
        mNumLeds = numLeds;
        //        mLedStrip = new SPI(socket, spiConfig, SPI.Sharing.Exclusive, null);
        mLedStrip = new SPI(spiConfig);

        // start with all the LEDs off
        for (int i = 0; i < mLedByteCount; i++)
        {
            mData[i] = MASK;
        }

        // give the strip an inital poke of the latch bytes (no idea
        // why this is needed)
        mLedStrip.Write(new byte[latchBytes]);

        // push the initial values (all off) to the strip
        SendUpdate();
    }
Пример #23
0
        private byte[] _tmpPixel = new byte[12];    // Temporary buffer
        #endregion

        #region Constructor and Destructor
        /// <summary>
        /// WS2811Led constructor.
        /// </summary>
        /// <param name="striplength">Number of leds in the strip. Warning : will alocate a memory buffer 12 times this number.</param>
        /// <param name="SPImodule">The SPI module the strip is connected to. Only the MOSI line is used. SPI1 is used if parameter omited.</param>
        /// <param name="speed">The Hardwired speed of the strip. 800Khz is used if parameter is omited.</param>
        /// <param name="linearizationfactor">If set to a value >0 (default value=2.25) will build the lookup table using human linear perceived luminosity instead of direct PWM values.</param>
        public WS2811Led(int striplength, SPI.SPI_module SPImodule = SPI.SPI_module.SPI1, WS2811Speed speed = WS2811Speed.S800KHZ, double linearizationfactor = 2.25)
        {
            _StripLength    = striplength;
            _StripLightNb   = striplength * 3;  //For later speed optimizations
            _StripBufferLen = striplength * 12; //For later speed optimizations

            // Initialize SPI
            // Set clock edge to false to remove the failing initial LED
            _WS2811SPIConfig = new SPI.Configuration(Cpu.Pin.GPIO_NONE, false, 0, 0, false, false, (uint)speed, SPImodule);
            try
            {
                _WS2811SPI = new SPI(_WS2811SPIConfig);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                throw;
            }


            // SPI Transmit buffer = 4 output byte per color, 3 colors per light
            _WS2811Buffer = new byte[4 * 3 * striplength];

            // Compute fast byte to SPI lookup table. By default the linearisation of human perceived luminosity is on (Weber–Fechner law)
            _WS2811Table = new byte[1024];
            BuildTable(linearizationfactor);

            //Clear all leds
            Clear();
            Transmit();
        }
Пример #24
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="csPin">CS pin for SPI interface</param>
        /// <param name="spiModule">SPI module</param>
        /// <param name="clockRateKhz">SPI clock rate (defaults to 1MHZ in other constructors)</param>
        public LIS302DL(Cpu.Pin csPin, SPI.SPI_module spiModule, uint clockRateKhz)
        {
            var spiConfig = new SPI.Configuration(csPin, false, 0, 0, true, true, clockRateKhz, spiModule);

            _spi = new SPI(spiConfig);
            Init();
        }
Пример #25
0
        /// <summary>
        /// Method for initializing the decoder. Must be called before ANY other method.
        /// </summary>
        public static void Initialize()
        {
            cancelPlayback = false;

            SPI.SPI_module spi_module;
            spi_module = SPI.SPI_module.SPI1;

            dataConfig = new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di2, false, 0, 0, false, true, 2000, spi_module);
            cmdConfig  = new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di9, false, 0, 0, false, true, 2000, spi_module);
            DREQ       = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di3, false, Port.ResistorMode.PullUp);

            spi = new SPI(dataConfig);

            Reset();

            SCIWrite(SCI_MODE, SM_SDINEW);
            SCIWrite(SCI_CLOCKF, 0x98 << 8);
            SCIWrite(SCI_VOL, 0x2828);

            //ClearPlayback();

            if (SCIRead(SCI_VOL) != (0x2828))
            {
                throw new Exception("VS1053: Failed to initialize MP3 Decoder.");
            }
            else
            {
                Debug.Print("VS1053: Initialized MP3 Decoder.");
            }
        }
Пример #26
0
        public ADC124S101(Cpu.Pin chipSelectPin,
                          float supplyVoltage,
                          uint clockRateKHz,
                          SPI.SPI_module spiModule)
        {
            if (chipSelectPin == Cpu.Pin.GPIO_NONE)
            {
                throw new ArgumentOutOfRangeException("chipSelectPin");
            }
            if (supplyVoltage <= 0.0f)
            {
                throw new ArgumentOutOfRangeException("supplyVoltage");
            }
            this.chipSelectPin = chipSelectPin;
            this.supplyVoltage = supplyVoltage;

            SPI.Configuration config = new SPI.Configuration(
                chipSelectPin,                                  //chip select port
                false,                                          //IC is accessed when chip select is low
                1,                                              //setup time 1 ms, is actually min 10 ns
                1,                                              //hold chip select 1 ms after transfer
                true,                                           //clock line is high if device is not selected
                false,                                          //data is sampled at falling edge of clock
                clockRateKHz,                                   //possible 10000 - 20000 KHz
                spiModule                                       //select SPI bus
                );
            this.spi = new SPI(config);
        }
Пример #27
0
        private void ConfigureBusForDevice(SpiDeviceSettings settings)
        {
            int newHash = settings.GetHashCode();

            if (newHash == _lastConfigHash)
            {
                return;
            }
            SPI.Configuration result;
            if (_knownConfigs.Contains(settings))
            {
                result = (SPI.Configuration)_knownConfigs[settings];
            }
            else
            {
                result = new SPI.Configuration(settings.SoftChipSelectEnabled ? Cpu.Pin.GPIO_NONE : (Cpu.Pin)settings.ChipSelectPin,
                                               settings.ChipSelectActiveState,
                                               settings.ChipSelectSetupTime,
                                               settings.ChipSelectHoldTime,
                                               settings.ClockIdleState,
                                               settings.ClockEdge,
                                               settings.ClockRateKHz,
                                               _module);
                _knownConfigs.Add(settings, result);
            }
            _spotSpi.Config = result;
            _lastConfigHash = newHash;
        }
Пример #28
0
        public Nokia_5110(bool useBacklight, Cpu.Pin latch, Cpu.Pin backlight,
                          Cpu.Pin reset, Cpu.Pin dataCommand)
        {
            if (useBacklight)
            {
                this.backlight = new PWM(backlight);
            }

            SPI.Configuration spiConfiguration = new SPI.Configuration(
                latch,              // chip select port
                false,              // IC is accessed when chip select is low
                0,                  // setup time 1 ms
                0,                  // hold chip select 1 ms after transfer
                false,              // clock line is low if device is not selected
                true,               // data is sampled at leading edge of clock
                4000,               // clockrate is 15 MHz
                SPI.SPI_module.SPI1 // use first SPI bus
                );

            spi = new SPI(spiConfiguration);

            this.reset    = new OutputPort(reset, true);
            this.dataMode = new OutputPort(dataCommand, true);
            Initialize();
        }
Пример #29
0
        public static void Upload(PageCollection pageCollection)
        {
            OutputPort onboardLed = new OutputPort(ShieldConfiguration.CurrentConfiguration.OnboardLedPin, true);

            SPI.Configuration spiConfig = new SPI.Configuration(
                ShieldConfiguration.CurrentConfiguration.SpiChipSelectPin,
                false,
                100,
                100,
                false,
                true,
                1000,
                ShieldConfiguration.CurrentConfiguration.SpiModule
                );
            var spi      = new SpotSpi(spiConfig);
            var uploader = new Uploader(spi);

            if (uploader.IsShieldInBootloaderMode())
            {
                var uploadStatusIndicator = new UploadStatusIndicator(
                    spi
                    );

                try
                {
                    //var securityRegisterData = uploader.SecurityRegisterRead();
                    //var userData = new byte[Constants.SecurityRegisterUserFieldLength];
                    //Array.Copy(securityRegisterData, 4, userData, 0, userData.Length);
                    //var userDataString = new string(System.Text.Encoding.UTF8.GetChars(userData));
                    //Debug.Print("Programming \"" + userDataString + "\"");
                    uploadStatusIndicator.Status = UploadStatusIndicator.UploadStatus.Uploading;
                    uploader.UploadBitstream(pageCollection);
                    uploadStatusIndicator.Status = UploadStatusIndicator.UploadStatus.Succeeded;
                }
                catch
                {
                    // Any exception is a failed upload

                    uploadStatusIndicator.Status = UploadStatusIndicator.UploadStatus.Failed;
                    throw;
                }
                finally
                {
                    onboardLed.Write(false);
                }
            }
            else
            {
                // Flash the onboard LED to indicate upload failure due to
                // not being in bootstrapping mode

                Thread.Sleep(500);
                onboardLed.Write(false);
                Thread.Sleep(500);
                onboardLed.Write(true);
                Thread.Sleep(500);
                onboardLed.Write(false);
            }
        }
Пример #30
0
 /// <summary>
 /// Writes an array of unsigned short arguments to the interface.
 /// </summary>
 /// <param name="spiDev">The underlying SPI object. Omit this parameter when you call that method</param>
 /// <param name="pConfig">The SPI configuration used for this transfer.</param>
 /// <param name="pArray">The buffer that will write to the interface.</param>
 public static void Write(this SPI spiDev, SPI.Configuration pConfig, UInt16[] pArray)
 {
     lock (Hardware.SpiLock)
     {
         spiDev.Config = pConfig;
         spiDev.Write(pArray);
     }
 }
Пример #31
0
 /// <summary>
 /// Writes an array of bytes to the interface, and reads an array of bytes from the interface.
 /// </summary>
 /// <param name="spiDev">The underlying SPI object. Omit this parameter when you call that method</param>
 /// <param name="pConfig">The SPI configuration used for this transfer.</param>
 /// <param name="writeBuffer">The buffer whose contents will be written to the interface.</param>
 /// <param name="writeOffset">The offset in writeBuffer to start write data from.</param>
 /// <param name="writeCount">The number of elements in writeBuffer to write.</param>
 /// <param name="readBuffer">The buffer that will store the data that is read from the interface.</param>
 /// <param name="readOffset">The offset in readBuffer to start read data from.</param>
 /// <param name="readCount">The number of elements in readBuffer to fill.</param>
 /// <param name="startReadOffset">The offset in time, measured in transacted elements from writeBuffer, when to start reading back data into readBuffer.</param>
 public static void WriteRead(this SPI spiDev, SPI.Configuration pConfig, byte[] writeBuffer, int writeOffset, int writeCount, byte[] readBuffer, int readOffset, int readCount, int startReadOffset)
 {
     lock (Hardware.SpiLock)
     {
         spiDev.Config = pConfig;
         spiDev.WriteRead(writeBuffer, writeOffset, writeCount, readBuffer, readOffset, readCount, startReadOffset);
     }
 }
Пример #32
0
        private uint spiSpeed = 8000; // kHz

        #endregion Fields

        #region Constructors

        //, int socketNumberTwo)
        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        /// <param name="socketNumberTwo">The second socket that this module is plugged in to.</param>
        public DisplayS22(int socketNumber)
            : base(WPFRenderOptions.Intercept)
        {
            // This finds the Socket instance from the user-specified socket number.
            // This will generate user-friendly error messages if the socket is invalid.
            // If there is more than one socket on this module, then instead of "null" for the last parameter,
            // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
            socket = Socket.GetSocket(socketNumber, true, this, null);
            socket.EnsureTypeIsSupported('S', this);

            socket.ReservePin(Socket.Pin.Three, this); // reset
            socket.ReservePin(Socket.Pin.Four, this); // back light
            socket.ReservePin(Socket.Pin.Five, this); // D/C
            socket.ReservePin(Socket.Pin.Six, this); // CS
            socket.ReservePin(Socket.Pin.Seven, this); // MOSI
            socket.ReservePin(Socket.Pin.Eight, this); // MISO
            socket.ReservePin(Socket.Pin.Nine, this); // SCK

            /*
             * Serial peripheral interface (SPI).
             * Pin 7 is MOSI line, pin 8 is MISO line, pin 9 is SCK line.
             * In addition, pins 3, 4 and 5 are general-purpose input/outputs, with pin 3 supporting interrupt capabilities.
            */

            pinReset = new GTI.DigitalOutput(socket, Socket.Pin.Three, false, this); // pin 3
            pinBacklight = new GTI.DigitalOutput(socket, Socket.Pin.Four, false, this); // pin 4
            pinDC = new GTI.DigitalOutput(socket, Socket.Pin.Five, false, this); // pin 5

            spiConfig = new GTI.SPI.Configuration(false, 0, 0, false, true, spiSpeed);
            netMFSpiConfig = new SPI.Configuration(socket.CpuPins[6], spiConfig.ChipSelectActiveState, spiConfig.ChipSelectSetupTime, spiConfig.ChipSelectHoldTime, spiConfig.ClockIdleState, spiConfig.ClockEdge, spiConfig.ClockRateKHz, socket.SPIModule);
            spi = new GTI.SPI(socket, spiConfig, GTI.SPI.Sharing.Shared, socket, Socket.Pin.Six, this);

            Reset();
            ConfigureDisplay();
            Clear();
            SetBacklight(true);
        }
Пример #33
0
        /// <summary>Constructor</summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public Display(ModelType model, int socketNumber)
            : base(WPFRenderOptions.Intercept)
        {
            #region UTFT
            ushort[] dsx = {239, 239, 239, 239, 239, 239, 175, 175, 239, 127, 127, 239, 271, 479, 239, 239, 239, 239, 239, 239, 479, 319, 239, 175, 127, 239, 239, 319, 319};
            ushort[] dsy = {319, 399, 319, 319, 319, 319, 219, 219, 399, 159, 127, 319, 479, 799, 319, 319, 319, 319, 319, 319, 799, 479, 319, 219, 159, 319, 319, 479, 479};
            byte[] dtm = { 16, 16, 16, 8, 8, 16, 8, (byte)DisplayTransferMode.SERIAL_4PIN, 16, (byte)DisplayTransferMode.SERIAL_5PIN, (byte)DisplayTransferMode.SERIAL_5PIN, 16, 16, 16, 8, 16, (byte)DisplayTransferMode.LATCHED_16, 8, 16, 8, 16, 16, 16, 8, (byte)DisplayTransferMode.SERIAL_5PIN, (byte)DisplayTransferMode.SERIAL_5PIN, (byte)DisplayTransferMode.SERIAL_4PIN, 16, 16 };

            disp_x_size = dsx[(byte)model];
            disp_y_size = dsy[(byte)model];
            display_transfer_mode = dtm[(byte)model];
            display_model = (byte)model;

            if (display_transfer_mode == (byte)DisplayTransferMode.SERIAL_4PIN)
            {
                display_transfer_mode = 1;
                display_serial_mode = (byte)DisplayTransferMode.SERIAL_4PIN;
            }
            if (display_transfer_mode == (byte)DisplayTransferMode.SERIAL_5PIN)
            {
                display_transfer_mode = 1;
                display_serial_mode = (byte)DisplayTransferMode.SERIAL_5PIN;
            }

            if (display_transfer_mode != 1)
            {
                //_set_direction_registers(display_transfer_mode);
                //P_RS = portOutputRegister(digitalPinToPort(RS));
                //B_RS = digitalPinToBitMask(RS);
                //P_WR = portOutputRegister(digitalPinToPort(WR));
                //B_WR = digitalPinToBitMask(WR);
                //P_CS = portOutputRegister(digitalPinToPort(CS));
                //B_CS = digitalPinToBitMask(CS);
                //P_RST = portOutputRegister(digitalPinToPort(RST));
                //B_RST = digitalPinToBitMask(RST);
                //if (display_transfer_mode == LATCHED_16)
                //{
                //    P_ALE = portOutputRegister(digitalPinToPort(SER));
                //    B_ALE = digitalPinToBitMask(SER);
                //    pinMode(SER, OUTPUT);
                //    cbi(P_ALE, B_ALE);
                //    pinMode(8, OUTPUT);
                //    digitalWrite(8, LOW);
                //}
                //pinMode(RS, OUTPUT);
                //pinMode(WR, OUTPUT);
                //pinMode(CS, OUTPUT);
                //pinMode(RST, OUTPUT);
            }
            else
            {
                //P_SDA = portOutputRegister(digitalPinToPort(RS));
                //B_SDA = digitalPinToBitMask(RS);
                //P_SCL = portOutputRegister(digitalPinToPort(WR));
                //B_SCL = digitalPinToBitMask(WR);
                //P_CS = portOutputRegister(digitalPinToPort(CS));
                //B_CS = digitalPinToBitMask(CS);
                //P_RST = portOutputRegister(digitalPinToPort(RST));
                //B_RST = digitalPinToBitMask(RST);
                //if (display_serial_mode != SERIAL_4PIN)
                //{
                //    P_RS = portOutputRegister(digitalPinToPort(SER));
                //    B_RS = digitalPinToBitMask(SER);
                //    pinMode(SER, OUTPUT);
                //}
                //pinMode(RS, OUTPUT);
                //pinMode(WR, OUTPUT);
                //pinMode(CS, OUTPUT);
                //pinMode(RST, OUTPUT);
            }
            #endregion

            socket = Socket.GetSocket(socketNumber, true, this, null);
            socket.EnsureTypeIsSupported('S', this);
            /*
             * Serial peripheral interface (SPI).
             * Pin 7 is MOSI line, pin 8 is MISO line, pin 9 is SCK line.
             * In addition, pins 3, 4 and 5 are general-purpose input/outputs, with pin 3 supporting interrupt capabilities.
            */

            pinReset = new GTI.DigitalOutput(socket, Socket.Pin.Three, false, this); // pin 3
            pinBacklight = new GTI.DigitalOutput(socket, Socket.Pin.Four, false, this); // pin 4
            pinDc = new GTI.DigitalOutput(socket, Socket.Pin.Five, false, this); // pin 5

            spiConfig = new GTI.SPI.Configuration(false, 0, 0, false, true, 12000);
            netMFSpiConfig = new SPI.Configuration(socket.CpuPins[6], spiConfig.ChipSelectActiveState, spiConfig.ChipSelectSetupTime, spiConfig.ChipSelectHoldTime, spiConfig.ClockIdleState, spiConfig.ClockEdge, spiConfig.ClockRateKHz, socket.SPIModule);
            spi = new GTI.SPI(socket, spiConfig, GTI.SPI.Sharing.Shared, socket, Socket.Pin.Six, this);

            Reset();
            ConfigureDisplay();
            Clear();
            SetBacklight(true);
        }
Пример #34
0
 /// <summary>
 /// �R���X�g���N�^
 /// </summary>
 /// <param name="pin">SS�s��</param>
 /// <param name="module">SPI���W���[��</param>
 public SpiController(Cpu.Pin pin, SPI.SPI_module module)
 {
     SPI.Configuration config = new SPI.Configuration(pin, false, 1, 1, false, true, 200, module);
     spi = new SPI(config);
 }
Пример #35
0
        public byte Wiper_Reg; //Wiper Register

        public MCP4131(Cpu.Pin cs_pin, SPI.SPI_module mod)
        {
            MCP4131_Config = new SPI.Configuration(cs_pin, false, 0, 0, false, true, 800, mod);
            MCP4131_SPI = new SPI(MCP4131_Config);
        }