SPI2 Limitations on G120E

I have many SPI devices being driven from a G120E on SPI1. I need to do some bus load balancing, which means putting several SPI devices on SPI2. The .NetMF SPI class requires that the CS pin be selected (in the SPI.Configuration) prior to instantiation, which means that I must dispose and re-instantiate the SPI object each time I access a device on the bus. Since SPI2 is also used for the internal G120E flash memory (per G120E datasheet section 8.6), will this create any problem? How is the bus managed for the EPROM access without conflicting with the .NetMF environment?

If required, I can instantiate once if I modify the hardware to use an address decoder with a single CS pin from the G120E, but the design currently uses multiple CS pins to save cost.

Are you able to create multiple SPI Bus objects, each with a different configuration?

EDIT: Oh and welcome to the forum.

[url]https://www.ghielectronics.com/docs/14/spi#80[/url]
welcome too!

1 Like

Wow, didn’t know I needed to wait so long to make my first reply!

Mr Smith: Yes, it would make sense to instantiate multiple SPI objects, one for each device on the bus. Unfortunately I can’t tell if the objects will interfere with each other on the shared MOSI, MISO, and CLK wires because the SPI class implementation is a black box without much documentation. (I suppose I could go reverse engineer the open source…) So if the SPI class can handle multiple objects using the same SPI module then my work is greatly simplified… Will this work?

Thanks for your expert help.

1 Like

@ PBgrammer - During the WriteRead call of the SPI bus there are no other operations running; it is atomic.

If you just look at the documentation page I pointed out, there’s clear example how to achieve multiple device use. SPI is by definition a shared bus, it would be a poor implementation if it did not support shared use, and as @ Mr John Smith says, individual operations are atomic so there will be no interference. (one caveat: if your device doesn’t really support CS, Chip Select, properly then your experience may vary - but they’re not real SPI :slight_smile: )

Hey @ Brett, thanks for the reference. When I looked at that example, I still didn’t get a good answer. Since the SPI.Configuration is publicly settable, it is possible to define a ConfigDeviceA which uses the SPI1 module and a ConfigDeviceB which uses the SPI2 module, then use the same MySPI object with the new configuration as is done in the example. So that would leave one SPI object controlling both SPI busses. Is this legit?

Or looking at it the other way, if I have two SPI objects, MySPI_1 and MySPI_2, I can setup one with ConfigDeviceA and the other with ConfigDeviceB, with both device configurations using the SPI1 module. Can I rely upon the SPI class to keep everything straight?

I agree that any decent SPI class implementation should be able to share devices on a bus that is, by definition, shared. My only question is how to use the implementation correctly to avoid conflicts, and especially where SPI2 is concerned because of the shared on-board flash memory.

Thanks.

Thanks,

Sorry, the example is explicit. Two devices, both connected to SPI1, with different CS pins. Not sure where you think SPI2 comes into play??

here’s the pattern.


SPI.Configuration ConfigDeviceA = new SPI.Configuration(Cpu.Pin.GPIO_Pin1, false, 0, 0, false, true, 1000, SPI.SPI_module.SPI1);
SPI.Configuration ConfigDeviceB = new SPI.Configuration(Cpu.Pin.GPIO_Pin4, false, 0, 0, false, true, 1000, SPI.SPI_module.SPI1);
SPI MySPI = new SPI(ConfigDeviceA); //create the thing


// accessing device A
MySPI.Config = ConfigDeviceA; //set the config to A
MySPI.WriteRead(tx_data, rx_data);  // send/recv to A

// accessing device B
MySPI.Config = ConfigDeviceB;  //swap over to B
MySPI.WriteRead(tx_data, rx_data);  //interact with B

1 Like

@ PBgrammer - Also, if you are doing threading of some sort and those spi config changes can happen on different threads. then


object locker = new object();
lock(locker){
    MySPI.Config = ConfigDeviceB;  //swap over to B
    MySPI.WriteRead(tx_data, rx_data);  //interact with B
}
1 Like