Interfacing the 6522 and a Raspberry Pi
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Interfacing the 6522 and a Raspberry Pi
So, I want to hook up the two ports on a 6522 VIA to GPIO pins on a Raspberry Pi. Obviously, this is going to need some level shifting as the 6522 is running at 5V and the RPI's pins are 3V3.
I decided to use a couple of TXB0108 chips, which are bidirectional. But I ran into a snag.
Long story short, when an output pin on the RPi wants to bring a signal low, the TXB0108 seems to have trouble pulling down the 5V side of the chip. It pulls the level down to about 2.5V. This results in unreliable behaviour, as you'd expect.
I've been poring over the 6522 datasheet, and my limited skills suggest to me that pins set as inputs (or outputs, for that matter) have pullup resistors. Is that right?
The TXB0108 doesn't always play well with pullups because it relies on overwhelming whatever else is going on on a signal line to determine in which direction it's translating. The datasheet suggests that any pullups, for example, should be no stronger than 50KΩ.
I would just like a sanity check here. I tested the circuit with the VIA disconnected, and applying a low signal from the RPi's GPIO pin to the 3V3 side did, indeed, cause the corresponding 5V pin to go to 0V. It's only when the VIA is connected that the signal won't go below 2.5V (and with a lot of noise, to boot). I tried using pulldowns on the line, but no joy.
Is there anything else I could do to counter this, or is the 6522 just too strong?
I'm contemplating switching from the TXB0108 to something like the SN74LVCC4245. This would involve using another line from the 6522 to switch which direction it's translating in, but that's okay. My main concern is my need to be able to have a bidirectional 8-bit data bus between RPi and 6522, and programmatically determining direction is not a problem.
Signals on the other port will be permanently either input or output. For signals going from the 6522 to the RPi, I can use some simple level shifter or even just resistor dividers. Those going the other way won't need translation.
I decided to use a couple of TXB0108 chips, which are bidirectional. But I ran into a snag.
Long story short, when an output pin on the RPi wants to bring a signal low, the TXB0108 seems to have trouble pulling down the 5V side of the chip. It pulls the level down to about 2.5V. This results in unreliable behaviour, as you'd expect.
I've been poring over the 6522 datasheet, and my limited skills suggest to me that pins set as inputs (or outputs, for that matter) have pullup resistors. Is that right?
The TXB0108 doesn't always play well with pullups because it relies on overwhelming whatever else is going on on a signal line to determine in which direction it's translating. The datasheet suggests that any pullups, for example, should be no stronger than 50KΩ.
I would just like a sanity check here. I tested the circuit with the VIA disconnected, and applying a low signal from the RPi's GPIO pin to the 3V3 side did, indeed, cause the corresponding 5V pin to go to 0V. It's only when the VIA is connected that the signal won't go below 2.5V (and with a lot of noise, to boot). I tried using pulldowns on the line, but no joy.
Is there anything else I could do to counter this, or is the 6522 just too strong?
I'm contemplating switching from the TXB0108 to something like the SN74LVCC4245. This would involve using another line from the 6522 to switch which direction it's translating in, but that's okay. My main concern is my need to be able to have a bidirectional 8-bit data bus between RPi and 6522, and programmatically determining direction is not a problem.
Signals on the other port will be permanently either input or output. For signals going from the 6522 to the RPi, I can use some simple level shifter or even just resistor dividers. Those going the other way won't need translation.
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
Re: Interfacing the 6522 and a Raspberry Pi
What kind of 6522 are you using? Even the two modern variants have very different output buffering to each other.
I would have thought a simple resistor divider would work fine in general, and the 6522 should be happy reading 3V3 as high even through a reasonable resistor (the top half of your divider).
I would have thought a simple resistor divider would work fine in general, and the 6522 should be happy reading 3V3 as high even through a reasonable resistor (the top half of your divider).
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
It's a WDC 65C22N.
I hadn't thought of the resistor divider working bi-directionally. What would you define as a 'reasonable' resistor?
I hadn't thought of the resistor divider working bi-directionally. What would you define as a 'reasonable' resistor?
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
Re: Interfacing the 6522 and a Raspberry Pi
That one has current limiting resistors - see pages 32-35 of the datasheet. They don't specify a value, but do say that it can source 3mA at 1.5V, which I guess from a 5V supply implies around 1K built-in resistance? The exact amount may not be very reliable.
If it is 1k, and you put 2k between each output and ground, you might get around the 3.3V output level you're looking for, and I don't think it will affect inputs much.
If it is 1k, and you put 2k between each output and ground, you might get around the 3.3V output level you're looking for, and I don't think it will affect inputs much.
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
I’ll give that a go and see what happens. Thanks.
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
- GARTHWILSON
- Forum Moderator
- Posts: 8774
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
For bidirectional translation, you could do something like this for each line:
- This assumes the '22 will recognize the RPi's high output voltage as a valid 1.
- It won't work with non-WCC VIAs which do not present a true CMOS input in input mode. (I got bit by this with Rockwell's.)
- The 15pF is there to keep the 4.7K*input_capacitance time constant from slowing things down.
- For steady-state DC, the 4.7K will not reduce the RPi's output to the '22. If the RPi can still output 3.3V with the 10K load, the '22 will get the full 3.3V.
- Going the other way, the '22 pulling up to 5V will result in the RPi getting 10K/(10K+4.7K)*5V=3.4V. (You could go down to a 4.3K, but I figured you're more likely to have 4.7K, and the extra .1V should be fine. Actually the resistors will have some tolerance too. Maybe it'd be best to go to 4.3K. Most people on this forum don't have the resistor collection that I do; so if this is your situation and you don't want to have to order and wait, let me know what values you have, and we can figure out something that will work.)
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
Thanks for the feedback. I tried Gfoot’s suggestion with various resistors. A 1K resistor gave a 3.3V signal, which seemed okay to me when I was reading it on the multimeter. But looking at it on the scope showed a very noisy signal (perhaps because this is a breadboard prototype) with spikes up to 4.4V!
A680Ω resistor gives a max of about 3V and an average of 2.5V. But there are some nasty transients. Again, this might be a breadboard problem.
My resistor collection is limited: I certainly don’t have a 4.4K - the nearest I have either side are 3.3K and 4.7K. Also, since I dropped my resistor box on the floor this morning (and because I haven’t mastered the colour codes) picking a resistor is now akin to Russian roulette. I’ll see if I can have a play with that circuit, though. After all, I’m only in this for the fun!
I did also wonder about using a 3V3 Zener attached between each line and ground.
A680Ω resistor gives a max of about 3V and an average of 2.5V. But there are some nasty transients. Again, this might be a breadboard problem.
My resistor collection is limited: I certainly don’t have a 4.4K - the nearest I have either side are 3.3K and 4.7K. Also, since I dropped my resistor box on the floor this morning (and because I haven’t mastered the colour codes) picking a resistor is now akin to Russian roulette. I’ll see if I can have a play with that circuit, though. After all, I’m only in this for the fun!
I did also wonder about using a 3V3 Zener attached between each line and ground.
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
Re: Interfacing the 6522 and a Raspberry Pi
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
Interesting. I'd rather not use boards like this, but this might be simple enough to reverse engineer to incorporate into my own design. Looks like two 10K resistors and one FET per channel. (Mind you, that still adds up to 48 components in total!)
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
So that Adafruit board basically works by connecting the source & drain connections of an N-channel enhancement mode FET to the 3V3 and 5V signal pins. The gate is connected to 3V3. Each side is also pulled up with a 10K resistor to the relevant voltage (3V3 or 5V).
I don't know enough about FETs to really understand what's going on here. But would I be right in thinking that whatever is on the receiving side needs to be open drain?
I don't know enough about FETs to really understand what's going on here. But would I be right in thinking that whatever is on the receiving side needs to be open drain?
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
Re: Interfacing the 6522 and a Raspberry Pi
No, it's fully bidirectional in the sense that either side can drive or not at any point in time. The receiving side needs to just be high impedance as usual for something that's receiving a signal. You need to choose the right transistors though (with a rather low gate threshold voltage), and put them in the right way around.
To understand it, I think it helps to separate the two cases based on which side is inputting and which is outputting.
If the 3V3 side (left) is in input mode and the 5V side (right) is in output mode, then if the 5V side outputs low then the body diode in the MOSFET will pull the 3V3 side low as well, winning out over the weak pull-up resistor on the 3V3 side; and if the 5V side outputs high then that won't happen, and the pull-up resistor on the 3V3 side will pull it high.
On the other hand, if the 3V3 side is outputting and the 5V side is inputting, then if the 3V3 side outputs low then it drives the MOSFET's source terminal to 0V, while the gate is held at 3V3, and (if you choose wisely) that's enough higher than the source terminal that the MOSFET will conduct enough to pull the 5V side low (beating the weak pull-up resistor on the 5V side). And if the 3V3 side is outputting high, then as the gate of the MOSFET is also at 3V3, that's not enough higher than the source to allow the MOSFET to conduct, and the 5V side's pull-up resistor will pull it all the way to 5V.
To understand it, I think it helps to separate the two cases based on which side is inputting and which is outputting.
If the 3V3 side (left) is in input mode and the 5V side (right) is in output mode, then if the 5V side outputs low then the body diode in the MOSFET will pull the 3V3 side low as well, winning out over the weak pull-up resistor on the 3V3 side; and if the 5V side outputs high then that won't happen, and the pull-up resistor on the 3V3 side will pull it high.
On the other hand, if the 3V3 side is outputting and the 5V side is inputting, then if the 3V3 side outputs low then it drives the MOSFET's source terminal to 0V, while the gate is held at 3V3, and (if you choose wisely) that's enough higher than the source terminal that the MOSFET will conduct enough to pull the 5V side low (beating the weak pull-up resistor on the 5V side). And if the 3V3 side is outputting high, then as the gate of the MOSFET is also at 3V3, that's not enough higher than the source to allow the MOSFET to conduct, and the 5V side's pull-up resistor will pull it all the way to 5V.
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
Great explanation. Thanks. The Adafruit product uses the BSS138, which seems to be readily available.
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
-
SpaceCoaster
- Posts: 33
- Joined: 11 Apr 2019
Re: Interfacing the 6522 and a Raspberry Pi
This is also described in the Philips document Bi-directional level shifter for I2C-bus and other systems.
- speculatrix
- Posts: 151
- Joined: 03 Apr 2018
- Contact:
Re: Interfacing the 6522 and a Raspberry Pi
Just thought I'd do a quick catch-up on this, and also invite comments as to whether my conclusions are sane.
I've tried a number of level-shifting approaches. For the control signals, I've connected directly between RPi and 65C22 for signals that are outputs from the Pi, inputs on the VIA. With signals going the other way, I've used a simple resistor voltage divider with 2K2 and 3K3 resistors. (I wondered about using 220Ω and 330Ω – comments?). That's also fine.
It's the eight-bit data bus that's tricky, because it needs to be bidrectional. I tried a number of things but am currently toiling away with, again, using a simple resistor divider with the same values. Which was fine for outputs from the 65C22 but not so good for inputs. Again, I found that when the VIA's port is in input mode, the signals never get pulled down much below 2.5V.
So I looked again at the schematic for the WDC 65C22N, which was the device I was using.
Now, I'm no expert (!), but my reading of the schematic is this. In input mode, DDR will be 0 (DATA doesn't matter). The inverter will turn this into a 1 and then the NOR gate will turn this into a 0. With the P-channel FET, this will cause the FET to be ON, because there is negative voltage at the Gate relative to the Source (which is at +5V). I know the resistor is a current-limiting one, but it feels to me like it would act as a pullup in this situation. (The N-channel FET will be off).
On the WDC 65C22S, however, the arrangement is different.
Here, when DDR is 0, the NAND will output a 1 and the NOR a 0, meaning that both FETs are OFF.
So I tried replacing the N device with an S device, and now it's working much better!
Is that a sensible interpretation?
I've tried a number of level-shifting approaches. For the control signals, I've connected directly between RPi and 65C22 for signals that are outputs from the Pi, inputs on the VIA. With signals going the other way, I've used a simple resistor voltage divider with 2K2 and 3K3 resistors. (I wondered about using 220Ω and 330Ω – comments?). That's also fine.
It's the eight-bit data bus that's tricky, because it needs to be bidrectional. I tried a number of things but am currently toiling away with, again, using a simple resistor divider with the same values. Which was fine for outputs from the 65C22 but not so good for inputs. Again, I found that when the VIA's port is in input mode, the signals never get pulled down much below 2.5V.
So I looked again at the schematic for the WDC 65C22N, which was the device I was using.
Now, I'm no expert (!), but my reading of the schematic is this. In input mode, DDR will be 0 (DATA doesn't matter). The inverter will turn this into a 1 and then the NOR gate will turn this into a 0. With the P-channel FET, this will cause the FET to be ON, because there is negative voltage at the Gate relative to the Source (which is at +5V). I know the resistor is a current-limiting one, but it feels to me like it would act as a pullup in this situation. (The N-channel FET will be off).
On the WDC 65C22S, however, the arrangement is different.
Here, when DDR is 0, the NAND will output a 1 and the NOR a 0, meaning that both FETs are OFF.
So I tried replacing the N device with an S device, and now it's working much better!
Is that a sensible interpretation?
It either works or catches fire. Either way is fun.
Zolatron 64 project (on Medium)
Zolatron 64 project (on Medium)
Re: Interfacing the 6522 and a Raspberry Pi
speculatrix wrote:
Is that a sensible interpretation?
Reading this thread back, I wonder whether you might just end up with so much circuit on the device side of the 6522 that you might as well not be using the 6522. It might be easier to just build whatever you actually want from other components that already provide 3.3V tristate I/O pins - then you only need to do level shifting on the CPU-side interface of the device, which is hopefully less bi-directional and has less need for "smartness" in the level shifting.