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

📄 upsd3400_upsd_usb.c

📁 upsd34xx系列单片机keil环境中的开发例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------------
upsd3400_upsd_usb.c

Version:
March 22, 2005 - Version 1.0 - Initial Release.

Description:  Basic USB Functions that include enumeration.

Note:
It is important to change, as appropriate, the PLL setting in the 
USBInitialize() function to match the MCU's oscillator in order for
USB to work properly.

Copyright (c) 2005 STMicroelectronics Inc.

This example demo code is provided as is and has no warranty,
implied or otherwise.  You are free to use/modify any of the provided
code at your own risk in your applications with the expressed limitation
of liability (see below) so long as your product using the code contains
at least one uPSD product (device).

LIMITATION OF LIABILITY:   NEITHER STMicroelectronics NOR ITS VENDORS OR 
AGENTS SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
------------------------------------------------------------------------------*/

#pragma NOAREGS

#include "upsd3400.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

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




void DK3400_EnableUSBPullUp(void)
/******************************************************************************
 Function   : void DK3400_EnableUSBPullUp()
 Parameters : (void)
 Description: Enables DK3400 USB PullUP
 ******************************************************************************
 {
// Enable DK3400_USB_PullUp
  UPSD_xreg.DIRECTION_C |=128;               // PC7=Out
  UPSD_xreg.DRIVE_C     &=127;               // PC7=Push/Pull
  UPSD_xreg.DATAOUT_C   |=128;               // PC7=1
 }



void DK3400_DisableUSBPullUp(void)
/******************************************************************************
 Function   : void DK3400_EnableUSBPullUp()
 Parameters : (void)
 Description: Enables DK3400 USB PullUP
 ******************************************************************************
 {
// Disable DK3400_USB_PullUp
  UPSD_xreg.DIRECTION_C &=127;               // PC7=in
 }







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

                              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;                                 //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] = 0x01 | (remotewakeupen<<1);
    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;
   }
  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
  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;
    UCON |= ENABLE_FIFO;
   }
  else
   {
    STALL_EP0();
    return;
   }
  TransmitZeroLengthEP0();
 }







void OnSetFeature()
/******************************************************************************
 Function   : void OnSetFeature()
 Parameters : none

⌨️ 快捷键说明

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