altera ps2.pdf

9
 PS/2 Core for Altera DE2/DE1 Boards Preliminary 1 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. 2 Functi ona l Descripti on The PS/2 Core handles the timing of the PS/2 Serial Data Tr ansmis sion Proto col. A device driver can communicate with it by reading/writing from/to its data and control registers. 3 Instantiating the Core in SOPC Build er Design ers can implement a PS/2 Core by using the SOPC Builder . There is no need to cong ure the core. The core comes with a 256-word FIFO for storing data received from a PS/2 device. 4 Sof twa re Pr ogr amming Mod el 4.1 Reg ister Map Device driver s control and communicat e with the PS/2 Core through two 32-bit register s. Com- munication wit h the PS/2 perip heral is done by wri ti ng or re ading the registers through the Avalon Slave Port. T able 1 shows the details for the registers. T abl e 1. PS/ 2 Cor e Reg ist er Map Of fs et Re gi ster R/W/C  Bit Desc ription in b ytes Name 31...16 15. . . 11 10 9 8 7...1 0 0   R/W RA VAIL  (1)  DATA 4   R/C  (1)  CE  (1)  RI  (1)  RE Notes on Table 1: (1) Reserved. Read values are undened. Write zero.  Altera Corporation - University Program October 2006 1

Upload: itha-masyhta

Post on 14-Apr-2018

247 views

Category:

Documents


0 download

TRANSCRIPT

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 1/9

 

PS/2 Core for Altera

DE2/DE1 Boards

Preliminary 

1 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.

2 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/writing from/to its data and control registers.

3 Instantiating the Core in SOPC Builder

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

4 Software Programming Model

4.1 Register Map

Device drivers control and communicate with the PS/2 Core through two 32-bit registers. Com-

munication 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 RegisterR/W/C

Bit Description

in bytes Name 31. . . 16 15. . . 11 10 9 8 7. . . 1 0

0d a t a  

R/W RAVAIL (1)  DATA 

4 c o n t r o l   R/C (1)  CE (1)  RI (1)  RE

Notes on Table 1: 

(1) Reserved. Read values are undefined. Write zero.

 Altera Corporation - University Program

October 2006

1

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 2/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

4.1.1 data Register

Table 2. data Register Bits Bit Number Bit Name Read/Write/Clear Description

7. . . 0 DATA R/W The value to transfer to/from the PS/2 core. When

 writing, the DATA field is interpreted as a command

to be sent to the PS/2 device. When reading, the DATA 

field is data from the PS/2 device.

31. . . 16 RAVAIL R The number of data items remaining in t he read F IFO

(including this read).

4.1.2 control  Register

Table 3. control Register Bits 

Bit Number Bit Name Read/Write/Clear Description

0 RE R/W Interrupt-enable bit for read interrupts.

8 RI R Indicates that a read interrupt is pending.

10 CE C 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 SOPC Builder-

generated software development kit (SDK) libraries, as listed below. These functions implementcommon operations that users need for the PS/2 Core. When using the Altera Debug Client, these

functions are automatically provided for use in a C-language application program. They are pre-

sented in Secition 4.3. To use the functions, the C code must include the statement:

# i n c l u d e " a l t _ u p _ p s 2 _ p o r t . h "  

In addition, some sample functions for specific communication with the keyboard or mouse are

also provided. They may 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 files,p s 2 _ k e y b o a r d . h  

andp s 2 _ m o u s e . h  

, have to be included. These functions are de-

scribed in Sections 4.4 and 4.5.

4.3 PS/2 Port Functions

4.3.1 enum PS2 DEVICE

The Enum type for PS/2 device type.

Enumerator:

PS2_MOUSE 

PS2_KEYBOARD 

PS2_UNKNOWN 

2 Altera Corporation - University Program

October 2006

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 3/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

4.3.2 alt u32 read ctrl reg ()

Read the contents of the Control register for the PS/2 port.

Returns:

Register contents (32 bits, bits 10, 8 and 0 are used for CE, RI and RE respectively. Other bits

are reserved)

4.3.3 void write ctrl reg (alt u32 ctrl data )

Set the contents of the Control register.

Parameters:

ctrl_data  – contents to be written into the Control register

4.3.4 alt u8 read RI bit (alt u32 ctrl reg )

Extract the RI (Read Interrupt) bit from the Control register.

Parameters:

ctrl_reg  – the Control register

Returns:

8-bit number, where bit 0 is the value of the RI bit

4.3.5 alt u8 read RE bit (alt u32 ctrl reg )

Extract the RE (Read Interrupt Enable) bit from the Control register.

Parameters:

ctrl_reg  – the Control register

Returns:

8-bit number, where bit 0 is the value of the RE bit

4.3.6 alt u8 read CE bit (alt u32 ctrl reg )

Extract the CE (Command Error) bit from the Control register.

Parameters:

ctrl_reg  – the Control register

Returns:

