Tuesday, 25 June 2019

Piface Digital 2 Problem: The Case of the Reluctant Model (Updated 2020-01-16)

Erle Stanley Gardner's 'The Case of the Reluctant Model'
Before the ESP8266, I started off using old Linux laptops, then Raspberry Pis as IoT devices. If it fits the space, power or cost budget, why not? Especially when Linux is a natural for remote operation and runs on nearly everything.

Piface Digital for the Raspberry Pi Model A


I was an early user of the Piface Digital running on the Raspberry Pi Model A. While not the cheapest systems around, you get get IoT systems up up and running very fast, e.g., as motor driver for an IoT robot platform. In addition to the relay outputs, here are also inputs with very handy LED indicators. There was even a PiFace Digital 2 which lets you upgrade to a Raspberry Pi Model B:

Piface Digital 2 for the Raspberry Pi Model B
I bought my Digital 2 and, so it was quite a shock when early in the year I used it with my Raspberry Pi Model B+ and it did not work:

# python3 /usr/share/doc/python3-pifacedigitalio/examples/blink.py
Traceback (most recent call last):
  File "/usr/share/doc/python3-pifacedigitalio/examples/blink.py", line 9, in <module>
    pifacedigital = pifacedigitalio.PiFaceDigital()
  File "/usr/lib/python3/dist-packages/pifacedigitalio/core.py", line 82, in __init__
    self.init_board()
  File "/usr/lib/python3/dist-packages/pifacedigitalio/core.py", line 107, in init_board
    h=self.hardware_addr, b=self.bus, c=self.chip_select))
pifacedigitalio.core.NoPiFaceDigitalDetectedError: No PiFace Digital board detected (hardware_addr=0, bus=0, chip_select=0).

I found the manual from the Farnell website, and all the jumpers checked OK. A quick search of the Internet produced the first clue here. It looks to be an error in the new Piface software. An upgrade to Raspbian had changed the default SPI frequency to 10MHz which is too high for the PiFace Digital 2, so it stopped working out of the box.

This is really Piface's problem - it dropped the ball maintaining the software, but for now, Mike Richards has a good writeup on the solution, which is to get the latest update directly from the Piface github.

So I upgraded and found exactly the same problem! Dropping the ball seems to be getting to be a habit for Piface, or perhaps something went awry during the upgrade.

From the original link, I simply need to specify the SPI speed in the file spi.py. In my installation it came up as /usr/lib/python3/dist-packages/pifacecommon/spi.py

At line 65:
        # create the spi transfer struct
        transfer = spi_ioc_transfer(
            tx_buf=ctypes.addressof(wbuffer),
            rx_buf=ctypes.addressof(rbuffer),
            len=ctypes.sizeof(wbuffer)
        )

Change to
        # create the spi transfer struct
        transfer = spi_ioc_transfer(
            tx_buf=ctypes.addressof(wbuffer),
            rx_buf=ctypes.addressof(rbuffer),
            len=ctypes.sizeof(wbuffer),
            speed_hz=ctypes.c_uint32(15000) # 2019-06-25
        )

And now the example program works and LED7 lit up:

# python3 /usr/share/doc/python3-pifacedigitalio/examples/blink.py

That seems to conclude the Case of the Reluctant Piface Model 2. Later when I checked, the same problem recurs if you upgraded the software to a Piface Digital 1. The same solution applies.

(Update 2020-01-16)
The piface github repository has updated their code, and the proper solution is now:
        # create the spi transfer struct
        transfer = spi_ioc_transfer(
            tx_buf=ctypes.addressof(wbuffer),
            rx_buf=ctypes.addressof(rbuffer),
            len=ctypes.sizeof(wbuffer),
            speed_hz=ctypes.c_uint32(self.speed_hz)
        )

Sadly if you have an up-to-date version of Raspbian, the ugrade instructions no longer work:
$ sudo apt-get install python{,3}-pifacedigitalio
Instead, use:
 pip3 install pifacedigitalio
Or,
 pip install pifacedigitalio

Happy trails.

No comments:

Post a Comment