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

📄 lpc24xx_usb.c

📁 NXP LPC系列AMR7的开发程序源码(LCD
💻 C
📖 第 1 页 / 共 3 页
字号:
      // Clear  Realize interrupt bit
      USBDEVINTCLR = bmUSB_EPRealizeInterrupt;
      // Realize endpoint
      USBREEP |= Mask;
      // Set endpoint maximum packet size
      USBEPIN     = EndPoint;
      USBMAXPSIZE = MaxPacketSize;
      // Wait for Realize complete
      while ((USBDEVINTST & bmUSB_EPRealizeInterrupt) == 0);
      // Assign EP interrupt line HP/LP
#if (USB_FAST_EP > 0) || (USB_HIGH_PRIORITY_EP > 0)
      if(FastIntr)
      {
        EpPriority |= Mask;
      }
      else
      {
        EpPriority &= ~Mask;
      }
#else
        EpPriority &= ~Mask;
#endif // (USB_FAST_EP > 0) || (USB_HIGH_PRIORITY_EP > 0)
      // Init EP max packet size
      EpInfo[EndPoint].MaxSize = MaxPacketSize;
      if(IntrEna)
      {
        // Enable endpoint interrupt
        USBEPINTEN |= Mask;
      }
      else
      {
        // Disable endpoint interrupt
        USBEPINTEN &=~Mask;
      }
      // input dir
      Mask = 2;
      EndPoint = CTRL_ENP_IN;
    }
    // Clear  Realize interrupt bit
    USBDEVINTCLR = bmUSB_EPRealizeInterrupt;
    // Realize endpoint
    USBREEP  |= Mask;
    // Set endpoint maximum packet size
    USBEPIN     = EndPoint;
    USBMAXPSIZE = MaxPacketSize;
    // Wait for Realize complete
    while ((USBDEVINTST & bmUSB_EPRealizeInterrupt) == 0);
    // Assign EP interrupt line HP/LP
#if (USB_FAST_EP > 0) || (USB_HIGH_PRIORITY_EP > 0)
    if(FastIntr)
    {
      EpPriority |= Mask;
    }
    else
    {
      EpPriority &= ~Mask;
    }
#else
      EpPriority &= ~Mask;
#endif // (USB_FAST_EP > 0) || (USB_HIGH_PRIORITY_EP > 0)
    // Init EP max packet size
    EpInfo[EndPoint].MaxSize = MaxPacketSize;
    if(IntrEna)
    {
      // Enable endpoint interrupt
      USBEPINTEN |= Mask;
    }
    else
    {
      // Disable endpoint interrupt
      USBEPINTEN &=~Mask;
    }
  }
  else
  {
    if((EndPoint == CTRL_ENP_OUT) || (EndPoint == CTRL_ENP_IN))
    {
      Mask = 0x3;
      // Reset EP max packet size
      EpInfo[0].MaxSize = 0;
      EndPoint = CTRL_ENP_IN;
    }
    Mask =~ Mask;
    // Disable relevant endpoint and interrupt
    USBREEP &= Mask;
    USBEPINTEN &= Mask;
    // Reset EP max packet size
    EpInfo[EndPoint].MaxSize = 0;
  }

  USBEPINTPRI = EpPriority;
  return(USB_OK);
}

/*************************************************************************
 * Function Name: USB_SetAdd
 * Parameters: Int32U DevAdd - device address between 0 - 127
 *
 * Return: none
 *
 * Description: Set device address
 *
 *************************************************************************/
static
void USB_SetAdd(Int32U DevAdd)
{
  USB_Cmd(CMD_USB_SET_ADDRESS,DevAdd | 0x80);
  USB_Cmd(CMD_USB_SET_ADDRESS,DevAdd | 0x80);
}

/*************************************************************************
 * Function Name: USB_ConnectRes
 * Parameters: Boolean Conn
 *
 * Return: none
 *
 * Description: Enable Pull-Up resistor
 *
 *************************************************************************/
void USB_ConnectRes (Boolean Conn)
{

  USB_Cmd(CMD_USB_SET_DEV_STAT, (Conn ? bmUSB_Connect : 0));

#if (USB_PORT_SEL==1)
Int32U Save;
  ENTR_CRT_SECTION(Save);
  I2C_STS_bit.TDI = 1;
  I2C_TX = 0x15A;             // Send ISP1301 address, R/W=0
  I2C_TX = Conn?0x006:0x007;  // Send OTG Control (Clear/Set) register address
  I2C_TX = 0x201;             // Clear DP_PULLUP bit, send STOP condition
  while(!I2C_STS_bit.TDI);
  EXT_CRT_SECTION(Save);
#else
  #ifdef USB2_ERRATA
  FIO0PIN_bit.P0_14 = !Conn;
  #endif // USB2_ERRATA
#endif // (USB_PORT_SEL==1)
}

