usbd_core.c ( File view )

  • By gregkam 2014-10-06
  • View(s):4272
  • Download(s):1
  • Point(s): 1
			/**
  ******************************************************************************
  * @file    usbd_core.c
  * @author  MCD Application Team
  * @version V1.1.0
  * @date    19-March-2012
  * @brief   This file provides all the USBD core functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "usbd_core.h"
#include "usbd_req.h"
#include "usbd_ioreq.h"
#include "usb_dcd_int.h"
#include "usb_bsp.h"

/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
* @{

*/


/** @defgroup USBD_CORE 
* @brief usbd core module
* @{

*/ 

/** @defgroup USBD_CORE_Private_TypesDefinitions
* @{

*/ 
/**
* @
}
*/ 


/** @defgroup USBD_CORE_Private_Defines
* @{

*/ 

/**
* @
}
*/ 


/** @defgroup USBD_CORE_Private_Macros
* @{

*/ 
/**
* @
}
*/ 




/** @defgroup USBD_CORE_Private_FunctionPrototypes
* @{

*/ 
static uint8_t USBD_SetupStage(USB_OTG_CORE_HANDLE *pdev);
static uint8_t USBD_DataOutStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum);
static uint8_t USBD_DataInStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum);
static uint8_t USBD_SOF(USB_OTG_CORE_HANDLE  *pdev);
static uint8_t USBD_Reset(USB_OTG_CORE_HANDLE  *pdev);
static uint8_t USBD_Suspend(USB_OTG_CORE_HANDLE  *pdev);
static uint8_t USBD_Resume(USB_OTG_CORE_HANDLE  *pdev);
#ifdef VBUS_SENSING_ENABLED
static uint8_t USBD_DevConnected(USB_OTG_CORE_HANDLE  *pdev);
static uint8_t USBD_DevDisconnected(USB_OTG_CORE_HANDLE  *pdev);
#endif
static uint8_t USBD_IsoINIncomplete(USB_OTG_CORE_HANDLE  *pdev);
static uint8_t USBD_IsoOUTIncomplete(USB_OTG_CORE_HANDLE  *pdev);
static uint8_t  USBD_RunTestMode (USB_OTG_CORE_HANDLE  *pdev) ;
/**
* @
}
*/ 

/** @defgroup USBD_CORE_Private_Variables
* @{

*/ 

__IO USB_OTG_DCTL_TypeDef SET_TEST_MODE;

USBD_DCD_INT_cb_TypeDef USBD_DCD_INT_cb = 
{

  USBD_DataOutStage,
  USBD_DataInStage,
  USBD_SetupStage,
  USBD_SOF,
  USBD_Reset,
  USBD_Suspend,
  USBD_Resume,
  USBD_IsoINIncomplete,
  USBD_IsoOUTIncomplete,
#ifdef VBUS_SENSING_ENABLED
USBD_DevConnected, 
USBD_DevDisconnected,    
#endif  

};

USBD_DCD_INT_cb_TypeDef  *USBD_DCD_INT_fops = &USBD_DCD_INT_cb;
/**
* @
}
*/ 

/** @defgroup USBD_CORE_Private_Functions
* @{

*/ 

/**
* @brief  USBD_Init
*         Initailizes the device stack and load the class driver
* @param  pdev: device instance
* @param  core_address: USB OTG core ID
* @param  class_cb: Class callback structure address
* @param  usr_cb: User callback structure address
* @retval None
*/
void USBD_Init(USB_OTG_CORE_HANDLE *pdev,
               USB_OTG_CORE_ID_TypeDef coreID,
               USBD_DEVICE *pDevice,                  
               USBD_Class_cb_TypeDef *class_cb, 
               USBD_Usr_cb_TypeDef *usr_cb)
{

  /* Hardware Init */
  USB_OTG_BSP_Init(pdev);  
  
  USBD_DeInit(pdev);
  
  /*Register class and user callbacks */
  pdev->dev.class_cb = class_cb;
  pdev->dev.usr_cb = usr_cb;  
  pdev->dev.usr_device = pDevice;    
  
  /* set USB OTG core params */
  DCD_Init(pdev , coreID);
  
  /* Upon Init call usr callback */
  pdev->dev.usr_cb->Init();
  
  /* Enable Interrupts */
  USB_OTG_BSP_EnableInterrupt(pdev);

}

