XaiJu
beneater
beneater

patreon


SPI: The serial peripheral interface

I've gotten a few requests for videos about different communication protocols, so here's a video about a communication protocol! SPI is one of the simplest IC-to-IC protocols. I explain the protocol and show it in action between the 6502 computer and a temperature sensor.

As always, thanks for your support and let me know if you see any errors or confusing bits in the video.

Thanks,

-Ben

SPI: The serial peripheral interface

Comments

A pity when you but the 6502 kit with the reference you can follow along on Youtube but with missing code it becomes a bit difficult

Pascal Voet

@Ben I could not find the source code of this program in https://eater.net/6502. Could you share it ? Thanks!

Jun-jie Liao

I'd love to see the code as well. I transcribed what I saw on the video, but that didn't include the conversions and fetching the calibration values. Fascinating project!

Keith O

Ben, will you be sharing your 6502 assembly code for the temperature calculations? I have my 6502 pulling raw data from the sensor and will next need to convert that raw data into something more friendly. :) Thanks!

rehsd

*sigh* See, I knew this would happen. I'll add that your "next thing" happened 20 years ago, because connectors are now complicated enough that the actual contacts are buried under multiple mechanical layers that go every which way, making "plug" and "socket" more descriptive anyway.

Oren Hazi

I'll publicly state my low opinion of snowflakes that want to change how things are named just in case someone might be offended. Next thing you know, we'll have to refer to connectors as "The one with the hole" & "The one that goes into the hole".

Scott Miller

I’ve just been working with SPI as it’s usually faster than I2C. It would helpful to spend an extra minute going into the 4 modes of SPI (it seems most modern devices support 00, at least). I still have to wrap my head around this when looking at traces. I also find my scope doesn’t always decode properly even though the device is fine - especially with noisy signals.

Darren Warner

Evan, I have a $13 logic analyzer (KeeYees USB Logic Analyzer Device with 12PCS 6 Colors Test Hook Clip Set USB Cable 24MHz 8CH 8 Channel UART IIC SPI Debug for Arduino FPGA M100 SCM https://www.amazon.com/dp/B07K6HXDH1/ref=cm_sw_r_cp_api_glt_fabc_5G8WR48VWD050MGST5TK?_encoding=UTF8&psc=1). Combined with PulseView (FOSS) it’s excellent for looking at this kind of protocol.

Darren Warner

You've got the best balance between teaching a hard topic using "simple" tools (that oscilloscope is a very nice toy). Thanks for another amazing video. During the video you've mention that a shift register could be used. Can you expand a little bit more on that?

Augusto Righetto

It seems like it would be good to address the topic even if a few react negatively to the new terms. I have also seen the terms "Main" used instead of "Master" and "Secondary" used instead of "Slave" to keep acronyms as-is.

Jeremy

Thank you for the excellent video.

Vinay G

Nice work. Just a note to add that you often see the chip select (/CS) referred to as slave select (/SS). Keep up the awesome work!

George M1GEO

You are amazing

Jakob Dannesboe

Thanks for the explanation - that makes perfect sense!

Tim Walkowski

I didn't explain it, but MISO is at bit 6 because in the spi_transceive subroutine, I use the "bit" instruction which (in relevant part) sets the overflow flag based on the contents of bit 6 of the accumulator. I then use a "bvc" (branch if overflow clear) instruction to control whether we shift a 1 or a 0 into the receive buffer. Additionally, putting SCK is at bit 0 lets me do "inc PORTA" and "dec PORTA" to toggle the clock line. It didn't matter where I put CS and MOSI.

Ben Eater

Ugh.. For some reason I can never get that right. Bit. Byte. You know what I mean :)

Ben Eater

The scope is still useful for making sure you're framing things properly. After that, as Mark suggested, using some sort of microcontroller is a good bet. I basically did something very similar in the early 6502 videos when I used an Arduino mega to snoop at what's on the bus.

Ben Eater

