PS/2 Controller


Table of Contents

Introduction
Controller interface
Port Descriptions
Interface description
PS/2 Keyboard protocol
Keyboard-to-Host communication
Host-to-Keyboard communication
PS/2 Mouse protocol
Mouse-to-host communication

Introduction

The PS/2 controller might be used to communicate both with a PS/2 keyboard and a PS/2 mouse. It provides the interface to the PS/2 protocol, handling the data transmission, error detection and the timing control.

This document will describe the interface and operation of the PS/2 controller and give an overview of the PS/2 protocol.

Controller interface

Port Descriptions

Figure 1. PS/2 Controller

PS/2 Controller

The PS/2 controller interface is illustrated in figure Figure 1, “PS/2 Controller”, with inputs shown on the left, outputs on the right, and PS2_CLK and PS2_DAT lines being bidirectional. Ports are used as follows:

  • clk - system clock input, must be 50MHz for the timing control to work properly.

  • reset - the active-high reset.

  • PS2_CLK and PS2_DAT - PS2 clock and data lines, respectively.

  • received_data - last data received from the PS/2 bus. 8 bit word.

  • received_data_en - new data signal. It is driven high for one clock cycle whenever a new data is received (i.e., received_data changes).

  • the_command - command to be sent, 8 bit input.

  • send_command - command ready signal, should be driven high to start transmission and held high until either transmission is done or error occurs.

  • command_was_sent - this signal is driven high once the command is sucessfully sent.

  • error_communication_timed_out - indicates the transmission error when sending the command.

Interface description

The controller interface consists of two parts: command-in and command-out. The command output will always take a precedence over the command input.

PS/2 commands may be sent using the four command-out ports: the_command, send_command, command_was_sent, and error_communication_timed_out. Once the_command lines have been set to the desired value, the send_command line is driven high, and the command transmission commences. The send_command line should be kept high until either command_was_sent or error_communication_timed_out signal is activated, meaning, respectively, success or failure of the transmission.

The commands are received automatically, and the most recent command is stored in the register connected to the received_data port. The signal received_data_en is driven high for one clock cycle whenever a new command is received.

PS/2 Keyboard protocol

The PS/2 keyboard communicates through the bi-directional half-duplex line. When the line is idle, the keyboard may send the data consisting of 8 bit words and representing the key codes. Additionally, there is a set of commands that the host may issue to the keyboard to control various operating parameters. The host has the priority over the communication line (i.e., command may be sent at any time and there will be no key codes transmitted until the command finishes).

Keyboard-to-Host communication

The keyboard will send a code consisting of one or more 8 bit words whenever the key is pressed, held down, or released. The codes for the conventional keyboard are listed below. "Make" code is sent when the key is pressed down, and repeated periodically if the key is held down. The "brake" code is sent when the key is released. The codes will appear on the received_data port of the PS/2 controller as they are received. It is user's responsibility to check the code before it is overwritten.

Host-to-Keyboard communication

The keyboard may be controlled using a set of commands listed and describe below. This mode of communication will inhibit the transmission of the key codes. Instead, the keyboard will transmit an acknowledge code upon receiving the command. The PS/2 controller provides a simple interface to send the commands, as described in the section called “Interface description”

PS/2 Mouse protocol

The PS/2 mouse operates in the similar manner as the PS/2 keyboard, except the data packets are different. The mouse sends the data using 3 byte packets. Every packet consists of relative movement since the last transmission and the button state. Additionally, there is a set of commands that the host may send to the mouse to control its operating modes.

Mouse-to-host communication

When in a normal mode of operation, mouse will periodically send the data packets containing the following information:

  • Button state - 3 bits representing the state of the left, right and middle buttons.

  • Relative position - two 9-bit 2's-complement numbers representing the amount of movement that has occured since the last transmission

The mouse has two internal counters to keep track of the relative position. The counters are incremented/decremented every time the mouse is moved. The counter step with respect to the physical distance depends on the resolution (which can be changed as described below), with the default being 4 counts/mm. The counters are reset everytime the position data is transmitted to the host.