Exemplo n.º 1
0
        public void BitBangSpiFreqTest()
        {
            Console.WriteLine("Testing bit bang SPI SCLK freq...");

            byte[]    MOSI = new byte[1024];
            double    expectedTime;
            Stopwatch timer    = new Stopwatch();
            double    baseTime = 0;
            int       numReads = 8;

            /* 1MHz SCLK */
            FX3.SetBitBangSpiFreq(1000000);

            /* Get base time (max SCLK with 0.5 microsecond stall) */
            FX3.SetBitBangStallTime(0.5);
            for (int i = 0; i < 4; i++)
            {
                timer.Restart();
                for (int trial = 0; trial < numReads; trial++)
                {
                    FX3.BitBangSpi(4000, 1, MOSI, 2000);
                }
                timer.Stop();
                baseTime += timer.ElapsedMilliseconds;
            }
            /* Average base time */
            baseTime /= 4.0;
            /* Subtract time for clocks in basetime */
            baseTime -= (4000000.0 / 1000000) * numReads;
            Console.WriteLine("Base bitbang SPI time: " + baseTime.ToString() + "ms");

            for (uint freq = 75000; freq <= 900000; freq += 25000)
            {
                Console.WriteLine("Testing freq of " + freq.ToString() + "Hz");
                FX3.SetBitBangSpiFreq(freq);
                /* Perform sets of 1 4000-bit transfer. Expected time is in ms */
                expectedTime = (4000000.0 / freq) * numReads;
                /* Add base time overhead */
                expectedTime += baseTime;
                timer.Restart();
                for (int trial = 0; trial < numReads; trial++)
                {
                    FX3.BitBangSpi(4000, 1, MOSI, 2000);
                }
                timer.Stop();
                Console.WriteLine("Expected time: " + expectedTime.ToString() + "ms, real time: " + timer.ElapsedMilliseconds.ToString() + "ms");
                Assert.AreEqual(expectedTime, timer.ElapsedMilliseconds, Math.Max(25, 0.1 * expectedTime), "ERROR: Invalid transfer time");
            }
        }
Exemplo n.º 2
0
        public void BitBangSpiModeTest()
        {
            Console.WriteLine("Starting bit bang SPI mode test...");

            byte[] MISO;

            uint mode;

            List <byte> MOSI = new List <byte>();

            for (int i = 0; i < 1024; i++)
            {
                MOSI.Add((byte)(i & 0x7F));
            }

            FX3.BitBangSpiConfig      = new BitBangSpiConfig(true);
            FX3.BitBangSpiConfig.SCLK = (FX3PinObject)FX3.DIO3;
            FX3.ReadPin(FX3.DIO3);
            FX3.ReadPin(FX3.DIO3);
            Random rnd = new Random();

            for (int trial = 0; trial < 64; trial++)
            {
                mode = (uint)(rnd.NextDouble() * 4);
                Console.WriteLine("Testing bit-bang SPI mode " + mode.ToString());
                FX3.BitBangSpiConfig.CPHA = ((mode & 0x1) != 0);
                FX3.BitBangSpiConfig.CPOL = ((mode & 0x2) != 0);

                for (uint len = 8; len < 64; len += 8)
                {
                    MISO = FX3.BitBangSpi(len, 1, MOSI.ToArray(), 1000);
                    Assert.AreEqual(len >> 3, MISO.Count(), "ERROR: Invalid data count");
                    for (int i = 0; i < MISO.Count(); i++)
                    {
                        Assert.AreEqual(MOSI[i], MISO[i], "ERROR: Invalid echo data");
                    }
                }

                /* Check SCLK pin level after transfer */
                if (FX3.BitBangSpiConfig.CPOL)
                {
                    Assert.AreEqual(1, FX3.ReadPin(FX3.DIO4), "ERROR: Expected SCLK to be idle high");
                }
                else
                {
                    Assert.AreEqual(0, FX3.ReadPin(FX3.DIO4), "ERROR: Expected SCLK to be idle low");
                }
            }
        }