Good feedback on cutting back and forth. In hindsight it also would have been much easier to edit if they were both on the screen at the same time. I'll see about changing it. And I might be able to get the temperature conversion working before I post the video, but no promises. I'm no assembly expert, so it's definitely a pain. Good to know that if I do get it working, it's worth throwing in even without much explanation.

Ben Eater

I am sure you will add some final comments about how one could make a third version using a shift register. I know of one YouTuber who makes shift register modules on breadboards.

Ryan Helinski

The volume seems a bit lower than other videos.

Sean

I may have missed it, but I didn't see how you came up with the bit positions for the MISO, MOSI, etc. in your code example. I am referring to DDRA for PORT A at Reset: 16:20. Specifically, why is MISO at bit 6? is it simply because that's how you connected the pins to the 6522 or is there another reason?

Tim Walkowski

Would adding a comment about CIPO/COPI be more likely to result in an angry internet mob than not mentioning it at all? Maybe I spend too much time on electronics twitter, but it seems like a catch 22. For what it's worth, I already have two dev boards with CIPO/COPI labeling, and both SparkFun and Adafruit seem to be on board. Yay more fragmentation.

Oren Hazi

LGTM! A small nit - at 28:56 you mention an octet as "between each set of 8 bytes".

Pim van Pelt

Inexpensive but needs some software: you could use a microcontroller to sample multiple digital signals and capture/stream the raw data. An STM32 has lots of 5V tolerant inputs. Decoding the raw signals and presenting them in a useful way would need some software. For a little bit more money, you could get one of those cheap USB logic analyzers, plus the Sigrok software for the user interface. These are good enough for SPI and I2C. For example: https://www.youtube.com/watch?v=7_gCUZg8nAo and https://www.youtube.com/watch?v=dobU-b0_L1I

Mark Day

While it's $200 USD, the Digilent Digital Discovery logic analyzer offers protocol analysis. It supports SPI, I²C, UART, CAN, and AVR. https://digilent.com/shop/digital-discovery-portable-usb-logic-analyzer-and-digital-pattern-generator/ I'll be using my Digilent for SPI analysis as I get into this. My Siglent scope also has SPI decode built into it, so I'll try that out, too. Lots for me to learn... :)

rehsd

Around 25:14, I was a bit confused that you had to send a register address again. How did it know that byte was a register address, and not data to be stored (with auto increment of the address, like reading the sensor data later). Looking at the data sheet, it says multi-byte writes have to alternate address and data, while multi-byte reads auto-increment the address. I don't know if this is worth pointing out in the video or in the description. Minor nit: when highlight code in your editor (for example at 25:57) it's nearly impossible to read the highlighted text. You let the code stay on screen, so we can read it after it gets unhighlighted. Also: that must be a 65C02 (since you're using STZ, TSB, TRB instructions). Brings back memories. :-)

Mark Day

Excellent! SPI and I2C are still in heavy use in electronics. I have also been wondering how a PS/2 mouse works and why the whole PC crashes when you unplug a PS/2 keyboard.

Ryan Helinski

Super interesting topic, well explained as usual. I’d love to recreate some of these experiments on my own bench, but I’ve got an inexpensive siglent SDS1202X-E scope that doesn’t decode. What’s the best way to analyze circuits like this outside of a major scope upgrade?

Evan Thayer

Awesome! Perfect timing, as I am just digging into SPI now. :)

rehsd

Very cool! At around 19 min, it might be slightly easier to understand if you have the program and the logic level drawing on the screen at the same time, side-by-side. It's also a shame that we don't get the temperature converted into Celsius for completeness, even if you don't linger on the conversion code -- but I guess looking at the C code, you don't really want to bang that out in assembly.

rhc

Exciting! I'd love to see you do more with this. An SD card reader would be great. That's something I want to implement in my homebrew for mass storage.

Brady Burnsides

Thanks Ben! Always exited when you bring something new to the 6502 computer.

Elekes Karoly

Ooh. Exciting. This is something I’ve been hoping for! Will take a look when I get chance later on! Thanks Ben!

Auctoris


More Creators