STM32 USB Design

Source: Internet
Author: User

First, let's look at the working process of USB.

When the USB device is connected to the host, the host starts enumerating the USB devices and sends instructions to the USB device to obtain a description of the USB device, including device descriptor, configuration description (config descriptor), Interface description (interface descriptor), endpoint description (endpoint descriptor), and so on. This information is transmitted to the host through endpoint 0 (endpoint 0). After you obtain the various descriptive information, the operating system configures the appropriate resources for it. This allows the host to communicate with the device.

USB communication has four modes of communication control, interrupt (interrupt), batch (bulk), and synchronization (synchronous). The USB communication is realized through the pipe. A pipeline is an abstract concept that refers to a virtual link between a host and a device. Instead of a USB communication host A and device B, which has bulk in (bulk input), bulk out (batch output), control out (controlled output) three ways of communication, then A and B communication pipeline has three. (Here is a concept, the data flow in the USB communication is relative to the device, in indicates that the device transmits data to the host, out indicates that the host box device transmits data). At the end of the device, each pipe corresponds to an endpoint, and the endpoint configures the associated registers and buffers. The endpoint needs to be set up before the communication. In communication, you simply write or read the data to the buffer, and set the bit-related bits.

The following specifically from the USB interrupt input and output to describe the Keil C MDK development environment based on the STM32 USB Interface microcontroller program design. It is worth mentioning that St or the relevant companies to provide us with a number of packaging functions and related examples, we can according to the examples and modifications to achieve our own needs of the USB communication program.

1.USB Descriptor Configuration

As you can see from the above, USB descriptors are a prerequisite for USB communication. The host must understand the device before it can communicate with it. In the example provided by St, the descriptor is defined in the USB_DES.C file, and the joystick example below illustrates the USB description of the negative configuration.

1.1 Device descriptors
Const U8 JOYSTICK_DEVICEDESCRIPTOR[JOYSTICK_SIZ_DEVICE_DESC] =
{
0x12,/* This description length */
Usb_device_descriptor_type,/* indicated as device Descriptor */
0x00
0x02
0x00
0x00
0x00
0x40,/* Maximum packet size is 64 bytes (for endpoint 0) */
0x84,/* Manufacturer id*/
0x19
0X06,/* Product id*/
0x04
0x00
0x02
1,
2,
3,
0x01/* Number of configuration descriptors */
}

The two important parameters of the device descriptor are the manufacturer ID and the product ID, and the host selects the appropriate driver for the device based on the above two IDs. In our application, we generally only need to modify the example of the two parameters here to complete the device descriptor settings.

1.2 Configuration Descriptor
Const U8 JOYSTICK_CONFIGDESCRIPTOR[JOYSTICK_SIZ_CONFIG_DESC] =
{
0x09
Usb_configuration_descriptor_type,
Joystick_siz_config_desc,
0x00
0x01,/* Number of interfaces */
0x01, the parameter value required by the/*set_configuration command */
0x00,/* The index value of the string describing the configuration */
0xE0,/* power supply mode selection, bus power supply, self-powered, support wakeup*/
0x32,/* Maximum supply current */
/************** Interface 1 Configuration ****************/
0x09
Usb_interface_descriptor_type,
0x00,/* Interface number */
0x00
0x02,/* End points */
0x00
0x00
0x00
0,/* Interface Descriptor index value */
/******************** Endpoint 1 Output Description ********************/
0x07
Usb_endpoint_descriptor_type,
0X81,/* endpoint address, b.7 for direction (1 is in,0 out) b.0-b.3 for endpoint label */
0X03,/* Endpoint data transfer method */
0x08,/* Maximum packet size */
0x00
0x20
/******************** Endpoint 1 Input description ********************/
0x07
Usb_endpoint_descriptor_type,
0x01,/* Endpoint address */
0X03,/* Endpoint data transfer method */
0x40,/* Maximum packet size */
0x00
0x20
}

The configuration descriptor includes the configuration of the interface and endpoint. If the device is a HID device, you should also include the HID description in the configuration descriptor, specifically describing the configuration of the joystick example.

There are other configurations that can be used to understand the relevant data and examples.

2USB communication execution process.

First, when the host data is transferred to the USB device, how does USB receive commands and data? USB first generates an interrupt, which is defined in the Usb_hp_can_tx_irqhandler and Usb_lp_can_rx0_irqhandler of the stm32fxxx_it.c file, and is typically used usb_lp_can_rx0_ Irqhandler. Continue to call the USB_ISTR () function in this function, which is the key to USB communication. It receives the host command and assigns the corresponding function to handle the dispatch. For this, the detailed process I do not quite understand now. If you understand it later, then the supplementary statement.

When the USB device accesses the host, the host enumerates the USB device, and he will ask the USB device to provide its own information, which is realized through endpoint0. Endpoint0 is a special endpoint, and each interface (interface) must have endpoint0. In general, we need to use multiple endpoints (as described earlier, the configuration descriptor defines the number of endpoints, the type, the size of the transmitted data, and so on). The endpoint needs to be initialized before the endpoint is used. This procedure is defined in the Xxx_reset () function in the Usb_prop.c file. As I define two modes of transport for endpoint 1:

/* Initialize Endpoint 1 */
Seteptype (ENDP1, ep_interrupt);
Seteprxaddr (ENDP1, endp1_rxaddr);
Seteprxcount (ENDP1, 8);
Seteprxstatus (ENDP1, ep_rx_valid);


/* Initialize Endpoint 1 */
Seteptype (ENDP1, ep_interrupt);
Seteptxaddr (ENDP1, endp1_txaddr);
Seteptxcount (ENDP1, 64);
Seteptxstatus (ENDP1, Ep_tx_nak);

After defining the endpoint, we can use the endpoint for data transfer.

Input data (in) to host: in transfer process is

1. Fill in the data into the buffer;
2. Set the USB data counter:
3. Set the USB output to be valid.

Xxx_send ()
{

/*copy mouse Position info in ENDP1 Tx Packet Memory area*/
Usertopmabuffercopy (Sendbuffer, ENDP1_TXADDR, 2); /*sendbuffer is the data to be exported, endp1_txaddr the outgoing buffer of endpoint 1, and 2 is the data size byte */
Seteptxcount (ENDP1, 2);
/* Enable endpoint for transmission */
Seteptxvalid (ENDP1);
}
Note In general, the input and output buffer address of the endpoint is undefined, and you must define a specific definition in usb_conf.h to refer to the definition of endpoint 0.

Read data from the host output (out): The Out transfer process is

1. Define the out callback function;
2. Read the data from the buffer:
3. Set the USB input to valid.

void Ep1_out_callback (void)
{
U8 Datalen;
Datalen = Geteprxcount (ENDP1);
Pmatouserbuffercopy (Rcvdata, endp1_rxaddr, datalen);
Seteprxvalid (ENDP1);
}
Note In general, the Declaration of the Epx_out_callback () callback function is an empty execution function. The # define Epx_in_callback nop_process in usb_conf.h need to be hidden. The new definition of void ep1_out_callback (void) in the right place (where the appropriate position is defined after the run does not appear to be a ep1_out_callback error on the line).

Transferred from: http://blog.csdn.net/sdlcgxcqx/article/details/7542144

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.