You must be logged in to reply.

Page 1 of 1 out of 5 messages.

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

Posted 2yr ago
by munderhill | Hero | 15,538 exp
Posted 2yr ago
by munderhill | Hero | 15,538 exp
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);
                }
            }
        }
    }
Reply #1 — Posted 2yr ago
by Mr. John Smith | Legend | 42,263 exp
Reply #1 — Posted 2yr ago
by Mr. John Smith | Legend | 42,263 exp
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
Reply #2 — Posted 2yr ago
by munderhill | Hero | 15,538 exp
Reply #2 — Posted 2yr ago
by munderhill | Hero | 15,538 exp
Mr. John Smith says:

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?
Reply #3 — Posted 2yr ago
by RobvanSchelven | Hero | 17,308 exp
Reply #3 — Posted 2yr ago
by RobvanSchelven | Hero | 17,308 exp
@munderhill - You could write directly in the MCU register that controls the SPI clock speed.
Reply #4 — Posted 2yr ago
by Mr. John Smith | Legend | 42,263 exp
Reply #4 — Posted 2yr ago
by Mr. John Smith | Legend | 42,263 exp
@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.

Page 1 of 1 out of 5 messages.

You must be logged in to reply.