8-bit number, where bit 0 is the value of the CE bit

 Altera Corporation - University Program

October 2006

3

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 4/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

4.3.7 alt u32 read data reg ()

Read the contents of the Data register.

Returns:

32 bits of the Data register. Bits 31-16 indicate the number of available bytes in the FIFO (RA-

 VAIL), bits 7-0 are the data received from the PS/2 device

4.3.8 alt u8 read data byte (alt u32 data reg )

Read the DATA byte from the Data register.

Parameters:

data_reg  – Data register

Returns:

Bits 7-0 of the Data register

4.3.9 alt u16 read num bytes available (alt u32 data reg )

Find the number of bytes available to read in the FIFO buffer of the PS/2 port.

Parameters:

data_reg  – the Data register

Returns:

The number represented by bits 31-16 of the Data register

4.3.10 PS2 DEVICE get mode ()

Check the PS/2 peripheral’s mode (whether it is a keyboard or a mouse).

Returns:

PS2_MOUSE for mouse, or PS2_KEYBOARD for keyboard

Note:

This operation will reset the PS/2 peripheral. Usually, it should be used only at the beginning 

of a program.

4.3.11 void clear FIFO ()

Clear the FIFO’s contents.

4 Altera Corporation - University Program

October 2006

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 5/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

4.3.12 int wait for ack (unsigned timeout )

 Wait for the acknowledge byte (0xFA) from the PS/2 peripheral.

Parameters:

timeout  – the number of cycles of timeout

Returns:

P S 2 _ S U C C E S S   on receving ACK signal, or P S 2 _ T I M E O U T   on timeout.

4.3.13 int write data byte (alt u8 byte )

Send a one-byte command to the PS/2 peripheral.

Parameters:

byte  – the one-byte command to be sent

Returns:

P S 2 _ E R R O R   if the CE bit of the Control register is set to 1, otherwise P S 2 _ S U C C E S S  

4.3.14 int write data byte with ack (alt u8 byte , unsigned timeout )

Send a one-byte command to the PS/2 peripheral and wait for the ACK signal.

Parameters:

byte  – the one-byte command to be sent. Seea l t _ u p _ p s 2 _ p o r t _ r e g s . h  

in the sdk directory 

or any reference for the PS/2 protocol for details.

Returns:

P S 2 _ E R R O R  if the CE bit of the Control register is set to 1, or

P S 2 _ T I M E O U T  on timeout, or

P S 2 _ -  

S U C C E S S  if the ACK signal is received before timeout

4.3.15 int read data byte with timeout (alt u8∗

byte , alt u32 time out )

Read the DATA byte from the PS/2 FIFO, using a user-defined timeout value.

Parameters:

byte  – the byte read from the FIFO for the PS/2 Core

time_out  – the user-defined timeout value. Setting  time_out  to 0 will disable the time-out

mechanism

Returns:

P S 2 _ S U C C E S S  on reading data, or

P S 2 _ T I M E O U T  on timeout

 Altera Corporation - University Program

October 2006

5

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 6/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

4.4 PS/2 Keyboard Functions

4.4.1 enum KB CODE TYPE

The Enum type for the type of keyboard code received.

Enumerator:

KB_ASCII_MAKE_CODE  — Make Code that corresponds to an ASCII character. For example,

the ASCII Make Code for letter A  is 1C

KB_BINARY_MAKE_CODE  — Make Code that corresponds to a non-ASCII character. For

example, the Binary (Non-ASCII) Make Code forL e f t A l t  

is 11

KB_LONG_BINARY_MAKE_CODE  — Make Code that has two bytes (the first byte is E0). For

example, the Long Binary Make Code forR i g h t A l t  

is "E0 11"

KB_BREAK_CODE  — Normal Break Code that has two bytes (the first byte is F0). For exam-

ple, the Break Code for letter A  is "F0 1C"

KB_LONG_BREAK_CODE  — Long Break Code that has three bytes (the first two bytes are E0,

F0). For example, the Long Break Code forR i g h t A l t  

is "E0 F0 11"

KB_INVALID_CODE  — Codes that the decode FSM cannot decode

4.4.2 int read make code (KB CODE TYPE ∗ decode mode , alt u8 ∗ buf )

Get the make code of the key when a key is pressed.

Parameters:

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 code of the key pressed

Note:

For K B _ L O N G _ B I N A R Y _ M A K E _ C O D E   and K B _ B R E A K _ C O D E  , only the second byte is retured. For

K B _ L O N G _ B R E A K _ C O D E  , only the third byte is returned

Returns:

P S 2 _ T I M E O U T   on timeout, or P S 2 _ E R R O R   on error, otherwise P S 2 _ S U C C E S S  

4.4.3 alt u32 set keyboard rate (alt u8 rate )

Set the repeat/delay rate of the keyboard.

Parameters:

rate  – an 8-bit number that represents the repeat/delay rate of the keyboard

Returns:

PS2_SUCCESS on success, otherwise PS2_ERROR

6 Altera Corporation - University Program

October 2006

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 7/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

4.4.4 alt u32 reset keyboard ()

Send the reset command to the keyboard.

Returns:

P S 2 _ S U C C E S S   on passing the BAT (Basic Assurance Test), otherwise P S 2 _ E R R O R  

4.5 PS/2 Mouse Functions

4.5.1 alt u8 reset mouse ()

Reset the mouse.

Returns:

P S 2 _ S U C C E S S  on BAT is passed, otherwise

P S 2 _ E R R O R  

4.5.2 int set mouse mode (alt u8 byte )

Set the operation mode of the mouse.

Parameters:

byte  – the byte representing the mode (see macro definitions for details)

See also:

PS/2 Mouse document

Returns:

P S 2 _ S U C C E S S  on receiving acknowledgment

 Altera Corporation - University Program

October 2006

7

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 8/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

4.6 Sample Program

Below is a sample program that shows some usage of the provided functions.

/ * * 

* A s i m p l e p r o g r a m t h a t i l l u s t r a t e s t h e u s a g e o f s o m e s d k f u n c t i o n s  

o f t h e  

* P S / 2 P o r t S D K  

* * / 

# i n c l u d e < a l t _ t y p e s . h >  

# i n c l u d e < s t d i o . h >  

# i n c l u d e " a l t _ u p _ p s 2 _ p o r t . h "  

# i n c l u d e " p s 2 _ k e y b o a r d . h "  

# i n c l u d e " p s 2 _ m o u s e . h "  

i n t m a i n ( )  

/ / c l e a r t h e F I F O f o r t h e P S / 2 p o r t  

c l e a r _ F I F O ( ) ;  

D E C O D E _ M O D E d e c o d e _ m o d e ;  

a l t _ u 8 b y t e ;  

/ / g e t w h e t h e r t h e P S / 2 d e v i c e i s a k e y b o a r d o r a m o u s e  

P S 2 _ D E V I C E m o d e = g e t _ m o d e ( ) ;  

i f ( m o d e = = P S 2 _ K E Y B O A R D )  

p r i n t f ( " % s " , " K E Y B O A R D . . . \ n " ) ;  

e l s e i f ( m o d e = = P S 2 _ M O U S E )  

p r i n t f ( " % s " , " M O U S E . . . \ n " ) ;  

i f ( m o d e = = P S 2 _ K E Y B O A R D )  

a l t _ u 8 k e y = 0 ;  

i n t s t a t u s = 0 ;  

d o { 

/ / w a i t f o r t h e u s e r ' s i n p u t a n d g e t t h e m a k e c o d e  

s t a t u s = g e t _ m a k e _ c o d e ( & d e c o d e _ m o d e , & k e y ) ;  

i f ( s t a t u s = = P S 2 _ S U C C E S S )  

/ / p r i n t o u t t h e r e s u l t  

s w i t c h ( d e c o d e _ m o d e )  

c a s e K B _ A S C I I _ M A K E _ C O D E :  

p r i n t f ( " A S C I I : \ t % c \ n " , k e y ) ;  

b r e a k ;  

c a s e K B _ L O N G _ B I N A R Y _ M A K E _ C O D E :  

p r i n t f ( " % s " , " L O N G " ) ;  

/ / f a l l t h r o u g h  

c a s e K B _ B I N A R Y _ M A K E _ C O D E :  

8 Altera Corporation - University Program

October 2006

7/27/2019 Altera PS2.pdf

http://slidepdf.com/reader/full/altera-ps2pdf 9/9

PS/2 CORE FOR A LTERA DE2/DE1 BOARDS Preliminary 

p r i n t f ( " M A K E C O D E : \ t % X \ n " , k e y ) ;  

b r e a k ;  

c a s e K B _ B R E A K _ C O D E :  

/ / d o n o t h i n g  

d e f a u l t :  

b r e a k ;  

e l s e  

p r i n t f ( " K e y b o a r d e r r o r . . . . \ n " ) ;  

} w h i l e ( 1 ) ;  

e l s e i f ( m o d e = = P S 2 _ M O U S E )  

i f ( r e s e t _ m o u s e ( ) = = P S 2 _ S U C C E S S )  

p r i n t f ( " M O U S E R E S E T T E D . . . \ n " ) ;  

i f ( s e t _ m o u s e _ m o d e ( M O U S E _ S T R E A M _ M O D E ) = = P S 2 _ S U C C E S S )  

p r i n t f ( " S e t M o u s e t o S t r e a m m o d e . . . \ n " ) ;  

r e t u r n 0 ;  

 When compiling the C program in the Altera Debug Client, you may wish to use the - m s m a l l c  

option so that the Small newlib C Library  is used to reduce the program size (See The HAL System

Library in the Nios ®  II Software Developer’s Handbook for details).

 Altera Corporation - University Program

October 2006

9