BMardle
Posts: 100
Joined: Wed Feb 13, 2013 4:00 pm
Location: Isle of Wight

MagPi67: Pi in Spectrum case: diodes on 'address' lines

Sun Feb 25, 2018 6:44 pm

Hi, all.
The project in MagPi 67 to put a Pi in a ZX Spectrum case sounds fun. (Unfortunately, the keyboard is the only part of my venerable Speccy that doesn't work; though my Interface 1 is duff too :roll: )
I'm a little baffled as to why there are diodes on the 'address' lines connected to the keyboard, though. Wouldn't it be possible to program the relevant GPIO pins as open-drain outputs? (The inputs would be programmed to have pull-ups.) Then the address lines could be shorted together harmlessly.
Bruce Mardle. "You know I yearn for a simpler time of barn dances and buggy rides before life was cheapened by heartless machines."

hippy
Posts: 3336
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Sun Feb 25, 2018 8:27 pm

As you say there is no need for diodes for a keypad matrix. Perhaps the author just didn't realise that.

PiGraham
Posts: 3313
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Sun Feb 25, 2018 8:39 pm

Diodes are usually used on a keyboard matrix to allow detection of multiple keys at once.

If the Spectrum uses the address bus directly to scan the keyboard then diodes would be important to prevent multiple keys shorting address lines together, which would be a very bad thing. Are the diodes you mention a legacy of the Spectrum or part of the Pi interface?

I looked up the article.
https://www.raspberrypi.org/magpi-issues/MagPi67.pdf
I agree the diodes aren't strictly necessary, but it might be preferabl to just configure column gpios as outputs and set themes high and low rather than having to tristate them )reconfigure as inputs in the idel state) . In that case you don't want to risk shorting columns together as that risks damage to the gpio.

BMardle
Posts: 100
Joined: Wed Feb 13, 2013 4:00 pm
Location: Isle of Wight

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Sun Feb 25, 2018 9:57 pm

PiGraham wrote:
Sun Feb 25, 2018 8:39 pm
Are the diodes you mention a legacy of the Spectrum or part of the Pi interface?
Part of the Pi interface. Effectively they're converting totem-pole outputs to open-drain ones... but you can effectively program the GPIOs to be open-drain outputs. Admittedly, adding the diode removes the possibility of a low output being connection to a high output due to a programming error.
Bruce Mardle. "You know I yearn for a simpler time of barn dances and buggy rides before life was cheapened by heartless machines."

PiGraham
Posts: 3313
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Mon Feb 26, 2018 7:59 am

BMardle wrote:
Sun Feb 25, 2018 9:57 pm
Admittedly, adding the diode removes the possibility of a low output being connection to a high output due to a programming error.
Not just a programming error. Running any other gpio program on that hardware which configures those gpio as output risks damage. If the hardware is dedicated to a single task it's not a problem, but having a keyboard hints at a more general purpose approach. The diodes are not essential but they seem to me to be a reasonable safety measure. 100R resistors would work as an alternative, just limiting short circuit current. It's a good idea to never connect a switch contact between two outputs. It's no bad thing to put a resistor in series with a switch to GND or VCC for the same reason. The gpio you intended to always be an input might get configured as an output of opposite polarity and result in an over-current condition.

BMardle
Posts: 100
Joined: Wed Feb 13, 2013 4:00 pm
Location: Isle of Wight

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Mon Feb 26, 2018 9:04 am

PiGraham wrote:
Mon Feb 26, 2018 7:59 am
The diodes are not essential but they seem to me to be a reasonable safety measure.
OK. You've convinced me. :D
Bruce Mardle. "You know I yearn for a simpler time of barn dances and buggy rides before life was cheapened by heartless machines."

BMardle
Posts: 100
Joined: Wed Feb 13, 2013 4:00 pm
Location: Isle of Wight

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Mon Feb 26, 2018 9:26 am

Although... with the keyboard plugged in, if you press 2 keys on the same address line at the same time, that shorts together 2 of the 'data' GPIOs. So, by the same logic, you should have diodes on those lines too ;)
Bruce Mardle. "You know I yearn for a simpler time of barn dances and buggy rides before life was cheapened by heartless machines."

PiGraham
Posts: 3313
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Mon Feb 26, 2018 9:49 am

BMardle wrote:
Mon Feb 26, 2018 9:26 am
Although... with the keyboard plugged in, if you press 2 keys on the same address line at the same time, that shorts together 2 of the 'data' GPIOs. So, by the same logic, you should have diodes on those lines too ;)
Making it indestructible does get a bit ridiculous for such a cheap SBC. It wouldn't be unreasonable to connect it directly and accept the small risk that you later do something that kills something on the Pi.

First line of defence can be the pins you know will be set as outputs. If you are a cautious or pessimistic person (or prone to mash the keyboard in frustration when your gpio experiments aren't working) why not put diodes or resistors on the rows as well?

hippy
Posts: 3336
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Mon Feb 26, 2018 12:20 pm

BMardle wrote:
Mon Feb 26, 2018 9:04 am
PiGraham wrote:
Mon Feb 26, 2018 7:59 am
The diodes are not essential but they seem to me to be a reasonable safety measure.
OK. You've convinced me. :D
I'm less convinced. Diodes may provide protection for the GPIO driving rows but there's no equivalent protection for reading columns.

I am less inclined to believe the author added them for protection against unexpected circumstances than I am in believing the author may simply have not been aware of the open-drain mode of use which avoids the need for diodes.

The diodes are needed, as noted, if the rows are always driven high and low, are a necessity to avoid damage, and I think that's why they are there. Looking at the source code will show how the author implemented their scan routine, whether it mandates diodes or not.

BMardle
Posts: 100
Joined: Wed Feb 13, 2013 4:00 pm
Location: Isle of Wight

Re: MagPi67: Pi in Spectrum case: diodes on 'address' lines

Wed Feb 28, 2018 1:15 pm

hippy wrote:
Mon Feb 26, 2018 12:20 pm
The diodes are needed, as noted, if the rows are always driven high and low, are a necessity to avoid damage, and I think that's why they are there. Looking at the source code will show how the author implemented their scan routine, whether it mandates diodes or not.
Indeed. My first paying job was designing embedded computers, so it still seems weird to me to use unnecessary components. (I was told my first product would retail for £100. "Oh, that's OK," I thought... then they told me what a pitiful fraction of that I could spend on components!)

If nothing else, the MagPi article is encouraging me to fix my Speccy's keyboard :)
Bruce Mardle. "You know I yearn for a simpler time of barn dances and buggy rides before life was cheapened by heartless machines."

Return to “The MagPi”

Who is online

Users browsing this forum: No registered users and 1 guest