AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 3:25 pm

Hello world!

I'm looking to interface my Pi 3 with an Apple II and would like to both read/write data from/to the Apple II bus and be able to do so within a clock-cycle of its 1mhz clock. The Pi 3 in its raw form is incapable of this feat, so I would need to add some middle-men to make it all work. First is the problem of not enough GPIO on the Pi... I would need some kind of GPIO expander or a device that allows me to communicate with it via SPI or bit-banging at very high speeds that can accommodate assembling 24-bits of data within the 1mhz clock-cycle *and* be able to output them at 5v; oh, and did I mention this should be bi-directional? I checked out the Guzunty Pi expander and several other expanders based on SPI but they either do not do the 3.3 <--> 5v translation necessary to make this happen or the SPI speeds they support are not fast enough to assemble 24-bits from serial within the 1mhz clock cycle needed. Someone recommended a CPLD such as the Lattice MachXO family but it doesn't do 5v.

My expertise is in software programming with some basic electronics so I'm able to put together some simple projects on the Arduino and Pi, but what I'm trying to do now is way outside of my experience and I would appreciate some advice from the experts. What approach would you guys recommend?

User avatar
joan
Posts: 15099
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 5:16 pm

If the Apple II bus is a single signal then SPI might work. If it's a parallel bus then forget it.

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 5:25 pm

Hi Joan, the Apple II bus is parallel. I didn't mean to drive the bus directly via SPI, instead I am hoping for some middle-ware that would have SPI on one end and GPIO on the other.

User avatar
mikronauts
Posts: 2817
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 6:39 pm

Hi,

Sorry, basically impossible as asked... but it really depends on exactly what you are trying to do.

1) pi emulating ram for a 6502 ... impossible without a big fpga that has enough block ram and level shifters and this case the fpga emulates the memory, which the pi could also access

2) run apple games ... just use a software emulator

If you tell us what you are actually trying to accomplish, perhaps there are other options.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 6:51 pm

Thanks for replying!

What I'm looking for is to implement a peripheral card on the Apple II utilizing the Raspberry Pi 3 as a powerful micro-controller. For this I need to be able to interface the Pi GPIO with the Apple II bus, and I need it to be able to communicate with the Apple II at 1mhz. If I can use 8 pins on the Pi as a parallel bus, connect them to three octal-transceivers (two for the 16-bit address bus, one for the 8-bit data bus) then in theory I should be able to write to the Apple II bus, but the question is, would I be able to do this within a clock cycle? The Pi's GPIO pins should be fast enough for me to toggle 8 pins three times within a 1mhz clock cycle, but I'm just guessing and would love to hear from you guys that have experience in this area.

User avatar
mikronauts
Posts: 2817
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 7:04 pm

You cannot get a guaranteed 1us response from a pi like that.

Not even 2us (as I recall that 500khz was the maximum respone rate)

Add an fpga or dual ported memory between the apple bus and a pi (if you need to use a Pi)
AnthonyPaulO wrote:
Mon Aug 28, 2017 6:51 pm
Thanks for replying!

What I'm looking for is to implement a peripheral card on the Apple II utilizing the Raspberry Pi 3 as a powerful micro-controller. For this I need to be able to interface the Pi GPIO with the Apple II bus, and I need it to be able to communicate with the Apple II at 1mhz. If I can use 8 pins on the Pi as a parallel bus, connect them to three octal-transceivers (two for the 16-bit address bus, one for the 8-bit data bus) then in theory I should be able to write to the Apple II bus, but the question is, would I be able to do this within a clock cycle? The Pi's GPIO pins should be fast enough for me to toggle 8 pins three times within a 1mhz clock cycle, but I'm just guessing and would love to hear from you guys that have experience in this area.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 7:53 pm

mikronauts wrote:
Mon Aug 28, 2017 7:04 pm
You cannot get a guaranteed 1us response from a pi like that.

