2023年10月26日 星期四

Klipper -- 加裝蜂鳴器

 參考 

蜂鳴器的種類

蜂鳴器分成電磁式與壓電式 (piezo buzzer)。電磁式蜂鳴器的工作電壓為 1.5V ~ 24V,壓電式蜂鳴器的工作電壓範圍比較廣,為 1V ~ 220V。因此要使用大於 24V的電壓,那就只能選擇壓電式蜂鳴器。但如果工作電壓較小,則建議使用電磁式蜂鳴器,因為壓電式蜂鳴器一般要使用 9V以上的電壓聲音才會比較大。

電磁式蜂鳴器的耗電流從 10mA ~ 150mA 都有,而壓電式蜂鳴器的耗電流大部分都在 10mA以下,且在蜂鳴器啟動時的瞬間,需消耗約三倍的電流,因此如果有省電或是攜帶型產品的需求,建議使用較省電的壓電式蜂鳴器。

前面所說的消耗電流是指輸出為交流的音頻時,以直流來看,電磁式喇叭的直流阻抗非常低,只有 4Ω ~ 16Ω,對直流幾乎等同短路。因此若選用電磁式喇叭,必須要注意沒有輸出時,輸出電壓必須為 0V,不然,不小心會把 MCU 控制板燒壞。我的經驗較幸運些,連接在風扇的端子,電壓選 5V,config 中將開機時誤設為 1。只是開不了機,沒有燒掉板子,喇叭的溫度高到燙手。

壓電式蜂鳴器則類似於電容,使用上較為安全,但輸出電壓要高一些,例如 24V,聲音才夠響。

另外,還有分有源蜂鳴器和無源蜂鳴器。有源,是只裡面有振盪源,接上直流電就會響,使用方便,缺點是無法控制頻率。

這是一個有源蜂鳴器,內部使用壓電式蜂鳴器。體積較大,聲音比較響,也比較好聽些。因為我想要控制頻率,手邊又沒有可用的壓電式蜂鳴器,就把線重焊,跳過電路板,當無源壓電式蜂鳴器使用。

設定

https://www.reddit.com/r/ender3/comments/n2fd1d/beeperbuzzer_pin/ ,網友的設定。

-------------
[output_pin BEEPER_pin]
pin: PB5
pwm: True         ; A piezo beeper needs a PWM signal, a DC buzzer doesn't.
value: 0          ; Silent at power on, set to 1 if active low.
shutdown_value: 0 ; Disable at emergency shutdown
cycle_time: 0.001 ; PWM frequency : 0.001 = 1ms will give a base tone of 1kHz
scale: 4000       ; PWM parameter will be in the range of (0-4000 Hz).
#enable beeper on printer

[gcode_macro M300]
default_parameter_S: 1000
default_parameter_P: 100
gcode:
    SET_PIN PIN=BEEPER_pin VALUE={S}
    G4 P{P}
    SET_PIN PIN=BEEPER_pin VALUE={S}
#Custom M300 code to allow beeper to be used in gcode

[gcode_macro end_tune]
gcode:
    M300 S3135 P75
    M300 S3135 P150
    M300 S1760 P150
    M300 S1760 P150
    M300 S2637 P150
    M300 S2637 P150
    M300 S3135 P150
-------------

https://github.com/jschuh/klipper-macros,較為完整的設定

--------------
[output_pin beeper]
pin: PC5 # Beeper pin. This parameter must be provided.
pwm: True # A piezo beeper needs a PWM signal, a DC buzzer doesn't.
#pwm: false
#scale: 2
value: 1 # Silent at power on, set to 1 if active low.
shutdown_value: 0

[gcode_macro m300]
description: Emits and audible beep.
  Usage: M300 [P<duration>] [S<frequency>]
gcode:
  {% set settings = printer.configfile.settings %}
  {% if "output_pin beeper" in printer %}
    {% set P = (params.P|default(100)|int, 0)|max %}
    {% set S = (params.S|default(1000)|int, 1)|max %}
    SET_PIN PIN=beeper VALUE={% if settings["output_pin beeper"].pwm %}{
        settings["output_pin beeper"].scale|default(1.0) * 0.5
      } CYCLE_TIME={ 1.0 / S }{% else %}1{% endif %}
    G4 P{P}
    SET_PIN PIN=beeper VALUE=0
  {% else %}
    {action_respond_info(
       "M300 is disabled. To enable create an [output_pin beeper] config.")}
  {% endif %}
--------------

test: SET_PIN PIN=beeper VALUE=0.5 CYCLE_TIME=0.0003

command: M300 P1000 S1000

3D Printer Ringtones | Improve your 3D Printer workflow with sound! | M300,建立 midi 的聲調

  • MIDI Files on bitmidi,https://bitmidi.com/
  • MIDI to M300,https://www.layerfused.com/MIDI-M300
  • Alex Yu's great MIDI to M300 converter,https://github.com/alexyu132/midi-m300
  • MIDI極短音效特選集,http://shells.tw/~bill/midi/midi_s/midi_s.htm

midi 檔案播放

可以用 VLC 軟體,搭配 fluidsynth,但需要 SoundFont 檔 (副檔名為 .sf2),可至 https://github.com/FluidSynth/fluidsynth/wiki/SoundFont 找到相關下載。


------------------
G28 W ; home all without mesh bed level
; start melody
M300 S294 P220 ;N1 D4 1/8
M300 S20000 P20
M300 S311 P220 ;N2 D#4 1/8
M300 S20000 P20
M300 S330 P220 ;N3 E4 1/8
M300 S20000 P20
M300 S523 P460 ;N4 C5 1/4
M300 S20000 P20
M300 S330 P220 ;N5 E4 1/8
M300 S20000 P20
M300 S523 P230 ;N6 C5 1/8
M300 S20000 P10
M300 S330 P220 ;N7 E4 1/8
M300 S20000 P20
M300 S523 P230 ;N8 C5 1/8
M300 S20000 P10
M300 S523 P950 ;N9 C5 1/2
M300 S20000 P10
M300 S523 P220 ;N10 C5 1/8
M300 S20000 P20
M300 S523 P220 ;N11 C5 1/8
M300 S20000 P20
M300 S587 P220 ;N12 D5 1/8
M300 S20000 P20
M300 S622 P220 ;N13 D#5 1/8
M300 S20000 P20
M300 S659 P220 ;N14 E5 1/8
M300 S20000 P20
M300 S523 P220 ;N15 C5 1/8
M300 S20000 P20
M300 S587 P220 ;N16 D5 1/8
M300 S20000 P20
M300 S659 P230 ;N17 E5 1/8
M300 S20000 P10
M300 S659 P230 ;N11 E5 1/8
M300 S20000 P10
M300 S494 P220 ;N19 B4 1/8
M300 S20000 P20
M300 S587 P460 ;N20 D5 1/2
M300 S20000 P20
M300 S523 P1420; C5 1/2+1/4
M300 S20000 P20
M300 S20000 P1920; pause one measure
G1 X0 Y200 Z30; home X axis
M84 ; disable motors
--------------------


