A look at the PS ∕ 2 protocol

Introduction

Soon we have everything in order to read keystrokes from a PS/2 keyboard with our FPGA. The levelshifter is already completed. Before we continue with the gateware to read keystrokes, let's first have a look at the PS/2 protocol.

Wikipedia already has a lot of information on it's PS/2 port page.

The PS/2 protocol is fairly simple. The connector carries Vcc (5V), GND as well the clock and data lines. Clock and data are normally high and can be pulled down by both the host and the device (our keyboard). The clock is always generated by the device.

To keep things even simpler we are only interested in the communication from device to host. There are mechanisms for the host to inhibit communication and send data to the device, but I won't go into detail about that as I won't use that for now.

When a key on the keyboard is pressed, the onboard controller begins to toggle the clock line while simultaneously outputting the corresponding data.

Measurements

Without saying too much, let's just have a look at a transmission example:

After connecting my Picoscope to the data and clock lines from the breakout board which is connected to the PS/2 keyboard and supplies it with power, I can view what happens when I press a key.

Measurement of the clock frequency

It seems we captured the transmission of two bytes. I have measured the clock in the previous screen capture to be 13.42 kHz.

If we have a look at the page for the PS/2 port on Wikipedia we can see that the clock can be anywhere in the range 10.0–16.7 kHz.

That seems right. Great!

How about we press a specific key on the keyboard and have a look at the response.

I decided to press the H key. The keycode for H should be 0x33.

H pressed on the keyboard

And yes, that seems to be correct.

Serial Decoding

Here all of the data in view is listed. The 0x33 is recognized correctly.

Let's release the key (we can't keep it pressed forever).

H pressed on the keyboard

As you can see this time we get two data bytes:

  • 0xF0
  • 0x33

What does 0xF0 stand for? Well it's that simple: 0xF0 is used as a suffix to indicate that a key was released instead of pressed.

Serial Decoding

And here again the serial decoding of the waveform data.

Summary

Well, that's it. The PS/2 protocol is really that simple. At least for what we're going to use it. With this information we can get started to develop the necessary gateware to read out keystrokes very soon.