Intel 8042 keyboard controller

Source: Internet
Author: User

This article from http://shanzy.bokee.com/834368.html

Ps/2 Keyboard hardware Overview

For the driver, the most important hardware related to the keyboard is two chips. One is an intel 8042 chip located on the motherboard. The CPU communicates directly with the chip through the I/O port to obtain the scan code for the buttons or send various keyboard commands. The other is the intel 8048 chip or its compatible chip, which is located on the keyboard. The chip is mainly used to obtain the scan code generated by the pressed key from the keyboard hardware and communicate with i8042, control the keyboard itself.

When a key is pressed on the keyboard, i8048 directly obtains the scan code generated by the keyboard hardware. I8048 is also responsible for the control of the keyboard itself, such as lighting the LED indicator, off the LED indicator. I8048 communicates with i8042 through the ps/2 port and transmits the obtained scan code to i8042. Through the read/write port, the CPU can directly read data in i8042 into the CPU register, or write data in the CPU register
I8042. Ps/2 port has a total of 6 pins, You can unplug the ps/2 plug to see, these 6 pins are respectively, clock, Data, power source, power + 5 V, two other pins are not used. Since there is only one pin for data transmission, data transmission on the ps/2 port is serial.

The following figure shows the Key Martix used to generate the scan code. You can see the chip in the keyboard (not i8048, is a compatible chip of other models ).

See: http://jiurl.nease.net/document/KbdDriver/JiurlKbd1.htm for detailed diagram

I8042 keyboard controller

The keyboard driver directly reads and writes the i8042 chip, and indirectly sends a command to the i8048 In the keyboard through i8042. Therefore, for the driver, only i8042 is directly associated, so we only introduce i8042, not i8048.

A chip like i8042 and i8048 is itself a small processor. It has its own processor, its own ram, its own registers, and so on.

I8042 has four 8 bits registers: Status Register, output buffer, input buffer, and control register ). Use two I/O Ports, 60 h and 64 h.

Status Register)

The Status Register is an 8-bit read-only register that can be read by the CPU at any time. The definitions are as follows:

Bit7: parity-even (P_e): Data parity error obtained from the keyboard
Bit6: RCV-TMOUT (r_t): receive timeout, set 1
Bit5: trans_tmout (t_t): sending timeout, set to 1
Bit4: kybd_inh (K_ I): 1. the keyboard is not disabled. The value 0 indicates that the keyboard is disabled.
Bit3: 1__data (C_D): 1. The input buffer contains the command, 0, and data.
Bit2: sys_flag (s_f)
Bit1: input_buf_full (I _ B _f): input buffer full set 1, i8042 Remove post 0
Bito: out_buf_full (o_ B _f)

Output buffer)

The output buffer is an 8-bit read-only register. The driver reads data from this register. The data includes scan codes, responses sent to the i8042 command, and indirect responses sent to the i8048 command.

Input buffer)

The input buffer is an 8-bit write-only register. Buffer the content sent by the driver. These include commands sent to i8042, commands indirectly sent to i8048 through i8042, and data as command parameters.

Control Register)

Also known as Controller Command Byte (Controller Command Byte ). The definitions are as follows:

Bit7: reserved, it should be 0
Bit6: translate the second scan code into the first one.
Bit5: Set 1 to disable the mouse
Bit4: Set 1 to disable the keyboard.
Bit3: Set 1 to ignore Bit4 in the Status Register.
Bit2: Set Bit2 in the Status Register
Bit1: set to 1, enable mouse interruption
BitO: set to 1, enable keyboard interruption

2 ports 0x60, 0x64

Call 0x60 as the data port in the driver.
Call 0x64 as the command port in the driver.

1.5 command

The driver can send commands directly to i8042 and indirectly to i8048 through i8042. The command is directly from <reference [1]>.

1.5.1 commands sent to i8042

The driver sends commands to the keyboard controller through 64 h writing port. There are a total of 12 commands, respectively

20 h
Prepare to read the Command Byte of the 8042 chip. The behavior is to place the current 8042 Command Byte content in Output Register, and the next read operation from Port 60 will read it.

60 h
Command Byte for writing 8042 chips; the next Byte written through 60 h will be placed in Command Byte.

A4h
Test whether the keyboard password is set. The test result is placed in Output Register, which can be read by 60 h. There are two possible values in the test result: FAh = password set; F1h = no password.

A5h
Set the keyboard password. The results are placed in the Input Register one by one through the 60 h port in order. The password ends with an empty byte (the content is 0 ).

A6h
Make the password take effect. Before publishing this command, you must first use the A5h command to set the password.