2023年10月15日 星期日

新購 ACASIS HS-710PB USB 3.0 12V 4A電源10口分線器

目前手邊在用的 7 port USB Hub,價格只要幾百元,當初是挑最便宜的。雖然聲稱是 USB 3.0 的規格,但 Linux 系統抓到的是 USB 2.0 HUB,而且常常會出問題,因此想換一個。原本只想換一樣 7 port 的就好,後來不小心看到有 16 port 的,但價格不低,就不要太貪心,先買 10 port 的吧。

網拍產品的標題為「 ACASIS HS-710PB USB 3.0 12V 4A電源10口分線器」,價格 NT$ 1,290。附一個 12V/4A 的電源供應器,有些較便宜的,電源供應器是 12V/2A 的。


好奇它是如何加到十個 port 的,使用 Linux 的 dmesg 看到的訊息如下。

----------------------------
[326717.201917] usb 1-8: new high-speed USB device number 44 using xhci_hcd
[326717.340618] usb 1-8: New USB device found, idVendor=0bda, idProduct=5411, bcdDevice= 2.02
[326717.340641] usb 1-8: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[326717.340652] usb 1-8: Product: USB2.1 Hub
[326717.340661] usb 1-8: Manufacturer: Generic
[326717.343176] hub 1-8:1.0: USB hub found
[326717.344225] hub 1-8:1.0: 4 ports detected
[326717.449440] usb 2-7: new SuperSpeed USB device number 2 using xhci_hcd
[326717.473282] usb 2-7: New USB device found, idVendor=0bda, idProduct=0411, bcdDevice= 2.02
[326717.473285] usb 2-7: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[326717.473286] usb 2-7: Product: USB3.2 Hub
[326717.473287] usb 2-7: Manufacturer: Generic
[326717.477616] hub 2-7:1.0: USB hub found
[326717.479084] hub 2-7:1.0: 4 ports detected
[326717.628960] usb 1-8.3: new high-speed USB device number 45 using xhci_hcd
[326717.728606] usb 1-8.3: New USB device found, idVendor=0bda, idProduct=5411, bcdDevice= 2.02
[326717.728614] usb 1-8.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[326717.728616] usb 1-8.3: Product: USB2.1 Hub
[326717.728618] usb 1-8.3: Manufacturer: Generic
[326717.730150] hub 1-8.3:1.0: USB hub found
[326717.731026] hub 1-8.3:1.0: 4 ports detected
[326717.791436] usb 2-7.3: new SuperSpeed USB device number 3 using xhci_hcd
[326717.813658] usb 2-7.3: New USB device found, idVendor=0bda, idProduct=0411, bcdDevice= 2.02
[326717.813661] usb 2-7.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[326717.813663] usb 2-7.3: Product: USB3.2 Hub
[326717.813664] usb 2-7.3: Manufacturer: Generic
[326717.818918] hub 2-7.3:1.0: USB hub found
[326717.820424] hub 2-7.3:1.0: 4 ports detected
[326717.884909] usb 1-8.4: new high-speed USB device number 46 using xhci_hcd
[326717.984578] usb 1-8.4: New USB device found, idVendor=0bda, idProduct=5411, bcdDevice= 2.02
[326717.984596] usb 1-8.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[326717.984602] usb 1-8.4: Product: USB2.1 Hub
[326717.984608] usb 1-8.4: Manufacturer: Generic
[326717.986572] hub 1-8.4:1.0: USB hub found
[326717.987505] hub 1-8.4:1.0: 4 ports detected
[326718.049471] usb 2-7.4: new SuperSpeed USB device number 4 using xhci_hcd
[326718.071675] usb 2-7.4: New USB device found, idVendor=0bda, idProduct=0411, bcdDevice= 2.02
[326718.071694] usb 2-7.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[326718.071702] usb 2-7.4: Product: USB3.2 Hub
[326718.071708] usb 2-7.4: Manufacturer: Generic
[326718.076772] hub 2-7.4:1.0: USB hub found
[326718.078548] hub 2-7.4:1.0: 4 ports detected
----------------------------

使用 lsusb 查看 USB 裝置。

----------------------------
Bus 002 Device 010: ID 0bda:0411 Realtek Semiconductor Corp. Hub
Bus 002 Device 009: ID 0bda:0411 Realtek Semiconductor Corp. Hub
Bus 002 Device 008: ID 0bda:0411 Realtek Semiconductor Corp. Hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 064: ID 262a:100d SAVITECH Corp. FiiO USB DAC Q1
Bus 001 Device 063: ID 0bda:5411 Realtek Semiconductor Corp. RTS5411 Hub
Bus 001 Device 062: ID 0bda:5411 Realtek Semiconductor Corp. RTS5411 Hub
Bus 001 Device 061: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 065: ID feed:0000 Vial Magicforce 68 with Trackpoint
Bus 001 Device 059: ID 0bda:5411 Realtek Semiconductor Corp. RTS5411 Hub
Bus 001 Device 004: ID 0bda:0169 Realtek Semiconductor Corp. Mass Storage Device
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
----------------------------

使用的是 Realtek 瑞昱半導體的晶片,型號  RTS5411。這是一個 1-4 的 USB HUB 控制晶片,兩個晶片串接就可以得到 7埠的 HUB,三個串接就是這個 10埠的 HUB 了。

以下說明引用自知乎網站。

RTS5411 的一般说明

RTS5411是一个USB3.0 HUB控制器,集成了USB3.0&USB2.0收发器、MCU、SIE、稳压器、和充电电路。RTS5411向后兼容USB2.0,USB1.1规范,支持超速,高速,全速,低速操作。

