I would like to control 10 different devices with SPI. As there is not enough ChipSelect outputs I use GPIO to toggle a MUX on the clock. Problem is I have to wait until the SPI is done transmitting before changing the GPIO to the next device. Is there a way to set a callback function that will execute once spi.xfer2 is done 'transmitting'? Waiting or sleeping is an accurate and would result in a waste of time.

10 devices, each transmission is 960 bytes long (or 7680 bits). I would like to do 60 transmissions to all the devices each second which means a total of 576kB or ~4Mb (4MHz should do it but I set the frequency it to 8Mhz). Python 3.5.3


2 Answers 2


When the spi.xfer2() call returns the SPI transfer has been completed. So unless you are doing the transfers in multiple threads there is no problem.

E.g. the following will work.

while True:
    select CS 1
    select CS 2   
    select CS 10

Muxing SCL seems unusual!

Why not multiplex CS (if you want to use the inbuilt drivers).

There is no magic about CS - you can use ANY GPIO pins. I believe pigpio has inbuilt support for this.

I would expect the return value from spiWrite to indicate success.

int spiWrite(unsigned handle, char *buf, unsigned count)
This function writes count bytes of data from buf to the SPI device associated with the handle. 

handle: >=0, as returned by a call to spiOpen
   buf: the data bytes to write
 count: the number of bytes to write

Returns the number of bytes transferred if OK, otherwise PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
  • 1
    The issue is not muxing CS or SCL but to know when to change the mux.
    – Gilad
    Oct 10, 2018 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.