Sergey Pastor / grbl1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usb_prop.c Source File

usb_prop.c

00001 /******************** (C) COPYRIGHT 2011 STMicroelectronics ********************
00002 * File Name          : usb_prop.c
00003 * Author             : MCD Application Team
00004 * Version            : V3.3.0
00005 * Date               : 21-March-2011
00006 * Description        : All processing related to Virtual Com Port Demo
00007 ********************************************************************************
00008 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00009 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
00010 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
00011 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
00012 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
00013 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00014 *******************************************************************************/
00015 
00016 /* Includes ------------------------------------------------------------------*/
00017 #include "usb_lib.h"
00018 #include "usb_conf.h"
00019 #include "usb_prop.h"
00020 #include "usb_desc.h"
00021 #include "usb_pwr.h"
00022 #include "hw_config.h"
00023 
00024 /* Private typedef -----------------------------------------------------------*/
00025 /* Private define ------------------------------------------------------------*/
00026 /* Private macro -------------------------------------------------------------*/
00027 /* Private variables ---------------------------------------------------------*/
00028 uint8_t Request = 0;
00029 
00030 LINE_CODING linecoding =
00031   {
00032     115200, /* baud rate*/
00033     0x00,   /* stop bits-1*/
00034     0x00,   /* parity - none*/
00035     0x08    /* no. of bits 8*/
00036   };
00037 
00038 /* -------------------------------------------------------------------------- */
00039 /*  Structures initializations */
00040 /* -------------------------------------------------------------------------- */
00041 
00042 DEVICE Device_Table =
00043   {
00044     EP_NUM,
00045     1
00046   };
00047 
00048 DEVICE_PROP Device_Property =
00049   {
00050     Virtual_Com_Port_init,
00051     Virtual_Com_Port_Reset,
00052     Virtual_Com_Port_Status_In,
00053     Virtual_Com_Port_Status_Out,
00054     Virtual_Com_Port_Data_Setup,
00055     Virtual_Com_Port_NoData_Setup,
00056     Virtual_Com_Port_Get_Interface_Setting,
00057     Virtual_Com_Port_GetDeviceDescriptor,
00058     Virtual_Com_Port_GetConfigDescriptor,
00059     Virtual_Com_Port_GetStringDescriptor,
00060     0,
00061     0x40 /*MAX PACKET SIZE*/
00062   };
00063 
00064 USER_STANDARD_REQUESTS User_Standard_Requests =
00065   {
00066     Virtual_Com_Port_GetConfiguration,
00067     Virtual_Com_Port_SetConfiguration,
00068     Virtual_Com_Port_GetInterface,
00069     Virtual_Com_Port_SetInterface,
00070     Virtual_Com_Port_GetStatus,
00071     Virtual_Com_Port_ClearFeature,
00072     Virtual_Com_Port_SetEndPointFeature,
00073     Virtual_Com_Port_SetDeviceFeature,
00074     Virtual_Com_Port_SetDeviceAddress
00075   };
00076 
00077 ONE_DESCRIPTOR Device_Descriptor =
00078   {
00079     (uint8_t*)Virtual_Com_Port_DeviceDescriptor,
00080     VIRTUAL_COM_PORT_SIZ_DEVICE_DESC
00081   };
00082 
00083 ONE_DESCRIPTOR Config_Descriptor =
00084   {
00085     (uint8_t*)Virtual_Com_Port_ConfigDescriptor,
00086     VIRTUAL_COM_PORT_SIZ_CONFIG_DESC
00087   };
00088 
00089 ONE_DESCRIPTOR String_Descriptor[4] =
00090   {
00091     {(uint8_t*)Virtual_Com_Port_StringLangID, VIRTUAL_COM_PORT_SIZ_STRING_LANGID},
00092     {(uint8_t*)Virtual_Com_Port_StringVendor, VIRTUAL_COM_PORT_SIZ_STRING_VENDOR},
00093     {(uint8_t*)Virtual_Com_Port_StringProduct, VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT},
00094     {(uint8_t*)Virtual_Com_Port_StringSerial, VIRTUAL_COM_PORT_SIZ_STRING_SERIAL}
00095   };
00096 
00097 /* Extern variables ----------------------------------------------------------*/
00098 /* Private function prototypes -----------------------------------------------*/
00099 /* Extern function prototypes ------------------------------------------------*/
00100 /* Private functions ---------------------------------------------------------*/
00101 /*******************************************************************************
00102 * Function Name  : Virtual_Com_Port_init.
00103 * Description    : Virtual COM Port Mouse init routine.
00104 * Input          : None.
00105 * Output         : None.
00106 * Return         : None.
00107 *******************************************************************************/
00108 void Virtual_Com_Port_init(void)
00109 {
00110 
00111   /* Update the serial number string descriptor with the data from the unique
00112   ID*/
00113   Get_SerialNum();
00114 
00115   pInformation->Current_Configuration = 0;
00116 
00117   /* Connect the device */
00118   PowerOn();
00119 
00120   /* Perform basic device initialization operations */
00121   USB_SIL_Init();
00122 
00123   ///* configure the USART to the default settings */
00124   //USART_Config_Default();
00125 
00126   bDeviceState = UNCONNECTED;
00127 }
00128 
00129 /*******************************************************************************
00130 * Function Name  : Virtual_Com_Port_Reset
00131 * Description    : Virtual_Com_Port Mouse reset routine
00132 * Input          : None.
00133 * Output         : None.
00134 * Return         : None.
00135 *******************************************************************************/
00136 void Virtual_Com_Port_Reset(void)
00137 {
00138   /* Set Virtual_Com_Port DEVICE as not configured */
00139   pInformation->Current_Configuration = 0;
00140 
00141   /* Current Feature initialization */
00142   pInformation->Current_Feature = Virtual_Com_Port_ConfigDescriptor[7];
00143 
00144   /* Set Virtual_Com_Port DEVICE with the default Interface*/
00145   pInformation->Current_Interface = 0;
00146 
00147 #ifdef STM32F10X_CL     
00148   /* EP0 is already configured by USB_SIL_Init() function */
00149   
00150   /* Init EP1 IN as Bulk endpoint */
00151   OTG_DEV_EP_Init(EP1_IN, OTG_DEV_EP_TYPE_BULK, VIRTUAL_COM_PORT_DATA_SIZE);
00152   
00153   /* Init EP2 IN as Interrupt endpoint */
00154   OTG_DEV_EP_Init(EP2_IN, OTG_DEV_EP_TYPE_INT, VIRTUAL_COM_PORT_INT_SIZE);
00155 
00156   /* Init EP3 OUT as Bulk endpoint */
00157   OTG_DEV_EP_Init(EP3_OUT, OTG_DEV_EP_TYPE_BULK, VIRTUAL_COM_PORT_DATA_SIZE);  
00158 #else 
00159 
00160   SetBTABLE(BTABLE_ADDRESS);
00161 
00162   /* Initialize Endpoint 0 */
00163   SetEPType(ENDP0, EP_CONTROL);
00164   SetEPTxStatus(ENDP0, EP_TX_STALL);
00165   SetEPRxAddr(ENDP0, ENDP0_RXADDR);
00166   SetEPTxAddr(ENDP0, ENDP0_TXADDR);
00167   Clear_Status_Out(ENDP0);
00168   SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
00169   SetEPRxValid(ENDP0);
00170 
00171   /* Initialize Endpoint 1 */
00172   SetEPType(ENDP1, EP_BULK);
00173   SetEPTxAddr(ENDP1, ENDP1_TXADDR);
00174   SetEPTxStatus(ENDP1, EP_TX_NAK);
00175   SetEPRxStatus(ENDP1, EP_RX_DIS);
00176 
00177   /* Initialize Endpoint 2 */
00178   SetEPType(ENDP2, EP_INTERRUPT);
00179   SetEPTxAddr(ENDP2, ENDP2_TXADDR);
00180   SetEPRxStatus(ENDP2, EP_RX_DIS);
00181   SetEPTxStatus(ENDP2, EP_TX_NAK);
00182 
00183   /* Initialize Endpoint 3 */
00184   SetEPType(ENDP3, EP_BULK);
00185   SetEPRxAddr(ENDP3, ENDP3_RXADDR);
00186   SetEPRxCount(ENDP3, VIRTUAL_COM_PORT_DATA_SIZE);
00187   SetEPRxStatus(ENDP3, EP_RX_VALID);
00188   SetEPTxStatus(ENDP3, EP_TX_DIS);
00189 
00190   /* Set this device to response on default address */
00191   SetDeviceAddress(0);
00192 #endif /* STM32F10X_CL */
00193 
00194   bDeviceState = ATTACHED;
00195 }
00196 
00197 /*******************************************************************************
00198 * Function Name  : Virtual_Com_Port_SetConfiguration.
00199 * Description    : Update the device state to configured.
00200 * Input          : None.
00201 * Output         : None.
00202 * Return         : None.
00203 *******************************************************************************/
00204 void Virtual_Com_Port_SetConfiguration(void)
00205 {
00206   DEVICE_INFO *pInfo = &Device_Info;
00207 
00208   if (pInfo->Current_Configuration != 0)
00209   {
00210     /* Device configured */
00211     bDeviceState = CONFIGURED;
00212   }
00213 }
00214 
00215 /*******************************************************************************
00216 * Function Name  : Virtual_Com_Port_SetConfiguration.
00217 * Description    : Update the device state to addressed.
00218 * Input          : None.
00219 * Output         : None.
00220 * Return         : None.
00221 *******************************************************************************/
00222 void Virtual_Com_Port_SetDeviceAddress (void)
00223 {
00224   bDeviceState = ADDRESSED;
00225 }
00226 
00227 /*******************************************************************************
00228 * Function Name  : Virtual_Com_Port_Status_In.
00229 * Description    : Virtual COM Port Status In Routine.
00230 * Input          : None.
00231 * Output         : None.
00232 * Return         : None.
00233 *******************************************************************************/
00234 void Virtual_Com_Port_Status_In(void)
00235 {
00236   if (Request == SET_LINE_CODING)
00237   {
00238     Request = 0;
00239   }
00240 }
00241 
00242 /*******************************************************************************
00243 * Function Name  : Virtual_Com_Port_Status_Out
00244 * Description    : Virtual COM Port Status OUT Routine.
00245 * Input          : None.
00246 * Output         : None.
00247 * Return         : None.
00248 *******************************************************************************/
00249 void Virtual_Com_Port_Status_Out(void)
00250 {}
00251 
00252 /*******************************************************************************
00253 * Function Name  : Virtual_Com_Port_Data_Setup
00254 * Description    : handle the data class specific requests
00255 * Input          : Request Nb.
00256 * Output         : None.
00257 * Return         : USB_UNSUPPORT or USB_SUCCESS.
00258 *******************************************************************************/
00259 RESULT Virtual_Com_Port_Data_Setup(uint8_t RequestNo)
00260 {
00261   uint8_t    *(*CopyRoutine)(uint16_t);
00262 
00263   CopyRoutine = NULL;
00264 
00265   if (RequestNo == GET_LINE_CODING)
00266   {
00267     if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
00268     {
00269       CopyRoutine = Virtual_Com_Port_GetLineCoding;
00270     }
00271   }
00272   else if (RequestNo == SET_LINE_CODING)
00273   {
00274     if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
00275     {
00276       CopyRoutine = Virtual_Com_Port_SetLineCoding;
00277     }
00278     Request = SET_LINE_CODING;
00279   }
00280 
00281   if (CopyRoutine == NULL)
00282   {
00283     return USB_UNSUPPORT;
00284   }
00285 
00286   pInformation->Ctrl_Info.CopyData = CopyRoutine;
00287   pInformation->Ctrl_Info.Usb_wOffset = 0;
00288   (*CopyRoutine)(0);
00289   return USB_SUCCESS;
00290 }
00291 
00292 /*******************************************************************************
00293 * Function Name  : Virtual_Com_Port_NoData_Setup.
00294 * Description    : handle the no data class specific requests.
00295 * Input          : Request Nb.
00296 * Output         : None.
00297 * Return         : USB_UNSUPPORT or USB_SUCCESS.
00298 *******************************************************************************/
00299 RESULT Virtual_Com_Port_NoData_Setup(uint8_t RequestNo)
00300 {
00301 
00302   if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
00303   {
00304     if (RequestNo == SET_COMM_FEATURE)
00305     {
00306       return USB_SUCCESS;
00307     }
00308     else if (RequestNo == SET_CONTROL_LINE_STATE)
00309     {
00310       return USB_SUCCESS;
00311     }
00312   }
00313 
00314   return USB_UNSUPPORT;
00315 }
00316 
00317 /*******************************************************************************
00318 * Function Name  : Virtual_Com_Port_GetDeviceDescriptor.
00319 * Description    : Gets the device descriptor.
00320 * Input          : Length.
00321 * Output         : None.
00322 * Return         : The address of the device descriptor.
00323 *******************************************************************************/
00324 uint8_t *Virtual_Com_Port_GetDeviceDescriptor(uint16_t Length)
00325 {
00326   return Standard_GetDescriptorData(Length, &Device_Descriptor);
00327 }
00328 
00329 /*******************************************************************************
00330 * Function Name  : Virtual_Com_Port_GetConfigDescriptor.
00331 * Description    : get the configuration descriptor.
00332 * Input          : Length.
00333 * Output         : None.
00334 * Return         : The address of the configuration descriptor.
00335 *******************************************************************************/
00336 uint8_t *Virtual_Com_Port_GetConfigDescriptor(uint16_t Length)
00337 {
00338   return Standard_GetDescriptorData(Length, &Config_Descriptor);
00339 }
00340 
00341 /*******************************************************************************
00342 * Function Name  : Virtual_Com_Port_GetStringDescriptor
00343 * Description    : Gets the string descriptors according to the needed index
00344 * Input          : Length.
00345 * Output         : None.
00346 * Return         : The address of the string descriptors.
00347 *******************************************************************************/
00348 uint8_t *Virtual_Com_Port_GetStringDescriptor(uint16_t Length)
00349 {
00350   uint8_t wValue0 = pInformation->USBwValue0;
00351   if (wValue0 > 4)
00352   {
00353     return NULL;
00354   }
00355   else
00356   {
00357     return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
00358   }
00359 }
00360 
00361 /*******************************************************************************
00362 * Function Name  : Virtual_Com_Port_Get_Interface_Setting.
00363 * Description    : test the interface and the alternate setting according to the
00364 *                  supported one.
00365 * Input1         : uint8_t: Interface : interface number.
00366 * Input2         : uint8_t: AlternateSetting : Alternate Setting number.
00367 * Output         : None.
00368 * Return         : The address of the string descriptors.
00369 *******************************************************************************/
00370 RESULT Virtual_Com_Port_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting)
00371 {
00372   if (AlternateSetting > 0)
00373   {
00374     return USB_UNSUPPORT;
00375   }
00376   else if (Interface > 1)
00377   {
00378     return USB_UNSUPPORT;
00379   }
00380   return USB_SUCCESS;
00381 }
00382 
00383 /*******************************************************************************
00384 * Function Name  : Virtual_Com_Port_GetLineCoding.
00385 * Description    : send the linecoding structure to the PC host.
00386 * Input          : Length.
00387 * Output         : None.
00388 * Return         : Linecoding structure base address.
00389 *******************************************************************************/
00390 uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length)
00391 {
00392   if (Length == 0)
00393   {
00394     pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
00395     return NULL;
00396   }
00397   return(uint8_t *)&linecoding;
00398 }
00399 
00400 /*******************************************************************************
00401 * Function Name  : Virtual_Com_Port_SetLineCoding.
00402 * Description    : Set the linecoding structure fields.
00403 * Input          : Length.
00404 * Output         : None.
00405 * Return         : Linecoding structure base address.
00406 *******************************************************************************/
00407 uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length)
00408 {
00409   if (Length == 0)
00410   {
00411     pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
00412     return NULL;
00413   }
00414   return(uint8_t *)&linecoding;
00415 }
00416 
00417 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
00418