usb_device.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. extern USBD_DescriptorsTypeDef FS_Desc;
  37. /*
  38. * -- Insert your variables declaration here --
  39. */
  40. /* USER CODE BEGIN 0 */
  41. /* USER CODE END 0 */
  42. /*
  43. * -- Insert your external function declaration here --
  44. */
  45. /* USER CODE BEGIN 1 */
  46. void USB_DEVICE_REINSERT(void){
  47. GPIO_InitTypeDef GPIO_InitStruct = {0};
  48. __HAL_RCC_GPIOA_CLK_ENABLE();
  49. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
  50. GPIO_InitStruct.Pin = GPIO_PIN_12;
  51. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  52. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  53. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  54. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  55. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_RESET);
  56. HAL_Delay(500);
  57. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_SET);
  58. HAL_Delay(500);
  59. }
  60. /* USER CODE END 1 */
  61. /**
  62. * Init USB device Library, add supported class and start the library
  63. * @retval None
  64. */
  65. void MX_USB_DEVICE_Init(void)
  66. {
  67. /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
  68. USB_DEVICE_REINSERT();
  69. /* USER CODE END USB_DEVICE_Init_PreTreatment */
  70. /* Init Device Library, add supported class and start the library. */
  71. if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
  72. {
  73. Error_Handler();
  74. }
  75. if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)
  76. {
  77. Error_Handler();
  78. }
  79. if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
  80. {
  81. Error_Handler();
  82. }
  83. if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
  84. {
  85. Error_Handler();
  86. }
  87. /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
  88. /* USER CODE END USB_DEVICE_Init_PostTreatment */
  89. }
  90. /**
  91. * @}
  92. */
  93. /**
  94. * @}
  95. */