STM32 USB design principles

Publisher:火星Latest update time:2015-08-13 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
First, let's take a look at the working process of USB.

           When a USB device is connected to the host, the host begins to enumerate the USB device and sends instructions to the USB device to obtain relevant description information of the USB device, including device description (device descriptor), configuration description (configuration descriptor), interface description (interface descriptor), endpoint description (endpoint descriptor), etc. This information is transmitted to the host through endpoint 0 (endpoint 0). After obtaining various description information, the operating system will configure corresponding resources for it. In this way, the host can communicate with the device.

           There are four communication modes of USB communication: control, interrupt, bulk and synchronous. USB communication is implemented through pipes. Pipes are an abstract concept, referring to the virtual link for communication between the host and the device. For example, a USB communication host A and device B, there are three communication modes: bulk in (batch input), bulk out (batch output), and control out (control output), then there are three communication pipes between A and B. (Here is a clear concept. In USB communication, the data flow direction is relative to the device. In means that the device transmits data to the host, and out means that the host box device transmits data). On the device side, each pipe corresponds to an endpoint, and the endpoint configures the relevant registers and buffers. Before communication, the endpoint needs to be set up. During communication, you only need to write or read data to the buffer and set the relevant bits.

           The following specifically describes the USB interface microcontroller program design of stm32 based on the keil C mdk development environment from the perspective of USB interrupt input and output. It is worth mentioning that st or related companies provide us with many encapsulation functions and related examples. We can modify them according to the examples to realize the USB communication program we need.

1. USB descriptor configuration
           From the above description, it can be seen that the USB descriptor is the premise of USB communication. The host must first understand the device before it can communicate with it. In the examples provided by st, the descriptors are defined in the usb_des.c file. The following is an example of the Joystick to illustrate the configuration of the USB descriptor.

1.1 Device descriptor
const u8 Joystick_DeviceDescriptor[JOYSTICK_SIZ_DEVICE_DESC] =
{
    0x12,                                                         
    USB_DEVICE_DESCRIPTOR_TYPE,         
    0x00,                                                         
    0x02,
    0x00,                                                         
    0x00,                                                       
    0x00, 0x40,                                                       
    0x84                                                         
    ,                                                          
    0x19,
    0x06,                                                         
    0x04,
    0x00,                                                         
    0x02,
    1,                                                               
    2,                                                             
    3,                                                             
    0x01                                                          
}
The two important parameters of the device descriptor are the manufacturer ID and the product ID. The host will select the corresponding driver for the device based on the above two IDs. In our application, we generally only need to modify the two parameters in the example to complete the setting of the device descriptor.

1.2 Configuration descriptor
const u8 Joystick_ConfigDescriptor[JOYSTICK_SIZ_CONFIG_DESC] =
{
    0x09,
    USB_CONFIGURATION_DESCRIPTOR_TYPE,
    JOYSTICK_SIZ_CONFIG_DESC,
     0x00,
    0x01,                      
    0x01,                        
    0x00,                        
    0xE0,                        
    0x32,                        

    0x09,         
    USB_ INTERFACE_DESCRIPTOR_TYPE,
    0x00,                      
    0x00,          
    0x02,                      
    0x00,         
    0x00,          
    0x00,         
    0,                            
   
    0x07,          
    USB_ENDPOINT_DESCRIPTOR_TYPE,
    0x81,                        
    0x03,                        
    0x08,                      
    0x00,
    0x20,   
         
    0x07,          
    USB_ENDPOINT_DESCRIPTOR_TYPE,
    0x01,                      
    0x03,                      
    0x40,                      
    0x00,
    0x20,          
}
         The configuration descriptor includes the configuration of the interface and endpoint. If the device is a HID device, the HID description should also be added to the configuration descriptor. The specific description can refer to the configuration of the Joystick example.

There are some other configurations that can be understood by referring to relevant materials and examples.

2. The execution process of USB communication.
             First, when the host data is transmitted to the USB device, how does the USB receive commands and data? The USB will first generate an interrupt. This interrupt is defined in the USB_HP_CAN_TX_IRQHandler and USB_LP_CAN_RX0_IRQHandler of the stm32fxxx_it.c file. Generally, USB_LP_CAN_RX0_IRQHandler is used. In this function, the USB_Istr() function is continued to be called. This function is the key to USB communication. It receives the host command and assigns the corresponding function to process it. For this point, I don’t understand the detailed process yet. If I understand it later, I will explain it again.

             When a USB device is connected to the host, the host will enumerate the USB device and ask the USB device to provide relevant information about itself, which is achieved through endpoint0. Endpoint0 is a special endpoint, and each interface must have endpoint0. Generally, we need to use multiple endpoints (as mentioned above, the configuration descriptor defines the number, type, transfer data size, etc. of the endpoint). The endpoint needs to be initialized before use. This process is defined in the xxx_reset() function in the usb_prop.c file. For example, I define two transmission modes for endpoint 1:

SetEPType(ENDP1, EP_INTERRUPT);
SetEPRxAddr(ENDP1, ENDP1_RXADDR);
SetEPRxCount(ENDP1, 8);
   SetEPRxStatus(ENDP1, EP_RX_VALID);


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

Input data to the host (in): The IN transmission process is
1. Fill the buffer with data;
2. Set the USB data counter:
3. Set the USB output to be valid.

XXX_send()
{

UserToPMABufferCopy(sendBuffer, ENDP1_TXADDR, 2);
    SetEPTxCount(ENDP1, 2);

SetEPTxValid(ENDP1);
}

Note that in general, the input and output buffer addresses of the endpoint are not defined and must be defined in usb_conf.h. For specific definitions, please refer to the definition of endpoint 0.

Read data output from the host (out): The out transmission process is
1. Define the out callback function;
2. Read data from the buffer:
3. Set USB input valid.

void EP1_OUT_Callback(void)
{
u8 DataLen;    
DataLen = GetEPRxCount(ENDP1);
PMAToUserBufferCopy(rcvData, ENDP1_RXADDR, DataLen);
SetEPRxValid(ENDP1);
}
               Note that in general, the declaration of the EPX_OUT_Callback() callback function is an empty execution function. #define EPX_IN_Callback NOP_Process in usb_conf.h needs to    be hidden. Then redefine void EP1_OUT_Callback(void) in the appropriate place (the appropriate place means that after the definition, there will be no error that EP1_OUT_Callback is not declared when running).

               In summary, the USB communication of the STM32 chip is briefly explained here. My level is limited, and there are inevitably mistakes in the above. I hope everyone will leave a message actively, discuss together, and make progress together. This article was written intermittently, which brought inconvenience to everyone. I apologize to everyone here. Anyway, I hope this article can be of some help to those friends who are still exploring the initial stage of STM32USB programming.
Keywords:STM32 Reference address:STM32 USB design principles

Previous article:stm32 usb enumeration process
Next article:stm32 usb data buffer question

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

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号