Skip to content

How to use SPI Bit Bang in MPF

Related Config File Sections:

Sometimes you want to read switches from PCBs which contain a shift register or SPI chip (i.e. a 74HCT165). This platforms uses two digital_outputs: and one switches: on another platform to address the SPI chip. Please note that this is relatively slow compared to platforms which interface to SPI natively (such as How to use MPF with Stern SPIKE / SPIKE 2 machines). The main purpose of this platform is to access Stern Spike boards using other control systems than Spike. Primarily, this allows you to use the Spike Trough on any system. However, if you are on Spike or any other platform which natively reads switches via SPI use those means since they are much more efficient.

This is an example:

hardware:
  platform: your_platform, spi_bit_bang      # add your platform first here
spi_bit_bang:
  miso_pin: s_miso
  cs_pin: o_cs
  clock_pin: o_clock
  bit_time: 50ms
  inputs: 8
digital_outputs:
  o_cs:
    number: 1
    type: driver
  o_clock:
    number: 2
    type: driver
switches:
  s_trough_0:
    number: 0
    platform: spi_bit_bang
  s_trough_1:
    number: 1
    platform: spi_bit_bang
  s_trough_2:
    number: 2
    platform: spi_bit_bang
  s_trough_3:
    number: 3
    platform: spi_bit_bang
  s_trough_4:
    number: 4
    platform: spi_bit_bang
  s_trough_5:
    number: 5
    platform: spi_bit_bang
  s_trough_6:
    number: 6
    platform: spi_bit_bang
  s_trough_7:
    number: 7
    platform: spi_bit_bang
  s_miso:
    number: 10

The refresh rate of your platform will be bit_time / (inputs + 2). For instance 8 inputs with 50ms bit_time will result in 2Hz update rate which is not terribly good.

bit_time determines how long MPF will wait after clocking the chip for miso_pin to settle. Depending on your platform it might this might need a while. Especially if your platform is connected via USB because of USB latency and jitter. If your inputs are local (i.e. on a RPi) this might be very short compared and you might be able to achieve 50Hz. At the default 2Hz you will wait in average 250ms for a switch change and 500ms in the worst case. Have that in mind.

What if it did not work?

Have a look at our hardware troubleshooting guide.


Something missing or wrong? You can fix it!

This website is edited by people like you! Is something wrong or missing? Is something out of date, or can you explain it better?

Please help us! You can fix it yourself and be an official "open source" contributor!

It's easy! See our Beginner's guide to editing the docs.

Page navigation via the keyboard: < >

You can navigate this site via the keyboard. There are two modes:

General navigation, when search is not focused:

  • F , S , / : open search dialog
  • P , , : go to previous page
  • N , . : go to next page

While using the search function:

  • Down , Up : select next / previous result
  • Esc , Tab : close search
  • Enter : go to highlighted page in the results