This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SPI with multiple chip selects

As I understand the SPI HW driver documentation, it appears to be at least biased for using a single chip select (slave select in the API). I have an application where I have 5 devices on the SPI bus.

Should I create an SPI master configuration structure for each of the 5 devices? Only the SPI_Pin_SS would be different in each of them, and I think I would need to close and open SPI to switch between the devices.

Or can I use a single structure with spi configuration function to configure the SPI pins (CLK, MOSI, MISO) and manually configure the chip selects? And then when I want to change device I want, just change the SPI_Pin_SS?

Another way? After reading RK's answer, began to wonder if could use GPIOTE to handle the multiple chip selects. Would it work to assign SPI_Pin_SS to an "unused" pin, and cofigure GPIOTE task to toggle desired CS when SPI toggles _SS ? I haven't explored GPIOTE at all yet so this may be a naive idea.

Parents
  • Changing SPI_Pin_SS won't help you - that's copied when you open the SPI device and stored, so you can change it as often as you like, won't make any difference at all until you close and reopen the SPI.

    What I'd do myself in this case is set SPI_Pin_SS to UNCONNECTED so the SPI driver doesn't really use it at all, set up 5 GPIO output lines for your chip selects, setting them as outputs and default high (and note the code in the spi_master which sets the output high before configuring it as an output and has a note as to why). I'd then manually manage which device is selected by setting the pin low before sending/receiving with the standard spi code using standard GPIO calls.

Reply
  • Changing SPI_Pin_SS won't help you - that's copied when you open the SPI device and stored, so you can change it as often as you like, won't make any difference at all until you close and reopen the SPI.

    What I'd do myself in this case is set SPI_Pin_SS to UNCONNECTED so the SPI driver doesn't really use it at all, set up 5 GPIO output lines for your chip selects, setting them as outputs and default high (and note the code in the spi_master which sets the output high before configuring it as an output and has a note as to why). I'd then manually manage which device is selected by setting the pin low before sending/receiving with the standard spi code using standard GPIO calls.

Children
  • Controlling the chip select pins outside of the SPI routines can work, and in fact I tried something like what you suggested (but setting _SS to an unused pin not to UNCONNECTED). But it isn't simple since would need to add a way to set the CS only after all bytes in buffer have been sent. The SPI functions already handle _SS correctly, would seem to be cleanest solution if the _SS could be changed in the config structure or the desired CS be passed as part of the _send_recv function.

  • Yes it would - however that's not the way it works if you look at the code. So either you do it manually or you open and close the device every time. If however you know when it's safe to open and to close the device, you probably already know when it's safe to change the chipselect.

    Or you derive off their source and write your own version I guess.

  • I did the same as RK suggested and created my function in which I reset transmission_completed flag then after spi_master_send_recv I wait for transmission_completed flag set with timeout. transmission_completed flag set in SPI event handler in SPI_MASTER_EVT_TRANSFER_COMPLETED. Obviously I wait for all data to be sent then continue code but SPI transfer is fast in my case and I don't bother about that inconvenience. In this case don't need to have fixed delays suggested by nszmnsky below

Related