USB4VC

USB4VC Technical Notes / Make Your Own Protocol Card / Tinkering Guide

Get USB4VC Official Discord Getting Started Table of Contents

This document contains technical information about how USB4VC works. Please read the whole article if you’re planning to make your own Protocol Cards.

Tinkering Guide

Working on USB4VC is very straightforward. It’s just some python scripts running on official RaspberryPi OS Lite.

To SSH into it, set up WiFi or plug in an Ethernet cable. The IP address should show up on the second page of main menu:

Alt text

Simply do (change the IP to your own) ssh pi@192.168.1.65.

Username and password is pi and usb4vc, all lower case.

All files are inside ~/usb4vc.

Linux Input Event Codes

Now, how does USB4VC read from USB inputs in the first place?

The answer is Linux Input Event Codes. Here’s a very simplified description:

$ ls /dev/input/
event0  event1  event2  mice  mouse0  mouse1
Byte Name
0-7 Timestamp
8-9 Event Type
10-11 Event Code
12-15 Event Value

Alt text

Alt text

Hardware Pinout

The Protocol Card connector directly maps to the Raspberry Pi Header, although the pins are flipped around.

Alt text

SPI Communication Protocol

Raspberri Pi communicates with Protocol Card through SPI. Here’s a quick introduction if you’re unfamiliar.

RPi is master, P-card is slave. SCLK is 2MHz.

Mode 0 is used. CPOL=0 (CLK idle low), CPHA=0 (data valid on CLK rising edge).

RPi and P-Card communicates via fixed-length 32-byte packets.

Detailed description of messaging protocol can be found in this document, note that there are multiple pages.

Here is a sample capture of pressing U on the keyboard:

Alt text

Here is a close-up of the first few bytes:

Alt text

Compare to the keyboard tab in the document:

Alt text

P-Card Interrupt Handling

When Protocol Card has data for RPi, it should prepare the SPI buffer, and switch its interrupt pin to HIGH (Pin 36).

Upon detecting this change, RPi will send out an ACK message. Upon receiving, the P-Card switches the interrupt pin to LOW.

RPi will then send out another ACK (or NOP) message to shift the data from P-Card to itself.

Here is a sample capture:

Alt text

Currently this feature is only used for changing keyboard LEDs.

Sample SPI Message Captures

More sample captures can be found here.

Click one and press Download.

Open with Saleae Logic app.

Latency Information

Input latency can be introduced during all stages of input chain. Using keyboard as example, we have:

The part we’re interested in is Step 3 and 4, as any time spent here is the additional delay from USB4VC.

To accurately measure the delay, I used a logic analyzer connecting to keyboard USB lines and PS/2 output on the IBM PC Protocol Card. Here is the delay in action:

Alt text

From the keystroke appearing on USB, to keystroke appearing on PS/2, it took 487 microseconds.

Zoomed out capture with multiple keypresses:

Alt text

I tested out different generations of Raspberry Pis, and here is the result:

RPi Generation Avg. Latency
2 1ms
3 0.63ms
4 0.5ms

You can find the capture files here, open with saleae app, search PID ACK for USB input events, more info in this video

Making your own Protocol Card

By now you should have a general idea of how USB4VC works on a hardware level. So here are more information on how to get started on making your own Protocol Card!

Any Restrictions?

Not really! USB4VC is under MIT license, so basically anything goes apart from liability and warranty.

Feel free to develop your own Protocol Cards, write about them, sell them, anything you want really.

Do let me know if you make one though! I’d be interested in checking it out!

Microcontroller Selection

The heart of a Protocol Card is the microcontroller. Picking one requires some considerations:

Here is a quick run-down of a few popular microcontrollers:

Arduino, 8-bit ATmega-based. (UNO, Nano, Micro, Leonardo, etc)

PROs

CONs

ARM-Based Development Boards (Teensy, ESP32, RP2040, etc)

PROs

CONs

Bare ARM Chip (STM32, NXP LPC, etc)

Finally, you can just program a bare 32-bit ARM microcontroller yourself.

PROs

CONs


In conclusion, my suggestion would be:

Typical Development Steps

With your microcontroller chosen, here is how I would approach making a new Protocol Card:

Research the protocol!

First of all, look up the protocol and get as much details as you can!

Reference waveform

If possible, use a logic analyzer or oscilloscope to capture a sample waveform with a real retro peripheral.

You might need to make a pass-through cable that exposes the signal lines.

No matter how good the documentation, nothing beats a reference capture from the real deal. This way you can verify the documentation is correct, and have something to compare to down the line.

SPI wire-up

Baseboard will send Keyboard/Mouse/Gamepad events over SPI even if nothing is connected, so no need to worry about that.

Take a look at the pinout section, connect your dev board to appropriate power, ground, and SPI pins.

SPI pins are 23, 21, 19, and 24. Also make sure everything is on the same ground.

Get SPI working

Use your logic analyzer or oscilloscope to confirm the SPI waveform is correct, and making sure the MCU can reliably receive SPI messages without data corruption and dropouts. See SPI protocol section for details.

This is important as any SPI message corruption might result in missed inputs or wrong inputs being sent to retro computer.

Try start simple and sending single keyboard key strokes. Print the received message out, and compare with this document.


For ARM-based MCUs, you can usually receive messages as SPI slave in interrupt mode. Tell it you want 32 Bytes, and an interrupt callback will fire once the message has been received. See my tutorial for more information, see also the code for existing P-Cards.

For STM32, set up SPI as follows:

Alt text

Then use HAL_SPI_TransmitReceive_IT() to start listening, and HAL_SPI_TxRxCpltCallback() will fire when message is received.

Work on protocols

Once SPI is working, you can move on to implementing the protocol itself. A few general tips:

Tips and tricks

Protocol Card PCB Design Notes

If you want to design a Protocol Card for volume production, here are some helpful information.

Alt text

Unique Protocol Card ID

Once your own P-Card has been tried and tested, it can be given a unique ID so the Baseboard can properly recognise it and display its protocol in the menus.

I’m still fleshing out how this will work, expect an update soon!

Questions or Comments?

Feel free to ask in official Discord Chatroom, raise a Github issue, DM on Twitter, or email dekunukem gmail.com!

Table of Contents

Main page

(Youtube Video) USB4VC in Action

Getting Started Guide

Kit Assembly Guide

Tinkering Guide / Make Your Own Protocol Card / Technical Notes

Kickstarter Acknowledgements