dcsimg
Skip to end of metadata
Go to start of metadata

 

Code Downloads

PS/2 Keyboard Interface Logic:  ps2_keyboard.vhd

Debounce Logic (must also be included in the project):  debounce.vhd

Features

  • VHDL source code of a PS/2 keyboard interface
  • Outputs the PS/2 make and break codes received from a keyboard
  • Sets flag output when new code is available
  • Validates parity, start, and stop bits of PS/2 transactions
  • Configurable system clock frequency
  • Synchronizes between PS/2 and system clock domains
  • Debounces incoming PS/2 signals

Introduction

This details a PS/2 keyboard interface component for use in CPLDs and FPGAs, written in VHDL.  The component receives data transactions from a PS/2 keyboard and provides the keyboard make and break codes to user logic over a parallel interface.  It was designed using Quartus II, version 12.1.  Figure 1 illustrates a typical example of the PS/2 keyboard interface integrated into a system.  An example design that implements this PS/2 keyboard interface component to make a PS/2 keyboard to ASCII converter is available here.

Figure 1.  Example Implementation

Background

PS/2 (IBM Personal System/2) is an interface for keyboards and mice to PC compatible computer systems via a 6-pin Mini-DIN connector.  The computing system must provide the keyboard or mouse with 5V source and ground connections.  Communication occurs over a 2-wire serial interface, consisting of a clock line and a data line.  Both lines require pull-up resistors (2kΩ shown in Figure 1).  The 120Ω series resistors in Figure 1 are required to interface the 3.3V FPGA I/O to the 5V signals.

Once powered, the keyboard goes through a self- initialization sequence.  Upon completion, it is ready to communicate keyboard events over the PS/2 interface. 

Figure 2 illustrates the transaction format.  Both clock and data signals are logic level high when inactive.  The keyboard provides both the clock and data.  The clock has a frequency between 10 kHz and 16.7 kHz (i.e. a 60-100us period).  The data begins with a start bit (logic low), followed by one byte of data, a parity bit, and finally a stop bit (logic high).  The data is sent LSB first.  Each bit should be read on the falling edge of the clock signal.  Once complete, both the clock and data signals return to logic level high.

Figure 2.  PS/2 Keyboard Transmission Timing Diagram

The data byte represents part of a keyboard scan code:  either a make code (key press) or a break code (key release).  Three different sets of scan codes exist, however the vast majority of keyboards use Scan Code Set 2, which is provided in the Appendix.  A make code usually consists of either one or two bytes.  If a make code uses two bytes, the first byte is x“E0”.  A given key’s break code is typically the same as its make code, except that break codes include an additional x“F0” byte as the 2nd to last byte.  (The PAUSE and PRNT SCRN keys are exceptions to the above.)

While it is possible to send data to a keyboard (to change its settings, etc.), this is generally unnecessary and is not included in this VHDL component.

Theory of Operation

Figure 3 conceptually illustrates the PS/2 keyboard interface component’s architecture.  The clock and data signals from the keyboard are first synchronized and debounced.  (The debounce component VHDL is provided above and documentation is available here.)  The resultant internal PS/2 data signal is then serially loaded into a shift register on falling edges of the PS/2 clock.  An idle counter determines when the transaction is finished, defined by the PS/2 clock remaining at a high logic level for more than 55us, i.e. longer than half of the worst-case PS/2 clock period.  Combinational error checking logic verifies the start bit, stop bit, and parity bit with the data.   When the PS/2 port is idle and the data is valid, the component outputs the received PS/2 code and sets the ps2_code_new signal high to indicate that a new code is available on the ps2_code bus.  The code remains available on the bus until another code is received.  The ps2_code_new signal remains high until another PS/2 transaction begins (when a low PS/2 clock signal clears the idle counter).

Figure 3.  PS/2 Keyboard Interface Logic Architecture

Port Descriptions

Table 1 describes the PS/2 keyboard interface’s ports.

Table 1.  Port Descriptions

PortWidthModeData TypeInterfaceDescription
clk1instandard logicuser logicSystem clock.
ps2_clk1instandard logicPS/2 KeyboardClock signal from PS/2 keyboard.
ps2_data1instandard logicPS/2 KeyboardData signal from PS/2 keyboard.
ps2_code_new1outstandard logicuser logicNew code available flag.  The flag is low during PS/2 transactions, and a low-to-high transition indicates that a new PS/2 code is available on the ps2_code bus.
ps2_code8outstandard logic vectoruser logic8-bit PS/2 code received from the keyboard.

