public void LoopPinTest() { Stopwatch timer = new Stopwatch(); long expectedTime; if (FX3.ActiveFX3.BoardType < FX3BoardType.iSensorFX3Board_C) { Console.WriteLine("The connected boards do not have loop back pins"); return; } for (double freq = 100; freq < 2000; freq += 100) { Console.WriteLine("Setting " + freq.ToString() + "Hz PWM on loop pin 1"); FX3.StartPWM(freq, 0.5, FX3.FX3_LOOPBACK1); Assert.AreEqual(freq, FX3.MeasurePinFreq(FX3.FX3_LOOPBACK2, 1, 1000, 100), 0.02 * freq, "ERROR: Invalid value measured on loop pin 2"); FX3.StopPWM(FX3.FX3_LOOPBACK1); Console.WriteLine("Setting " + freq.ToString() + "Hz PWM on loop pin 2"); FX3.StartPWM(freq, 0.5, FX3.FX3_LOOPBACK2); Assert.AreEqual(freq, FX3.MeasurePinFreq(FX3.FX3_LOOPBACK1, 1, 1000, 100), 0.02 * freq, "ERROR: Invalid value measured on loop pin 1"); FX3.StopPWM(FX3.FX3_LOOPBACK2); } Console.WriteLine("Testing SPI triggering with loop back pins..."); FX3.StartPWM(100, 0.5, FX3.FX3_LOOPBACK1); FX3.DrPin = FX3.FX3_LOOPBACK2; FX3.DrActive = true; expectedTime = 1000 * 1000 / 100; timer.Start(); FX3.TransferArray(new uint[1], 1, 1000); timer.Stop(); Console.WriteLine("Elapsed stream time " + timer.ElapsedMilliseconds.ToString() + "ms"); Assert.AreEqual(expectedTime, timer.ElapsedMilliseconds, 0.05 * expectedTime, "ERROR: Invalid stream time"); }
public void SpiTransferTest() { Console.WriteLine("Starting SPI transfer test..."); uint writeVal; uint[] readArray; List <uint> writeData = new List <uint>(); /* Set word length of 8 */ Console.WriteLine("Testing word length of 8 bits..."); FX3.WordLength = 8; TestSpiFunctionality(); for (int bit = 0; bit < 8; bit++) { Console.WriteLine("Testing bit " + bit.ToString()); Assert.AreEqual(1U << bit, FX3.Transfer(1U << bit), "ERROR: SPI loop back failed"); } /* Bits outside of word length should not be echo'd */ for (int bit = 8; bit < 32; bit++) { Console.WriteLine("Testing bit " + bit.ToString()); Assert.AreEqual(0, FX3.Transfer(1U << bit), "ERROR: SPI loop back failed"); } /* Set word length of 16 */ Console.WriteLine("Testing word length of 16 bits..."); FX3.WordLength = 16; for (int bit = 0; bit < 16; bit++) { Console.WriteLine("Testing bit " + bit.ToString()); Assert.AreEqual(1U << bit, FX3.Transfer(1U << bit), "ERROR: SPI loop back failed"); } /* Bits outside of word length should not be echo'd */ for (int bit = 16; bit < 32; bit++) { Console.WriteLine("Testing bit " + bit.ToString()); Assert.AreEqual(0, FX3.Transfer(1U << bit), "ERROR: SPI loop back failed"); } /* Set word length of 24 */ Console.WriteLine("Testing word length of 24 bits..."); FX3.WordLength = 24; for (int bit = 0; bit < 24; bit++) { Console.WriteLine("Testing bit " + bit.ToString()); Assert.AreEqual(1U << bit, FX3.Transfer(1U << bit), "ERROR: SPI loop back failed"); } /* Bits outside of word length should not be echo'd */ for (int bit = 24; bit < 32; bit++) { Console.WriteLine("Testing bit " + bit.ToString()); Assert.AreEqual(0, FX3.Transfer(1U << bit), "ERROR: SPI loop back failed"); } /* Set word length of 32 */ Console.WriteLine("Testing word length of 32 bits..."); FX3.WordLength = 32; for (int bit = 0; bit < 32; bit++) { Console.WriteLine("Testing bit " + bit.ToString()); Assert.AreEqual(1U << bit, FX3.Transfer(1U << bit), "ERROR: SPI loop back failed"); } Console.WriteLine("Testing random 32-bit values..."); var rnd = new Random(); for (int trial = 0; trial < 256; trial++) { writeVal = (uint)(rnd.NextDouble() * uint.MaxValue); Console.WriteLine("Writing 0x" + writeVal.ToString("X8")); Assert.AreEqual(writeVal, FX3.Transfer(writeVal), "ERROR: SPI loop back failed"); } Console.WriteLine("Testing array based SPI transfers..."); for (int writeSize = 1; writeSize < 10; writeSize++) { writeData.Clear(); for (uint i = 0; i < writeSize; i++) { writeData.Add(i); } for (uint numBuffers = 1; numBuffers < 10; numBuffers++) { for (uint numCaptures = 1; numCaptures < 10; numCaptures++) { Console.WriteLine("Testing write data array " + writeSize.ToString() + " words long, with " + numCaptures.ToString() + " numcaptures and " + numBuffers.ToString() + " numbuffers"); readArray = FX3.TransferArray(writeData, numCaptures, numBuffers); /* Size should be write data count * numbuffers * numCaptures */ Assert.AreEqual(writeSize * numBuffers * numCaptures, readArray.Count(), "ERROR: Invalid data size received"); int i = 0; /* Check echo data */ for (int index = 0; index < readArray.Count(); index++) { i = index % writeSize; Assert.AreEqual(writeData[i], readArray[i], "ERROR: Invalid SPI data at index " + index.ToString()); } } } } }
public void SpiStallTimeTest() { Console.WriteLine("Starting SPI stall time test..."); double expectedTime; Stopwatch timer = new Stopwatch(); double baseTime = 0; int numReads = 5; uint[] MOSI = new uint[200]; /* Stall time for transfer reads (32 bits) */ Console.WriteLine("Testing stall time for 32-bit reads (transfer stream)..."); FX3.WordLength = 32; FX3.SclkFrequency = 15000000; FX3.ChipSelectLeadTime = SpiLagLeadTime.SPI_SSN_LAG_LEAD_ONE_HALF_CLK; FX3.ChipSelectLagTime = SpiLagLeadTime.SPI_SSN_LAG_LEAD_ONE_HALF_CLK; /* Get base stall time (5us stall) */ for (int i = 0; i < 8; i++) { timer.Restart(); for (int trial = 0; trial < numReads; trial++) { FX3.TransferArray(MOSI, 5); } timer.Stop(); baseTime += timer.ElapsedMilliseconds; } /* Average base time */ baseTime /= 8.0; Console.WriteLine("Base SPI transfer time with 5us stall: " + baseTime.ToString() + "ms"); for (ushort stallTime = 50; stallTime >= 7; stallTime--) { Console.WriteLine("Testing stall time of " + stallTime.ToString() + "us"); FX3.StallTime = stallTime; /* Perform sets of 5 sets of 200 32-bit transfers (999 stalls). Expected time is in ms */ expectedTime = (stallTime - 5) * numReads; /* Add base time overhead */ expectedTime += baseTime; timer.Restart(); for (int trial = 0; trial < numReads; trial++) { FX3.TransferArray(MOSI, 5); } 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"); System.Threading.Thread.Sleep(100); } /* Stall time for generic reads */ Console.WriteLine("Testing stall time for 16-bit reads (generic stream)..."); FX3.WordLength = 16; /* Get base stall time (5us stall) */ baseTime = 0; for (int i = 0; i < 8; i++) { timer.Restart(); for (int trial = 0; trial < numReads; trial++) { FX3.ReadRegArray(MOSI, 5); } timer.Stop(); baseTime += timer.ElapsedMilliseconds; } /* Average base time */ baseTime /= 8.0; Console.WriteLine("Base SPI transfer time with 5us stall: " + baseTime.ToString() + "ms"); for (ushort stallTime = 50; stallTime >= 7; stallTime--) { Console.WriteLine("Testing stall time of " + stallTime.ToString() + "us"); FX3.StallTime = stallTime; /* Perform sets of 5 sets of 200 16-bit transfers (999 stalls). Expected time is in ms */ expectedTime = (stallTime - 5) * numReads; /* Add base time overhead */ expectedTime += baseTime; timer.Restart(); for (int trial = 0; trial < numReads; trial++) { FX3.ReadRegArray(MOSI, 5); } 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"); System.Threading.Thread.Sleep(100); } FX3.StallTime = 5; }