PS/2 技术参考

 PS/2 技术参考 著Adam Chapweske 译Roy Show
第1 页共1 页 02-11-24发布
第一章 PS/2 鼠标键盘协议
Introduction:
引言
The PS/2 device interface, used by many modern mice and keyboards, was developed by IBM and originally
appeared in the IBM Technical Reference Manual. However, this document has not been printed for many
years and as far as I know, there is currently no official publication of this information. I have not had access
to the IBM Technical Reference Manual, so all information on this page comes from my own experiences as
well as help from the references listed at the bottom of this page.
PS/2 设备接口用于许多现代的鼠标和键盘它是由IBM 开发并且最初出现在IBM 技术参考手册里但
是当我知道的时候这篇文件就已经很多年没有印刷了因此关于这个内容现在没有官方的出版物我
无法访问IBM 的技术参考手册所以本网页中的所有信息都来自于我自己的经验及本页最下面列出的
参考的帮助
译者注这些参考的条目在本章的结尾处
This document descibes the interface used by the PS/2 mouse, PS/2 keyboard, and AT keyboard. I'll cover
the physical and electrical interface, as well as the protocol. If you need higher-level information, such as
commands, data packet formats, or other information specific to the keyboard or mouse, I have written
separate documents for the two devices:
这个文件描述了用于PS/2 鼠标PS/2 键盘及AT 键盘的接口我将论及物理和电气接口也包括协议
如果你需要更高级的信息诸如命令数据包的格式或者其他关于键盘鼠标的特别细节那么我对这两
种设备写了独立的文件
The PS/2 (AT) Keyboard Interface
The PS/2 Mouse Interface
译者注这两篇文章已经包含到这篇译文中来了是第二章和第三章
I also encourage you to check out my homepage for more information related to this topic, including projects,
code, and links related to the mouse and keyboard.
我同样鼓励你在我的主页上校对更多与这个话题相关的信息包括工程代码和与鼠标键盘有关的链接
The Connector:
连接器
The physical PS/2 port is one of two styles of connectors: The 5-pin DIN or the 6-pin mini-DIN. Both
connectors are completely (electrically) similar; the only practical difference between the two is the
arrangement of pins. This means the two types of connectors can easily be changed with simple hard-wired
adaptors. These cost about $6 each or you can make your own by matching the pins on any two connectors.
The DIN standard was created by the German Standardization Organization (Deutsches Institut fuer Norm) .
Their website is at http://www.din.de/ (this site is in German, but most of their pages are also available in
English.)
物理上的PS/2 端口是两类连接器中的一种5 脚的DIN 或6 脚的mini-DIN 这两种连接器在电气特
性上是十分类似的实际上两者只有一点不同那就是管脚的排列这就意味着这两类连接器可以很容
易用一种简单的硬件连线的适配器来转换这种适配器大约每个值6 美元或者你可以根据任意两种连
接器的对应管脚关系做你自己的适配器DIN 标准是由德国标准化组织(Deutsches Institut fuer Norm)建
立的他们的网站在 http://www.din.de/ 这个站点是德文的但他们的很多网页同样可用于英文
PS/2 技术参考 著Adam Chapweske 译Roy Show
第2 页共2 页 02-11-24发布
PC keyboards can have either a 6-pin mini-DIN or a 5-pin DIN connector. If your keyboard has a 6-pin
mini-DIN and your computer has a 5-pin DIN (or visa versa), the two can be made compatible with the
adaptors described above. Keyboards with the 6-pin mini-DIN are often referred to as "PS/2" keyboards,
while those with the 5-pin DIN are called "AT" devices ("XT" keyboards also used the 5-pin DIN, but they are
quite old and haven't been made for many years.) All modern keyboards built for the PC are either PS/2, AT,
or USB. This document does not apply to USB devices, which use a completely different interface.
PC 键盘可以有6 脚的mini-DIN 或5 脚的DIN 连接器如果你的键盘是6 脚的mini-DIN 而你的计算机
是5 脚的DIN 或者相反这两类连接器可以用上面提到的适配器来兼容具有6 脚mini-DIN 的键盘
通常被叫做PS/2 键盘而那些有5 脚DIN 叫做AT 设备XT 键盘也使用5 脚DIN 但它们
非常古老并且多年前就不生产了所有现代的为PC 建造的键盘不是PS/2,AT 就是USB 的这篇文章
不适用于USB 设备它们使用了一种完全不同的接口
Mice come in a number of shapes and sizes (and interfaces.) The most popular type is probably the PS/2
mouse, with USB mice gaining popularity. Serial mice are also quite popular, but the computer industry is
abandoning them in support of USB and PS/2 devices. This document applies only to PS/2 mice. If you
want to interface a serial mouse, check out Microchip's appnote #519, "Implementing a Simple Serial Mouse
Controller."
鼠标流行着大量的形状和大小和接口最流行的类型可能算是PS/2 鼠标现在USB 鼠标渐渐开始
流行起来了串行鼠标同样非常流行但计算机工业放弃了它们转而支持USB 和PS/2 设备这篇文章
仅适用于PS/2 鼠标如果你要知道一个串行鼠标的接口请查验Microchip 的519 号应用实现一个
简单的串行鼠标控制器
As a side note, there is one other type of connector you may run into on keyboards. While most keyboard
cables are hard-wired to the keyboard, there are some whose cable is not permanently attached and come as a
separate component. These cables have a DIN connector on one end (the end that connects to the computer)
and a SDL (Sheilded Data Link) connector on the keyboard end. SDL was created by a company called
"AMP." This connector is somewhat similar to a telephone connector in that it has wires and springs rather
than pins, and a clip holds it in place. If you need more information on this connector, you might be able to
find it on AMP's website at http://www.connect.amp.com/. I have only seen this type of connector on (old)
XT keyboards, although there may be AT keyboards that also use the SDL. Don't confuse the SDL connector
with the USB connector--they probably both look similar in my diagram below, but they are actually very
different. Keep in mind that the SDL connector has springs and moving parts, while the USB connector does
not.
作为边注还有另外一种类型的连接器你可以在键盘上碰到虽然多数键盘电缆都是固定连接到键盘的
但还有一些键盘电缆不是永久挂接的而是作为一个单独的部件这种电缆在一端有一个DIN 的连接
器这端连接到计算机而连接到键盘的那一端是一个SDL 屏蔽的数据连接器连接器SDL 是由
一个叫AMP 的公司建立的这种连接器有点象电话连接器它有金属丝和弹簧代替管脚用一个卡子
保持它在适当的位置上如果你需要知道关于这种连接器的更多信息你可以在AMP 的网站找到它
AMP 的网站在 http://www.connect.amp.com/ 尽管可能AT 键盘同样可以使用SDL 但我只在一个很老
的XT 键盘上看见过这种连接器不要把SDL 连接器和USB 连接器混淆起来这两个可能在我下面画
的示意图中看起来有点类似但是它们实际上是非常不同的记住SDL 有弹簧和活动部件而USB 连
接器没有
The pinouts for each connector are shown below:
每种连接器的引脚定义如下所示
PS/2 技术参考 著Adam Chapweske 译Roy Show
第3 页共3 页 02-11-24发布
Male 公的 Female 母的 5-pin DIN (AT/XT): 5 脚DIN(AT/XT)
1 - Clock 1 时钟
2 - Data 2 数据
3 - Not Implemented 3 未实现保留
4 - Ground 4 电源地
(Plug) 插头 (Socket) 插座 5 - +5v 5 电源+5V
Male 公的 Female 母的 6-pin Mini-DIN (PS/2): 6 脚Mini-DIN(PS/2)
1 - Data 1 数据
2 - Not Implemented 2 未实现保留
3 - Ground 3 电源地
4 - +5v 4 电源+5V
(Plug) 插头 (Socket) 插座 5 - Clock 5 时钟
6 - Not Implemented 6 未实现保留
6-pin SDL: 6 脚SDL
A - Not Implemented A 未实现保留
B - Data B 数据
C - Ground C 电源地
D - Clock D 时钟
E - +5v E 电源+5V
F - Not Implemented F 未实现保留
General Description:
一般性描述
(Note: Throughout this document, I may use the more general term "host" to refer to the computer--or
whatever the keyboard/mouse is connected to-- and the term "device" will refer to the keyboard/mouse.)
注意遍及这篇文章我使用了更普通的术语host 使之计算机或者键盘/鼠标连接到的单元而术
语device 是指键盘/鼠标
译者注在下面的译文中host 可能被翻译成主机
There are four interesting pins on the connectors just described: Ground, +5v, Data, and Clock. The +5V is
supplied by the host (computer) and the keyboard/mouse's ground is connected to the host's electrical ground.
Data and Clock are both open collector, which means they are normally held at a high logic level but can easily
be pulled down to ground (logic 0.) Any device you connect to a PS/2 mouse, keyboard, or host should have
large pull-up resistors on the Clock and Data lines. You apply a "0" by pulling the line low and you apply a
"1" by letting the line float high. Refer to Figure 1 for a general interface to Data and Clock. (Note: if you
are going to use a microcontroller such as the PIC, where I/O is bidirectional, you may skip the transistors and
buffers and use the same pin for both input and output. With this configuration, a "1" is asserted by setting
the pin to input and let the resistor pull the line high. A "0" is then asserted by changing the pin to output and
write a "0" to that pin, which will pull the line to ground.)
在刚才提到连接器上有四个有趣的管脚电源地5V 数据和时钟host 计算机提供5V 并
且键盘/鼠标的地连接到host 的电源地上数据和时钟都是集电极开路的这就意味着它们通常保持高
电平而且很容易下拉到地逻辑0 任何你连接到PS/2 鼠标键盘或host 的设备在时钟和数据线上要
PS/2 技术参考 著Adam Chapweske 译Roy Show
第4 页共4 页 02-11-24发布
有一个大的上拉电阻置0 就把线拉低置1 就让线上浮成高电平参考图1 中数据和时钟线的
一般接口结构注意如果你打算使用象PIC 这样的微控制器由于它们的I/O 管脚是双向的你可
以跳过晶体管和缓冲门并且通用同一个管脚进行输入和输出在这种组态情况下要设置管脚为输入
就写入1 使得电阻上拉线上的电平要改变管脚为输出就写入0 到那个管脚把线路下拉到地
Figure 1: Open-collector interface to Data
and Clock. Data and Clock are read on the
microcontroller's port A and B, respectively.
Both lines are normally held at +5V, but can
be pulled to ground by asserting logic 1 on
C and D. As a result, Data equals D,
inverted, and Clock equals C, inverted.
图1 数据线和时钟线的集电极开路接口
数据和时钟分别由微控制器的A 端口和B
端口读入这两条线通常保持+5V 但可
以往端口C 和D 写入1 来拉到地结果是
数据是D 的反相时钟是C 的反相
The PS/2 mouse and keyboard implement a bidirectional synchronous serial protocol. In other words, Data is
sent one bit at a time on the Data line and is read on each time Clock is pulsed. The keyboard/mouse can
send data to the host and the host can send data to the device, but the host always has priority over the bus and
can inhibit communication from the keyboard/mouse at any time by holding Clock low.
PS/2 鼠标和键盘履行一种双向同步串行协议换句话说每次数据线上发送一位数据并且每在时钟线
上发一个脉冲就被读入键盘/鼠标可以发送数据到主机而主机也可以发送数据到设备但主机总是
在总线上有优先权它可以在任何时候抑制来自于键盘/鼠标的通讯只要把时钟拉低即可
Data sent from the keyboard/mouse to the host is read on the falling edge of the clock signal (when Clock goes
from high to low); data sent from the host to the keyboard/mouse is read on the rising edge (when Clock goes
from low to high.) Regardless of the direction of communication, the keyboard/mouse always generates the
clock signal. If the host wants to send data, it must first tell the device to start generating a clock signal (that
process is described in the next section.) The maximum clock frequency is 33 kHz and most devices operate
within 10-20kHz. If you want to build a PS/2 device, I would recommend keeping this frequency around 15
kHz. This means Clock should be high for about 40 microseconds and low for 40 microseconds.
从键盘/鼠标发送到主机的数据在时钟信号的下降沿当时钟从高变到低的时候被读取从主机发送
到键盘/鼠标的数据在上升沿当时钟从低变到高的时候被读取不管通讯的方向怎样键盘/鼠标总
是产生时钟信号如果主机要发送数据它必须首先告诉设备开始产生时钟信号这个过程在下一章节
中被描述最大的时钟频率是33kHz 而且大多数设备工作在10 20kHz 如果你要制作一个PS/2 设
备我推荐你把频率控制在15kHz 左右这就意味着时钟应该是高40 微秒低40 微秒
All data is arranged in bytes with each byte sent in a frame consisting of 11-12 bits. These bits are:
所有数据安排在字节中每个字节为一帧包含了11 12 个位这些位的含义如下
1 start bit. This is always 0. 1 个起始位总是为0
8 data bits, least significant bit first. 8 个数据位低位在前
PS/2 技术参考 著Adam Chapweske 译Roy Show
第5 页共5 页 02-11-24发布
1 parity bit (odd parity). 1 个校验位奇校验
1 stop bit. This is always 1. 1 个停止位总是为1
1 acknowledge bit (Host-to-device communication only) 1 个应答位仅在主机对设备的通讯中
The parity bit is set if there is an even number of 1's in the data bits and reset (0) if there is an odd number of
1's in the data bits. The number of 1's in the data bits plus the parity bit always add up to an odd number (odd
parity.) This is used for error detection.
如果数据位中包含偶数个1 校验位就会置1 如果数据位中包含奇数个1 校验位就会置0 数据位中
1 的个数加上校验位总是为奇数这就是奇校验这是用来进行错误检测的
When the host is sending data to the keyboard/mouse, a handshaking bit is sent from the device to
acknowledge the packet was received. This bit is not present when the device sends data to the host.
当主机发送数据给键盘/鼠标时设备回送一个握手信号来应答数据包已经收到这个位不会出现在设
备发送数据到主机的过程中
Device-to-Host Communication:
设备到主机的通讯过程
The Data and Clock lines are both open collector (normally held at a high logic level.) When the keyboard or
mouse wants to send information, it first checks Clock to make sure it's at a high logic level. If it's not, the
host is inhibiting communication and the device must buffer any to-be-sent data until it regains control of the
bus (the keyboard has a 16-byte buffer and the mouse's buffer stores only the last packet sent.) If the Clock
line is high, the device can begin to transmit its data.
数据和时钟线都是集电极开路结构正常保持高电平当键盘或鼠标等待发送数据时它首先检查时
钟以确认它是否是高电平如果不是那么是主机抑制了通讯设备必须缓冲任何要发送的数据直到重
新获得总线的控制权键盘有16 字节的缓冲区而鼠标的缓冲区仅存储最后一个要发送的数据包如
果时钟线是高电平设备就可以开始传送数据
As I mentioned in the previous section, the keyboard and mouse use a serial protocol consisting of 11-bit
frames. These bits are:
如我在上一节提及的键盘和鼠标使用一种每帧包含11 位的串行协议这些位含义是
1 start bit. This is always 0. 1 个起始位总是为0
8 data bits, least significant bit first. 8 个数据位低位在前
1 parity bit (odd parity). 1 个校验位奇校验
1 stop bit. This is always 1. 1 个停止位总是为1
Each bit is read by the host on the falling edge of the clock, as is illustrated in Figures 2 & 3.
每位在时钟的下降沿被主机读入如图2 和3 所示
Figure 2: Device-to-host
communication. The Data
line changes state when Clock
is high and that data is latched
on the falling edge of the clock
signal.
PS/2 技术参考 著Adam Chapweske 译Roy Show
第6 页共6 页 02-11-24发布
图2 设备到主机的通讯当时钟为高数据线改变状态在时钟信号的下降沿数据被锁存
Figure 3: Scan code for the "Q"
key (15h) being sent from a
keyboard to the computer.
Channel A is the Clock signal;
channel B is the Data signal.
图3 Q 键的扫描码从键盘发送
到计算机通道A 是时钟信号
通道B 是数据信号
The clock frequency is 10-16.7kHz.
The time from the rising edge of a
clock pulse to a Data transition
should be at least 5 microseconds. The time from a data transition to the falling edge of a clock pulse should
be at least 5 microseconds and no greater than 25 microseconds. This timing is very important--you should
follow it exactly. The host may pull the line low before the 11th clock pulse (stop bit), causing the device to
abort sending the current byte (this is very rare.) After the stop bit is transmitted, the device should wait at
least 50 microseconds before sending the next packet. This gives the host time to inhibit transmission while
it processes the received byte (the host will usually automatically do this after each packet is received.) The
device should wait at least 50 microseconds after the host releases an inhibit before sending any data.
时钟频率为10 16.7kHz 从时钟脉冲的上升沿到一个数据转变的时间至少要有5 微秒数据变化到时
钟脉冲的下降沿的时间至少要有5 微秒并且不大于25 微秒这个定时非常重要你应该严格遵循它
主机可以在第11 个时钟脉冲停止位之前把线拉低导致设备放弃发送当前字节这是非常罕见的
在停止位发送后设备在发送下个包前至少应该等待50 毫秒这将给主机时间当它处理接收到的字节
时抑制发送主机在收到每个包时通常自动做这个在主机释放抑制后设备至少应该在发送任何
数据前等50 毫秒
I would recommend the following process for sending a single byte from an emulated keyboard/mouse to the
host:
我推荐下面的过程发送一个单一字节从仿真键盘/鼠标到主机
1) Wait for Clock = high.
2) Delay 50 microseconds.
3) Clock still = high?
No--goto step 1
4) Data = high?
No--Abort (and read byte from host)
5) Delay 20 microseconds (=40 microseconds to the time Clock is pulled low in sending the start bit.)
6) Output Start bit (0) / After sending each of these bits, test
7) Output 8 data bits > Clock to make sure host hasn't pulled it
8) Output Parity bit / low (which would abort this transmission.)
9) Output Stop bit (1)
10) Delay 30 microseconds (=50 microseconds from the time Clock is released in sending the stop bit)
1) 等待Clock = high
2) 延时 50 微秒
PS/2 技术参考 著Adam Chapweske 译Roy Show
第7 页共7 页 02-11-24发布
3) Clock s 仍旧为 high?
No—到第1 步
4) Data = high?
No—放弃 (并且从主机读取字节)
5) 延迟 20 毫秒 (=40 微秒 to the time Clock is pulled low in sending the start bit.)
6) 输出起始位 (0) / 在发送所有这些位的每一位后
7) 输出 8 个数据位 > 测试时钟确认主机是否把它拉低了
8) 输出校验位 / 这说明主机要放弃这次传送
9) 输出停止位 (1)
10) 延迟30 毫秒 (=50 微秒 from the time Clock is released in sending the stop bit)
The process for sending a single bit should then be as follows:
按如下的过程发送单个位
1) Set/Reset Data
2) Delay 20 microseconds
3) Bring Clock low
4) Delay 40 microseconds
5) Release Clock
6) Delay 20 microseconds
1) 设置/复位数据
2) 延迟20 微秒
3) 把时钟拉低
4) 延迟40 微秒
5) 释放时钟
6) 延迟20 微秒
Here is some sample code written for the PIC16F84 that follows the above algorithms to send a byte to the
host. "Delay" is a self-explanitory macro; "CLOCK" and "DATA" are the bits connected to the Clock and
Data lines; "TEMP0", "PARITY", and "COUNTER" are all general purpose registers. Note that in the
"PS2outBit" routine, the Data and Clock lines are brought low by setting the appropriate I/O pin to output (it's
assumed their output was set to "0" at the beginning of the program.) And they are allowed to float (high) by
setting the I/O pin to input (and allow a pull-up resistor to pull the line high.) This was written for a PIC
running at 4.61 MHz +/- 25% (RC oscillator: 5k/20pF). This is very important for timing considerations.
这里有一些采用上面算法来发送一个字节到主机的PIC16F84 的简单样例代码Delay 是一自说明宏
CLOCK 和DATA 是连接到时钟和数据线上的位单元TEMP0 PARITY 和COUNTER
都是一般目的的寄存器注意在PS2outBit 例程中数据线和时钟线通过设置适当的I/O 脚为输出态
来拉低电平在程序的开始它们的输出被置为0 通过置I/O 脚为输出它们就允许上浮为高电平
允许上来电阻拉线路为高电平下面给PIC 用的代码运行于4.61MHz +/-25% RC 振荡器5k/20pF
这个值对定时条件非常重要
ByteOut movwf TEMP0 ;Save to-be-sent byte
InhibitLoop btfss CLOCK ;Check for inhibit
goto InhibitLoop
Delay 50 ;Delay 50 microseconds
PS/2 技术参考 著Adam Chapweske 译Roy Show
第8 页共8 页 02-11-24发布
btfss CLOCK ;Check again for inhibit
goto InhibitLoop
btfss DATA ;Check for request-to-send
retlw 0xFF
clrf PARITY ;Init reg for parity calc
movlw 0x08
movwf COUNTER
movlw 0x00
call BitOut ;Output Start bit (0)
btfss CLOCK ;Test for inhibit
goto ByteOutEnd
Delay 4
ByteOutLoop movf TEMP0, w
xorwf PARITY, f ;Calculate parity
call BitOut ;Output Data bits
btfss CLOCK ;Test for inhibit
goto ByteOutEnd
rrf TEMP0, f
decfsz COUNTER, f
goto ByteOutLoop
Delay 2
comf PARITY, w
call BitOut ;Output Parity bit
btfss CLOCK ;Test for inhibit
goto ByteOutEnd
Delay 5
movlw 0xFF
call BitOut ;Output Stop bit (1)
Delay 48
retlw 0x00
ByteOutEnd bsf STATUS, RP0 ;Host has aborted
bsf DATA ;DATA=1
bsf CLOCK ;CLOCK=1
bcf STATUS, RP0
retlw 0xFE
BitOut bsf STATUS, RP0
andlw 0x01
btfss STATUS, Z
bsf DATA
btfsc STATUS, Z
bcf DATA
Delay 21
bcf CLOCK
Delay 45
bsf CLOCK
PS/2 技术参考 著Adam Chapweske 译Roy Show
第9 页共9 页 02-11-24发布
bcf STATUS, RP0
Delay 5
return
Host to Device Communication:
主机到设备的通讯
The packet is sent a little differently in host-to-device communication...
被发送的包有点不同于主机到设备通讯过程
First of all, the PS/2 device always generates the clock signal. If the host wants to send data, it must first put
the Clock and Data lines in a "Request-to-send" state as follows:
首先PS/2 设备总是产生时钟信号如果主机要发送数据它必须首先把时钟和数据线设置为请求
发送状态如下示

你可能感兴趣的:(OS)