Setting the Timing Parameters

The system clock speed affects the debounce time and the idle counter timing.  The two GENERIC parameters declared in the ENTITY, clk_freq and debounce_counter_size must be set appropriately for the component to operate correctly.  The clk_freq parameter must be set to the system clock clk frequency in Hz.  The default setting in the provided code is 50 MHz (the frequency at which the component was simulated and tested).   The debounce_counter_size parameter must be set such that 2 debounce_counter_size / clk_freq = 5us, as described in the documentation for the debounce component here.  For a 50 MHz system clock, debounce_counter_size = 8.

Example Transaction

Figure 4 shows the timing diagram of an example transaction.  Once the PS/2 clock signal goes low, the ps2_code_new flag deasserts to indicate that a new PS/2 keyboard transaction is in progress.  When the transaction completes, the ps2_code_new flag asserts to indicate that a new PS/2 code has been received and is available on the ps2_code bus.  In this case, the PS/2 code received is x“1C”, which is the make code for the “A” key.

Figure 4.  Example PS/2 Transaction

Conclusion

This PS/2 keyboard interface is a programmable logic component that receives transactions from PS/2 keyboards.  It synchronizes the clocks domains, debounces the input signals, performs error checking, and notifies the user logic when new codes from the keyboard are available on its parallel output bus.

Appendix:  Scan Code Set 2

Table A1.  Keyboard Scan Code Set 2

KeyMake Code (hex)Break Code (hex)
A1CF0,1C
B32F0,32
C21F0,21
D23F0,23
E24F0,24
F2BF0,2B
G34F0,34
H33F0,33
I43F0,43
J3BF0,3B
K42F0,42
L4BF0,4B
M3AF0,3A
N31F0,31
O44F0,44
P4DF0,4D
Q15F0,15
R2DF0,2D
S1BF0,1B
T2CF0,2C
U3CF0,3C
V2AF0,2A
W1DF0,1D
X22F0,22
Y35F0,35
Z1AF0,1A
045F0,45
116F0,16
21EF0,1E
326F0,26
425F0,25
52EF0,2E
636F0,36
73DF0,3D
83EF0,3E
946F0,46
`0EF0,0E
-4EF0,4E
=55FO,55
\5DF0,5D
BKSP66F0,66
SPACE29F0,29
TAB0DF0,0D
CAPS58F0,58
L SHFT12FO,12
L CTRL14FO,14
L GUIE0,1FE0,F0,1F
L ALT11F0,11
R SHFT59F0,59
R CTRLE0,14E0,F0,14
R GUIE0,27E0,F0,27
R ALTE0,11E0,F0,11
APPSE0,2FE0,F0,2F
ENTER5AF0,5A
ESC76F0,76
F15F0,05
F26F0,06
F34F0,04
F40CF0,0C
F53F0,03
F60BF0,0B
F783F0,83
F80AF0,0A
F91F0,01
F109F0,09
F1178F0,78
F127F0,07
PRNT SCRNE0,12,E0,7CE0,F0,7C,E0,F0,12
SCROLL7EF0,7E
PAUSEE1,14,77,E1,F0,14,F0,77-NONE-
[54FO,54
INSERTE0,70E0,F0,70
HOMEE0,6CE0,F0,6C
PG UPE0,7DE0,F0,7D
DELETEE0,71E0,F0,71
ENDE0,69E0,F0,69
PG DNE0,7AE0,F0,7A
U ARROWE0,75E0,F0,75
L ARROWE0,6BE0,F0,6B
D ARROWE0,72E0,F0,72
R ARROWE0,74E0,F0,74
NUM77F0,77
KP /E0,4AE0,F0,4A
KP *7CF0,7C
KP -7BF0,7B
KP +79F0,79
KP ENE0,5AE0,F0,5A
KP .71F0,71
KP 070F0,70
KP 169F0,69
KP 272F0,72
KP 37AF0,7A
KP 46BF0,6B
KP 573F0,73
KP 674F0,74
KP 76CF0,6C
KP 875F0,75
KP 97DF0,7D
]5BF0,5B
;4CF0,4C
'52F0,52
,41F0,41
.49F0,49
/4AF0,4A

Contact

Comments, feedback, and questions can be sent to eewiki@digikey.com.

 

  • No labels