Subversion Repositories dualCDC

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    usbd_desc_template.c
  4.   * @author  MCD Application Team
  5.   * @brief   This file provides the USBD descriptors and string formatting method.
  6.   *          This template should be copied to the user folder,
  7.   *          renamed and customized following user needs.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
  12.   * All rights reserved.</center></h2>
  13.   *
  14.   * This software component is licensed by ST under Ultimate Liberty license
  15.   * SLA0044, the "License"; You may not use this file except in compliance with
  16.   * the License. You may obtain a copy of the License at:
  17.   *                      www.st.com/SLA0044
  18.   *
  19.   ******************************************************************************
  20.   */
  21.  
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "usbd_core.h"
  24. #include "usbd_desc.h"
  25. #include "usbd_conf.h"
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* Private define ------------------------------------------------------------*/
  29. #define USBD_VID                      0x0483
  30. #define USBD_PID                      0xaaaa  /* Replace '0xaaaa' with your device product ID */
  31. #define USBD_LANGID_STRING            0xbbb  /* Replace '0xbbb' with your device language ID */
  32. #define USBD_MANUFACTURER_STRING      "xxxxx" /* Add your manufacturer string */
  33. #define USBD_PRODUCT_HS_STRING        "xxxxx" /* Add your product High Speed string */
  34. #define USBD_PRODUCT_FS_STRING        "xxxxx" /* Add your product Full Speed string */
  35. #define USBD_CONFIGURATION_HS_STRING  "xxxxx" /* Add your configuration High Speed string */
  36. #define USBD_INTERFACE_HS_STRING      "xxxxx" /* Add your Interface High Speed string */
  37. #define USBD_CONFIGURATION_FS_STRING  "xxxxx" /* Add your configuration Full Speed string */
  38. #define USBD_INTERFACE_FS_STRING      "xxxxx" /* Add your Interface Full Speed string */
  39.  
  40. /* Private macro -------------------------------------------------------------*/
  41. /* Private function prototypes -----------------------------------------------*/
  42. uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  43. uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  44. uint8_t *USBD_Class_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  45. uint8_t *USBD_Class_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  46. uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  47. uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  48. uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
  49. #ifdef USBD_SUPPORT_USER_STRING_DESC
  50. uint8_t *USBD_Class_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
  51. #endif /* USBD_SUPPORT_USER_STRING_DESC */
  52.  
  53. /* Private variables ---------------------------------------------------------*/
  54. USBD_DescriptorsTypeDef Class_Desc =
  55. {
  56.   USBD_Class_DeviceDescriptor,
  57.   USBD_Class_LangIDStrDescriptor,
  58.   USBD_Class_ManufacturerStrDescriptor,
  59.   USBD_Class_ProductStrDescriptor,
  60.   USBD_Class_SerialStrDescriptor,
  61.   USBD_Class_ConfigStrDescriptor,
  62.   USBD_Class_InterfaceStrDescriptor,
  63. };
  64.  
  65. /* USB Standard Device Descriptor */
  66. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  67. #pragma data_alignment=4
  68. #endif
  69. __ALIGN_BEGIN   uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  70. {
  71.   0x12,                       /* bLength */
  72.   USB_DESC_TYPE_DEVICE,       /* bDescriptorType */
  73.   0x00,                       /* bcdUSB */
  74.   0x02,
  75.   0x00,                       /* bDeviceClass */
  76.   0x00,                       /* bDeviceSubClass */
  77.   0x00,                       /* bDeviceProtocol */
  78.   USB_MAX_EP0_SIZE,           /* bMaxPacketSize */
  79.   LOBYTE(USBD_VID),           /* idVendor */
  80.   HIBYTE(USBD_VID),           /* idVendor */
  81.   LOBYTE(USBD_PID),           /* idVendor */
  82.   HIBYTE(USBD_PID),           /* idVendor */
  83.   0x00,                       /* bcdDevice rel. 2.00 */
  84.   0x02,
  85.   USBD_IDX_MFC_STR,           /* Index of manufacturer string */
  86.   USBD_IDX_PRODUCT_STR,       /* Index of product string */
  87.   USBD_IDX_SERIAL_STR,        /* Index of serial number string */
  88.   USBD_MAX_NUM_CONFIGURATION  /* bNumConfigurations */
  89. }; /* USB_DeviceDescriptor */
  90.  
  91. /* USB Standard Device Descriptor */
  92. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  93. #pragma data_alignment=4
  94. #endif
  95. __ALIGN_BEGIN   uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
  96. {
  97.   USB_LEN_LANGID_STR_DESC,
  98.   USB_DESC_TYPE_STRING,
  99.   LOBYTE(USBD_LANGID_STRING),
  100.   HIBYTE(USBD_LANGID_STRING),
  101. };
  102.  
  103. uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] =
  104. {
  105.   USB_SIZ_STRING_SERIAL,
  106.   USB_DESC_TYPE_STRING,
  107. };
  108.  
  109. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  110. #pragma data_alignment=4
  111. #endif
  112. __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
  113.  
  114. /* Private functions ---------------------------------------------------------*/
  115. static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len);
  116. static void Get_SerialNum(void);
  117.  
  118. /**
  119.   * @brief  Returns the device descriptor.
  120.   * @param  speed: Current device speed
  121.   * @param  length: Pointer to data length variable
  122.   * @retval Pointer to descriptor buffer
  123.   */
  124. uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  125. {
  126.   *length = sizeof(USBD_DeviceDesc);
  127.   return (uint8_t *)USBD_DeviceDesc;
  128. }
  129.  
  130. /**
  131.   * @brief  Returns the LangID string descriptor.
  132.   * @param  speed: Current device speed
  133.   * @param  length: Pointer to data length variable
  134.   * @retval Pointer to descriptor buffer
  135.   */
  136. uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  137. {
  138.   *length = sizeof(USBD_LangIDDesc);
  139.   return (uint8_t *)USBD_LangIDDesc;
  140. }
  141.  
  142. /**
  143.   * @brief  Returns the product string descriptor.
  144.   * @param  speed: Current device speed
  145.   * @param  length: Pointer to data length variable
  146.   * @retval Pointer to descriptor buffer
  147.   */
  148. uint8_t *USBD_Class_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  149. {
  150.   if (speed == USBD_SPEED_HIGH)
  151.   {
  152.     USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
  153.   }
  154.   else
  155.   {
  156.     USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
  157.   }
  158.   return USBD_StrDesc;
  159. }
  160.  
  161. /**
  162.   * @brief  Returns the manufacturer string descriptor.
  163.   * @param  speed: Current device speed
  164.   * @param  length: Pointer to data length variable
  165.   * @retval Pointer to descriptor buffer
  166.   */
  167. uint8_t *USBD_Class_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  168. {
  169.   USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
  170.   return USBD_StrDesc;
  171. }
  172.  
  173. /**
  174.   * @brief  Returns the serial number string descriptor.
  175.   * @param  speed: Current device speed
  176.   * @param  length: Pointer to data length variable
  177.   * @retval Pointer to descriptor buffer
  178.   */
  179. uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  180. {
  181.   *length = USB_SIZ_STRING_SERIAL;
  182.  
  183.   /* Update the serial number string descriptor with the data from the unique ID*/
  184.   Get_SerialNum();
  185.  
  186.   return (uint8_t *)USBD_StringSerial;
  187. }
  188.  
  189. /**
  190.   * @brief  Returns the configuration string descriptor.
  191.   * @param  speed: Current device speed
  192.   * @param  length: Pointer to data length variable
  193.   * @retval Pointer to descriptor buffer
  194.   */
  195. uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  196. {
  197.   if (speed == USBD_SPEED_HIGH)
  198.   {
  199.     USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
  200.   }
  201.   else
  202.   {
  203.     USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
  204.   }
  205.   return USBD_StrDesc;
  206. }
  207.  
  208. /**
  209.   * @brief  Returns the interface string descriptor.
  210.   * @param  speed: Current device speed
  211.   * @param  length: Pointer to data length variable
  212.   * @retval Pointer to descriptor buffer
  213.   */
  214. uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
  215. {
  216.   if (speed == USBD_SPEED_HIGH)
  217.   {
  218.     USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
  219.   }
  220.   else
  221.   {
  222.     USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
  223.   }
  224.   return USBD_StrDesc;
  225. }
  226.  
  227. /**
  228.   * @brief  Create the serial number string descriptor
  229.   * @param  None
  230.   * @retval None
  231.   */
  232. static void Get_SerialNum(void)
  233. {
  234.   uint32_t deviceserial0, deviceserial1, deviceserial2;
  235.  
  236.   deviceserial0 = *(uint32_t *)DEVICE_ID1;
  237.   deviceserial1 = *(uint32_t *)DEVICE_ID2;
  238.   deviceserial2 = *(uint32_t *)DEVICE_ID3;
  239.  
  240.   deviceserial0 += deviceserial2;
  241.  
  242.   if (deviceserial0 != 0U)
  243.   {
  244.     IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8U);
  245.     IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4U);
  246.   }
  247. }
  248.  
  249. /**
  250.   * @brief  Convert Hex 32Bits value into char
  251.   * @param  value: value to convert
  252.   * @param  pbuf: pointer to the buffer
  253.   * @param  len: buffer length
  254.   * @retval None
  255.   */
  256. static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len)
  257. {
  258.   uint8_t idx = 0U;
  259.  
  260.   for (idx = 0U ; idx < len ; idx ++)
  261.   {
  262.     if (((value >> 28)) < 0xAU)
  263.     {
  264.       pbuf[ 2U * idx] = (value >> 28) + '0';
  265.     }
  266.     else
  267.     {
  268.       pbuf[2U * idx] = (value >> 28) + 'A' - 10U;
  269.     }
  270.  
  271.     value = value << 4;
  272.  
  273.     pbuf[2U * idx + 1] = 0U;
  274.   }
  275. }
  276.  
  277. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  278.