Exemplo n.º 3
0
        public void BitBangSpiStallTimeTest()
        {
            Console.WriteLine("Testing bit bang SPI stall time...");

            byte[]    MOSI = new byte[1024];
            double    expectedTime;
            Stopwatch timer    = new Stopwatch();
            double    baseTime = 0;
            int       numReads = 5;

            FX3.SetBitBangSpiFreq(500000);

            /* Get base time (with half microsecond stall) */
            FX3.SetBitBangStallTime(0.5);
            for (int i = 0; i < 4; i++)
            {
                timer.Restart();
                for (int trial = 0; trial < numReads; trial++)
                {
                    FX3.BitBangSpi(4, 1001, MOSI, 2000);
                }
                timer.Stop();
                baseTime += timer.ElapsedMilliseconds;
            }
            /* Average base time */
            baseTime /= 4.0;
            Console.WriteLine("Base bitbang SPI time: " + baseTime.ToString() + "ms");

            for (double stallTime = 50; stallTime >= 5; stallTime--)
            {
                Console.WriteLine("Testing stall time of " + stallTime.ToString() + "us");
                FX3.SetBitBangStallTime(stallTime);
                /* Perform sets of 1001 4-bit transfers (1000 stalls). Expected time is in ms */
                expectedTime = stallTime * numReads;
                /* Add base time overhead */
                expectedTime += baseTime;
                timer.Restart();
                for (int trial = 0; trial < numReads; trial++)
                {
                    FX3.BitBangSpi(4, 1001, MOSI, 2000);
                }
                timer.Stop();
                Console.WriteLine("Expected time: " + expectedTime.ToString() + "ms, real time: " + timer.ElapsedMilliseconds.ToString() + "ms");
                Assert.AreEqual(expectedTime, timer.ElapsedMilliseconds, 0.5 * baseTime, "ERROR: Invalid transfer time");
            }
        }
Exemplo n.º 4
0
        public void BitBangSpiTest()
        {
            Console.WriteLine("Starting bit bang SPI functionality test...");

            byte[] MISO;

            List <byte> MOSI = new List <byte>();

            for (int i = 0; i < 1024; i++)
            {
                MOSI.Add((byte)(i & 0x7F));
            }

            Console.WriteLine("Testing bit bang SPI word length...");
            for (uint wordLen = 8; wordLen <= 512; wordLen += 8)
            {
                Console.WriteLine("Testing bit length of " + wordLen.ToString());
                /* Override SPI pins */
                FX3.BitBangSpiConfig = new BitBangSpiConfig(true);
                MISO = FX3.BitBangSpi(wordLen, 1, MOSI.ToArray(), 1000);
                Assert.AreEqual(Math.Ceiling(wordLen / 8.0), MISO.Count(), "ERROR: Invalid data count");
                for (int i = 0; i < MISO.Count(); i++)
                {
                    Assert.AreEqual(MOSI[i], MISO[i], "ERROR: Invalid echo data");
                }

                /* Override DIO3/4 pins */
                FX3.BitBangSpiConfig.MOSI = (FX3PinObject)FX3.DIO3;
                FX3.BitBangSpiConfig.MISO = (FX3PinObject)FX3.DIO4;
                MISO = FX3.BitBangSpi(wordLen, 1, MOSI.ToArray(), 1000);
                Assert.AreEqual(Math.Ceiling(wordLen / 8.0), MISO.Count(), "ERROR: Invalid data count");
                for (int i = 0; i < MISO.Count(); i++)
                {
                    Assert.AreEqual(MOSI[i], MISO[i], "ERROR: Invalid echo data");
                }
            }

            Console.WriteLine("Testing restore hardware SPI functionality...");
            FX3.RestoreHardwareSpi();
            TestSpiFunctionality();
        }