I have a number of SPI ICs but limited GPIOs on my microcontroller, so want to multiplex the CS pins to something that requires only a couple of GPIOs. I use the predetermined clock, MISO and MOSI on the microcontroller.

My initial thought was a ring counter, but all the ones I have found set a single pin to high and the rest low. I need a single low and the rest high. I could use a shift register, but seems like overkill. What is the best IC to use for this application?

  • 1
    \$\begingroup\$ here's an app note showing 3/4 gpio (plus latch) to 8/16 chip-select lines, with 74hc logic // i2c devices also a nice option \$\endgroup\$
    – Pete W
    Jan 27 at 19:42
  • \$\begingroup\$ So, how many slaves there are, and how many GPIO pins you have available? \$\endgroup\$
    – Justme
    Jan 27 at 20:03

Use a shift register; there's nothing overkill with that (it's not "complexer" than a ring counter). You can drive it with another SPI peripheral of your microcontroller, or you can just bitbang it, since you will change that less often than you'll talk to your multiple peripherals. 2 additional pins.

Alternatively, I²C port expanders are a thing (2 pins), and I'd be surprised if you can't find a 1-wire-protocol device that does the same thing (which will be harder to use). A cheap microcontroller can be attached via UART to do the same (1 to 2 pins).

But honestly, if you're running out of GPIO for CS, it might be a good point to either switch to a microcontroller with more GPIO, or simply add a cheap second microcontroller which takes the complete SPI handling load from your main microcontroller and communicates with that e.g. via UART. A pretty original and comparatively cheap source for "microcontroller as IO expanders on speed" is using one of the ubiquitous Cortex-M chips that bring an SWD interface and just using that to manipulate the microcontroller as a "puppet" device. In PoC||GTFO 0x10, Micah Scott documented such SWD marionettes.

  • 1
    \$\begingroup\$ With SWD itself being quite close to half-duplex SPI with no CS, on the electrical level. \$\endgroup\$ Jan 27 at 18:10
  • \$\begingroup\$ Perfect, thank you. Good to know I am barking up the right tree. I have requirement for 10 CS lines, and have 5 GPIOs available, so shift register is good. \$\endgroup\$
    – bgarrood
    Jan 28 at 7:19
  • \$\begingroup\$ @JanDorniak absolutely! But compare the price of an entry-level STM32 MCU to that of an SPI IO expander that you can configure to do multiple channels of PWM, for example. I find the aspect of "do consider your MCUs as complex statemachines. Nobody forces you to consider them to execute code on their own" quite refreshing. \$\endgroup\$ Jan 28 at 8:33
  • 1
    \$\begingroup\$ @MarcusMüller oh, I know. Iirc F0s go as low as 0.5$, definitely below 1$. I once did something similar, using bitbanged SWD to use one STM32 to program another by writing directly to the flash controller registers. That's quite a bit of code and has a bit of overhead over regular SPI iirc \$\endgroup\$ Jan 28 at 8:39
  • 1
    \$\begingroup\$ Come to think, a company I worked for had a similar project, used F0s as cheap DACs. \$\endgroup\$ Jan 28 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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