So, I’ve got 2 SPI devices (LCD and Flash Memory) on SPI3 of a G120. I’ve implemented a BusManager class to change configs based on the device being accessed, but if the clock speed of the configs are different I get erratic behavior. I initialize the LCD first @ 10MHz, then when the Flash is selected, I change to a config where the clock speed is 25MHz. This causes the Flash methods to return invalid data. I have tried to destroy the SPI device first and recreate it if I am switching config, but that did not work either. Any ideas are appreciated!
Below is the bus manager class.
public class SpiBusManager
{
private SPI _spi;
private Hashtable _configurations;
private object _lockObject = new object();
public SpiBusManager()
{
_configurations = new Hashtable();
}
public void AddConfiguration(string device, SPI.Configuration config)
{
if (!_configurations.Contains(device))
{
if (_spi == null)
{
_spi = new SPI(config);
}
_configurations.Add(device, config);
}
}
public void WriteRead(string device, byte[] write, byte[] read)
{
lock(_lockObject)
{
if (_configurations.Contains(device))
{
_spi.Config = (SPI.Configuration)_configurations[device];
_spi.WriteRead(write, read, 0);
}
}
}
public void WriteRead(string device, byte[] writeBuffer, byte[] readBuffer, int startReadOffset)
{
lock (_lockObject)
{
if (_configurations.Contains(device))
{
_spi.Config = (SPI.Configuration)_configurations[device];
_spi.WriteRead(writeBuffer, readBuffer, startReadOffset);
}
}
}
public void WriteRead(string device, byte[] writeBuffer, int writeOffset, int writeCount, byte[] readBuffer, int readOffset, int readCount, int startReadOffset)
{
lock (_lockObject)
{
if (_configurations.Contains(device))
{
_spi.Config = (SPI.Configuration)_configurations[device];
_spi.WriteRead(writeBuffer, writeOffset, writeCount, readBuffer, readOffset, readCount, startReadOffset);
}
}
}
public void Write(string device, byte[] writeBuffer)
{
lock (_lockObject)
{
if (_configurations.Contains(device))
{
_spi.Config = (SPI.Configuration)_configurations[device];
_spi.Write(writeBuffer);
}
}
}
}