You are on page 1of 8

PS/2 Core for Altera DE-Series Boards

For Quartus II 11.0

Core Overview

The PS/2 Serial Port on Altera DE2/DE1 boards is intended for connecting a keyboard or a mouse to the board. The PS/2 Core provides a connection to the PS/2 Serial Port and presents an easy-to-use communication interface to PS/2 peripherals.

Functional Description

The PS/2 Core handles the timing of the PS/2 Serial Data Transmission Protocol. A device driver can communicate with it by reading from and writing to its data and control registers.

Instantiating the Core in SOPC Builder

Designers can implement a PS/2 Core by using the SOPC Builder. There is no need to congure the core. The core comes with a 256-word FIFO for storing data received from a PS/2 device.

4
4.1

Software Programming Model


Register Map

Device drivers control and communicate with the PS/2 Core through two 32-bit registers. Communication with the PS/2 peripheral is done by writing or reading the registers through the Avalon Slave Port. Table 1 shows the details for the registers. Table 1. PS/2 Core register map Offset Register R/W/C in bytes Name 31. . . 16 0 data R/W RAVAIL 4 control R/C
Notes on Table 1: (1) Reserved. Read values are undened. Write zero.

15 RVALID
(1)

Bit description 14. . . 11 10 9


(1)

8 RI

CE

(1)

7. . . 1 0 DATA (1) RE

Altera Corporation - University Program May 2011

PS/2 C ORE FOR A LTERA DE-S ERIES B OARDS

For Quartus II 11.0

4.1.1 data Register Table 2. data register bits Bit number Bit name Read/Write/Clear 7. . . 0 DATA R/W

15

RVALID

31. . . 16

RAVAIL

Description The value to transfer to/from the PS/2 core. When writing, the DATA eld is interpreted as a command to be sent to the PS/2 device. When reading, the DATA eld is data from the PS/2 device. Indicates whether the DATA eld is valid. If RVALID=1, then the DATA eld is valid, else the DATA is undened. The number of data items remaining in the read FIFO (including this read).

4.1.2 control Register Table 3. control register bits Bit number Bit name Read/Write/Clear 0 RE R/W 8 RI R 10 CE C

Description Interrupt-enable bit for read interrupts. Indicates that a read interrupt is pending. Indicates that an error occurred while trying to send a command to a PS/2 device.

4.2

Software Functions

The PS/2 Core is packaged with C-language functions accessible through the the hardware abstraction layer (HAL) as listed below. These functions implement common operations that users need for the PS/2 Core. To use the functions, the C code must include the statement: #include "altera_up_avalon_ps2.h" In addition, some sample functions for specic communication with the keyboard or mouse are also provided. They serve as a good starting point if the user wishes to develop more features with the PS/2 Port. To use the keyboard or mouse communication functions, the corresponding header les, altera_up_ps2_keyboard.h and altera_up_ps2_mouse.h, have to be included. These functions are described below.

Altera Corporation - University Program May 2011

PS/2 C ORE FOR A LTERA DE-S ERIES B OARDS

For Quartus II 11.0

4.3
4.3.1

PS/2 Port Documentation


PS2 DEVICE

Prototype:
typedef enum { PS2_MOUSE = 0; PS2_KEYBOARD = 1; PS2_UNKNOWN = 2; } PS2_DEVICE;

Include: Fields:

<altera_up_avalon_ps2.h> PS2_MOUSE Indicate that the device is a PS/2 Mouse. PS2_KEYBOARD Indicate that the device is a PS/2 Keyboard. PS2_UNKNOWN The program cannot determine what type the device is.

4.3.2

alt up ps2 init void alt_up_ps2_init(alt_up_ps2_dev *ps2) <altera_up_avalon_ps2.h> ps2 the PS/2 device structure. Initialize the PS/2 device and detect device type (mouse or keyboard). The function will set the device_type eld of ps2 to PS2_MOUSE or PS2_KEYBOARD upon successful initialization, otherwise the intialization is unsuccessful.

Prototype: Include: Parameters: Description: Notes:

4.3.3

alt up ps2 enable read interrupt void alt_up_ps2_enable_read_interrupt(alt_up_ps2_dev *ps2) <altera_up_avalon_ps2.h> ps2 the PS/2 device structure. nothing Enable read interrupts for the PS/2 port.