/**
* @brief  USBD_DeInit 
*         Re-Initialize th device library
* @param  pdev: device instance
* @retval status: status
*/
USBD_Status USBD_DeInit(USB_OTG_CORE_HANDLE *pdev)
{

  /* Software Init */
  
  return USBD_OK;

}

/**
* @brief  USBD_SetupStage 
*         Handle the setup stage
* @param  pdev: device instance
* @retval status
*/
static uint8_t USBD_SetupStage(USB_OTG_CORE_HANDLE *pdev)
{

  USB_SETUP_REQ req;
  
  USBD_ParseSetupRequest(pdev , &req);
  
  switch (req.bmRequest & 0x1F) 
  {

  case USB_REQ_RECIPIENT_DEVICE:   
    USBD_StdDevReq (pdev, &req);
    break;
    
  case USB_REQ_RECIPIENT_INTERFACE:     
    USBD_StdItfReq(pdev, &req);
    break;
    
  case USB_REQ_RECIPIENT_ENDPOINT:        
    USBD_StdEPReq(pdev, &req);   
    break;
    
  default:           
    DCD_EP_Stall(pdev , req.bmRequest & 0x80);
    break;
  
}  
  return USBD_OK;

}

/**
* @brief  USBD_DataOutStage 
*         Handle data out stage
* @param  pdev: device instance
* @param  epnum: endpoint index
* @retval status
*/
static uint8_t USBD_DataOutStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum)
{

  USB_OTG_EP *ep;
  
  if(epnum == 0) 
  {

    ep = &pdev->dev.out_ep[0];
    if ( pdev->dev.device_state == USB_OTG_EP0_DATA_OUT)
    {

      if(ep->rem_data_len > ep->maxpacket)
      {

        ep->rem_data_len -=  ep->maxpacket;
        
        if(pdev->cfg.dma_enable == 1)
        {

          /* in slave mode this, is handled by the RxSTSQLvl ISR */
          ep->xfer_buff += ep->maxpacket; 
        
}        
        USBD_CtlContinueRx (pdev, 
                            ep->xfer_buff,
                            MIN(ep->rem_data_len ,ep->maxpacket));
      
}
      else
      {

        if((pdev->dev.class_cb->EP0_RxReady != NULL)&&
           (pdev->dev.device_status == USB_OTG_CONFIGURED))
        {

          pdev->dev.class_cb->EP0_RxReady(pdev); 
        
}
        USBD_CtlSendStatus(pdev);
      
}
    
}
  
}
  else if((pdev->dev.class_cb->DataOut != NULL)&&
          (pdev->dev.device_status == USB_OTG_CONFIGURED))
  {

    pdev->dev.class_cb->DataOut(pdev, epnum); 
  
}  
  return USBD_OK;

}

