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"); } } }
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(); }