Analysis of USB Bus Driver Under WinCE

Source: Internet
Author: User

There are a lot of readily available information about USB. I will summarize some of the basic information here.

I. Advantages of USB

(1) easy to use. Supports hot swapping.

(2) wide application scope. The USB system has less additional information and high bandwidth utilization. Both synchronous transmission and asynchronous transmission are supported.

(3) Strong error correction capability. The USB system manages device plugging in real time. The USB protocol provides functions such as transmission error management and error recovery, and handles transmission errors based on different transmission types.

(4) bus power supply. The USB bus can provide 5 V voltage, 100mA current, and up to 500mA current for devices connected to it. USB devices can also be self-powered.

(5) low cost. The USB interface circuit is simple and easy to implement, especially for low-speed devices. The USB system interface/cable is also relatively simple, and the cost is lower than the serial port/parallel port.

2. USB hardware structure

USB adopts a four-wire cable, two of which are used to transmit data, and the other two provide power for downstream devices. USB is a token-based bus. Similar to the token-based bus of a ring network or FDDI. The broadcast token of the USB master controller. the device on the bus checks whether the address in the token is consistent with itself and responds to the host by receiving or sending data. USB manages the USB bus power supply by supporting the suspend/resume operation. The USB system uses a cascade star topology, which consists of three basic components: Host, hub, and functional devices.
3. transmission type of the USB device

1) control transmission)

Control Transmission sends device request information, mainly used to read device configuration information and device status, set device address, set device properties, send control commands and other functions. A full-speed device controls a maximum of 64 bytes for each transmission, while a low-speed device controls a maximum of 8 bytes for each transmission.

2) synchronous transmission (isochronous transfer)

Synchronous transmission only applies to full speed/High Speed devices. Synchronous transmission is performed once every millisecond. It has a large bandwidth and is often used by voice devices. The maximum load per synchronization transmission is 1023 bytes.

3) interrupt transfer)

Interrupted transmission is used to support periodic transmission requirements with a small amount of data. The interrupted transmission period of the full-speed device can be 1 ~ 255 ms, and the interrupted transmission period of low-speed devices is 10 ~ 255 Ms. The maximum payload for each interrupted transmission of a full-speed device can be 64 bytes, while the maximum payload for each interrupted transmission of a low-speed device is only 8 bytes.

4) bulk transfer)

Block data transmission is non-periodic data transmission. Only full-speed/high-speed devices support block data transmission. At the same time, block data transmission is performed only when the bus bandwidth is effective. The maximum load per data transmission can be 64 bytes.

4. Software Design for USB devices

The Software Design of the USB device consists of two parts:

Single-Chip Microcomputer Software on the USB device: Mainly completes the processing of the USB protocol and data exchange.

PC program: consists of USB communication program and user service program. The user service program communicates with the system USB di through the USB communication program.

The debugging of the USB device software is basically divided into the following three steps:

1. Use PC debugging software to connect the USB protocol on the peripheral end

2. Use the debugged USB peripheral interface to develop and debug PC-Side Software

3. Add other USB peripherals to debug the entire system.

5. Components of the USB System Under wince6.0

The USB System Structure in wince6.0 is displayed.

USB System Structure in wince6.0

 

The USB system software consists of two layers: The driver layer of the higher USB device and the USB function layer implemented by wince.

The middle layer of the USB system structure is composed of a higher general serial bus driver usbd module and a lower controller driver HCD module. The HCD module provides underlying support for the usbd module, and the usbd module implements high-level usbd interface functions. The USB driver uses the interface functions provided by usbd to communicate with peripheral devices.

6. Write the driver program for the USB device under wince6.0

In wince6.0, you can write USB drivers in the following three ways:

Use functions provided by the usbd module to operate USB devices

Use stream interface functions

Use existing wince transmission functions

The usbd transmission functions include:

Aborttransfer

Closetransfer

Getisochresults

Issubulktransfer

Issuecontroltransfer

Issueisochtransfer

Issueinterrupttransfer

Istransfercomplete

Issuevendortransfer