RTS5411为每个下游端口提供电池充电功能。并且符合电池充电规范1.2,可为为各种手提设备充电。根据BC1.2规范,RTS5411不仅下游端口可以作为充电器,上游端口同样支持两个特别的功能。RTS5411上游端口第一个充电功能被叫做“ACA-dock”模式。支持ACA-dock的设备,它将作为各种外设的主机。然而,尽管处于ACA-dock模式,这个设备也能够同时被充电;RTS5411上游端口第二个充电功能被叫做“chaarger detection”功能。RTS5411可以识别插在上游端口的BC1.2充电器模式,包括CDP、DCP和SDP。RTS5411支持自动侦测开关机制,这样可以在合适的模式下,给手提设备充电。

RTS5411可以通过灵活的ISP通道用外部SPI flash更新固件。可以用外部的SPI flash或者EPROM配置大量特性和设置。仅使用USB线和RTS5411下载工具就可实现ISP功能。

RTS5411的特性和设置也可以通过SMBUS接口或者内部eFuse配置。


RTS5411可以通过各种接口和其他设备通信,例如GPIO,I2C,SMBUS。利用这些接口可以灵活拓展各种应用。


此外,RTS5411支持一个特别的节约电源功能,delink模式。即使RTS5411上游接口连接另外的主机或hub上,但是如果没有设备连接RTS5411,RTS5411进入delink模式,为系统节约电源。

RTS5411 的特性

这部分详见DATASHEET. 此处列出重要特性。

  • MTT(Multiple Transaction Translator)
    1. 每个下游端口有1个TT。
    2. 当多个下游端口全速并发时有更大的数据吞吐量。
  • 符合USB充电规范1.2版本和其他手持设备
    1. DCP模式模式。
    2. CDP模式。
    3. ACA-Dock功能。
    4. 为接在上游端口兼容BC1.2的充电器提供侦测功能。
    5. D+/D– Divider Modes 2.0V/2.7V, 2.7/2.0V and 2.7/2.7V。
    6. D+/D- 1.2V Mode.
  • RTS5411的上游端口可以侦测它所连的DSP是一个SDP、CDP或者DCP。当RTS5411连接到一个充电端口,它被允许从DSP吸收更多的电流。
  • 下游端口支持Gang mode和Individual mode。
  • 支持USB2.0 LPM-L1功能。
  • 支持USB3.0 U1/U2/U3省电模式。
  • 集成Fast 8051微处理器。
  • 支持12MHz晶体时钟。
  • 集成3.3V内部LDO输出,输入范围4.5V-5.5V。
  • 集成1.2V开关稳压器输出,输入范围3.0V-5.5V。
  • 支持Efuse配置功能或者例如non-removable port、gang mode等配置。
  • 支持“Delink”省电功能。
    1. 如果没有任何设备接在RTS5411的下游端口,RTS5411将断开自己和主机或者HUB的下游端口的连接,从而进一步实现省电功能。一旦有任何设备连接下游端口,上游端口将自动重新连接。



2023年10月12日 星期四

PS/2 Mouse/Keyboard Protocol

說明:為了使用 Trackpoint 控制器,將網路找到的 PS2 介紹文件備份在此,下面連結似為原文出處,但無法開啟。

http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/PS2/ps2.htm


PS/2 Mouse/Keyboard Protocol

This article is Copyright 1999, Adam Chapweske

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.

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:

The PS/2 (AT) Keyboard Interface
The PS/2 Mouse Interface
I also encourage you to check this site's main page for more information related to this topic, including projects, code, and links related to the mouse and keyboard.  Please send an email if you find any mistakes or bad advice on this site.

The Physical Interface:

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

PC keyboards use 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.

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.  Just a few years ago, serial mice were 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 or USB mouse, there's plenty of information available elsewhere on the web.

The cable connecting the keyboard/mouse to the computer is usually about six feet long and consists of four to six 26 AWG wires surrounded by a thin layer of mylar foil sheilding.  If you need a longer cable, you can buy PS/2 extenstion cables from most consumer electronics stores.  You should not connect multiple extension cables together.  If you need a 30-foot keyboard cable, buy a 30-foot keyboard cable.  Do not simply connect five 6-foot cables together.  Doing so could result in poor communication between the keyboard/mouse and the host.

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.

The pinouts for each connector are shown below:
 

Male

(Plug)
Female 

(Socket)
5-pin DIN (AT/XT): 
1 - Clock
2 - Data
3 - Not Implemented
4 - Ground
5 - Vcc (+5V)


Male

(Plug)
Female

(Socket)
6-pin Mini-DIN (PS/2):
1 - Data
2 - Not Implemented
3 - Ground
4 - Vcc (+5V)
5 - Clock
6 - Not Implemented


 

6-pin SDL:
A - Not Implemented
B - Data
C - Ground
D - Clock
E - Vcc (+5V)
F - Not Implemented


The Electrical Interface:

Note:  Throughout this document, I will 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.