/*************************************************************************
 * Function Name: USB_Configure
 * Parameters: Boolean Configure
 *
 * Return: none
 *
 * Description: Configure device
 *  When Configure != 0 enable all Realize EP
 *
 *************************************************************************/
static
void USB_Configure (Boolean Configure)
{
  USB_Cmd(CMD_USB_CFG_DEV,Configure);
}

#if USB_REMOTE_WAKEUP != 0
/*************************************************************************
 * Function Name: USB_Wake-up
 * Parameters: none
 *
 * Return: none
 *
 * Description: Wake up Usb
 *
 *************************************************************************/
static
void USB_WakeUp (void)
{
  USBCLKCTRL = (1<<1) |   // Device clk enable
               (1<<4);    // AHB clk enable

  while((USBCLKST & ((1<<1) | (1<<4))) != ((1<<1) | (1<<4)));
  USB_Cmd(CMD_USB_SET_DEV_STAT,bmUSB_Connect);
}
#endif // USB_REMOTE_WAKEUP != 0

/*************************************************************************
 * Function Name: USB_GetDevStatus
 * Parameters: USB_DevStatusReqType_t Type
 *
 * Return: Boolean
 *
 * Description: Return USB device status
 *
 *************************************************************************/
Boolean USB_GetDevStatus (USB_DevStatusReqType_t Type)
{
  switch (Type)
  {
  case USB_DevConnectStatus:
    return(USB_DevStatus.Connect);
  case USB_SuspendStatus:
    return(USB_DevStatus.Suspend);
  case USB_ResetStatus:
    return(USB_DevStatus.Reset);
  }
  return(FALSE);
}

/*************************************************************************
 * Function Name: USB_SetStallEP
 * Parameters: USB_Endpoint_t EndPoint, Boolean Stall
 *
 * Return: none
 *
 * Description: The endpoint stall/unstall
 *
 *************************************************************************/
void USB_SetStallEP (USB_Endpoint_t EndPoint, Boolean Stall)
{
Boolean CurrStallStatus;
  assert((USBREEP & (1UL<<EndPoint)));  // check whether is a realized EP
  USB_GetStallEP(EndPoint,&CurrStallStatus);
  if(CurrStallStatus != Stall)
  {
    USB_Cmd(CMD_USB_SET_EP_STAT | EndPoint, (Stall ? bmUSB_EpStall : 0));
  }
}

/*************************************************************************
 * Function Name: USB_GetStallEP
 * Parameters: USB_Endpoint_t EndPoint, pBoolean pStall
 *
 * Return: none
 *
 * Description: Get stall state of the endpoint
 *
 *************************************************************************/
static
void USB_GetStallEP (USB_Endpoint_t EndPoint, pBoolean pStall)
{
  assert((USBREEP & (1UL<<EndPoint)));  // check whether is a realized EP
  *pStall = (USB_Cmd(CMD_USB_SEL_EP | EndPoint, 0) & bmUSB_EpStallStatus) != 0;
}

/*************************************************************************
 * Function Name: USB_EpValidate
 * Parameters: USB_Endpoint_t EndPoint
 *
 * Return: USB_ErrorCodes_t
 *
 * Description: Validate/Clear EP buffer
 *
 *************************************************************************/
USB_ErrorCodes_t USB_EpValidate(USB_Endpoint_t EndPoint)
{
  assert((USBREEP & (1UL<<EndPoint)));  // check whether is a realized EP
  if(EndPoint & 1)
  {
    // IN EP (Tx)
    // Validate buffer
    USB_Cmd(CMD_USB_SEL_EP | EndPoint,0);
    USB_Cmd(CMD_USB_VAL_BUF,0);
  }
  else
  {
    // OUT EP (Rx)
    // Select endpoint
    USB_Cmd(CMD_USB_SEL_EP | EndPoint,0);
    // Clear selected end point
    if(USB_Cmd(CMD_USB_CLR_BUF,0) & 1)
    {
      return(UB_EP_SETUP_OVERWRITE);
    }
  }
  return(USB_OK);
}

/*************************************************************************
 * Function Name: USB_EpWrite
 * Parameters: USB_Endpoint_t EndPoint,
 *             const unsigned char *pData, pInt32U pCount
 *
 * Return: USB_ErrorCodes_t
 *
 * Description: Endpoint Write (IN)
 *
 *************************************************************************/
