usbd_desc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. ******************************************************************************
  3. * @file : usbd_desc.c
  4. * @version : v1.0_Cube
  5. * @brief : This file implements the USB Device descriptors
  6. ******************************************************************************
  7. *
  8. * COPYRIGHT(c) 2016 STMicroelectronics
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "usbd_core.h"
  36. #include "usbd_desc.h"
  37. #include "usbd_conf.h"
  38. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  39. * @{
  40. */
  41. /** @defgroup USBD_DESC
  42. * @brief USBD descriptors module
  43. * @{
  44. */
  45. /** @defgroup USBD_DESC_Private_TypesDefinitions
  46. * @{
  47. */
  48. /**
  49. * @}
  50. */
  51. /** @defgroup USBD_DESC_Private_Defines
  52. * @{
  53. */
  54. #define USBD_VID 0x16D0
  55. #define USBD_LANGID_STRING 1033
  56. #define USBD_MANUFACTURER_STRING (uint8_t*)"codesrc.com"
  57. #define USBD_PID_FS 0x0BD4
  58. #define USBD_PRODUCT_STRING_FS (uint8_t*)"SCSI2SD 2020"
  59. #define USBD_CONFIGURATION_STRING_FS (uint8_t*)"SCSI2SD Config"
  60. #define USBD_INTERFACE_STRING_FS (uint8_t*)"SCSI2SD Interface"
  61. #define USB_SIZ_BOS_DESC 0x0C
  62. /**
  63. * @}
  64. */
  65. /** @defgroup USBD_DESC_Private_Macros
  66. * @{
  67. */
  68. static void Get_SerialNum(void);
  69. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
  70. /**
  71. * @}
  72. */
  73. /** @defgroup USBD_DESC_Private_Variables
  74. * @{
  75. */
  76. uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
  77. uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
  78. uint8_t * USBD_FS_ManufacturerStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length);
  79. uint8_t * USBD_FS_ProductStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length);
  80. uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
  81. uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
  82. uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length);
  83. #if (USBD_LPM_ENABLED == 1)
  84. uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  85. #endif /* (USBD_LPM_ENABLED == 1) */
  86. USBD_DescriptorsTypeDef FS_Desc =
  87. {
  88. USBD_FS_DeviceDescriptor,
  89. USBD_FS_LangIDStrDescriptor,
  90. USBD_FS_ManufacturerStrDescriptor,
  91. USBD_FS_ProductStrDescriptor,
  92. USBD_FS_SerialStrDescriptor,
  93. USBD_FS_ConfigStrDescriptor,
  94. USBD_FS_InterfaceStrDescriptor,
  95. #if (USBD_LPM_ENABLED == 1)
  96. USBD_FS_USR_BOSDescriptor
  97. #endif /* (USBD_LPM_ENABLED == 1) */
  98. };
  99. USBD_DescriptorsTypeDef HS_Desc =
  100. {
  101. USBD_FS_DeviceDescriptor,
  102. USBD_FS_LangIDStrDescriptor,
  103. USBD_FS_ManufacturerStrDescriptor,
  104. USBD_FS_ProductStrDescriptor,
  105. USBD_FS_SerialStrDescriptor,
  106. USBD_FS_ConfigStrDescriptor,
  107. USBD_FS_InterfaceStrDescriptor,
  108. #if (USBD_LPM_ENABLED == 1)
  109. USBD_FS_USR_BOSDescriptor
  110. #endif /* (USBD_LPM_ENABLED == 1) */
  111. };
  112. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  113. #pragma data_alignment=4
  114. #endif
  115. /* USB Standard Device Descriptor */
  116. __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  117. {
  118. 0x12, /*bLength */
  119. USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
  120. #if (USBD_LPM_ENABLED == 1)
  121. 0x01, /*bcdUSB */ /* changed to USB version 2.01
  122. in order to support LPM L1 suspend
  123. resume test of USBCV3.0*/
  124. #else
  125. 0x00, /*bcdUSB */
  126. #endif /* (USBD_LPM_ENABLED == 1) */
  127. 0x02,
  128. 0x00, /*bDeviceClass*/
  129. 0x00, /*bDeviceSubClass*/
  130. 0x00, /*bDeviceProtocol*/
  131. USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
  132. LOBYTE(USBD_VID), /*idVendor*/
  133. HIBYTE(USBD_VID), /*idVendor*/
  134. LOBYTE(USBD_PID_FS), /*idVendor*/
  135. HIBYTE(USBD_PID_FS), /*idVendor*/
  136. 0x00, /*bcdDevice rel. 2.00*/
  137. 0x02,
  138. USBD_IDX_MFC_STR, /*Index of manufacturer string*/
  139. USBD_IDX_PRODUCT_STR, /*Index of product string*/
  140. USBD_IDX_SERIAL_STR, /*Index of serial number string*/
  141. USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
  142. } ;
  143. /* USB_DeviceDescriptor */
  144. /** BOS descriptor. */
  145. #if (USBD_LPM_ENABLED == 1)
  146. __ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END =
  147. {
  148. 0x5,
  149. USB_DESC_TYPE_BOS,
  150. 0xC,
  151. 0x0,
  152. 0x1, /* 1 device capability*/
  153. /* device capability*/
  154. 0x7,
  155. USB_DEVICE_CAPABITY_TYPE,
  156. 0x2,
  157. 0x2, /* LPM capability bit set*/
  158. 0x0,
  159. 0x0,
  160. 0x0
  161. };
  162. #endif /* (USBD_LPM_ENABLED == 1) */
  163. /* USB Standard Device Descriptor */
  164. __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  165. {
  166. USB_LEN_LANGID_STR_DESC,
  167. USB_DESC_TYPE_STRING,
  168. LOBYTE(USBD_LANGID_STRING),
  169. HIBYTE(USBD_LANGID_STRING),
  170. };
  171. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  172. #pragma data_alignment=4
  173. #endif
  174. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  175. /**
  176. * @}
  177. */
  178. /** @defgroup USBD_DESC_Private_FunctionPrototypes
  179. * @{
  180. */
  181. #define USB_SIZ_STRING_SERIAL 0x1A
  182. __ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
  183. USB_SIZ_STRING_SERIAL,
  184. USB_DESC_TYPE_STRING,
  185. };
  186. /**
  187. * @}
  188. */
  189. /** @defgroup USBD_DESC_Private_Functions
  190. * @{
  191. */
  192. /**
  193. * @brief USBD_FS_DeviceDescriptor
  194. * return the device descriptor
  195. * @param speed : current device speed
  196. * @param length : pointer to data length variable
  197. * @retval pointer to descriptor buffer
  198. */
  199. uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
  200. {
  201. *length = sizeof(USBD_FS_DeviceDesc);
  202. return USBD_FS_DeviceDesc;
  203. }
  204. /**
  205. * @brief USBD_FS_LangIDStrDescriptor
  206. * return the LangID string descriptor
  207. * @param speed : current device speed
  208. * @param length : pointer to data length variable
  209. * @retval pointer to descriptor buffer
  210. */
  211. uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
  212. {
  213. *length = sizeof(USBD_LangIDDesc);
  214. return USBD_LangIDDesc;
  215. }
  216. /**
  217. * @brief USBD_FS_ProductStrDescriptor
  218. * return the product string descriptor
  219. * @param speed : current device speed
  220. * @param length : pointer to data length variable
  221. * @retval pointer to descriptor buffer
  222. */
  223. uint8_t * USBD_FS_ProductStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
  224. {
  225. if(speed == 0)
  226. {
  227. USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  228. }
  229. else
  230. {
  231. USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
  232. }
  233. return USBD_StrDesc;
  234. }
  235. /**
  236. * @brief USBD_FS_ManufacturerStrDescriptor
  237. * return the manufacturer string descriptor
  238. * @param speed : current device speed
  239. * @param length : pointer to data length variable
  240. * @retval pointer to descriptor buffer
  241. */
  242. uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
  243. {
  244. USBD_GetString (USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  245. return USBD_StrDesc;
  246. }
  247. /**
  248. * @brief USBD_FS_SerialStrDescriptor
  249. * return the serial number string descriptor
  250. * @param speed : current device speed
  251. * @param length : pointer to data length variable
  252. * @retval pointer to descriptor buffer
  253. */
  254. uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
  255. {
  256. *length = USB_SIZ_STRING_SERIAL;
  257. // Update the serial number string descriptor with the data from the unique
  258. // ID
  259. Get_SerialNum();
  260. return (uint8_t *) USBD_StringSerial;
  261. }
  262. /**
  263. * @brief Create the serial number string descriptor
  264. * @param None
  265. * @retval None
  266. */
  267. static void Get_SerialNum(void)
  268. {
  269. uint32_t deviceserial0, deviceserial1, deviceserial2;
  270. // UID_BASE good for STM32F2 and F4
  271. #ifndef UID_BASE
  272. #define UID_BASE 0x1FFF7A10
  273. #endif
  274. #define DEVICE_ID1 (UID_BASE)
  275. #define DEVICE_ID2 (UID_BASE + 0x4)
  276. #define DEVICE_ID3 (UID_BASE + 0x8)
  277. deviceserial0 = *(uint32_t *) DEVICE_ID1;
  278. deviceserial1 = *(uint32_t *) DEVICE_ID2;
  279. deviceserial2 = *(uint32_t *) DEVICE_ID3;
  280. deviceserial0 += deviceserial2;
  281. if (deviceserial0 != 0)
  282. {
  283. IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
  284. IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
  285. }
  286. }
  287. /**
  288. * @brief Convert Hex 32Bits value into char
  289. * @param value: value to convert
  290. * @param pbuf: pointer to the buffer
  291. * @param len: buffer length
  292. * @retval None
  293. */
  294. static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
  295. {
  296. uint8_t idx = 0;
  297. for (idx = 0; idx < len; idx++)
  298. {
  299. if (((value >> 28)) < 0xA)
  300. {
  301. pbuf[2 * idx] = (value >> 28) + '0';
  302. }
  303. else
  304. {
  305. pbuf[2 * idx] = (value >> 28) + 'A' - 10;
  306. }
  307. value = value << 4;
  308. pbuf[2 * idx + 1] = 0;
  309. }
  310. }
  311. /**
  312. * @brief USBD_FS_ConfigStrDescriptor
  313. * return the configuration string descriptor
  314. * @param speed : current device speed
  315. * @param length : pointer to data length variable
  316. * @retval pointer to descriptor buffer
  317. */
  318. uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
  319. {
  320. if(speed == USBD_SPEED_HIGH)
  321. {
  322. USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  323. }
  324. else
  325. {
  326. USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length);
  327. }
  328. return USBD_StrDesc;
  329. }
  330. /**
  331. * @brief USBD_FS_InterfaceStrDescriptor
  332. * return the interface string descriptor
  333. * @param speed : current device speed
  334. * @param length : pointer to data length variable
  335. * @retval pointer to descriptor buffer
  336. */
  337. uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length)
  338. {
  339. if(speed == 0)
  340. {
  341. USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  342. }
  343. else
  344. {
  345. USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length);
  346. }
  347. return USBD_StrDesc;
  348. }
  349. #if (USBD_LPM_ENABLED == 1)
  350. /**
  351. * @brief Return the BOS descriptor
  352. * @param speed : Current device speed
  353. * @param length : Pointer to data length variable
  354. * @retval Pointer to descriptor buffer
  355. */
  356. uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  357. {
  358. UNUSED(speed);
  359. *length = sizeof(USBD_FS_BOSDesc);
  360. return (uint8_t*)USBD_FS_BOSDesc;
  361. }
  362. #endif /* (USBD_LPM_ENABLED == 1) */
  363. /**
  364. * @}
  365. */
  366. /**
  367. * @}
  368. */
  369. /**
  370. * @}
  371. */
  372. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/