Not even 2us (as I recall that 500khz was the maximum respone rate)

Add an fpga or dual ported memory between the apple bus and a pi (if you need to use a Pi)
AnthonyPaulO wrote:
Mon Aug 28, 2017 6:51 pm
Thanks for replying!

What I'm looking for is to implement a peripheral card on the Apple II utilizing the Raspberry Pi 3 as a powerful micro-controller. For this I need to be able to interface the Pi GPIO with the Apple II bus, and I need it to be able to communicate with the Apple II at 1mhz. If I can use 8 pins on the Pi as a parallel bus, connect them to three octal-transceivers (two for the 16-bit address bus, one for the 8-bit data bus) then in theory I should be able to write to the Apple II bus, but the question is, would I be able to do this within a clock cycle? The Pi's GPIO pins should be fast enough for me to toggle 8 pins three times within a 1mhz clock cycle, but I'm just guessing and would love to hear from you guys that have experience in this area.
Are you sure? From this article it seems that we can get 14mhz (22mhz with optimizations) : http://codeandlife.com/2012/07/03/bench ... pio-speed/

However, I'm not sure how dependable/sustainable this is so I'm hoping someone can confirm this is a reasonable approach.

User avatar
joan
Posts: 15099
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 8:22 pm

How does spinning a GPIO as fast as possible relate to what you want? This sort of benchmark is of no practical use.

Does the Pi control the Apple II bus clock? If so you can slow the bus down to a Pi usable rate. If not you don't have a chance under Linux and I doubt you have a chance with bare metal (no operating system) either.

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 8:46 pm

joan wrote:
Mon Aug 28, 2017 8:22 pm
How does spinning a GPIO as fast as possible relate to what you want? This sort of benchmark is of no practical use.

Does the Pi control the Apple II bus clock? If so you can slow the bus down to a Pi usable rate. If not you don't have a chance under Linux and I doubt you have a chance with bare metal (no operating system) either.
I don't understand why you don't think this benchmark is of practical use. If the benchmark shows that I can (reliably) toggle the GPIO at 22mhz then that means that I should definitely be able to transmit information to the Apple II at the 1mhz rate it requires, no? At a minimum 1mbps, right? If so, then it should be a simple matter of determining the maximum mbps that I can achieve given 22mhz, right? If not then let me know if I'm missing something, I'm not an electronics expert and could use some guidance.

Many thanks in advance!

p.s. The Pi does not control the Apple II bus clock, the goal is to communicate with the Apple II bus at the 1mhz clock speed it requires. I would think that with an Arm processor running at 2+ ghz and GPIO that can toggle at 22mhz (some people even claim 100+mhz : http://www.icrobotics.co.uk/wiki/index. ... ransmitter) I should be able to feed a 1mhz Apple II.

User avatar
rpdom
Posts: 17722
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 9:04 pm

AnthonyPaulO wrote:
Mon Aug 28, 2017 8:46 pm
I would think that with an Arm processor running at 2+ ghz
Is that on the mythical Pi4? :lol:

Just being able to toggle the bus at a particular speed isn't much use. You need to be bale to process the information. That will take many more clock cycles, and unless you are going to go bare metal (no OS) there may be a hundred or more other tasks taking up CPU time.

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 9:16 pm

rpdom wrote:
Mon Aug 28, 2017 9:04 pm
AnthonyPaulO wrote:
Mon Aug 28, 2017 8:46 pm
I would think that with an Arm processor running at 2+ ghz
Is that on the mythical Pi4? :lol:

Just being able to toggle the bus at a particular speed isn't much use. You need to be bale to process the information. That will take many more clock cycles, and unless you are going to go bare metal (no OS) there may be a hundred or more other tasks taking up CPU time.
Sorry, 1.2ghz lol... In my mind I am targetting the new Arm A64 processors that will become available at 2 or 3+ghz by the time my project is finished.

I am using Ultibo so yes, bare metal... no processes, no interrupts, just bare metal.

User avatar
mikronauts
Posts: 2817
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 10:36 pm

it's not the feeding

it is the responding witin 1us

google:

hard real time

6502 bus cycle documentation
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Mon Aug 28, 2017 11:10 pm

mikronauts wrote:
Mon Aug 28, 2017 10:36 pm
it's not the feeding

it is the responding witin 1us

google:

hard real time

6502 bus cycle documentation
Oh you mean reacting to a 1mhz clock tick? The hope is that I will have everything pre-set by the time the next tick comes along (polling for a transition on the 1mhz clock GPIO; no interrupts of course). I need to test this of course, I just wanted to see if this was at all feasible.

piras77
Posts: 147
Joined: Mon Jun 13, 2016 11:39 am

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Aug 29, 2017 6:15 am

AnthonyPaulO wrote:
Mon Aug 28, 2017 3:25 pm
First is the problem of not enough GPIO on the Pi... I would need some kind of GPIO expander or a device that allows me to communicate with it via SPI or bit-banging at very high speeds that can accommodate assembling 24-bits of data within the 1mhz clock-cycle
Do you really need an expander? Most of the Pi's have 28 GPIO pins on the header, the Compute Modules provide all 54 pins.

What exactly are you going to do? Can you sketch out a dialogue on the bus?

I guess you could scan at 1 MHz for edges and output signals, however not on Linux in userland (process interruption and suspension). You need bare-metal. There, I could imagine, you could send and receive blocks of data at 1 MHz or even more if the Pi is the bus master (i.e. if it provides the clock signal).

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 05, 2017 6:05 pm

I've put the pedal to the metal and have some results to post. First, I'd like to point out that the link I previously posted regarding the Pi as FM Transmitter does not use bit-banging on the GPIO; instead they use the clock and dynamically modulate the signal to achieve 100+ mhz speeds, which is completely different.

With that out of the way I proceeded to test the GPIO output toggle frequency on the Pi 3 going bare metal and utilizing low-level direct access to the GPIO registers and clocked that at almost 60mhz. This allows me to utilize the GPIO as a parallel bus for transmitting out-bound data at 60mhz, so for example you can send 60 million (8-bit) bytes per second (or as many bits as you have available within a GPIO bank). That's the theory at least according to the timings and oscilloscope... in practice I have yet to put something on the other end to actually consume and verify that the data I'm sending is valid, which I will eventually do once I get the reads working.

Reading seems to be giving me a problem. Last night I connected a 1mhz clock to an input GPIO pin, disabled the pull-up, and polled the register directly for changes. I first read the initial value and then polled in a tight loop (no interrupts) waiting for a transition (which is basically just looping until the current value is no longer the same as the last value); in the loop I increment a counter to see how many loops it would take for it to transition. I was surprised to see a 1 and I'm still trying to recover from my shock. I was expecting some room to maneuver here, perhaps somewhere around 30 iterations given that I was able to toggle the GPIO output at 60mhz. I copy and pasted a duplicate of the loop so that it would check for two transitions in a row and still each loop took only 1 iteration before a transition was detected. This has me scratching my head wondering where I'm going wrong; surely this can't be right?! To make matters worse I run it a few times and sometimes one of the loops will take 3 iterations to detect a transition. What the?

I'm going to debug this when I get back home and try to narrow the problem down. Perhaps it's receiving too much current since I used a 1K and 2K ohm resistors as voltage dividers to shift the 5v clock down to 3.3? If so I'll try perhaps 10/20 K or 100/200 K ohm instead. Perhaps I'm not supposed to poll that register and instead poll one of the other registers that are meant for detecting edge and level changes? Perhaps I'm seeing noise? I don't know what to think at this point. I hope it's not a limitation of the Pi as it seems awful asymmetrical for the output to toggle 60mhz and for the input to not be able to keep up with a measly 1mhz. I'll post my findings soon.

piras77
Posts: 147
Joined: Mon Jun 13, 2016 11:39 am

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Wed Sep 06, 2017 5:43 am

AnthonyPaulO wrote:
Tue Sep 05, 2017 6:05 pm
...going bare metal and utilizing low-level direct access to the GPIO registers and clocked that at almost 60mhz...
Just to make sure and out of curiosity: you set GPSET0 & GPCLR0 registers in turn (in a counter loop?) and you got a valid logic output on the pin header for a 1:1 square wave at 60 MHz?!
AnthonyPaulO wrote:
Tue Sep 05, 2017 6:05 pm
Reading seems to be giving me a problem ... I was surprised to see a 1 and I'm still trying to recover from my shock.
Yes, this seems to be a bit too little. Can you post the source code?

Anyway, the values you see by these methods are absolute limits. To do something useful, you needs additional registers (counter, pointer) and especially access memory areas to read/write your data from/to (which involves hard to predict delays due to cache activity). Still, I'm quite curious what you are going to achieve. :-)

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Wed Sep 06, 2017 2:26 pm

Okay, armed with a borrowed oscilloscope, a 3.3v arduino, and my good looks I generated a square wave from 1s down to 6us and the Pi was able to keep up. I checked the 1mhz clock and it seemed fine, connected the Pi to the clock and confirmed we have the same problem. I then swapped out the 1k/2k resistors for 100k/200k resistors and voila! We're in business! Except... not really. I sampled a bit of the clock signal and found that it wasn't clean... at times it would glitch and invert the bit for a cycle or two. I'm using the 1mhz clock on my Apple II so I'm sure it's not the clock itself, and I checked it out on the oscilloscope and it was rock solid, so something is happening after the signal leaves the Apple II; I should have attached my oscilloscope to the circuit after the resistors to see if anything's changed. Given that the resistors seems to be the source of the problem I may have to look at them again and make sure the Pi isn't drawing more current than it should. I may also have to change my approach, which is currently polling the GPIO register in a tight loop; perhaps I should sample it instead at, say, 1/8us. I may also have to look into whether the breadboard I'm using is an issue at this frequency. So many things to look at and not enough experience to narrow it down. I'll post some updates soon.

@Piras77 : Yes, I set GPSET0 and GPCLR0 in a tight loop and get almost 60mhz wave, but not quite cleanly square; check out https://github.com/hzeller/rpi-gpio-dma-demo, he benchmarked this nicely.

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Thu Sep 07, 2017 12:40 pm

Unfortunately I was unable to get much done last night (my wife was chewing my ear out until 1am from the moment I stepped in the door) but I did measure the 1mhz clock frequency after the 100K/200K resistors and the square wave looks like shark fins. Since the unofficial specs state that the most I should sink into the GPIO Input pins are .5ma and perhaps the 16.67uA I'm feeding through is too low, I swapped out the resistors for some 3.5K/7K versions (using 1K, 2K, and 500 ohm resistors) and the wave now looks like triangles, worse than before. It seems I definitely can't do this on the breadboard and should probably use an actual level-converter to do this rather than rely on resistors for dividing voltage. I'm hoping this solves the glitching I spoke about and gives me a free path forward to transmitting 8-bits at a time clocked to a 1mhz clock; will post updates tonight if external marital factors don't get in the way of the pursuit of happiness.

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 12, 2017 6:48 pm

Okay, after a lot of experimentation and testing I've managed to confirm that I can reliably read an incoming 1mhz signal using a Pi 3 via GPIO; however, it seems that is pretty much the limit of what can be done here since you don't have a lot of bandwidth left to respond to it, so as long as your logic is not too heavy you're good to go. I'm going to keep experimenting with different techniques to see if I can squeeze more bandwidth out of this and I'm also going to test reading a full byte at a time which should work but the proof is in the pudding. If anyone can find information on someone reading (not toggling) the GPIO at faster speeds let me know!

piras77
Posts: 147
Joined: Mon Jun 13, 2016 11:39 am

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 12, 2017 7:27 pm

AnthonyPaulO wrote:
Tue Sep 12, 2017 6:48 pm
Okay, after a lot of experimentation and testing I've managed to confirm that I can reliably read an incoming 1mhz signal using a Pi 3 via GPIO;
The maximum average rate for reading the GPIO Level register is far beyond that.

https://github.com/ma16/rpio/blob/relea ... ughput.pdf

So what exactly do you mean by reliable? How do you manage do pace your GPIO Level register reads to match a sampling frequency of exactly 1 MHz?

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 12, 2017 8:44 pm

At first I got really excited when I saw your pdf documenting much higher speeds, but then I read the not-so-fine-print :
This project tries to determine the maximum I/O throughput for the Raspberry Pi in order to estimate its fitness for data
sampling. The maximum I/O throughput should be considered as upper bound for data sampling, rather than an actually
achievable sampling rate. – In order to estimate the sampling rate, a data signal would be required. However, this
project simply accesses peripheral registers and ignores the sampled values
.
If I don't send/receive any data via the GPIO and all I do is read the register in a tight loop then yes, you can achieve amazing speeds... useless, but amazing. I'm actually samping a 1mhz signal via GPIO at 1mhz... I don't have much time left for heavy-duty logic, but something light is achievable.

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 12, 2017 8:46 pm

For pacing I read the rising and falling edge events; two of them count as one cycle, then I keep going until a certain number of iterations. I then calculate the sampling rate based on how long it spent in real-time and how many cycles it completed.

piras77
Posts: 147
Joined: Mon Jun 13, 2016 11:39 am

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 12, 2017 8:52 pm

AnthonyPaulO wrote:
Tue Sep 12, 2017 8:44 pm
If I don't send/receive any data via the GPIO and all I do is read the register in a tight loop then yes, you can achieve amazing speeds... useless, but amazing.
In §11 it samples the Level register and saves it to memory in blocks up to a few MB at a rate of 14 MS/s (11 MS/s on Pi-2).
AnthonyPaulO wrote:
Tue Sep 12, 2017 8:44 pm
I'm actually samping a 1mhz signal via GPIO at 1mhz... I don't have much time left for heavy-duty logic, but something light is achievable.
Again, how do you sample at a rate of exactly 1 MHz?

piras77
Posts: 147
Joined: Mon Jun 13, 2016 11:39 am

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 12, 2017 8:57 pm

AnthonyPaulO wrote:
Tue Sep 12, 2017 8:46 pm
For pacing I read the rising and falling edge events; two of them count as one cycle, then I keep going until a certain number of iterations. I then calculate the sampling rate based on how long it spent in real-time and how many cycles it completed.
So you don't sample at fixed 1 MHz but on an average of 1 MHz. That means the average time between samples is 1us, but several of them may be (for example) more than 10us apart. So you don't know whether you missed any signals.

AnthonyPaulO
Posts: 29
Joined: Fri Aug 11, 2017 1:07 pm

Re: SPI or BitBang via GPIO to 5v device and vice-versa

Tue Sep 12, 2017 8:59 pm

He can sample the Level register all he wants, but unless it actually contains data it's useless. I would love to see if he can achieve those rates with, say, a teensy feeding data to the Pi via GPIO at Mhz speeds. I find that as soon as those pins start dancing your sampling rate goes down considerably.

I don't understand your question of how I sample at that rate; I already explained how I sample it. Do you mean how I feed it? I use an Arduino that is providing data serially (one bit at a time) at exactly 1mhz.

I'm not missing any bits, at least not in the tests I've done. I've captured the sampled data to my SD and it is exactly what I sent it.

Return to “Interfacing (DSI, CSI, I2C, etc.)”