STM32 USB high-speed USB port supports 4G networking

1 Introduction

      The STM32F4USB driver (FULL SPEED) is currently available, but there are many problems, such as the NAK problem, which leads to errors in reading and writing the U disk. A 1ms delay must be added when performing OUT transmission in the USB underlying driver. Wait for more questions. The high-speed USB driver in this article refers to the article by netizen @sakumisu: [Debugging dwc2 usb host notes based on CherryUSB](https://club.rt-thread.org/ask/article/9c31ce71ead26c2b.html "Debugging dwc2 usb host based on CherryUSB Note"), thanks for his research and debugging on USB.

  
2. Debugging of STM32 high-speed USB

     STM32 high-speed USB can use internal DMA to transmit USB data packets, which will greatly increase the speed of USB communication, and can solve various problems encountered in the process of using FULL SPEED, so take time to modify the USB driver of STM32F , change the USB RNDIS device driver to a high-speed USB interface, the code modification is as follows:


    
2.1 Initialization of IO port

    To use the high-speed USB interface, you need to replace the USB IO pins. The well-known PA10 and PA11 can only be used for full-speed USB. After replacing it with high-speed USB, you need to use the PB14 and PB15 tube scripts.
   

 void HAL_HCD_MspInit(HCD_HandleTypeDef* hpcd)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hpcd->Instance==USB_OTG_FS)
  {
  /* USER CODE BEGIN USB_OTG_FS_MspInit 0 */

  /* USER CODE END USB_OTG_FS_MspInit 0 */
   __HAL_RCC_GPIOA_CLK_ENABLE();
   
    /**USB_OTG_FS GPIO Configuration    
    PA12     ------> USB_OTG_FS_DP
    PA11     ------> USB_OTG_FS_DM 
    PA9      ------> USB_VBUS
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    //HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


    GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

     /* Peripheral clock enable */
    __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
    /* USB_OTG_FS interrupt Init */
    HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(OTG_FS_IRQn);

  /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */

  /* USER CODE END USB_OTG_FS_MspInit 1 */
  }
  else if(hpcd->Instance==USB_OTG_HS)
  {

     __HAL_RCC_GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_14 | GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF12_OTG_HS_FS;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
    /* USB_OTG_FS interrupt Init */
    HAL_NVIC_SetPriority(OTG_HS_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
  }

}

      When debugging the program, I always think that this place is simple and can’t make mistakes. As a result, after the USB driver part is modified, no matter how I debug, I can’t enter the USB interrupt. Finally, I checked that the USB high-speed pins are configured as GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS As a result, I did not check the manual when modifying this part of the code. According to the macro definition of GPIO_AF10_OTG_FS, I found that there is a definition of GPIO_AF10_OTG_HS, so I changed it directly. It seems that the boat is always capsized in the small river ditch, and I always think that this place is simple and can't go wrong. As a result, the problem of IO configuration has been encountered twice. The last time was to debug the SPI2 driver, modify the IO, read the manual, and read the manual.

2.2 Modification of USB driver

     The revision of the USB driver mainly modifies the drv_usbh.c file of the operating system device driver interface. In this file, the USB driver is initialized to use full-speed USB and high-speed USB, and the macro definition DRV_USBH_USE_HS is used to switch. This macro definition can be added in rtconfig.h, if this macro is defined, it means high-speed USB is used, if it is not defined, full-speed USB is used. There are a lot of codes, please refer to the git warehouse link at the back of the blog.

    The improvement of the STM32F low-level USB driver is compatible with the original full-speed USB function. The specific improvements are as follows:

1. When using high-speed USB, you need to open the DMA setting.

2. When using high-speed USB, you need to turn off the NAK interrupt. If you don’t turn off the NAK interrupt, the program will be transferred to your doubtful life.

3. The driver program uses a high-speed USB port, and actually works in full-speed mode.

4. Pay attention to the changes of hardware pins when using, you can directly connect the USB port to PB14 and PB15 with a jumper wire.

Use PB14, PB15 to connect high-speed USB interface


 

 3. Code warehouse

     The optimization of the high-speed USB driver made above has been uploaded to the GIT repository. See version label V1.2 STM32 USB HOST driver 4G network card https://github.com/longtengmcu/USB-HOST-driver-4G-rndis-device

 4. Performance improvement

        Use FTP to download files and write them into FLASH, download a 310Kbyte file, the download time is 10 seconds when using full-speed USB, and about 7 seconds when using high-speed USB. The most critical thing is to solve the various problems of returning NAK when the device is driven by USB at full speed. If this article is helpful to you, please reward me for tea.

        Reference article: The first STM32 USB host to drive 4G rndis devices 6335%2522%252C%2522scm % 2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165718138716782388036335&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog- 2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-117398738-null-null.185%5Ev2%5Econtrol&utm_term =%E9%A6%96%E5%88%9B&spm=1018.2226.3001.4450

Guess you like

Origin blog.csdn.net/fhqlongteng/article/details/125660600
usb
usb