• stm32 HSUSB


    /

    stm32f407xx.h

    #define USB_OTG_HS_PERIPH_BASE 0x40040000UL

    #define USB_OTG_HS ((USB_OTG_GlobalTypeDef *) USB_OTG_HS_PERIPH_BASE)

    1. 定义全局变量 USBD_HandleTypeDef hUsbDeviceHS;并默认全零初始化
    2. /* USB Device handle structure */
    3. typedef struct _USBD_HandleTypeDef
    4. {
    5. uint8_t id;
    6. uint32_t dev_config;
    7. uint32_t dev_default_config;
    8. uint32_t dev_config_status;
    9. USBD_SpeedTypeDef dev_speed;
    10. USBD_EndpointTypeDef ep_in[16];
    11. USBD_EndpointTypeDef ep_out[16];
    12. __IO uint32_t ep0_state;
    13. uint32_t ep0_data_len;
    14. __IO uint8_t dev_state;
    15. __IO uint8_t dev_old_state;
    16. uint8_t dev_address;
    17. uint8_t dev_connection_status;
    18. uint8_t dev_test_mode;
    19. uint32_t dev_remote_wakeup;
    20. uint8_t ConfIdx;
    21. USBD_SetupReqTypedef request;
    22. USBD_DescriptorsTypeDef *pDesc;
    23. USBD_ClassTypeDef *pClass[USBD_MAX_SUPPORTED_CLASS];
    24. void *pClassData;
    25. void *pClassDataCmsit[USBD_MAX_SUPPORTED_CLASS];
    26. void *pUserData[USBD_MAX_SUPPORTED_CLASS];
    27. void *pData;
    28. void *pBosDesc;
    29. void *pConfDesc;
    30. uint32_t classId;
    31. uint32_t NumClasses;
    32. #ifdef USE_USBD_COMPOSITE
    33. USBD_CompositeElementTypeDef tclasslist[USBD_MAX_SUPPORTED_CLASS];
    34. #endif /* USE_USBD_COMPOSITE */
    35. } USBD_HandleTypeDef;

    1. 定义全局变量USBD_DescriptorsTypeDef HS_Desc,并赋值初始化
    2. /** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
    3. * @brief Private variables.
    4. * @{
    5. */
    6. USBD_DescriptorsTypeDef HS_Desc =
    7. {
    8. USBD_HS_DeviceDescriptor
    9. , USBD_HS_LangIDStrDescriptor
    10. , USBD_HS_ManufacturerStrDescriptor
    11. , USBD_HS_ProductStrDescriptor
    12. , USBD_HS_SerialStrDescriptor
    13. , USBD_HS_ConfigStrDescriptor
    14. , USBD_HS_InterfaceStrDescriptor
    15. #if (USBD_LPM_ENABLED == 1)
    16. , USBD_HS_USR_BOSDescriptor
    17. #endif /* (USBD_LPM_ENABLED == 1) */
    18. };
    19. //=====================================================//
    20. //=====================================================//
    21. /* USB Device descriptors structure */
    22. typedef struct
    23. {
    24. uint8_t *(*GetDeviceDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    25. uint8_t *(*GetLangIDStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    26. uint8_t *(*GetManufacturerStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    27. uint8_t *(*GetProductStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    28. uint8_t *(*GetSerialStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    29. uint8_t *(*GetConfigurationStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    30. uint8_t *(*GetInterfaceStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    31. #if (USBD_CLASS_USER_STRING_DESC == 1)
    32. uint8_t *(*GetUserStrDescriptor)(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
    33. #endif /* USBD_CLASS_USER_STRING_DESC */
    34. #if ((USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1))
    35. uint8_t *(*GetBOSDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
    36. #endif /* (USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1) */
    37. } USBD_DescriptorsTypeDef;
    38. //=====================================================//
    39. //=====================================================//
    40. /**
    41. * @brief Return the device descriptor
    42. * @param speed : Current device speed
    43. * @param length : Pointer to data length variable
    44. * @retval Pointer to descriptor buffer
    45. */
    46. uint8_t * USBD_HS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    47. {
    48. UNUSED(speed);
    49. *length = sizeof(USBD_HS_DeviceDesc);
    50. return USBD_HS_DeviceDesc;
    51. }
    52. /**
    53. * @brief Return the LangID string descriptor
    54. * @param speed : Current device speed
    55. * @param length : Pointer to data length variable
    56. * @retval Pointer to descriptor buffer
    57. */
    58. uint8_t * USBD_HS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    59. {
    60. UNUSED(speed);
    61. *length = sizeof(USBD_LangIDDesc);
    62. return USBD_LangIDDesc;
    63. }
    64. /**
    65. * @brief Return the product string descriptor
    66. * @param speed : current device speed
    67. * @param length : pointer to data length variable
    68. * @retval pointer to descriptor buffer
    69. */
    70. uint8_t * USBD_HS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    71. {
    72. if(speed == 0)
    73. {
    74. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_HS, USBD_StrDesc, length);
    75. }
    76. else
    77. {
    78. USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_HS, USBD_StrDesc, length);
    79. }
    80. return USBD_StrDesc;
    81. }
    82. /**
    83. * @brief Return the manufacturer string descriptor
    84. * @param speed : Current device speed
    85. * @param length : Pointer to data length variable
    86. * @retval Pointer to descriptor buffer
    87. */
    88. uint8_t * USBD_HS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    89. {
    90. UNUSED(speed);
    91. USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
    92. return USBD_StrDesc;
    93. }
    94. /**
    95. * @brief Return the serial number string descriptor
    96. * @param speed : Current device speed
    97. * @param length : Pointer to data length variable
    98. * @retval Pointer to descriptor buffer
    99. */
    100. uint8_t * USBD_HS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    101. {
    102. UNUSED(speed);
    103. *length = USB_SIZ_STRING_SERIAL;
    104. /* Update the serial number string descriptor with the data from the unique
    105. * ID */
    106. Get_SerialNum();
    107. /* USER CODE BEGIN USBD_HS_SerialStrDescriptor */
    108. /* USER CODE END USBD_HS_SerialStrDescriptor */
    109. return (uint8_t *) USBD_StringSerial;
    110. }
    111. /**
    112. * @brief Return the configuration string descriptor
    113. * @param speed : Current device speed
    114. * @param length : Pointer to data length variable
    115. * @retval Pointer to descriptor buffer
    116. */
    117. uint8_t * USBD_HS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    118. {
    119. if(speed == USBD_SPEED_HIGH)
    120. {
    121. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_HS, USBD_StrDesc, length);
    122. }
    123. else
    124. {
    125. USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_HS, USBD_StrDesc, length);
    126. }
    127. return USBD_StrDesc;
    128. }
    129. /**
    130. * @brief Return the interface string descriptor
    131. * @param speed : Current device speed
    132. * @param length : Pointer to data length variable
    133. * @retval Pointer to descriptor buffer
    134. */
    135. uint8_t * USBD_HS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    136. {
    137. if(speed == 0)
    138. {
    139. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_HS, USBD_StrDesc, length);
    140. }
    141. else
    142. {
    143. USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_HS, USBD_StrDesc, length);
    144. }
    145. return USBD_StrDesc;
    146. }
    147. #if (USBD_LPM_ENABLED == 1)
    148. /**
    149. * @brief Return the BOS descriptor
    150. * @param speed : Current device speed
    151. * @param length : Pointer to data length variable
    152. * @retval Pointer to descriptor buffer
    153. */
    154. uint8_t * USBD_HS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
    155. {
    156. UNUSED(speed);
    157. *length = sizeof(USBD_HS_BOSDesc);
    158. return (uint8_t*)USBD_HS_BOSDesc;
    159. }
    160. #endif /* (USBD_LPM_ENABLED == 1) */

    1. 全局宏定义,区分高速和全速USB
    2. /* #define for FS and HS identification */
    3. #define DEVICE_FS 0
    4. #define DEVICE_HS 1

    /

  • 相关阅读:
    讲清楚两个简单的概念之进程、线程
    ant-design-vue 实现表格表头纵排列
    通过字符设备驱动的分步实现编写LED驱动,另外实现特备文件和设备的绑定
    基于UWB技术高精度人员定位系统源码
    Nginx学习笔记11——防盗链与http的referer
    2023年“走红军走过的路”徒步穿越系列活动(大通湖站)盛大开赛!
    CH34X-MPHSI高速Master扩展应用—I2C设备调试
    maven compile无效的标记: --release
    使用树莓派学习PostgreSQL
    MySQL 主从复制与读写分离
  • 原文地址:https://blog.csdn.net/zlf19910726/article/details/134458180