AAh
Self-check. The diagnostic results are stored in Output Register and can be read through 60 h. 55 h = OK.

ADh
Disable the keyboard interface. Bit-4 of Command Byte is set. When this command is published, the Keyboard will be banned from sending data to the Output Register.

AEh
Open the keyboard interface. Command Byte bit-4 is cleared. After the command is published, the Keyboard is allowed to send data to the Output Register.

C0h
Prepare to read the Input Port. The content of the Input Port is placed in Output Register and can be read through Port 60 h.

D0h
Prepare to read The Outport. The result is stored in Output Register and then read through port 60 h.

D1h
Prepare to write the Output port. The bytes written through Port 60 h will be placed in the Output Port.

D2h
Prepare to write data to Output Register. After 60 h, the bytes written to the Input Register will be put into the Output Register. This function is used to simulate data sent from the Keyboard. If the interrupt is allowed, an interrupt is triggered.

1.5.2 commands sent to 8048

There are 10 commands in total, which are

EDh
Set the LED. When the Keyboard receives this command, an LED sets the session to start. The Keyboard first replies with an ACK (FAh), then waits for the LED setting byte written from Port 60 h. If it waits for one, It replies with an ACK again, and then sets the LED according to this byte. Then wait... Until a non-LED byte is set (a high byte is set), the session ends when the LED setting ends.

EEh
Diagnose Echo. This command is purely used to check whether the Keyboard is normal. If it is normal, when the Keyboard receives this command, it will return an EEh byte.

F0h
Select Scan code set. The Keyboard system may have three Scan code sets. When the Keyboard receives this command, it returns an ACK and waits for a Scan code set code from Port 60. The system must send a Scan code set code to the Keyboard after this command. When the Keyboard receives this code, it will reply an ACK again and then Scan the code
Set is set as required by the received Scan code set code.

F2h
Read the Keyboard ID. Because the 8042 chip can not only be connected to the Keyboard. This command is used to read the device ID after 8042. The device ID is 2 bytes and the Keyboard ID is 83ABh. When the Keyboard receives this command, it first returns an ACK and then returns the two-byte Keyboard ID one by one.

F3h
Set Typematic Rate/Delay. When the Keyboard receives this command, it returns an ACK. Then wait for the setting byte from 60 h. Once an ACK is received, an ACK is returned and the Keyboard Rate/Delay value is set as the corresponding value.

F4h
Clear the Output Buffer of the keyboard. Once the Keyboard receives this command, it clears the Output buffer and replies with an ACK. Then continue to accept the Keyboard.

F5h
Set the default status (w/Disable ). Once the Keyboard receives this command, the Keyboard is fully initialized to the default state. All previous settings will be invalid -- Output buffer is cleared, and Typematic Rate/Delay is set to the default value. Then, reply to an ACK and wait for the next command. It should be noted that after this command is executed, the keyboard's access to the keys is forbidden. Enable is required if you want the keyboard to accept the key input.
Keyboard.

F6h
Set the default status. The only difference between the F5 command and the command is that after the command is executed, the keyboard can receive the keys.

FEh
Resend. If the Keyboard receives this command, you must resend the data that was just sent to 8042 Output Register. After the system detects an error from the Keyboard, you can use the self-command to re-Send the sent bytes to the Keyboard.

FFh
Reset Keyboard. If the Keyboard receives this command, it first returns an ACK, starts its own Reset program, and performs its own Basic correctness Test (BAT-Basic Assurance Test ). After all these ends, a single-byte ending code (AAh = Success, FCh = Failed) is returned to the system, and the Scan code set of the keyboard is set to 2.

1.5.3 read data

00 h/FFh
When an error is detected when you press or release the key, put this Byte after the Output Bufer. If the Output Buffer is full, the last byte of the Output Buffer is replaced by this byte. Use 00 h for Scan code set 1 and FFh for Scan code 2 and Scan Code 3.

AAh
BAT completes the code. If the keyboard detection is successful, the byte is sent to 8042 Output Register.

EEh
Echo response. The Keyboard uses EEh to respond to the Echo request sent from 60 h.

F0h
Scan code set 2 and Scan code set 3 are used as the Break Code prefix.

FAh
ACK. When the Keyboard receives a valid command or data from Port 60 at any time, it returns a FAh.

FCh
BAT failure code. If the keyboard detection fails, the byte is sent to the 8042 Output Register.

FEh
Resend. When the Keyboard receives an invalid command or data from Port 60 at any time, or the data parity error, it returns a FEh and requires the system to resend the command or data.