USB_ErrorCodes_t USB_EpWrite (USB_Endpoint_t EndPoint,
                              const unsigned char *pData, pInt32U pCount)
{
Int32U Data, Status, ActSize, Count, CurrSize, SizeHold;

  assert((USBREEP & (1UL<<EndPoint)));  // check whether is a realized EP

  SizeHold = 0;
  ActSize = *pCount;
  *pCount = 0;

  do
  {
    // Get EP Status
    Status = USB_Cmd(CMD_USB_SEL_EP | EndPoint,0);
    // Check EP state Stalled/Unstalled
    if(Status & bmUSB_ST)
    {
      // EP stalled
      return(USB_EP_STALLED);
    }

    // Check Buffers state Full/Empty
    if(Status & bmUSB_FE)
    {
      //  Buffer/s are full
      break;
    }

    // Get smaller of the rest of the user buffer and the received data.
    CurrSize = MIN(EpInfo[EndPoint].MaxSize,(ActSize-SizeHold));

    // Convert EP physical address to logical and set write enable bit
    USBCTRL = ((EndPoint << 1) & 0x3C) | bmUSB_CtrlWrEna;

    // Set data size
    USBTXPLEN = CurrSize;
    if(CurrSize == 0)
    {
      TDATA = 0;
    }
    else
    {
      for (Count = 0; Count < CurrSize; ++Count)
      {
        Data  = *pData++;
        if (++Count < CurrSize) // fix overwrite problem
        {
          Data |= (*pData++) << 8;
          if (++Count < CurrSize)
          {
            Data |= (*pData++) << 16;
            if (++Count < CurrSize)
            {
              Data |= (*pData++) << 24;
            }
          }
        }
        TDATA = Data;
      }
    }

    USBCTRL = 0;
    SizeHold += CurrSize;
    // Clear for pending TX interrupt
    USB_EpIntrClr(EndPoint);
    USB_EpValidate(EndPoint);
  }
  while(SizeHold < ActSize);

  *pCount = SizeHold;
  return(USB_OK);
}

/*************************************************************************
 * Function Name: USB_EpRead
 * Parameters: USB_Endpoint_t EndPoint, pInt8U pData, pInt32U pCount
 *
 * Return: USB_ErrorCodes_t
 *
 * Description: Endpoint Read (OUT)
 *
 *************************************************************************/
USB_ErrorCodes_t USB_EpRead (USB_Endpoint_t EndPoint, pInt8U pData,
                             pInt32U pCount)
{
Int32U Data, Status, ActSize, Count, CurrSize, EpCount, SizeHold;

  assert((USBREEP & (1UL<<EndPoint)));  // check whether is a realized EP

  SizeHold = 0;
  ActSize = *pCount;
  *pCount = 0;

  do
  {
    // Get EP Status
    Status = USB_Cmd(CMD_USB_SEL_EP | EndPoint,0);

    // Check EP state Stalled/Unstalled
    if(Status & bmUSB_ST)
    {
      // EP stalled
      return(USB_EP_STALLED);
    }

    // Check Buffers state Full/Empty
    if(!(Status & bmUSB_FE))
    {
      //  Buffers are empty
      break;
    }

    // Convert EP physical address to logical and set read enable bit
    USBCTRL = ((EndPoint << 1) & 0x3C) | bmUSB_CtrlRdEna;
    while (USBRXPLEN_bit.PKT_RDY == 0);

    // Get data size
    EpCount = USBRXPLEN_bit.PKT_LNGTH;

    // Get smaller of the rest of the user buffer and the received data.
    CurrSize = MIN(EpCount,EpInfo[EndPoint].MaxSize);

    SizeHold += CurrSize;
    if(CurrSize == 0)
    {
      volatile int Dummy = USBRXDATA;
    }
    else
    {
      // the buffer size must be enough bigger to get all available data from
      // the EP in other case remaining data may be lost
      if(EpInfo[EndPoint].MaxSize <= (ActSize-SizeHold))
      {
        *pCount = SizeHold;
        return (USB_BUF_OVERFLOW);
      }

      for(Count = 0; Count < CurrSize; ++Count)
      {
        Data = USBRXDATA;
        *pData++ = Data;
        if(++Count < CurrSize) // fix overwrite problem
        {
          *pData++ = Data >> 8;
          if(++Count < CurrSize)
          {
            *pData++ = Data >> 16;
            if(++Count < CurrSize)
            {
              *pData++ = Data >> 24;
            }
          }
        }
      }
    }

    USBCTRL = 0;
    if(UsbEpDoubleBuffType[EndPoint])
    {
      if(EpInfo[EndPoint].MaxSize <= (ActSize - SizeHold))
      {
        // clear pending interrupt when "user" buffer is not full
        // and can receive more data if is available
        USB_EpIntrClr(EndPoint);
        USB_EpValidate(EndPoint);
      }
      else
      {
        USB_EpValidate(EndPoint);
        // rest of the "user" buffer size isn't enough bigger
        // to get MAXIMUM EP size
        break;
      }
    }
    else
    {
      break;
    }
  } while(SizeHold < ActSize);

  *pCount = SizeHold;
  if(Status & bmUSB_STP)
  {
    return (UB_EP_SETUP_OVERWRITE);
  }

⌨️ 快捷键说明

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