⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upsd3400_upsd_usb.c

📁 U-disk reference design
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------------
upsd3400_upsd_usb.c

Version:
September 13, 2005 Ver 1.1 - Updated disclaimer, renamed file.
March 22, 2005 - Version 1.0 - Initial Release.

Description:  Basic USB Functions that include enumeration.

Copyright (c) 2005 STMicroelectronics Inc.

THIS INFORMATION (or THIS SOFTWARE or THIS DOCUMENT) IS FOR GUIDANCE ONLY. ST 
MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS 
SOFTWARE nor for any infringement of patents or other rights of third parties 
which may result from its use. ST MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR 
ANY DIRECT, INDIRECT INCIDENTAL OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY 
CLAIMS ARISING IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE, 
OR USE OF THIS SOFTWARE. Specifications mentioned in this publication are 
subject to change without notice. This publication supersedes and replaces all 
information previously supplied. STMicroelectronics products are not authorized 
for use as critical components in life support devices or systems without the 
express written approval of STMicroelectronics.

------------------------------------------------------------------------------*/

#pragma NOAREGS

#include "upsd3400.h"
#include "upsd3400_hardware.h"
#include "upsd3400_usb.h"
#include "upsd3400_upsd_usb.h"
#include "upsd3400_usb_app.h"

#define MASS_STORAGE 1

// Constant Variables for USB Descriptors
extern const device_descriptor code deviceDesc;
extern const configuration_descriptor code configDesc;
extern const uchar * const code stringDescTable[];

unsigned char data usbState, ep0state, ep1state;
static unsigned char confignum;
static unsigned char remotewakeupen;
static unsigned char DevIdleRate;

setup_buffer setupPacket;
static unsigned char* pTransmitBufferEP0;
static unsigned char  bytesToTransmitEP0;

//data unsigned char xdata * TxBufferEP0;
//data unsigned char xdata * RxBufferEP0;

#define USB_FIFO ((unsigned char volatile xdata *) USB_BASE_ADDR)


/******************************************************************************

                              PRODUCT SPECIFIC AREA

 ******************************************************************************



/******************************************************************************

                              PRODUCT INDEPENDENT AREA

 ******************************************************************************/



void OnUsbReset()
/******************************************************************************
 Function   : void OnUsbReset()
 Parameters : none
 Description: USB driver module initialization routine.
 ******************************************************************************/
 {
  UCTL &= ~USBEN;
  UCTL |= USBEN;                             //USB enable

  UIE0  = RSTIE | SUSPENDIE;                 //Enable RESET, SUSPEND, RESUME INT
  UIE1  = IN0IE;                             //Enable EP0 IN INT
  UIE2  = OUT0IE;                            //Enable EP0 OUT INT
  UIE3  = 0;

  UPAIR = 0;
          //1+2+4+8;                       

  USEL  = OUTDIR | SELEP0;                   //Select EP0 OUT
  UCON  = ENABLE_FIFO | EPFIFO_BSY;

  USEL  = INDIR | SELEP0;                    //Select EP0 IN
  UCON  = ENABLE_FIFO;

  confignum = 0;
  usbState  = US_DEFAULT;
  ep0state  = US_EPDEFAULT;
  ep1state  = US_EPDEFAULT;
 }








void STALL_EP0()
/******************************************************************************
 Function   : void STALL_EP0()
 Parameters : none
 Description: Stalls EP0.
              This endpoint is halted or a control pipe request is not supported.
              Endpoint can be unstalled by Bus Reset.
 ******************************************************************************/
 {
  USEL     = INDIR | SELEP0;
  UCON    |= STALL | ENABLE_FIFO;
  UCON    &= ~TOGGLE;                        //clear toggle
  USIZE    = 0;
  ep0state = US_EPSTALL;
 }






void STALL_EP1()
/******************************************************************************
 Function   : void STALL_EP1()
 Parameters : none
 Description: Stalls EP1.
              This endpoint is halted or a control pipe request is not supported.
              Endpoint can be unstalled by Bus Reset.
 ******************************************************************************/
 {
  USEL     = INDIR | SELEP1;
  UCON    |= STALL;
  UCON    &= ~TOGGLE;                        //clear toggle
  USIZE    = 0;
  ep1state = US_EPSTALL;
 }





/*
void RemoteWakeup()
/******************************************************************************
 Function   : void RemoteWakeup()
 Parameters : none
 Description: USB device remote wakeup
 ******************************************************************************/
/*
 {
  data int i;
  if (remotewakeupen)
   {
    UCTL |= WAKEUP;

    for (i=0;i<10000;i++)                    // delay cca 10ms();
     {
      i++;
      i--;
     }
    UCTL &= ~WAKEUP;
    usbState = US_DEFAULT;
   }
 }
*/





void OnUsbSuspend()
/******************************************************************************
 Function   : void OnUsbSuspend()
 Parameters : none
 Description: service routine for USB Suspend event
 ******************************************************************************/
 {
  usbState =  US_SUSPENDED;
 }






void OnUsbResume()
/******************************************************************************
 Function   : void OnUsbResume()
 Parameters : none
 Description: service routine for USB Host resume event
 ******************************************************************************/
 {
  usbState =  US_DEFAULT;
 }







void TransmitZeroLengthEP0()
/******************************************************************************
 Function   : void TransmitZeroLengthEP0()
 Parameters : none
 Description: Transmits zero length data of EP0.
 ******************************************************************************/
 {
  data unsigned int count;

  USEL  = INDIR | SELEP0;
  UCON ^= TOGGLE;
  count = 0;
  while (UCON & EPFIFO_BSY)                  //check busy
   {
    count++;
    if (count==0)
     {
      USIZE = 0;
      return;
     }
   }
  USIZE = 0;
 }