83ABh
After receiving an F2h command from 60 h, the keyboard replies 83 h AND ABh in sequence. 83AB is the ID of the keyboard.

Scan code
Except for the special bytes above, the rest are Scan code.

Port 1.6

First, we will introduce the read and write operations on the port. The READ_PORT_UCHAR function is used in the driver for read operations, and the READ_PORT_UCHAR function uses the CPU read PORT command, in. The WRITE_PORT_UCHAR function is used in the driver for write operations, and the WRITE_PORT_UCHAR function uses the CPU to write port commands, out.

1.6.1 read Status Register

The method of reading status registers to read 64 h ports.

1.6.2 read data

The data to be read includes the scan code of the i8042 key obtained from i8048, The ACK of the i8042 command, and the ack of the i8048 command obtained from i8048. the RESEND command must be resending, the result of some commands that need to return results.

When data needs to be read by the driver, the data is put into the output buffer, and the bit0 (OUTPUT_BUFFER_FULL) of the Status Register is set to 1, causes a keyboard interruption (the IRQ of the keyboard interruption is 1 ). The keyboard interrupt service routine provided by the keyboard driver is executed due to keyboard interruption. In the keyboard interrupt service routine, the driver reads data from i8042. Once the data is read, bit0 of the Status Register is cleared.

To read data, first read the Status Register and Judge bit0. The Status Register bit0 is 1, indicating that there is data in the output buffer. Ensure that the Status Register bit0 is 1, and then perform read operations on port 60 h to read data.

Here is a useful topic. The IRQ mentioned above is Interrupt Request line, which is a hardware line and is different from the Interrupt vector. The interrupt vector is the sequence number used to find the interrupt service routine in the Interrupt Descriptor Table (IDT. The IRQ of the keyboard is 1, and the interrupt vector corresponding to the keyboard interrupt service routine is not 1. This should be clarified.

1.6.3 send a command to i8042

When the command is sent to i8042, the command is put into the input buffer, and the Bit1 of the Status Register is set to 1, indicating that the input buffer is full, and the Bit2 of the Status Register is set to 1, indicates that a command is written into the input buffer.

To send a command to i8042, read the Status Register and Judge bit1. The Status Register bit1 is 0, which indicates that the input buffer is empty and can be written. Ensure that the Status Register bit1 is 0, write the 64 h port, and write the command.

1.6.4 send commands indirectly to i8048

Send these commands to i8042, i8042 will forward i8048, the command is put into the input buffer, and the Bit1 of the Status Register is set to 1, indicating that the input buffer is full, and the Bit2 of the Status Register is set to 1, indicates that a command is written into the input buffer. Here, we should note that the command to i8048 is to write the 60 h port, while the parameter to be followed by the command is also to write the 60 h port. How can I determine whether the content in the input buffer is a command or a parameter? We will discuss it in the parameters of the subsequent commands.

To send a command to i8048, read the Status Register and Judge bit1. The Status Register bit1 is 0, indicating that the input buffer is empty and can be written. Ensure that the Status Register bit1 is 0, and then write the 60 h port to write the command.

1.6.5 command sending Parameters

Some commands require parameters. After sending the command, we send its parameters. The parameters are placed in the input buffer, and Bit1 of the Status Register is set to 1, indicating that the input buffer is full. Here, we should note that the command to i8048 is to write the 60 h port, the command parameter, and the 60 h port. How can I determine whether the content in the input buffer is a command or a parameter. I8042 is determined in this way. If the Bit3 of the Current Status Register is 1, it indicates that a command has been written before, then the 60 h port is written into the content in the input buffer, it is treated as a parameter of the previous command and causes the Status Register
Bit3 is set to 0. If Bit3 in the Current Status Register is 0, it indicates that no commands have been written before. Now, by writing the 60 h port to the content in the input buffer, it is used as a command that is indirectly sent to i8048, and the Bit3 of the Status Register is set to 1.

To send parameters to i8048, read the Status Register and Judge bit1. The Status Register bit1 is 0, indicating that the input buffer is empty and can be written. Ensure that the Status Register bit1 is 0, and then perform write operations on port 60 h to write parameters.

For more information about the PS/2 Keyboard hardware, see the following documents.

[1] http://pagoda-ooos.51.net/ OS _book/driver/driver-keyboard_2.htm (Chinese)
[2] http://panda.cs.ndsu.nodak.edu /~ Achapwes/picmicro/ps2.pdf (Chinese)
[3] http://panda.cs.ndsu.nodak.edu /~ Achapwes/picmicro/(English)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.