Prototype: Include: Parameters: Returns: Description: 4.3.4

alt up ps2 disable read interrupt void alt_up_ps2_disable_read_interrupt(alt_up_ps2_dev *ps2) <altera_up_avalon_ps2.h> ps2 the PS/2 device structure. nothing Diaable read interrupts for the PS/2 port.

Prototype: Include: Parameters: Returns: Description:

Altera Corporation - University Program May 2011

PS/2 C ORE FOR A LTERA DE-S ERIES B OARDS

For Quartus II 11.0

4.3.5

alt up ps2 write data byte int alt_up_ps2_write_data_byte(alt_up_ps2_dev *ps2, unsigned char byte) <altera_up_avalon_ps2.h> ps2 the PS/2 device structure. byte the byte to be written to the PS/2 port. 0 on success, or -EIO on failure. Write a byte to the PS/2 port.

Prototype: Include: Parameters: Returns: Description:

4.3.6

alt up ps2 write data byte with ack int alt_up_ps2_write_data_byte_with_ack(alt_up_ps2_dev *ps2, unsigned char byte) <altera_up_avalon_ps2.h> ps2 the PS/2 device structure. byte the byte to be written to the PS/2 port. 0 on success, -EIO on write failure, or -ETIMEDOUT on timeout when waiting for the acknowledgment. Write a byte to the PS/2 port and wait for the acknowledgment. The timeout value is dened in the PS/2 device structure .

Prototype: Include: Parameters: Returns: Description: Notes:

4.3.7

alt up ps2 read data byte int alt_up_ps2_read_data_byte(alt_up_ps2_dev *ps2, unsigned char *byte) <altera_up_avalon_ps2.h> ps2 the PS/2 device structure. byte pointer to the memory location to store the byte. 0 on success, or -ETIMEDOUT when timeout. Read a byte from the PS/2 port. User can set disable the timeout by setting the timeout in to 0.

Prototype: Include: Parameters: Returns: Description: Notes:

4.3.8

alt up ps2 clear fo void alt_up_ps2_clear_fifo(alt_up_ps2_dev *ps2) <altera_up_avalon_ps2.h> ps2 the PS/2 device structure. Clear the FIFO for the PS/2 port.

Prototype: Include: Parameters: Description:

Altera Corporation - University Program May 2011

PS/2 C ORE FOR A LTERA DE-S ERIES B OARDS

For Quartus II 11.0

4.3.9

alt up ps2 read fd int alt_up_ps2_read_fd(alt_fd *fd, char *ptr, int len) <altera_up_avalon_ps2.h> fd the le descriptor for the PS/2 device. ptr memory location to store the bytes read. len number of bytes to be read. the number of bytes actually read. Read len bytes from the PS/2 device.

Prototype: Include: Parameters:

Returns: Description:

4.3.10

alt up ps2 write fd int alt_up_ps2_write_fd(alt_fd *fd, const char *ptr, int len) <altera_up_avalon_ps2.h> fd the le descriptor for the PS/2 device. ptr memory location storing the bytes to write. len number of bytes to write. the number of bytes actually written. Write len bytes to the PS/2 device from memory location pointed by ptr .

Prototype: Include: Parameters:

Returns: Description:

4.3.11

alt up ps2 open dev alt_up_ps2_dev* alt_up_ps2_open_dev(const char *name) <altera_up_avalon_ps2.h> name the specied name of the device in SOPC Builder the PS/2 device structure Open a PS/2 device structure with name in SOPC Builder.

Prototype: Include: Parameters: Returns: Description:

Altera Corporation - University Program May 2011

PS/2 C ORE FOR A LTERA DE-S ERIES B OARDS

For Quartus II 11.0

4.4
4.4.1

PS/2 Keyboard Documentation


KB CODE TYPE

Prototype:
typedef enum { KB_ASCII_MAKE_CODE = 1; KB_BINARY_MAKE_CODE = 2; KB_LONG_BINARY_MAKE_CODE = 3; KB_BREAK_CODE = 4; KB_LONG_BREAK_CODE = 5; KB_INVALID_CODE = 6; } KB_CODE_TYPE;

