usb_device.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usb_device.c
  5. * @version : v2.0_Cube
  6. * @brief : This file implements the USB Device
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2025 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usb_device.h"
  22. #include "usbd_core.h"
  23. #include "usbd_desc.h"
  24. #include "usbd_cdc.h"
  25. #include "usbd_cdc_if.h"
  26. /* USER CODE BEGIN Includes */
  27. /* USER CODE END Includes */
  28. /* USER CODE BEGIN PV */
  29. /* Private variables ---------------------------------------------------------*/
  30. /* USER CODE END PV */
  31. /* USER CODE BEGIN PFP */
  32. /* Private function prototypes -----------------------------------------------*/
  33. /* USER CODE END PFP */
  34. /* USB Device Core handle declaration. */
  35. USBD_HandleTypeDef hUsbDeviceFS;
  36. /*
  37. * -- Insert your variables declaration here --
  38. */
  39. /* USER CODE BEGIN 0 */
  40. /* USER CODE END 0 */
  41. /*
  42. * -- Insert your external function declaration here --
  43. */
  44. /* USER CODE BEGIN 1 */
  45. //将USB-P拉低,再拉高,以模拟重新拔插的过程
  46. //此函数需要放在USB初始化函数的前面,否则不会生效
  47. void USB_DEVICE_REINSERT(void)
  48. {
  49. GPIO_InitTypeDef GPIO_InitStruct = {0};
  50. /* GPIO Ports Clock Enable */
  51. __HAL_RCC_GPIOA_CLK_ENABLE();
  52. /*Configure GPIO pin Output Level */
  53. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
  54. /*Configure GPIO pin : PA12 */
  55. GPIO_InitStruct.Pin = GPIO_PIN_12;
  56. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  57. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  58. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  59. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  60. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_RESET);
  61. HAL_Delay(500);
  62. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_SET);
  63. HAL_Delay(500);
  64. }
  65. /* USER CODE END 1 */
  66. /**
  67. * Init USB device Library, add supported class and start the library
  68. * @retval None
  69. */
  70. void MX_USB_DEVICE_Init(void)
  71. {
  72. /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
  73. USB_DEVICE_REINSERT();
  74. /* USER CODE END USB_DEVICE_Init_PreTreatment */
  75. /* Init Device Library, add supported class and start the library. */
  76. if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
  77. {
  78. Error_Handler();
  79. }
  80. if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)
  81. {
  82. Error_Handler();
  83. }
  84. if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
  85. {
  86. Error_Handler();
  87. }
  88. if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
  89. {
  90. Error_Handler();
  91. }
  92. /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
  93. /* USER CODE END USB_DEVICE_Init_PostTreatment */
  94. }
  95. /**
  96. * @}
  97. */
  98. /**
  99. * @}
  100. */