void TransmitEP0()
/******************************************************************************
 Function   : void TransmitEP0()
 Parameters : none
 Description: Transmits next segment of descriptor buffer (pTransmitBufferEP0).
 ******************************************************************************/
 {
  data unsigned char i;
  data unsigned char nBytes;
  data unsigned  int count;
  data unsigned ZLPacket;

  if (bytesToTransmitEP0 == 0)
   {
    USEL  = OUTDIR | SELEP0;                 //Select EP0 OUT
    UCON |= ENABLE_FIFO;
    UCON |= TOGGLE;
    USIZE = 0;
    return;
   }
  else
   {
    USEL  = INDIR | SELEP0;                  //select EP0 IN
    UCON |= ENABLE_FIFO;

    count = 0;
    while (UCON & EPFIFO_BSY)                //check busy
     {
      count++;
      if (count==0)
       {
        USIZE = 0;
        return;
       }
     }

    UCON ^= TOGGLE;

    if (pTransmitBufferEP0 == NULL)
     {
      bytesToTransmitEP0 = 0;
      ZLPacket = 0;
     }
    else
     {
      ZLPacket = (bytesToTransmitEP0 == EP0_PKT_SIZE)?1:0;
     }

    nBytes = bytesToTransmitEP0;
    if (nBytes > EP0_PKT_SIZE)
     {
      nBytes = EP0_PKT_SIZE;
     }

    for(i=0; i<nBytes; i++)
     {
      USB_FIFO[i] = *pTransmitBufferEP0++;
     }
    bytesToTransmitEP0 -= nBytes;
    USIZE = nBytes;

    if (ZLPacket)
     {
      pTransmitBufferEP0 = NULL;
      bytesToTransmitEP0 = 255;
     }
   }
 }







void OnGetStatus()
/******************************************************************************
 Function   : void OnGetStatus()
 Parameters : none
 Description: Handler for GET_STATUS requests.
 ******************************************************************************/
 {
  USEL  = INDIR | SELEP0;                    //select EP0 OUT
  UCON |= TOGGLE;                            //DATA1
  UCON |= ENABLE_FIFO;

  
  if((setupPacket.wIndex.lo == 0x00) & (setupPacket.wIndex.hi == 0x00))
   {
    USB_FIFO[0] = ((BM_ATTRIBUTES & 0x40)>>6); //|(remotewakeupen<<1);
	// Not supported - Suba 05/18/2005
	//Added macro for device powered/bus powered status instead of hard coding - Suba 05/26/2005
    USB_FIFO[1] = 0;
   }
  else if((setupPacket.wIndex.lo == 0x81) & (setupPacket.wIndex.hi == 0x00))
   {
                                             //endpoint1
    if(ep1state == US_EPSTALL)
     {
      USB_FIFO[0] = 1;
     }
    else
     {
      USB_FIFO[0] = 0;
     }
    USB_FIFO[1] = 0;
   }
   //added get status support for endpoint 0x01(EP3_ADDR)
   else if((setupPacket.wIndex.lo == 0x01) & (setupPacket.wIndex.hi == 0x00))
   {
                                             //endpoint1
    if(ep1state == US_EPSTALL)
     {
      USB_FIFO[0] = 1;
     }
    else
     {
      USB_FIFO[0] = 0;
     }
    USB_FIFO[1] = 0;
   }
  else
   {
    STALL_EP0();                // No features currently implemented, so stall EP0
    return;
   }
  USIZE = 0x02;

  bytesToTransmitEP0 = 0;
 }






void OnClearFeature()
/******************************************************************************
 Function   : void OnClearFeature()
 Parameters : none
 Description: Handler for ClearFeature()
 ******************************************************************************/
 {
  /*                                           //clear device remote wakeup enable- not supported Suba 05/18/2005
  if((setupPacket.wValue.lo == 0x01)&&(setupPacket.wIndex.lo== 0x00))
   {
    remotewakeupen = 0x00;
   }
                                             //clear EP1 STALL
  else
   */
  if((setupPacket.wValue.lo == 0x00) && (setupPacket.wIndex.lo == EP1_ADDR))
   {
    ep1state = US_EPDEFAULT;

    USEL =  INDIR | SELEP1;
    UCON &= ~STALL;
   }
   //Suba 05/18/2005 - added clear feature support for EP3_ADDR-0x01
  else
  
  if((setupPacket.wValue.lo == 0x00) && (setupPacket.wIndex.lo == EP3_ADDR))
   {
    ep1state = US_EPDEFAULT;

    USEL =  INDIR | SELEP1;
    UCON &= ~STALL;
   } 
  else
   {
    STALL_EP0();
    return;
   }
  TransmitZeroLengthEP0();
 }







void OnSetFeature()
/******************************************************************************
 Function   : void OnSetFeature()
 Parameters : none
 Description: Handler for Set_FEATURE requests.
 ******************************************************************************/
 {//set device remote wakeup enable-not supported Suba 05/18/2005
  /*if((setupPacket.wValue.lo == 0x01)&&(setupPacket.wIndex.lo== 0x00))
   {

    remotewakeupen = 0x01;                   //device remote wakeup
   }
                                             //set EP1 STALL
  else*/ if((setupPacket.wValue.lo == 0x00) && (setupPacket.wIndex.lo== EP1_ADDR))
   {
    ep1state = US_EPSTALL;                   //EP halt
    USEL     = INDIR | SELEP1;
    UCON    |= STALL;
   }
   //Suba 05/18/2005 - added set feature support for EP3_ADDR-0x01
  else if((setupPacket.wValue.lo == 0x00) && (setupPacket.wIndex.lo== EP3_ADDR))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -