兰博

Arduino builds USBPS2 mouse adapter

 
Overview

The personal website www.lab-z.com  has been using Arduino to make various devices. In some specific cases, the company's machines do not support the USB interface, so if you want to use the USB interface keyboard and mouse on your computer, you will encounter problems. For traditional wired mice, it can be done directly through the adapter on Taobao (this can be achieved because the USB mouse chip can directly identify the currently used interface), but unfortunately, the wireless mouse does not have such an interface, so it cannot be used through This adapter is used to complete the transfer. So, use an Arduino to make a device that converts wireless USB to PS/2.

USB is an external bus standard jointly launched by Intel and other companies in 1996 for connection and communication between computers and external devices. Compared with previous interfaces such as serial and parallel ports, USB has the advantages of easy expansion, can connect to a variety of peripherals, and supports hot swapping. Because of the above advantages, the USB interface quickly became popular and was soon used to connect keyboards, mice and hosts. Before the popularity of USB, the PS/2 interface was specially designed for keyboard and mouse. Earlier, there was also a serial port as a mouse interface. The keyboard and mouse can be connected to the computer through this interface. The two interfaces, PS/2 and USB, are completely different in appearance and protocol. Usually if you want to connect a USB keyboard and mouse device to the PS2 interface, you can use the following adapter. 67472cadb2d51ea235e7f99818d26186.jpgThere are actually no components in it, just physical connections, connecting USB6's D+ D- to PS2's CLK and DATA. The keyboard and mouse device determines the interface type and automatically selects the protocol when it is powered on. Unfortunately, many wireless keyboard and mouse receivers do not have this function, so users cannot use USB wireless keyboards and mice with the PS2 interface. Of course, there has never been a wireless keyboard or mouse with a PS2 interface on the market. This time I am using Arduino to make a device that converts a wireless mouse into a PS2.

* 2. Describe the challenges faced by the project and the basic principles of the problems solved as follows: the mouse sends movement and keystroke data to the USB receiver through the wireless protocol. After the receiver obtains the data, it reports it to the Arduino USB Host Shield using the HID protocol. Next Arduino is responsible for parsing the USB data and converting it into the PS2 protocol and sending it to the host:tbl.jpg

[Reference 1] For the X direction, there are Bit 4 (X sign bit) of Byte 1 to indicate the direction (positive and negative), and Bit 6 (X overflow) and Byte 2 of Byte 1 to indicate the movement distance. So the X movement range of the PS/2 mouse is -255 ~ +255.

* 3. Describe the key points involved in the hardware and software parts of the project. The target of this transfer is the Logitech M105 wireless mouse. In addition, there are the following components

     
element quantity Reason for selection
USB Host Shield Mini 1 In order to ensure that the adapter is small in size, USB Host Shield Mini is selected as the USB Host this time;
3.3V Arduino Pro Micro 1 The common Arduino Pro Micro is 5V. This time I chose the 3.3V version, because the signals of this version are also 3.3V level and can communicate directly with the USB Host Mini Shield. If you choose the 5V version, normal communication can only be achieved by adding a level conversion circuit;
PS2 male cable 1 Used to connect PS2 to PC
3mm copper post screw several overall fixed

The circuit diagram is as follows:image002.png

The above is mainly divided into two parts. One is the connection between USB Host Mini and Arduino Pro Micro 3.3V. The two parties communicate through SPI. It is once again emphasized that the 3.3V version of Arduino Pro Micro must be used for direct communication; the other part is the communication between Arduino and PS2. Because Arduino is 3.3V, the following circuit needs to be used for bidirectional level conversion, from Arduino D3_3.3V and D3_5V communicates,image003.png

The PCB design is as follows:

image004.png

Preview: image005.pngAfter the hardware is determined, software design can begin. The first step: analysis of USB data. Pay special attention to the fact that different mice emit different formats. The Logitech M185 wireless mouse is used here. First, use USBLyzer to analyze the HID data of the USB receiver. The captured data is as follows:image006.png

Then determine the data format through experiments:

