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

📄 upsd3400_upsd_usb.c

📁 upsd34xx系列单片机keil环境中的开发例程
💻 C
📖 第 1 页 / 共 2 页
字号:
 Description: Handler for Set_FEATURE requests.
 ******************************************************************************/
 {
                                             //set device remote wakeup enable
  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;
   }
  else
   {
    STALL_EP0();
    return;
   }

  TransmitZeroLengthEP0();
 }





void OnSetAddress()
/******************************************************************************
 Function   : void OnSetAddress()
 Parameters : none
 Description: Handler for SET_ADDRESS packets
 ******************************************************************************/
 {
  usbState = US_ADDRESSED;
  TransmitZeroLengthEP0();
 }





void OnGetDescriptor()
/******************************************************************************
 Function   : void OnGetDescriptor()
 Parameters : none
 Description: Handler for GET_DESCRIPTOR() requests
 ******************************************************************************/
 {
  data unsigned int bytesRequested;

  switch (setupPacket.wValue.hi)
   {
    case DT_DEVICE:
     {
      pTransmitBufferEP0 = (unsigned char*) &deviceDesc;
      bytesToTransmitEP0 = sizeof(deviceDesc);
      break;
     }

    case DT_CONFIGURATION:
     {
      pTransmitBufferEP0 = (unsigned char*) &configDesc;
      bytesToTransmitEP0 = configDesc.wTotalLength.lo;
      break;
     }

    case DT_STRING:
     {
      pTransmitBufferEP0 = (unsigned char*) stringDescTable[setupPacket.wValue.lo<4?setupPacket.wValue.lo:4];
      bytesToTransmitEP0 = *pTransmitBufferEP0;         // choose requested string
      break;
     }

    default:
     {
      STALL_EP0();                        // Unrecognized descriptor, so stall EP0
      return;
     }
   }

  bytesRequested = (setupPacket.wLength.hi << 8) | setupPacket.wLength.lo;
  if (bytesToTransmitEP0 > bytesRequested)
   {
    bytesToTransmitEP0 = bytesRequested;  //adjust size if host wants shorter data
   }

  UCON &= ~TOGGLE;                           // Set DATA0
  TransmitEP0();
 }










void OnSetDescriptor()
/******************************************************************************
 Function   : void OnSetDescriptor()
 Parameters : none
 Description: Handler for SET_DESCRIPTOR request
 ******************************************************************************/
 {
  STALL_EP0();
 }








void OnGetConfiguration()
/******************************************************************************
 Function   : void OnGetConfiguration()
 Parameters : none
 Description: Handler for GET_CONFIGURATION request
 ******************************************************************************/
 {

  USEL        = INDIR | SELEP0;              //select EP0 OUT
  UCON       |= TOGGLE;                      //Expect DATA1
  USB_FIFO[0] = confignum;

  USIZE       = 1;
  bytesToTransmitEP0 = 0x00;
 }







void OnSetConfiguration()
/******************************************************************************
 Function   : void OnSetConfiguration()
 Parameters : none
 Description: Handler for SET_CONFIGURATION requests.
 ******************************************************************************/
 {
  if (setupPacket.wValue.lo == 0)     // configuration value is 0 => address state
   {
    if (usbState == US_CONFIGURED)
     {
      TransmitZeroLengthEP0();
     }
    usbState = US_ADDRESSED;
   }
  else
  if (setupPacket.wValue.lo == 1)            // corresponds to configuration value
   {
    if (usbState != US_CONFIGURED)
     {
// Bulk transactions are initialized to DATA0
// when the endpoint is configured by an appropriate control transfer.
// The host will also start the first bulk transaction with DATA0.
// This recovery after STALL sets the data toggle bit to DATA0 for the endpoint
// on both the host and the device.
/*

      USEL   = OUTDIR | SELEP3;              //Select EP3 OUT
      UCON   = ENABLE_FIFO | EPFIFO_BSY;
      USIZE  = 0;

      USEL   = INDIR | SELEP1;               //Select EP1 IN
      UCON   = ENABLE_FIFO;

      UIE1  |= IN1IE;                        //Enable EP1 IN INT
      UIE2  |= OUT3IE;                       //Enable EP3 OUT INT


//      USEL   = OUTDIR | SELEP3;            //Select EP0 OUT
//      USIZE  = 0;                          //flush FIFO

      UIF1  &= ~IN1F;                        //clear INT Flag
      UIF2  &= ~OUT3F;                       //clear INT Flag
      */
      usbState  = US_CONFIGURED;
     }
    confignum = setupPacket.wValue.lo;
    TransmitZeroLengthEP0();
//    OnDeviceConfigured();
   }
  else
   {
    STALL_EP0();                             // stall EP0
   }
 }







void OnGetInterface()
/******************************************************************************
 Function   : void OnGetInterface()
 Parameters : none
 Description: Handler for GET_INTERFACE request
 ******************************************************************************/
 {

  USEL  = INDIR | SELEP0;                    //select EP0 OUT
  UCON |= TOGGLE;                            //Expect DATA1
  USIZE = 0;                                 //zero length data
 }








void OnSetInterface()
/******************************************************************************
 Function   : void OnSetInterface()
 Parameters : none
 Description: Handler for SET_INTERFACE() requests.
 ******************************************************************************/
 {
  if (setupPacket.wValue.lo == 0)
   {
    TransmitZeroLengthEP0();                 //zero length data
   }
  else
   {
    STALL_EP0();
   }
 }







void UsbInitialize()
/******************************************************************************
 Function   : void UsbInitialize()
 Parameters : none
 Description: USB driver module initialization routine.
 ******************************************************************************/
 {
  data unsigned int count;

  UCTL &= ~VISIBLE;                          // Disable USB FIFO in RAM area

  CCON1  &= ~0x07;            //CPUCLK prescaler = 1; only for silicon revision #0

/*                                             //Set PLL clk
  CCON0 |= 0xE0;                             //PLL enable
  CCON1 = 0x68;                              //40MHz, PLLM=22, PLLD=8

  CCON0 |= 0xE0;                             //PLL enable
  CCON1  = 0xE9;                             //33MHz, PLLM=30, PLLD=9

  CCON0 |= 0x70;                             //PLL enable
  CCON1  = 0xE3;                             //30MHz, PLLM=14, PLLD=3

  CCON0 |= 0xE0;                             //PLL enable
  CCON1  = 0x23;                             //24MHz, PLLM=18, PLLD=3

  CCON0 |= 0xE0;                             //PLL enable
  CCON1  = 0x63;                             //20MHz, PLLM=22, PLLD=3

  CCON0  |= 0xE0;                            //PLL enable
  CCON1   = 0xC3;                            //16MHz, PLLM=28, PLLD=3

  CCON0  |= 0xE0;                            //PLL enable
  CCON1   = 0xE2;                            //12MHz, PLLM=18, PLLD=2

  CCON0  |= 0xE0;                            //PLL enable
  CCON1   = 0xE0;                            //6MHz, PLLM=18, PLLD=0

  CCON0  |= 0xE0;                            //PLL enable
  CCON1   = 0xEF;                            //3MHz, PLLM=18, PLLD=-1
  */

  CCON0 |= 0xE0;                             //PLL enable
  CCON1 = 0x68;                              //40MHz, PLLM=22, PLLD=8

//  CCON0 |= 0xE0;                             //PLL enable
//  CCON1  = 0x23;                             //24MHz, PLLM=18, PLLD=3


  for (count=0; count<1000; count++)         // (wait 200us min.)
   {
    ACC = 0;                               //now wait for PLL output is stabilized
   }

  IP     = 0;                          //USB interrupt has the highest priority!!!
  IPA    = 1;              // (in DK3420, but actually it is not so important (:-)
  IEA   |= 0x01;                             // Enable USB Interrupt
  EA     = 1;

  UBASEH = USB_BASE_ADDR >> 8;               // Set USB FIFO base address
  UBASEL = USB_BASE_ADDR;                    // Set USB FIFO base address
  UCTL  |= VISIBLE;                          // Enable USB FIFO in RAM area

  remotewakeupen = 0x01;

  OnUsbReset();

  UCTL &= ~VISIBLE;                          // Disable USB FIFO in RAM area
 }