Function for establishing a communication pipeline between usbd and a USB device:

Abortpipetransfer

Closepipe

Isdefaultpipehalted

Openpipe

Resetdefaultpipe

Resetpipe

USB functions for data packaging on the bus include:

Getframelength

Getframenumber

Releaseframelengthcontrol

Setframelength

Takeframelengthcontrol

Usbd interacts with USB devices through the following functions:

Findinterface

Getdeviceinfo

Getusbdversion

Loadgenericintefacedrive

Openclientregistrykey

Registerclientdriverid

Registerclientsettings

Registernotificationroutine

Translatestringdescr

Unregisternotificationroutine

All USB device drivers must present certain entry points in their DLL libraries for proper interaction with the usbd module. The entry point required for USB driver in wince6.0 is:

Usbdeviceattach

Usbinstalldriver

Usbuninstalldriver

The USB deviceattach function is used to call this function when a USB device is connected to a computer. This function is mainly used to initialize a USB device, obtain information about a USB device, and configure a USB device, and apply for required resources. The function is prototype:

Bool usbdeviceattach (usb_handle hdevice,

Lpcusb_funcs lpusbfuncs,

Lpcusb_interface lpinterface,

Lpcwstr szuniquedriverid,

Lpbool facceptcontrol,

Lpcusb_driver_settings lpdriversettings,

DWORD dwunused );

Below are some settings for the USB driver development registry key:

All drivers in winceare loaded by the device.exe process in the form of dll. therefore, the dllentry function must be implemented in each driver.
The driver information of the USB host is saved under the HKEY_LOCAL_MACHINE \ drivers \ USB \ loadclients \ key in the registry. The first time we inserted a USB device. Because this information does not exist, a"