Vcc/Ground provide power to the keyboard/mouse.  The keyboard or mouse should not draw more than 100 mA from the host and care must be taken to avoid transient surges.  Such surges can be caused by "hot-plugging" a keyboard/mouse (ie, connect/disconnect the device while the computer's power is on.)  Older motherboards had a surface-mounted fuse protecting the keyboard and mouse ports.  When this fuse blew, the motherboard was useless to the consumer, and non-fixable to the average technician.  Most newer motherboards use auto-reset "Poly" fuses that go a long way to remedy this problem.  However, this is not a standard and there's still plenty of older motherboards in use.  Therefore, I recommend against hot-plugging a PS/2 mouse or keyboard.

Summary: Power Specifications
Vcc = +5V.  
Max Current = 100 mA.

The Data and Clock lines are both open-collector with pullup resistors to +5V.  An "open-collector" interface has two possible state: low, or high impedance.  In the "low" state, a transistor pulls the line to ground level.  In the "high impedance" state, the interface acts as an open circuit and doesn't drive the line low or high. Furthermore, a "pullup" resistor is connected between the bus and Vcc so the bus is pulled high if none of the devices on the bus are actively pulling it low.  The exact value of this resistor isn't too important (1~10 kOhms); larger resistances result in less power consumption and smaller resistances result in a faster rise time.  A general open-collector interface is shown below:

Figure 1: General open-collector interface.  Data and Clock are read on the microcontroller's pins 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.


Note: When looking through examples on this website, you'll notice I use a few tricks when implementing an open-collector interface with PIC microcontrollers.  I use the same pin for both input and output, and I enable the PIC's internal pullup resistors rather than using external resistors.  A line is pulled to ground by setting the corresponding pin to output, and writing a "zero" to that port.  The line is set to the "high impedance" state by setting the pin to input.  Taking into account the PIC's built-in protection diodes and sufficient current sinking, I think this is a valid configuration.  Let me know if your experiences have proved otherwise.

Communication: General Description

The PS/2 mouse and keyboard implement a bidirectional synchronous serial protocol.  The bus is "idle" when both lines are high (open-collector).  This is the only state where the keyboard/mouse is allowed begin transmitting data.  The host has ultimate control over the bus and may inhibit communication at any time by pulling the Clock line low.  

The device always generates the clock signal.  If the host wants to send data, it must first inhibit communication from the device by pulling Clock low.  The host then pulls Data low and releases Clock.  This is the "Request-to-Send" state and signals the device to start generating clock pulses.

Summary: Bus States
Data = high, Clock = high:  Idle state.
Data = high, Clock = low:  Communication Inhibited.
Data = low, Clock = high:  Host Request-to-Send

  All data is transmitted one byte at a time and each byte is sent in a frame consisting of 11-12 bits.  These bits are:
  • 1 start bit.  This is always 0.
  • 8 data bits, least significant bit first.
  • 1 parity bit (odd parity).
  • 1 stop bit.  This is always 1.
  • 1 acknowledge bit (host-to-device communication only)

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.  The keyboard/mouse must check this bit and if incorrect it should respond as if it had received an invalid command.

Data sent from the device to the host is read on the falling edge of the clock signal; data sent from the host to the device is read on the rising edge.  The clock frequency must be in the range 10 - 16.7 kHz.  This means clock must be high for 30 - 50 microseconds and low for 30 - 50 microseconds..  If you're designing a keyboard, mouse, or host emulator, you should modify/sample the Data line in the middle of each cell.  I.e.  15 - 25 microseconds after the appropriate clock transition.  Again, the keyboard/mouse always generates the clock signal, but the host always has ultimate control over communication.

Timing is absolutely crucial.  Every time quantity I give in this article must be followed exactly.

Communication: Device-to-Host

The Data and Clock lines are both open collector.  A resistor is connected between each line and +5V, so the idle state of the bus is high. When the keyboard or mouse wants to send information, it first checks the Clock line 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 the host releases Clock.  The Clock line must be continuously high for at least 50 microseconds before the device can begin to transmit its data. 

As I mentioned in the previous section, the keyboard and mouse use a serial protocol with 11-bit frames.  These bits are:

  • 1 start bit.  This is always 0.
  • 8 data bits, least significant bit first.
  • 1 parity bit (odd parity).
  • 1 stop bit.  This is always 1.
The keyboard/mouse writes a bit on the Data line when Clock is high, and it is read by the host when Clock is low.  Figures 2 and 3 illustrate this.

Figure 2:  Device-to-host communication.  The Data line changes state when Clock is high and that data is valid when Clock is low.

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.

The clock frequency is 10-16.7 kHz.  The time from the rising edge of a clock pulse to a Data transition must be at least 5 microseconds.  The time from a data transition to the falling edge of a clock pulse must be at least 5 microseconds and no greater than 25 microseconds. 

The host may inhibit communication at any time by pulling the Clock line low for at least 100 microseconds.  If a transmission is inhibited before the 11th clock pulse, the device must abort the current transmission and prepare to retransmit the current "chunk" of data when host releases Clock.  A "chunk" of data could be a make code, break code, device ID, mouse movement packet, etc.  For example, if a keyboard is interrupted while sending the second byte of a two-byte break code, it will need to retransmit both bytes of that break code, not just the one that was interrupted.

If the host pulls clock low before the first high-to-low clock transition, or after the falling edge of the last clock pulse, the keyboard/mouse does not need to retransmit any data.  However, if new data is created that needs to be transmitted, it will have to be buffered until the host releases Clock.  Keyboards have a 16-byte buffer for this purpose.  If more than 16 bytes worth of keystrokes occur, further keystrokes will be ignored until there's room in the buffer.  Mice only store the most current movement packet for transmission.

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:

  • Inhibit communication by pulling Clock low for at least 100 microseconds.
  • Apply "Request-to-send" by pulling Data low, then release Clock.
The device should check for this state at intervals not to exceed 10 milliseconds.  When the device detects this state, it will begin generating Clock signals and clock in eight data bits and one stop bit.  The host changes the Data line only when the Clock line is low, and data is read by the device when Clock is high.  This is opposite of what occours in device-to-host communication.

After the stop bit is received, the device will acknowledge the received byte by bringing the Data line low and generating one last clock pulse.  If the host does not release the Data line after the 11th clock pulse, the device will continue to generate clock pulses until the the Data line is released (the device will then generate an error.)

The host may abort transmission at time before the 11th clock pulse (acknowledge bit) by holding Clock low for at least 100 microseconds.

To make this process a little easier to understand, here's the steps the host must follow to send data to a PS/2 device:

1)   Bring the Clock line low for at least 100 microseconds.
2)   Bring the Data line low.
3)   Release the Clock line.
4)   Wait for the device to bring the Clock line low.
5)   Set/reset the Data line to send the first data bit
6)   Wait for the device to bring Clock high.
7)   Wait for the device to bring Clock low.
8)   Repeat steps 5-7 for the other seven data bits and the parity bit
9)   Release the Data line.
10) Wait for the device to bring Data low.
11) Wait for the device to bring Clock  low.
12) Wait for the device to release Data and Clock


Figure 3 shows this graphically and Figure 4 separates the timing to show which signals are generated by the host, and which are generated by the PS/2 device.  Notice the change in timing for the "ack" bit--the data transition occours when the Clock line is high (rather than when it is low as is the case for the other 11 bits.)

Figure 3:  Host-to-Device Communication.

Figure 4:  Detailed host-to-device communication.