Include: Fields:

Description:

<altera_up_ps2_keyboard.h> KB_ASCII_MAKE_CODE Make code that corresponds to an ASCII character. For example, the ASCII make code for key [ A ] is 1C. KB_BINARY_MAKE_CODE Make code that corresponds to a nonASCII character. For example, the binary (non-ASCII) make code for key [Left Alt] is 11. KB_LONG_BINARY_MAKE_CODE Make code that has two bytes (the rst byte is E0). For example, the long binary make code for key [Right Alt] is "E0 11". KB_BREAK_CODE Break code that has two bytes (the rst byte is F0). For example, the break code for key [ A ] is "F0 1C". KB_LONG_BREAK_CODE Long break code that has three bytes (with the rst two bytes "E0 F0"). For example, the long break code for key [Right Alt] is "E0 F0 11". KB_INVALID_CODE Scan codes that the decoding FSM is unable to decode. The enum type for the type of keyboard code received.

Altera Corporation - University Program May 2011

PS/2 C ORE FOR A LTERA DE-S ERIES B OARDS

For Quartus II 11.0

4.4.2

decode scancode int decode_scancode(alt_up_ps2_dev *ps2, KB_CODE_TYPE *decode_mode, alt_u8 *buf, char *ascii) <altera_up_ps2_keyboard.h> ps2 the PS/2 device structure. The actually connected PS/2 device has to be a keyboard otherwise the functions behavior is undened. decode_mode indicates which type of code (Make Code, Break Code, etc.) is received from the keyboard when the key is pressed. buf points to the location that stores the make/break code of the key pressed. ascii pointer to the memory location to store the pressed ASCII character. If a non-ASCII key is pressed, ascii will be set to 0 0 for success, or negative errno for corresponding errors. Communicate with the PS/2 keyboard and get the make code of the key when a key is pressed. For KB_LONG_BINARY_MAKE_CODE and KB_BREAK_CODE , only the second byte is returned. For KB_LONG_BREAK_CODE , only the third byte is returned.

Prototype:

Include: Parameters:

Returns: Description: Notes:

4.4.3

set keyboard rate alt_u32 set_keyboard_rate(alt_up_ps2_dev *ps2, alt_u8 rate) <altera_up_ps2_keyboard.h> rate an 8-bit number that represents the repeat/delay rate of the keyboard. 0 on success, negative value on error. Set the repeat/delay rate of the keyboard.

Prototype: Include: Parameters: Returns: Description:

4.4.4

translate make code void translate_make_code(KB_CODE_TYPE decode_mode, alt_u8 makecode, char *str) <altera_up_ps2_keyboard.h> decode_mode the type of the make code (ASCII, binary, or long binary) makecode the last byte of the make code (if the make code has multiple bytes) str the pointer to the memory location to store the description string Translate the make code into string description.

Prototype: Include: Parameters:

Description:

Altera Corporation - University Program May 2011

PS/2 C ORE FOR A LTERA DE-S ERIES B OARDS

For Quartus II 11.0

4.4.5

reset keyboard alt_u32 reset_keyboard() <altera_up_ps2_keyboard.h> 0 on passing the BAT (Basic Assurance Test), negative value on error. Send the reset command to the keyboard.

Prototype: Include: Parameters: Returns: Description:

4.5
4.5.1

PS/2 Mouse Documentation


alt up ps2 mouse reset int alt_up_ps2_mouse_reset(alt_up_ps2_dev *ps2) <altera_up_ps2_mouse.h> ps2 the PS/2 mouse device strcture 0 on BAT is passed, -EINVAL when the PS/2 device is not mouse, or -EIO if error occurs. Reset the mouse.

Prototype: Include: Parameters: Returns: Description:

4.5.2

alt up ps2 mouse set mode int alt_up_ps2_mouse_set_mode(alt_up_ps2_dev *ps2, alt_u8 byte) <altera_up_ps2_mouse.h> ps2 the PS/2 mouse device strcture byte the byte representing the mode (see macro denitions for details). 0 on receiving acknowledgment, or negative number for errors. Set the operation mode of the mouse. PS/2 Mouse document

Prototype: Include: Parameters:

Returns: Description: See also:

Altera Corporation - University Program May 2011

You might also like