Kleber Silva / IOTON-API

Dependents:   ton_demo ton_template

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbd_desc.c Source File

usbd_desc.c

00001 /**
00002   ******************************************************************************
00003   * @file    umart_lite_plus_teste/src/usbd_desc.c
00004   * @author  MCD Application Team
00005   * @version V1.2.0
00006   * @date    26-December-2014
00007   * @brief   This file provides the USBD descriptors and string formating method.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software
00020   * distributed under the License is distributed on an "AS IS" BASIS,
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "usbd_core.h"
00030 #include "usbd_desc.h"
00031 #include "usbd_conf.h"
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 #define USBD_VID                      0x0483
00036 #define USBD_PID                      0x5740
00037 #define USBD_LANGID_STRING            0x409
00038 #define USBD_MANUFACTURER_STRING      "STMicroelectronics"
00039 #define USBD_PRODUCT_HS_STRING        "STM32 Virtual ComPort in HS Mode"
00040 #define USBD_PRODUCT_FS_STRING        "STM32 Virtual ComPort in FS Mode"
00041 #define USBD_CONFIGURATION_HS_STRING  "VCP Config"
00042 #define USBD_INTERFACE_HS_STRING      "VCP Interface"
00043 #define USBD_CONFIGURATION_FS_STRING  "VCP Config"
00044 #define USBD_INTERFACE_FS_STRING      "VCP Interface"
00045 
00046 /* Private macro -------------------------------------------------------------*/
00047 /* Private function prototypes -----------------------------------------------*/
00048 uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
00049 uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
00050 uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
00051 uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
00052 uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
00053 uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
00054 uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
00055 #ifdef USB_SUPPORT_USER_STRING_DESC
00056 uint8_t *USBD_VCP_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
00057 #endif /* USB_SUPPORT_USER_STRING_DESC */
00058 
00059 /* Private variables ---------------------------------------------------------*/
00060 USBD_DescriptorsTypeDef VCP_Desc = {
00061   USBD_VCP_DeviceDescriptor,
00062   USBD_VCP_LangIDStrDescriptor,
00063   USBD_VCP_ManufacturerStrDescriptor,
00064   USBD_VCP_ProductStrDescriptor,
00065   USBD_VCP_SerialStrDescriptor,
00066   USBD_VCP_ConfigStrDescriptor,
00067   USBD_VCP_InterfaceStrDescriptor,
00068 };
00069 
00070 /* USB Standard Device Descriptor */
00071 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
00072   #pragma data_alignment=4
00073 #endif
00074 __ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
00075   0x12,                       /* bLength */
00076   USB_DESC_TYPE_DEVICE,       /* bDescriptorType */
00077   0x00,                       /* bcdUSB */
00078   0x02,
00079   0x00,                       /* bDeviceClass */
00080   0x00,                       /* bDeviceSubClass */
00081   0x00,                       /* bDeviceProtocol */
00082   USB_MAX_EP0_SIZE,           /* bMaxPacketSize */
00083   LOBYTE(USBD_VID),           /* idVendor */
00084   HIBYTE(USBD_VID),           /* idVendor */
00085   LOBYTE(USBD_PID),           /* idVendor */
00086   HIBYTE(USBD_PID),           /* idVendor */
00087   0x00,                       /* bcdDevice rel. 2.00 */
00088   0x02,
00089   USBD_IDX_MFC_STR,           /* Index of manufacturer string */
00090   USBD_IDX_PRODUCT_STR,       /* Index of product string */
00091   USBD_IDX_SERIAL_STR,        /* Index of serial number string */
00092   USBD_MAX_NUM_CONFIGURATION  /* bNumConfigurations */
00093 }; /* USB_DeviceDescriptor */
00094 
00095 /* USB Standard Device Descriptor */
00096 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
00097   #pragma data_alignment=4
00098 #endif
00099 __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = {
00100   USB_LEN_LANGID_STR_DESC,
00101   USB_DESC_TYPE_STRING,
00102   LOBYTE(USBD_LANGID_STRING),
00103   HIBYTE(USBD_LANGID_STRING),
00104 };
00105 
00106 uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] =
00107 {
00108   USB_SIZ_STRING_SERIAL,
00109   USB_DESC_TYPE_STRING,
00110 };
00111 
00112 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
00113   #pragma data_alignment=4
00114 #endif
00115 __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
00116 
00117 /* Private functions ---------------------------------------------------------*/
00118 static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
00119 static void Get_SerialNum(void);
00120 
00121 /**
00122   * @brief  Returns the device descriptor.
00123   * @param  speed: Current device speed
00124   * @param  length: Pointer to data length variable
00125   * @retval Pointer to descriptor buffer
00126   */
00127 uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
00128 {
00129   *length = sizeof(USBD_DeviceDesc);
00130   return (uint8_t*)USBD_DeviceDesc;
00131 }
00132 
00133 /**
00134   * @brief  Returns the LangID string descriptor.
00135   * @param  speed: Current device speed
00136   * @param  length: Pointer to data length variable
00137   * @retval Pointer to descriptor buffer
00138   */
00139 uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
00140 {
00141   *length = sizeof(USBD_LangIDDesc);
00142   return (uint8_t*)USBD_LangIDDesc;
00143 }
00144 
00145 /**
00146   * @brief  Returns the product string descriptor.
00147   * @param  speed: Current device speed
00148   * @param  length: Pointer to data length variable
00149   * @retval Pointer to descriptor buffer
00150   */
00151 uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
00152 {
00153   if(speed == 0)
00154   {
00155     USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length);
00156   }
00157   else
00158   {
00159     USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length);
00160   }
00161   return USBD_StrDesc;
00162 }
00163 
00164 /**
00165   * @brief  Returns the manufacturer string descriptor.
00166   * @param  speed: Current device speed
00167   * @param  length: Pointer to data length variable
00168   * @retval Pointer to descriptor buffer
00169   */
00170 uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
00171 {
00172   USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
00173   return USBD_StrDesc;
00174 }
00175 
00176 /**
00177   * @brief  Returns the serial number string descriptor.
00178   * @param  speed: Current device speed
00179   * @param  length: Pointer to data length variable
00180   * @retval Pointer to descriptor buffer
00181   */
00182 uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
00183 {
00184   *length = USB_SIZ_STRING_SERIAL;
00185 
00186   /* Update the serial number string descriptor with the data from the unique ID*/
00187   Get_SerialNum();
00188 
00189   return (uint8_t*)USBD_StringSerial;
00190 }
00191 
00192 /**
00193   * @brief  Returns the configuration string descriptor.
00194   * @param  speed: Current device speed
00195   * @param  length: Pointer to data length variable
00196   * @retval Pointer to descriptor buffer
00197   */
00198 uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
00199 {
00200   if(speed == USBD_SPEED_HIGH)
00201   {
00202     USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length);
00203   }
00204   else
00205   {
00206     USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
00207   }
00208   return USBD_StrDesc;
00209 }
00210 
00211 /**
00212   * @brief  Returns the interface string descriptor.
00213   * @param  speed: Current device speed
00214   * @param  length: Pointer to data length variable
00215   * @retval Pointer to descriptor buffer
00216   */
00217 uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
00218 {
00219   if(speed == 0)
00220   {
00221     USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length);
00222   }
00223   else
00224   {
00225     USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length);
00226   }
00227   return USBD_StrDesc;
00228 }
00229 
00230 /**
00231   * @brief  Create the serial number string descriptor
00232   * @param  None
00233   * @retval None
00234   */
00235 static void Get_SerialNum(void)
00236 {
00237   uint32_t deviceserial0, deviceserial1, deviceserial2;
00238 
00239   deviceserial0 = *(uint32_t*)DEVICE_ID1;
00240   deviceserial1 = *(uint32_t*)DEVICE_ID2;
00241   deviceserial2 = *(uint32_t*)DEVICE_ID3;
00242 
00243   deviceserial0 += deviceserial2;
00244 
00245   if (deviceserial0 != 0)
00246   {
00247     IntToUnicode (deviceserial0, &USBD_StringSerial[2] ,8);
00248     IntToUnicode (deviceserial1, &USBD_StringSerial[18] ,4);
00249   }
00250 }
00251 
00252 /**
00253   * @brief  Convert Hex 32Bits value into char
00254   * @param  value: value to convert
00255   * @param  pbuf: pointer to the buffer
00256   * @param  len: buffer length
00257   * @retval None
00258   */
00259 static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
00260 {
00261   uint8_t idx = 0;
00262 
00263   for( idx = 0; idx < len; idx ++)
00264   {
00265     if( ((value >> 28)) < 0xA )
00266     {
00267       pbuf[ 2* idx] = (value >> 28) + '0';
00268     }
00269     else
00270     {
00271       pbuf[2* idx] = (value >> 28) + 'A' - 10;
00272     }
00273 
00274     value = value << 4;
00275 
00276     pbuf[ 2* idx + 1] = 0;
00277   }
00278 }
00279 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/