예제 #1
0
        /// <summary>
        /// Construct the driver using the specific SPI port.
        /// </summary>
        /// <param name="busSelector">Which SPI bus to use</param>
        /// <param name="width">Width of display in pixels</param>
        /// <param name="height">Height of display in pixels</param>
        /// <param name="dcPin">The Gpio pin to use for D/C (Data or Command) signal</param>
        /// <param name="rstPin">The Gpio pin to use for hardware reset of the display</param>
        /// <param name="csPin">The Cpio pin to use for Chip Select signal for SPI communication</param>
        /// <param name="bufSize">The size of the internal display buffer</param>
        public ILI9341_SPI(string busSelector, uint width, uint height, GpioPin dcPin, GpioPin rstPin, GpioPin csPin, uint bufSize = 256)
        {
            if (dcPin == null || rstPin == null || csPin == null)
            {
                throw new ArgumentNullException();
            }
            _dcPin  = dcPin;
            _rstPin = rstPin;
            _csPin  = csPin;

            _dcPin.SetDriveMode(GpioPinDriveMode.Output);
            _rstPin.SetDriveMode(GpioPinDriveMode.Output);
            _csPin.SetDriveMode(GpioPinDriveMode.Output);

            SpiBusInfo busInfo = SpiDevice.GetBusInfo(busSelector);

            var spiConnSettings = new SpiConnectionSettings(csPin.PinNumber)
            {
                ClockFrequency = 10000000,     //10 MHz SPI clock,max period for read is 150ns, write is 100ns
                DataBitLength  = 8,            //8-bit data length
                Mode           = SpiMode.Mode3 //mode 3, data read on the rising edge - idle high
            };

            _coreWidth  = Width = width;
            _coreHeight = Height = height;
            _buffer     = new byte[bufSize];

            _font = new PixelFont7X9();//default font

            _spiDevice = SpiDevice.FromId(busSelector, spiConnSettings);
        }
예제 #2
0
        public static void Main()
        {
            SpiDevice             spiDevice;
            SpiConnectionSettings connectionSettings;

            Debug.WriteLine("Hello from sample for System.Device.Spi!");
            // You can get the values of SpiBus
            SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);

            Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
            Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
            Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
            Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");
            foreach (var data in spiBusInfo.SupportedDataBitLengths)
            {
                Debug.WriteLine($"  {data}");
            }

            // Note: the ChipSelect pin should be adjusted to your device, here 12
            connectionSettings = new SpiConnectionSettings(1, 12);
            // You can adjust other settings as well in the connection
            connectionSettings.ClockFrequency = 1_000_000;
            connectionSettings.DataBitLength  = 8;
            connectionSettings.DataFlow       = DataFlow.LsbFirst;
            connectionSettings.Mode           = SpiMode.Mode2;

            // Then you create your SPI device by passing your settings
            spiDevice = SpiDevice.Create(connectionSettings);

            // You can write a SpanByte
            SpanByte writeBufferSpanByte = new byte[2] {
                42, 84
            };

            spiDevice.Write(writeBufferSpanByte);
            // Or a ushort buffer
            ushort[] writeBufferushort = new ushort[2] {
                4200, 8432
            };
            spiDevice.Write(writeBufferushort);
            // Or simply a byte
            spiDevice.WriteByte(42);

            // The read operations are similar
            SpanByte readBufferSpanByte = new byte[2];

            // This will read 2 bytes
            spiDevice.Read(readBufferSpanByte);
            ushort[] readUshort = new ushort[4];
            // This will read 4 ushort
            spiDevice.Read(readUshort);
            // read 1 byte
            byte readMe = spiDevice.ReadByte();

            Debug.WriteLine($"I just read a byte {readMe}");

            // And you can operate full transferts as well
            SpanByte writeBuffer = new byte[4] {
                0xAA, 0xBB, 0xCC, 0x42
            };
            SpanByte readBuffer = new byte[4];

            spiDevice.TransferFullDuplex(writeBuffer, readBuffer);
            // Same for ushirt arrays:
            ushort[] writeBufferus = new ushort[4] {
                0xAABC, 0x00BB, 0xCC00, 0x4242
            };
            ushort[] readBufferus = new ushort[4];
            spiDevice.TransferFullDuplex(writeBufferus, readBufferus);

            Thread.Sleep(Timeout.Infinite);

            // Browse our samples repository: https://github.com/nanoframework/samples
            // Check our documentation online: https://docs.nanoframework.net/
            // Join our lively Discord community: https://discord.gg/gCyBu8T
        }