usb_device.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // __HAL_RCC_USB_FORCE_RESET();
  50. // HAL_Delay(200);
  51. // __HAL_RCC_USB_RELEASE_RESET();
  52. GPIO_InitTypeDef GPIO_InitStruct = {0};
  53. /* GPIO Ports Clock Enable */
  54. __HAL_RCC_GPIOA_CLK_ENABLE();
  55. /*Configure GPIO pin Output Level */
  56. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
  57. /*Configure GPIO pin : PA12 */
  58. GPIO_InitStruct.Pin = GPIO_PIN_12;
  59. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  60. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  61. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  62. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  63. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_RESET);
  64. HAL_Delay(500);
  65. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_SET);
  66. HAL_Delay(500);
  67. }
  68. /* USER CODE END 1 */
  69. /**
  70. * Init USB device Library, add supported class and start the library
  71. * @retval None
  72. */
  73. void MX_USB_DEVICE_Init(void)
  74. {
  75. /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
  76. USB_DEVICE_REINSERT();
  77. /* USER CODE END USB_DEVICE_Init_PreTreatment */
  78. /* Init Device Library, add supported class and start the library. */
  79. if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
  80. {
  81. Error_Handler();
  82. }
  83. if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)
  84. {
  85. Error_Handler();
  86. }
  87. if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
  88. {
  89. Error_Handler();
  90. }
  91. if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
  92. {
  93. Error_Handler();
  94. }
  95. /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
  96. /* USER CODE END USB_DEVICE_Init_PostTreatment */
  97. }
  98. /**
  99. * @}
  100. */
  101. /**
  102. * @}
  103. */