BOOL ReadSetupPacket()
/******************************************************************************
 Function   : BOOL ReadSetupPacket()
 Parameters : none
 Description: Reads a setup packet from USB Setup Command Register.
              Returns TRUE if successful; stalls the endpoint and returns
              FALSE on an invalid packet size.
 ******************************************************************************/
 {
  data unsigned char i;
  unsigned char* p = (unsigned char*) &setupPacket;



  USTA &= ~SETUP;                            // Clear setup bit in USTA

  for (i=0; i<8; i++)
   {
    USCI = i;
    *p++ = USCV;
   }



  return TRUE;
 }








void OnSetupPacket()
/******************************************************************************
 Function   : void OnSetupPacket()
 Parameters : none
 Description: Handler for SETUP packets received on EP0.
              Handle standard request and Mass Storage class request.
 ******************************************************************************/
 {
  if ((setupPacket.bmRequestType & 0x60) == 0)               //Standard request...
   {
    switch (setupPacket.bRequest)            // Standard request
     {
      case GET_STATUS:        OnGetStatus();        break;
      case CLEAR_FEATURE:     OnClearFeature();     break;
      case SET_FEATURE:       OnSetFeature();       break;
      case SET_ADDRESS:       OnSetAddress();       break;
      case GET_DESCRIPTOR:    OnGetDescriptor();    break;
      case SET_DESCRIPTOR:    OnSetDescriptor();    break;
      case GET_CONFIGURATION: OnGetConfiguration(); break;
      case SET_CONFIGURATION: OnSetConfiguration(); break;
      case GET_INTERFACE:     OnGetInterface();     break;
      case SET_INTERFACE:     OnSetInterface();     break;
      default:
      break;
     }
   }

/* Class request specific commands */
  else
  if (setupPacket.bmRequestType & 0x20)
   {
    if ((setupPacket.bmRequestType == CLASS_INTERFACE_TO_HOST)
    && (setupPacket.bRequest == 0xFE))       //Get Max LUN
     {
/*
      Get Max LUN (class-specific request)
      The device may implement several logical units that share common device characteristics.
      The host uses bCBWLUN to designate which logical unit of the device is the
      destination of the CBW. The Get Max LUN device request is used to determine
      the number of logical units supported by the device.
      Logical Unit Numbers on the device shall be numbered contiguously starting
      from LUN 0 to a maximum LUN of 15 (Fh).
      The device shall return one byte of data that contains the maximum LUN supported
      by the device.
      We have no LUN is associated with the device, the value returned is 0.
      */
      USEL        = INDIR | SELEP0;          //select EP0 OUT
      UCON       |= TOGGLE;                  //Expect DATA1
      USB_FIFO[0] = 0x00;                    //no LUN associated with the device
      USIZE       = 1;
     }
    else
    if ((setupPacket.bmRequestType == CLASS_INTERFACE_TO_DEVICE)
    && (setupPacket.bRequest == 0xFF))       //Bulk-Only Mass Storage Reset
     {
/*
      Bulk-Only Mass Storage Reset (class-specific request)
      This request is used to reset the mass storage device and its associated
      interface.This class-specific request shall ready the device for the next
      CBW from the host.The host shall send this request via the default pipe to
      the device. The device shall preserve the value of its bulk
      data toggle bits and endpoint STALL conditions despite the Bulk-Only Mass
      Storage Reset.
      The device shall NAK the status stage of the device request until the Bulk-Only
      Mass Storage Reset is complete.
      */
     }
   }
  else
   {
    STALL_EP0();                // It's not a request we handle, so stall endpoint
   }

  return;
 }


/* *************************************************************************
  ***                                                                      ***
  **                         *** End of File ***                            **
  ***                                                                      ***
   *************************************************************************  */

⌨️ 快捷键说明

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