Unrecognized USB device "dialog box, ask the user to enter the name of the driver. The name is the file name of the USB host driver DLL. After a name is entered, the system automatically calls the usbinstalldriver function of the DLL. This function is used to add information about the USB host driver to the registry so that the USB device can be identified when the device is inserted again. The prototype is as follows:
Bool usbinstalldriver (lpcwstr szdriverlibfile );
Szdriverlibfile is the name of the input DLL file. If the return value is true, the registration is successful.
When registering USB host information with the Registry, you cannot use common registry functions. You can only use the registration functions provided by usbd.
Bool registerclientdriverid (lpcwstr szuniquedriverid );
Bool registerclientsettings (maid );
These two functions can be called dynamically or statically in usbd. dll.
The dynamic mode is as follows:
Hinstance hinst = loadlibrary (L "usbd. dll ");
If (hinst ){
Lpregister_client_driver_id lpregisterclientid =
(Lpregister_client_driver_id) getprocaddress (
Hinst,
L "registerclientdriverid ");
If (! Lpregisterclientid)
Return false;
Lpregister_client_settings lpregisterclientsetting =
(Lpregister_client_settings) getprocaddress (
Hinst,
L "registerclientsettings ");
If (! Lpregisterclientsetting)
Return false;
Else
Return false;
After that, you can use the lpregisterclientid and lpregisterclientsetting function pointers to call these functions. Remember to use freelibrary.
Static mode:
Add in the. cpp source file
# Pragma comment (Lib, "usbd. lib ")
Add $ (_ sysgenoakroot) \ Lib \ $ (_ cpuindpath) \ usbd. lib to the targetlibs variable of the source file.
In this way, you can directly use these two functions. 1) bool registerclientdriverid (lpcwstr szuniquedriverid)
This function registers the ID of the USB host driver.
2) bool registerclientsettings (maid, maid, maid)
This function registers driver information.
Set szdriverlibfile to the name of the DLL driver passed in by the usbinstalldriver function.
Szuniquedriverid is set to the driver ID registered by calling registerclientdriverid.
Erved is set to null.
Lpdriversettings this parameter is a usb_driver_settings struct. The statement is as follows:
Typedef struct {
DWORD dwcount;
DWORD dwvendorid;
DWORD dwproductid;
DWORD dwreleasenumber;
DWORD dwdeviceclass;
DWORD dwdevicesubclass;
DWORD dwdeviceprotocol;
DWORD dwinterfaceclass;
DWORD dwinterfacesubclass;
DWORD dwinterfaceprotocol;
} Usb_driver_settings;
Count is the structure size, and other items correspond to the USB descriptor.
The fields except count can be set to usb_no_info if no specific value is set.
The information in this struct reflects the HKEY_LOCAL_MACHINE \ drivers \ USB \ loadclients \ key in the registry, which is used to find the USB driver when the USB device is inserted. The following is an example:
Bool usbinstalldriver (lpcwstr szdriverlibfile)
{
Retailmsg (1, (text ("usbinstalldriver \ r \ n ")));
Retailmsg (1, (text ("usbinstalldriver: % s \ r \ n"), szdriverlibfile ));
Bool fret = false;
Usb_driver_settings driversettings;
Driversettings. dwcount = sizeof (driversettings );
Driversettings. dwvendorid = 0x10c4;
Driversettings. dwproductid = 0x0003;
Driversettings. dwreleasenumber = usb_no_info;
 
Driversettings. dwdeviceclass = usb_no_info;
Driversettings. dwdevicesubclass = usb_no_info;
Driversettings. dwdeviceprotocol = usb_no_info;
 
Driversettings. dwinterfaceclass = 0;
Driversettings. dwinterfacesubclass = 0;
Driversettings. dwinterfaceprotocol = 0;
 
Fret = registerclientdriverid (L "usbtest ");
If (fret ){
Fret = registerclientsettings (
Szdriverlibfile,
L "usbtest ",
Null,
& Driversettings );
If (! Fret)
Retailmsg (1, (text ("registerclientsettings error \ r \ n ")));
} Else
Retailmsg (1, (text ("registerclientdriverid error \ r \ n"); Return fret;
}

In wince, the configuration information is divided into three groups, each group has three values,
Group 1:
Dwvendorid, dwproductid, dwreleasenumber
Group 2:
Dwdeviceclass, dwdevicesubclass, and dwdeviceprotocol
Group 3:
Dwinterfaceclass, dwinterfacesubclass, dwinterfaceprotocol
If the registration is successful, "group 1 \ group 2 \ group 3 \ register ID \ DLL" will appear under the HKEY_LOCAL_MACHINE \ drivers \ USB \ loadclients \ key, the key value is the DLL driver name. Each group is composed of three values underlined. If a value is set to usb_no_info, the key name does not include this value. If every value in the entire group is set to usb_no_info, the key name is default.
In the above example, the following key name is generated in my system:
HKEY_LOCAL_MACHINE \ drivers \ USB \ loadclients \ 4292_3 \ Default \ 0_0_0 \ usbtest \ DLL = "myusbtest" (my driver is myusbtest. dll)
When a user inserts a USB device, it reads the descriptor of the USB device and searches for the driver name in the registry based on the value in the descriptor.
Now let's assume that wince only supports the USB keyboard, and we implement a USB mouse driver by ourselves. Without notice, our USB mouse driver cannot be called. The reason is the process of searching for the driver of a USB device. The Registry Information of the usbhid driver provided by wince is
HKEY_LOCAL_MACHINE \ drivers \ USB \ loadclients \ Default \ 3 \ hid_class \ DLL = "usbhid. dll"
The third group of information only uses dwinterfaceclass, while the USB keyboard and USB mouse are only different from dwinterfaceprotocol. So, 3 sums up all the hid, and when we insert the USB mouse into the system, it will call usbhid. DLL driver processing, but it only includes the keyboard driver, no mouse driver, so the mouse cannot be used. To make the custom USB mouse usable, set the value of the third group as follows:
HKEY_LOCAL_MACHINE \ drivers \ USB \ loadclients \ Default \ 3_1_1 \ hid_class \ DLL = "usbhid. dll"
In this way, after the mouse with the value of 3_1_2 is inserted, because the corresponding key value cannot be found, we will be prompted to enter the USB mouse driver.

 

 

 

 

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.