Referring to Figure 4, there's two time quantities the host looks for.  (a) is the time it takes the device to begin generating clock pulses after the host initially takes the Clock line low, which must be no greater than 15 ms. (b) is the time it takes for the  packet to be sent, which must be no greater than 2ms.  If either of these time limits is not met, the host should generate an error.  Immediately after the "ack" is received, the host may bring the Clock line low to inhibit communication while it processes data.  If the command sent by the host requires a response, that response must be received no later than 20 ms after the host releases the Clock line.  If this does not happen, the host generates an error.

    Other Sources / References:


    The PS/2 Mouse Interface

      

    The PS/2 Mouse Interface
    This article is Copyright 2001, Adam Chapweske 

    Electrical Interface / Protocol:

    The PS/2 mouse uses the same protocol as the PS/2 (AT) keyboard.  This standard originally appeared in the IBM technical reference manual, but I am not aware of any current official publication of this standard.  However, you may click here for the (detailed) information I have gathered about that protocol.

    Inputs, Resolution, and Scaling:

    The standard PS/2 mouse supports the following inputs: X (right/left) movement, Y (up/down) movement, left button, middle button, and right button. The mouse reads these inputs at a regular freqency and updates various counters and flags to reflect movement and button states.  There are many PS/2 pointing devices that have additional inputs and may report data differently than described in this document.  One popular extension I cover later in this document is the Microsoft Intellimouse, which includes support for the standard inputs as well as a scrolling wheel and two additional buttons.

    The standard mouse has two counters that keep track of movement: the X-movement counter and the Y-movement counter.  These  are 9-bit 2's complement values and each has an associated overflow flag.  Their contents, along with the state of the three mouse buttons, are sent to the host in the form of a 3-byte movement data packet (as described in the next section.)  The movement counters represent the amount of movement that has occurred since the last movment data packet was sent to the host.

    When the mouse reads its inputs, it records the current state of its buttons, then checks for movement. If movement has occurred, it increments (for +X or +Y movement) or decrements (for -X or -Y movement) its X and/or Y movement counters. If either of the counters has overflowed, it sets the appropriate overflow flag.

    The parameter that determines the amount by which the movement counters are incremented/decremented is the resolution. The default resolution is 4 counts/mm and the host may change that value using the "Set Resolution" (0xE8) command.

    There is a parameter that does not effect the movement counters, but does effect the reported(1) value of these counters.  This parameter is scaling.  By default, the mouse uses 1:1 scaling, which has no effect on the reported mouse movement.  However, the host may select 1:2 scaling by sending the "Set Scaling 2:1" (0xE7) command.  If 2:1 scaling is enabled, the mouse will apply the following algorithm to the counters before sending their contents to the host:
     
     

    Movement Counter
    Reported Movement
    0
    0
    1
    1
    2
    1
    3
    3
    4
    6
    5
    9
    N > 5
    2 * N

    Movement Data Packet:

    The standard PS/2 mouse sends movement (and button) information to the host using the following 3-byte packet (4):

     

    Byte 1 
    Bit 7
    Bit 6
    Bit 5
    Bit 4
    Bit 3
    Bit 2
    Bit 1
    Bit 0
    Y overflow
    X overflow
    Y sign bit
    X sign bit
    Always 1
    Middle Btn
    Right Btn
    Left Btn
    Byte 2
    X Movement
    Byte 3 
    Y Movement
    The movement counters are 9-bit 2's complement integers, where the most significant bit appears as a sign bit in Byte 1 of the movement data packet. These counters are updated when the mouse reads its input and finds movement has occurred. Their value is the amount of movement that has occurred since the last movement data packet was sent to the host (ie, after a packet is sent to the host, the movement counters are reset.) The range of values that can be expressed by the movement counters is -255 to +255. If this range is exceeded, the appropriate overflow bit is set and the counter is not incremented/decremented until it is reset.

    As I mentioned earlier, the movement counters are reset whenever a movement data packet is successfully sent to the host. They are also reset after the mouse receives any command from the host other than the "Resend" (0xFE) command.

    Modes of Operation:

    Data reporting is handled according to the mode in which the mouse is operating.  There are four standard modes of operation: 

    • Reset - The mouse enters Reset mode at power-up or after receiving the "Reset" (0xFF) command. 
    • Stream - This is the default mode (after Reset finishes executing) and is the mode in which most software uses the mouse.  If the host has previously set the mouse to Remote mode, it may re-enter Stream mode by sending the "Set Stream Mode" (0xEA) command to the mouse. 
    • Remote - Remote mode is useful in some situations and may be entered by sending the "Set Remote Mode" (0xF0) command to the mouse. 
    • Wrap - This mode isn't particularly useful except for testing the connection between the mouse and its host.  Wrap mode may be entered by sending the "Set Wrap Mode" (0xEE) command to the mouse.  To exit Wrap mode, the host must issue the "Reset" (0xFF) command or "Reset Wrap Mode" (0xEC) command.  If the "Reset" (0xFF) command is recieved, the mouse will enter Reset mode.  If the "Reset Wrap Mode" (0xEC) command is received, the mouse will enter the mode it was in prior to Wrap Mode.
    (Note: The mouse may also enter "extended" modes of operation, as described later in this document.  However, this is not a feature of the standard PS/2 mouse.)

    Reset Mode:

    The mouse enters reset mode at power-on or in response to the "Reset" (0xFF) command. After entring this mode, the mouse performs a diagnostic self-test referred to as BAT (Basic Assurance Test) and sets the follwing default values:

    • Sample Rate - 100 samples/sec
    • Resolution - 4 counts/mm
    • Scaling - 1:1
    • Data Reporting Disabled
    It then sends a BAT completion code of either 0xAA (BAT successful) or 0xFC (Error). If the host receives a response other than 0xAA, it may cycle the mouse's power supply, causing the mouse to reset and re-execute its BAT.

    Following the BAT completion code (0xAA or 0xFC), the mouse sends its device ID of 0x00. This distinguishes it from a keyboard, or a mouse in an extended mode. I have read documents saything the host is not supposed to transmit any data until it receives a device ID.  However I've found that some BIOS's will send the "Reset" (0xFF) command immediately following the 0xAA received after a power-on reset.

    After the mouse has sent its device ID to the host, it will enter Stream Mode.  Note that one of the default values set by the mouse is "Data Reporting Disabled".  This means the mouse will not send any movement data packets to the host until the "Enable Data Reporting" (0xF4) command is received.

    Stream Mode:

    In stream mode, the mouse sends movement data when it detects movement or a change in state of one or more mouse buttons. The maximum rate at which this data reporting may occur is known as the sample rate.  This parameter ranges from 10 samples/sec to 200 samples/sec. Its default value is 100 samples/sec and the host may change that value by using the "Set Sample Rate" (0xF3) command.  Stream mode is the default mode of operation.

    Remote Mode:

    In this mode, the mouse reads its inputs and updates its counters/flags at the current sampling rate, but it only notifies the host of movement (and change in button state) when that information is requested by the host. The host does this by issuing the "Read Data" (0xEB) command. After receiveing this command, the mouse will send a movement data packet, and reset its movement counters.

    Wrap Mode:

    This is an "echoing" mode in which every byte received by the mouse is sent back to the host. Even if the byte represents a valid command, the mouse will not respond to that command--it will only echo that byte back to the host. There are two exceptions to this: the "Reset" (0xFF) command and "Reset Wrap Mode" (0xEC) command. The mouse treats these as valid commands and does not echo them back to the host.

    Intellimouse Extensions:

    A popular extension to the standard PS/2 mouse is the Microsoft Intellimouse.  This includes support for a total of five mouse buttons and three axises of movement (right-left, up-down, and a scrolling wheel).  These additional features require the use of a 4-byte movement data packet rather than the standard 3-byte packet.  Since standard PS/2 mouse drivers cannot recognize this packet format, the Microsoft Intellimouse is required to operate exactly like a standard PS/2 mouse unless it knows the drivers support the extended packet format.  This way, if a Microsoft Intellimouse is used on a computer which only supports the standard PS/2 mouse, the Microsoft Intellimouse will still function, except for its scrolling wheel and 4th and 5th buttons.

    The Microsoft Intellimouse operates just like a standard PS/2 mouse (ie, it uses a 3-byte movement data packet, responds to all commands in the same way as a standard PS/2 mouse, and reports a device ID of 0x00.)  To enter scrolling wheel mode, the host sends the following command sequence:

    Set sample rate 200
    Set sample rate 100
    Set sample rate 80
    The host then issues the "Get device ID" command (0xF2) and waits for a response.  If a standard PS/2 mouse (ie, non-Intellimouse) is attached, it will respond with a device ID of 0x00.  In this case, the host will recognize the fact that the mouse does have a scrolling wheel and will continue to treat it as a standard PS/2 mouse.  However, if a Microsoft Intellimouse is attached, it will respond with an ID of 0x03.  This tells the host that the attached pointing device has a scrolling wheel and the host will then expect the mouse to use the following 4-byte movement data packet:
     

    Byte 1 
    Bit 7
    Bit 6
    Bit 5
    Bit 4
    Bit 3
    Bit 2
    Bit 1
    Bit 0
    Y overflow
    X overflow
    Y sign bit
    X sign bit
    Always 1
    Middle Btn
    Right Btn
    Left Btn
    Byte 2
    X Movement
    Byte 3 
    Y Movement
    Byte 4
    Z Movement
    Z Movement is a 2's complement number that represents the scrolling wheel's movement since the last data report.  Valid values are in the range of -8 to +7. This means the number is actually represented only by the least significant four bits; the upper four bits act only as sign extension bits.
    To enter scrolling wheel + 5 button mode, the host sends the following command sequence:
    Set sample rate 200
    Set sample rate 200
    Set sample rate 80
    The host then issues the "Get device ID" command (0xF2) and waits for a response.  A Microsoft Intellimouse will respond with a device ID of 0x04, then use the following 4-byte movement data packet:
     

    Byte 1 
    Bit 7
    Bit 6
    Bit 5
    Bit 4
    Bit 3
    Bit 2
    Bit 1
    Bit 0
    Y overflow
    X overflow
    Y sign bit
    X sign bit
    Always 1
    Middle Btn
    Right Btn
    Left Btn
    Byte 2
    X Movement
    Byte 3 
    Y Movement
    Byte 4
    Always 0
    Always 0
    5th Btn
    4th Btn
    Z3
    Z2
    Z1
    Z0
    Z0-Z3 is a 2's complement number which represents the amount of movement that has occurred since the last data report.  Valid values range from -8 to +7.
    4th Btn: 1 = 4th mouse button is pressed; 0 = 4th mouse button is not pressed.
    5th Btn: 1 = 5th mouse button is pressed; 0 = 5th mouse button is not pressed.
    You may have seen mice with two scrolling wheels--one vertical and the other horizontal.  These mice use the Microsoft Intellimouse data packet format as described above.  If the vertical wheel is scrolled upward, the Z-counter is incremented by one and if that wheel is scrolled down, the Z-counter is decremented by one.  This is normal operation for a scrolling wheel.  However, if the horizontal wheel is scrolled right, the Z-counter is incremented by two and if it is scrolled left, the Z-counter is decremented by two.  This seems like an odd way to implement the second scrolling wheel, but it works since the placement of the two wheels make it impossible to use both of them at the same time (and if you try to trick the software and use both at the same time, it will ignore the horizontal wheel.)

    Command Set:

    The following are the only commands that may be sent to the mouse... If the mouse is in Stream mode, the host should disable data reporting (command 0xF5) before sending any other commands...

    • 0xFF (Reset) - The mouse responds to this command with "acknowledge" (0xFA) then enters Reset Mode.
    • 0xFE (Resend) - The host sends this command whenever it receives invalid data from the mouse. The mouse responds by resending the last(2) packet(3) it sent to the host.   If the mouse responds to the "Resend" command with another invalid packet, the host may either issue another "Resend" command, issue an "Error" command, cycle the mouse's power supply to reset the mouse, or it may inhibit communication (by bringing the Clock line low).  The action taken depends on the host.
    • 0xF6 (Set Defaults) - The mouse responds with "acknowledge" (0xFA) then loads the following values:  Sampling rate = 100, Resolution = 4 counts/mm, Scaling = 1:1, Disable Data Reporting.  The mouse then resets its movement counters and enters stream mode.
    • 0xF5 (Disable Data Reporting) - The mouse responds with "acknowledge" (0xFA) then disables data reporting and resets its movement counters.  This only effects data reporting in Stream mode and does not disable sampling.  Disabled stream mode funcions the same as remote mode.
    • 0xF4 (Enable Data Reporting) - The mouse responds with "acknowledge" (0xFA) then enables data reporting and resets its movement counters.  This command may be issued while the mouse is in Remote Mode (or Stream mode), but it will only effect data reporting in Stream mode.
    • 0xF3 (Set Sample Rate) - The mouse responds with "acknowledge" (0xFA) then reads one more byte from the host.  The mouse saves this byte as the new sample rate. After receiving the sample rate, the mouse again responds with "acknowledge" (0xFA) and resets its movement counters.  Valid sample rates are 10, 20, 40, 60, 80, 100, and 200 samples/sec.
    • 0xF2 (Get Device ID) - The mouse responds with "acknowledge" (0xFA) followed by its device ID (0x00 for the standard PS/2 mouse.)  The mouse should also reset its movement counters.
    • 0xF0 (Set Remote Mode) - The mouse responds with "acknowledge" (0xFA) then resets its movement counters and enters remote mode.
    • 0xEE (Set Wrap Mode) - The mouse responds with "acknowledge" (0xFA) then resets its movement counters and  enters wrap mode.
    • 0xEC (Reset Wrap Mode) - The mouse responds with "acknowledge" (0xFA) then resets its movement counters and enters the mode it was in prior to wrap mode (Stream Mode or Remote Mode.)
    • 0xEB (Read Data) - The mouse responds with acknowledge (0xFA) then sends a movement data packet. This is the only way to read data in Remote Mode.  After the data packets has been successfully sent, it resets its movement counters.
    • 0xEA (Set Stream Mode) - The mouse responds with "acknowledge" then resets its movement counters and enters steram mode.
    • 0xE9 (Status Request) - The mouse responds with "acknowledge" then sends the following 3-byte status packet (then resets its movement counters.): 
      •  

        Byte 1 
        Bit 7
        Bit 6
        Bit 5
        Bit 4
        Bit 3
        Bit 2
        Bit 1
        Bit 0
        Always 0
        Mode
        Enable
        Scaling
        Always 0
        Left Btn
        Middle Btn
        Right Btn
        Byte 2
        Resolution
        Byte 3 
        Sample Rate

        Right, Middle, Left Btn = 1 if button pressed; 0 if button is not pressed.
        Scaling = 1 if scaling is 2:1; 0 if scaling is 1:1. (See commands 0xE7 and 0xE6)
        Enable = 1 if data reporting is enabled; 0 if data reporting is disabled. (See commands 0xF5 and 0xF4)
        Mode = 1 if Remote Mode is enabled; 0 if Stream mode is enabled. (See commands 0xF0 and 0xEA)
         

    • 0xE8 (Set Resolution) - The mouse responds with acknowledge (0xFA) then reads one byte from the host and again responds with acknowledge (0xFA) then resets its movement counters.  The byte read from the host determines the resolution as follows: 
       
      Byte Read from Host
      Resolution
      0x00
      1 count/mm
      0x01
      2 count/mm
      0x02
      4 count/mm
      0x03
      8 count/mm
    • 0xE7 (Set Scaling 2:1) - The mouse responds with acknowledge (0xFA) then enables 2:1 scaling (discussed earlier in this document.)
    • 0xE6 (Set Scaling 1:1) - The mouse responds with acknowledge (0xFA) then enables 1:1 scaling (discussed earlier in this document.)
    The only commands the standard PS/2 mouse will send to the host are the "Resend" (0xFE) and "Error" (0xFC) commands.  They both work the same as they do as host-to-device commands. 

    Initialization:

    The PS/2 mouse is normally detected/initialized only when the computer is booting up.  That is, the mouse is not hot-pluggable and you must restart your computer whenever you add/remove a PS/2 mouse (furthermore, some motherboards may be damaged if you add/remove a PS/2 mouse while the computer is running.)

    The initial detection of the PS/2 mouse occurrs during POST.  If a mouse is detected, the BIOS will allow the operating system to configure/enable the mouse.  Otherwise, it will inhibit communication on the mouse's bus.  If you boot the computer with a mouse attached, then detach/reattach the mouse while in Windows, the OS may be able to detect the mouse was reattached.  Microsoft tried to support this, but it only works about 50% of the time.

    The following is the communication between my computer (running Win98SE) and mouse when it boots up with a standard PS/2 mouse attached.  It is fairly typical of how a PS/2 mouse is initialized and if you want to emulate a PS/2 mouse it must (at minimum) be able to support the following sequence of commands... 

    • Power-on Reset:
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  FF  Reset command
      Mouse: FA  Acknowledge
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  FF  Reset command
      Mouse: FA  Acknowledge
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  FF  Reset command
      Mouse: FA  Acknowledge
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft
      Mouse: FA  Acknowledge       : Scrolling Mouse mode
      Host:  C8  decimal 200       :
      Mouse: FA  Acknowledge       :
      Host:  F3  Set Sample Rate   :
      Mouse: FA  Acknowledge       :
      Host:  64  decimal 100       :
      Mouse: FA  Acknowledge       :
      Host:  F3  Set Sample Rate   :
      Mouse: FA  Acknowledge       :
      Host:  50  decimal 80        :
      Mouse: FA  Acknowledge       :
      Host:  F2  Read Device Type  :
      Mouse: FA  Acknowledge       :
      Mouse: 00  Mouse ID          : Response 03 if microsoft scrolling mouse
      Host:  F3  Set Sample Rate 
      Mouse: FA  Acknowledge
      Host:  0A  decimal 10
      Mouse: FA  Acknowledge
      Host:  F2  Read Device Type
      Mouse: FA  Acknowledge
      Mouse: 00  Mouse ID
      Host:  E8  Set resolution
      Mouse: FA  Acknowledge
      Host:  03  8 Counts/mm
      Mouse: FA  Acknowledge
      Host:  E6  Set Scaling 1:1
      Mouse: FA  Acknowledge
      Host:  F3  Set Sample Rate
      Mouse: FA  Acknowledge
      Host:  28  decimal 40
      Mouse: FA  Acknowledge
      Host:  F4  Enable
      Mouse: FA  Acknowledge
      Initialization complete...

      If I then press the Left Button...
      Mouse: 09 1 1 00001001; bit0 = Left button state; bit3 = always 1
      Mouse: 00 1 1 No X-movement
      Mouse: 00 1 1 No Y-movement
      ... and release the Left Button:
      Mouse: 08 0 1 00001000 bit0 = Left button state; bit3 = always 1
      Mouse: 00 1 1 No X-movement
      Mouse: 00 1 1 No Y-movement

    The following is the communication between my computer (running Win98SE) and mouse when it boots up with an (emulated) Intellimouse... 
    • Power-on Reset:
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  FF  Reset command
      Mouse: FA  Acknowledge
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  FF  Reset command
      Mouse: FA  Acknowledge
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  FF  Reset command
      Mouse: FA  Acknowledge
      Mouse: AA  Self-test passed
      Mouse: 00  Mouse ID
      Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft 
      Mouse: FA  Acknowledge       : Scrolling Mouse mode
      Host:  C8  decimal 200       :
      Mouse: FA  Acknowledge       :
      Host:  F3  Set Sample Rate   :
      Mouse: FA  Acknowledge       :
      Host:  64  decimal 100       :
      Mouse: FA  Acknowledge       :
      Host:  F3  Set Sample Rate   :
      Mouse: FA  Acknowledge       :
      Host:  50  decimal 80        :
      Mouse: FA  Acknowledge       :
      Host:  F2  Read Device Type  :
      Mouse: FA  Acknowledge       :
      Mouse: 03  Mouse ID          : Response 03 if microsoft scrolling mouse
      Host:  E8  Set Resolution 
      Mouse: FA  Acknowledge 
      Host:  03  8 counts/mm
      Mouse: FA  Acknowledge
      Host:  E6  Set scaling 1:1
      Dev:   FA  Acknowledge
      Host:  F3  Set Sample Rate 
      Mouse: FA  Acknowledge
      Host:  28  decimal 40
      Mouse: FA  Acknowledge
      Host:  F4  Enable device 
      Mouse: FA  Acknowledge

      If I then press the left mouse button:
      Mouse: 09  00001001 bit0 = Left button state; bit3 = always 1
      Mouse: 00  No X-movement
      Mouse: 00  No Y-movement
      Mouse: 00  No Z-movement

      ...and then release the left mouse button button:
      Mouse: 08  00001000 bit0 = Left button state; bit3 = always 1
      Mouse: 00  No X-movement
      Mouse: 00  No Y-movement
      Mouse: 00  No Z-movement

    After I downloaded/installed the Microsoft's Intellimouse drivers with support for the 4th and 5th buttons, the following sequence was found:
    ... (starts same as before) ...
    Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft 
    Mouse: FA  Acknowledge       : Scrolling Mouse mode.
    Host:  C8  decimal 200       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  64  decimal 100       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  50  decimal 80        :
    Mouse: FA  Acknowledge       :
    Host:  F2  Read Device Type  :
    Mouse: FA  Acknowledge       :
    Mouse: 03  Mouse ID          : Response 03 if microsoft scrolling mouse.
    Host:  F3  Set Sample Rate   : Attempt to Enter Microsoft 5-button 
    Mouse: FA  Acknowledge       : Scrolling Mouse mode.
    Host:  C8  decimal 200       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  C8  decimal 200       :
    Mouse: FA  Acknowledge       :
    Host:  F3  Set Sample Rate   :
    Mouse: FA  Acknowledge       :
    Host:  50  decimal 80        :
    Mouse: FA  Acknowledge       :
    Host:  F2  Read Device Type  :
    Mouse: FA  Acknowledge       :
    Mouse: 04  Mouse ID         : Response 04 if 5-button scrolling mouse.
    ... rest of initialization same as before ...
    Emulation/Interfacing:
    • Click here for routines that emulate a PS/2 mouse or keyboard
    • Click here for routines that emulate a PS/2 host (ie, interface a mouse/keyboard)
    • Click here for a fully-functional PS/2 mouse written for the PIC16F84.
    If you want to build a truely fully-implemented mouse or host, you should implement all of the features described in this document (except for, of course, the Microsoft Intellimouse extensions, which are optional).  However, at an absolute minimum, your device should operate as follows:
    To Emulate a Mouse:
    • Never send data when  the "Clock" line low.  If the host pulls the "Data" line low, prepare to read a byte from the host.
    • ~500 milliseconds after powerup, transmit "0xAA, 0x00".
    • Wait for the host to send the enable (0xF4) command before sending any movement/button data.
    • Emulate the various mouse functions as follows:
      Emulated Action
      Data sent to host
      Move up one
      0x08,0x00,0x01
      Move down one
      0x28,0x00,0xFF
      Move right one
      0x08,0x01,0x00
      Move left one
      0x18,0xFF,0x00
      Press left button
      0x09,0x00,0x00
      Release left button
      0x08,0x00,0x00
      Press middle button
      0x0C,0x00,0x00
      Release middle button
      0x08,0x00,0x00
      Press right button
      0x0A,0x00,0x00
      Release right button
      0x08,0x00,0x00
    • Respond to the "Reset" (0xFF) command with "0xFA" then goto the beginning of your program. (ie, send 0xAA, 0x00, then wait for the enable command before sending any movement/button data.)
    • Respond to the "Get Device ID" (0xF2) command with "0xFA, 0x00".
    • Respond to the "Status Request" (0xE9) command with "0xFA, 0x00, 0x02, 0x64".
    • Respond to all other commands with acknowledge (0xFA).
    To Interface a Mouse:
    • Wait for the mouse to send "0xAA", then send the "Enable" (0xF4) command.
    • The mouse will then send a 3-byte movement packets as described earlier in this document.


    Footnotes:

    1) 2:1 scaling only applies to the automatic data reporting in Stream mode. It does not effect the reported data sent in response to the "Read Data" (0xEB) command.

    2)  The mouse and host do not buffer "Resend" (0xFF) commands. This means "0xFE" will never be sent in response to the "Resend" command. 

    3)   A "packet" may be a 3-byte movement data packet, a 4-byte movement data packet (for the Intellimouse), a 3-byte status packet (see "Status Request" [0xE9] command) a 2-byte completion-code-ID packet (0xAA,0x00 or 0xFC,0x00), or a 1-byte response to a command.

    4)  A little advice from my own experience...  Even though bit 3 of the first byte in a movement data packet is supposed to be set, some drivers (such as the standard PS/2 mouse driver included with Windows 98SE) don't care and just ignore that bit.  However, other drivers do check that bit and if it is not set, it is considered an error.  I mention this so that, if you're designing a mouse, you double-check that this bit is set in every movement data packet sent by your mouse.  If it is not, your mouse may work properly when you test it on your computer, but it may not work on other computers that use different mouse drivers. 

    For example, if using MS Intellimouse drivers and bit 3 of the first byte in a movement data packet is not set, the driver will discard that packet, then send the "Disable Data Reporting"  (0xF5) command, followed by the "Set Defaults" (0xF6) command, then it will reinitialize the mouse using the same command sequence as it does when Windows boots up (see the "Initialization" section above.)

    Other Sources / References:


      2023年10月11日 星期三

      3D 列印筆記

       

      火山口加熱塊

      有不同的稱呼,火山加熱頭/加熱塊。特點:特別的結構設計,比一般加熱塊增加約2倍的加熱行程,使線材能熔融更完全,增加層與層之間的貼合度。可直接安裝在創想喉管上。

      加熱棒和熱敏電阻為通用,只有火山頭噴嘴較長。還有專用的火山加熱頭矽膠保護套,用以阻隔外界冷風的影響,維持加熱塊溫度。

      起子充磁器 DIY

      參考 起子充磁器的原理

      結構看起來很簡單,只是兩塊磁鐵相對組立而已。主要是確定物理原理,如兩個磁鐵之間極性關係。


      網誌存檔