Is is possible to 'bit-bang' the CS pin of SPI?

For my project I'm trying to use the esp32 to communicate with some devices over SPI. The issue here is that I'm running low on pins, and I don't have enough CS pins to accommodate all of the SPI peripheral devices.
What I'm trying to do is use an I/O expander to give myself extra pins that can be used as CS. The I/O expander that I'm using (https://www.ti.com/lit/ds/symlink/pcf8575.pdf) works by sending it a message over I2C and telling it to toggle some pins to either high or low. I've already figured out how to get the esp32 to communicate with the I/O expander over I2C.
Since CS only requires toggling the pin high or low, I figure I can just use the I/O expander to toggle the CS pin low, start an SPI transaction, and then after my SPI transaction is finished, I can toggle the CS pin high again. Essentially, this is what I'm proposing:

Would this work correctly if I tried to program it in Arduino? My worry is that the SPI library might force me to define a specific esp32 pin as the CS and not let me begin an SPI transaction this way.

Yes.

It would be a very bad piece of design if it did.

Did you know you can bit bang the whole SPI process?

Note that while you can program the ESP32 processor with the Arduino IDE, it is not actually an Arduino.

Suggest that you use an output shift register on the SPI bus to select other devices on the same bus.

Select the shift reg. Fill the shift reg. Deselect the shift register and SPI to the device the shift register is holding selected..

You can daisy-chain shift registers to multiply pins and they are a fraction of the price of a full IO expander.

If you have digital inputs to fulfill, add input shift regs at the end of the chain.

I have to wonder about void setup. would you have to start I2C first, step through the SPI devices and (device.begin, CS) every one of them? What would you put in place of CS when you get to (device.begin, CS)?

The SPI library doesn't know anything about CS pins, it relies on the rest of your code to set the CS pins correctly. So your idea will work fine the with the SPI library.

The problem may be libraries you want to use for specific SPI devices like LCD displays or sensors. They may require you to specify which pin is the CS pin for the device, which is a problem if that pin is a pin on the I/O extender rather than an Arduino pin.

1 Like

Well given the fact that it is not called void setup, I would have to wonder how qualified you are to give advice here?

It is called the setup function, void is not part of its name, it tells the compile that it is not expected to return a value.

So to answer your worries then you set the chip enable low and then call the begin function. Then put the chip enable to high. You do this for every device you have.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.