/**
* @brief  USBD_DataInStage 
*         Handle data in stage
* @param  pdev: device instance
* @param  epnum: endpoint index
* @retval status
*/
static uint8_t USBD_DataInStage(USB_OTG_CORE_HANDLE *pdev , uint8_t epnum)
{

  USB_OTG_EP *ep;
  
  if(epnum == 0) 
  {

    ep = &pdev->dev.in_ep[0];
    if ( pdev->dev.device_state == USB_OTG_EP0_DATA_IN)
    {

      if(ep->rem_data_len > ep->maxpacket)
      {

        ep->rem_data_len -=  ep->maxpacket;
        if(pdev->cfg.dma_enable == 1)
        {

          /* in slave mode this, is handled by the TxFifoEmpty ISR */
          ep->xfer_buff += ep->maxpacket;
        
}
        USBD_CtlContinueSendData (pdev, 
                                  ep->xfer_buff, 
                                  ep->rem_data_len);
      
}
      else
      {
 /* last packet is MPS multiple, so send ZLP packet */
        if((ep->total_data_len % ep->maxpacket == 0) &&
           (ep->total_data_len >= ep->maxpacket) &&
             (ep->total_data_len < ep->ctl_data_len ))
        {

          
          USBD_CtlContinueSendData(pdev , NULL, 0);
          ep->ctl_data_len = 0;
        
}
        else
        {

          if((pdev->dev.class_cb->EP0_TxSent != NULL)&&
             (pdev->dev.device_status == USB_OTG_CONFIGURED))
          {

            pdev->dev.class_cb->EP0_TxSent(pdev); 
          
}          
          USBD_CtlReceiveStatus(pdev);
        
}
      
}
    
}
    if (pdev->dev.test_mode == 1)
    {

      USBD_RunTestMode(pdev); 
      pdev->dev.test_mode = 0;
    
}
  
}
  else if((pdev->dev.class_cb->DataIn != NULL)&& 
          (pdev->dev.device_status == USB_OTG_CONFIGURED))
  {

    pdev->dev.class_cb->DataIn(pdev, epnum); 
  
}  
  ret
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
.gitignore19.00 B2013-04-23|01:09
01.97 kB
.gitignore8.00 B2013-04-23|01:09
build.mk1.82 kB2013-04-23|01:09
01.97 kB
default_handlers.c6.01 kB2013-04-23|01:09
default_hooks.c159.00 B2013-04-23|01:09
syscalls.c3.28 kB2013-04-23|01:09
Makefile327.00 B2013-04-23|01:09
README.md217.00 B2013-04-23|01:09
config.mk.example151.00 B2013-04-23|01:09
01.97 kB
FreeRTOSConfig.h8.59 kB2013-04-23|01:09
arm_common_tables.h1.09 kB2013-04-23|01:09
arm_math.h234.69 kB2013-04-23|01:09
core_cm0.h31.20 kB2013-04-23|01:09
core_cm3.h68.09 kB2013-04-23|01:09
core_cm4.h77.42 kB2013-04-23|01:09
core_cm4_simd.h23.42 kB2013-04-23|01:09
core_cmFunc.h15.32 kB2013-04-23|01:09
core_cmInstr.h15.73 kB2013-04-23|01:09
stm32f4xx.h507.96 kB2013-04-23|01:09
system_stm32f4xx.h2.17 kB2013-04-23|01:09
01.97 kB
01.97 kB
cortex-m4.mk646.00 B2013-04-23|01:09
01.97 kB
stm32f4.mk131.00 B2013-04-23|01:09
command.mk768.00 B2013-04-23|01:09
toolchain.mk150.00 B2013-04-23|01:09
01.97 kB
build.mk227.00 B2013-04-23|01:09
startup_stm32f4xx.s22.85 kB2013-04-23|01:09
system_stm32f4xx.c22.24 kB2013-04-23|01:09
01.97 kB
.gitignore8.00 B2013-04-23|01:09
build.mk1.04 kB2013-04-23|01:09
01.97 kB
01.97 kB
eeprom.h1.97 kB2013-04-23|01:09
gpio.h14.62 kB2013-04-23|01:09
i2c.h1.63 kB2013-04-23|01:09
interrupt.h1.61 kB2013-04-23|01:09
led.h904.00 B2013-04-23|01:09
rcc.h1.65 kB2013-04-23|01:09
spi.h3.46 kB2013-04-23|01:09
01.97 kB
irq.h6.68 kB2013-04-23|01:09
timer.h2.26 kB2013-04-23|01:09
usart.h3.38 kB2013-04-23|01:09
usb_cdc.h1.32 kB2013-04-23|01:09
01.97 kB
eeprom.c2.97 kB2013-04-23|01:09
fault.c1.38 kB2013-04-23|01:09
gpio.c5.12 kB2013-04-23|01:09
i2c.c9.10 kB2013-04-23|01:09
interrupt.c744.00 B2013-04-23|01:09
led.c2.11 kB2013-04-23|01:09
rcc.c11.74 kB2013-04-23|01:09
spi.c12.33 kB2013-04-23|01:09
timer.c10.08 kB2013-04-23|01:09
usart.c10.83 kB2013-04-23|01:09
usb_cdc.c1.21 kB2013-04-23|01:09
01.97 kB
gdb-init61.00 B2013-04-23|01:09
gdb-tdesc-cortexm-fpa.xml5.74 kB2013-04-23|01:09
openocd-stm32f4.cfg124.00 B2013-04-23|01:09
stm32_flash.ld5.33 kB2013-04-23|01:09
01.97 kB
01.97 kB
main.c2.92 kB2013-04-23|01:09
01.97 kB
main.c1.51 kB2013-04-23|01:09
01.97 kB
.gitignore32.00 B2013-04-23|01:09
build.mk1.12 kB2013-04-23|01:09
main.c740.00 B2013-04-23|01:09
01.97 kB
main.c11.32 kB2013-04-23|01:09
01.97 kB
main.c2.51 kB2013-04-23|01:09
01.97 kB
main.c2.14 kB2013-04-23|01:09
01.97 kB
main.c1.03 kB2013-04-23|01:09
01.97 kB
main.c1.49 kB2013-04-23|01:09
01.97 kB
main.c1.90 kB2013-04-23|01:09
01.97 kB
.gitignore8.00 B2013-04-23|01:09
build.mk1.17 kB2013-04-23|01:09
01.97 kB
01.97 kB
usb_bsp.h2.29 kB2013-04-23|01:09
usb_conf.h10.48 kB2013-04-23|01:09
usb_core.h12.29 kB2013-04-23|01:09
usb_dcd.h4.48 kB2013-04-23|01:09
usb_dcd_int.h3.03 kB2013-04-23|01:09
usb_defines.h6.52 kB2013-04-23|01:09
usb_hcd.h2.76 kB2013-04-23|01:09
usb_hcd_int.h3.65 kB2013-04-23|01:09
usb_otg.h1.97 kB2013-04-23|01:09
usb_regs.h21.22 kB2013-04-23|01:09
usbd_cdc_core.h3.87 kB2013-04-23|01:09
usbd_cdc_if_template.h1.85 kB2013-04-23|01:09
usbd_cdc_vcp.h2.89 kB2013-04-23|01:09
usbd_conf.h3.29 kB2013-04-23|01:09
usbd_core.h2.50 kB2013-04-23|01:09
usbd_def.h4.82 kB2013-04-23|01:09
usbd_desc.h3.35 kB2013-04-23|01:09
usbd_ioreq.h2.89 kB2013-04-23|01:09
usbd_req.h2.50 kB2013-04-23|01:09
usbd_usr.h3.00 kB2013-04-23|01:09
01.97 kB
usb_bsp.c3.27 kB2013-04-23|01:09
usb_core.c55.46 kB2013-04-23|01:09
usb_dcd.c9.21 kB2013-04-23|01:09
usb_dcd_int.c21.32 kB2013-04-23|01:09
usb_hcd.c5.62 kB2013-04-23|01:09
usb_hcd_int.c21.58 kB2013-04-23|01:09
usb_otg.c9.48 kB2013-04-23|01:09
usbd_cdc_core.c24.57 kB2013-04-23|01:09
usbd_cdc_if_template.c6.07 kB2013-04-23|01:09
usbd_cdc_vcp.c6.71 kB2013-04-23|01:09
usbd_core.c11.39 kB2013-04-23|01:09
usbd_desc.c8.23 kB2013-04-23|01:09
usbd_ioreq.c5.38 kB2013-04-23|01:09
usbd_req.c19.55 kB2013-04-23|01:09
usbd_usr.c3.48 kB2013-04-23|01:09
...
Sponsored links

usbd_core.c (278.02 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D