Mulitple SPI devices on the same bus cannot have different clock speeds

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

I encountered something similar when switching between the XBee Wifi and the dSpin stepper drivers. What I would do was flush the bus on a device that doesn’t exist (i.e. a different chip select pin). So I would do a R/W on this non existent device before switching to the new device.

Xbee -> Null Device -> dSpin -> Null Device -> Xbee

@ Mr. John Smith - I tried what I think you are describing, but no joy. Could you provide a code example?

@ munderhill - You could write directly in the MCU register that controls the SPI clock speed.

@ munderhill - Oh right, there was one more thing. The Null device had the same settings as the dSpin device in terms of clock speed and such. I’ll see if I can find that code somewhere. I know I had this problem with the XBee and then again with the Wiznet Module.