image007.pngEach time the mouse receiver will send 8 bytes of data to the host: AA XX BB CC DD EE FF The lower three bits of GGAA are for the three mouse buttons; XX is always 00; BB CC gives the x-direction movement data (0xCCBB); DD EE gives the movement data in the y direction (0xEEDD); FF is the distance moved by the roller; GG is described in the Descriptor as AC Pan, which is a horizontal roller, but it does not exist on the M185 used by the author. This scroll wheel, so it's always 0. The format sent by the mouse is the following structure. struct USBMouseData_LogitechM185{             struct  {                    unsigned char _left_btn : 1; // 1 byte unsigned char _right_btn : 1; unsigned char _middle_btn : 1; unsigned char _dummy : 5; }; char na1; int _delta_x; int _delta_y; char _delta_z;             char na2; };< br> < br> The analysis of the mouse has been introduced many times in previous articles, so I won’t repeat it here. Another thing to note is that the USB Host Library will compare this data with the previous data. If there is a difference, the new data will not be sent to the upper layer. This design will affect the mouse wheel, because the amount of data rotated by the wheel is not large, and it often only scrolls one line. This will cause the scrolling to be insensitive, so the following modifications are made in USB_Host_Shield_Library_2.0hiduniversal.cpp:

bool HIDUniversal::BuffersIdentical(uint8_t len, uint8_t *buf1, uint8_t *buf2) {        for(uint8_t i = 0; i < len; i++)                if(buf1[i] != buf2[i])                        return false;        if (buf1[6]!=0) return false; //LABZDebug        return true;}< br> < br> As long as the seventh value in the data transmitted by USB is not zero, it is considered that this data should be sent to the upper layer for processing. USB data will be processed in void on_usb_data(USBMouseData_LogitechM185* data) function. Here the received mouse XY movement data is processed. The movement range of the PS2 mouse is -255 to +255, and the movement range of the Logitech M185 mouse is -32767 to +32767. The usual idea is to use the MAP function for processing, but the result of doing so will cause the mouse sensitivity to decrease. After research, it was found that this is because most of the movement values ​​are very small. Using the MAP function will make this value smaller, resulting in a decrease in sensitivity. Therefore, write the mousemap() function, and the movement exceeding 255 will be directly changed to 255, and the absolute value less than 255 will not be processed.

int mousemap(int value) {  if (value< -255) {return -255;}  if ((value>=-255)&&(value< =255)) {return value;}    if (value>255) {return 255;}}< br> < br> After the above processing, the data is sent to the host through the PS2 interface through the PS2MouseSample() function. There are many Arduino PS/2 libraries on the Internet, but most of them use Arduino as PS2 Host. Here we need to use Arduino as PS2 Device, so we use the library https://github.com/harvie/ps2dev . Initially, I used a PS/2 to USB adapter cable to conduct experiments (theoretically, the current PS/2 already supports hot-swapping, but I have indeed seen hot-swapping burning out the motherboard. To be on the safe side, I don’t recommend it. Hot-swappable PS/2 devices on the motherboard).

image008.jpg

However, during the experiment, it was found that the PS2Dev library could not work on it. Later, I tried the DSLogic logic analyzer [Control 2] and found that it could not parse the PS/2 protocol. Finally, I bought the Kingst LA2016usb logic analyzer. image009.png* 4. The project material list shows that the target of this transfer is Logitech M105 wireless mouse, in addition to the following components

     
element quantity Reason for selection
USB Host Shield Mini 1 In order to ensure that the adapter is small in size, USB Host Shield Mini is selected as the USB Host this time;
3.3V Arduino Pro Micro 1 The common Arduino Pro Micro is 5V. This time I chose the 3.3V version, because the signals of this version are also 3.3V level and can communicate directly with the USB Host Mini Shield. If you choose the 5V version, normal communication can only be achieved by adding a level conversion circuit;
PS2 male cable 1 Used to connect PS2 to PC
3mm copper post screw several overall fixed

* 5. Upload project picturespic1.pngpic2.jpg

 6. Station B project demonstration video https://www.bilibili.com/video/BV1zK411T7nN/  
References:
https://isdaman.com/alsos/hardware/mouse/ps2interface.htm 
http://www.lab- z.com/dslogic/ DSLogic logic analyzer trial 
https://github.com/liumazi/MzMouse 

http://www-ug.eecg.toronto.edu/msl/nios_devices/datasheets/PS2%20Keyboard%20Protocol.htm

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2024-11-21 15:14:18

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号