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

📄 td1120p.c

📁 Usb Host/Periphel Control TD1120 codes
💻 C
📖 第 1 页 / 共 5 页
字号:
Notes     : 
------------------------------------------------------------------------------*/
SctBool TD1120P_IsVBusOn(TdiPeriphController* pc)
   {
   U16 readRegister;

   readRegister = SYS_Read1120P_Register(pc->ioBase, TD1120_DEV_CTRL_SET_REG);
   if (readRegister & TD1120_DEV_CTRL_VBUS)
      {
      return SCC_TRUE;
      }
   return SCC_FALSE;
   }
   

/*------------------------------------------------------------------------------
Name      : TD1120P_ResetController
Purpose   : Performence a function reset.
Arguments : pc - peripheral controller handle
Return    : None
Notes     : .
------------------------------------------------------------------------------*/
SctStatus TD1120P_ResetController(TdiPeriphController *pc)
   {
   
   if (pc == NULL)
      {
      PRINTF1(("\nTD1120P_ResetController: Invalid handle."));
      return SCS_ERROR_INVALID_HANDLE;
      }
      
   /* Reset the controller */
   SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_CTRL_SET_REG, \
      TD1120_DEV_CTRL_SOFT_RESET);
   return (SCS_SUCCESS);
   }

/*------------------------------------------------------------------------------
Name      : TD1120P_Connect
Purpose   : Requests that the pull-up on the D+ data line be connected (or 
            disconnected).
Arguments : pc - peripheral controller handle
Returns   : None
Notes     : 
-----------------------------------------------------------------------------*/
SctStatus TD1120P_Connect(TdiPeriphController* pc)
   {
   /* Attach a pull-up resistor on the D+ line */
   SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_CTRL_SET_REG, \
      TD1120_DEV_CTRL_CONNECT_RESISTOR);
   return (SCS_SUCCESS);
   }
   
/*------------------------------------------------------------------------------
Name      : TD1120P_Disconnect
Purpose   : Requests that the pull-up on the D+ data line be disconnected.
Arguments : pc - peripheral controller handle
Returns   : None
Notes     : 
-----------------------------------------------------------------------------*/
SctStatus TD1120P_Disconnect(TdiPeriphController* pc)
   {
   /* Detach the pull-up resistor on the D+ line */
   SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_CTRL_CLEAR_REG, \
      TD1120_DEV_CTRL_CONNECT_RESISTOR);
   return (SCS_SUCCESS);
   }
   
/*------------------------------------------------------------------------------
Name      : TD1120P_ControlBusParams
Purpose   : Control the bus parameters
Arguments : pc - peripheral controller handle 
            controlCommand - control command
            testMode - test mode
Returns   : None
Notes     : 
-----------------------------------------------------------------------------*/
SctStatus TD1120P_ControlBusParams(TdiPeriphController* pc, U32 controlCommand, \
                                   U8 testMode)
   {
   
   if (pc == NULL)
      {
      PRINTF1(("\nTD1120P_ControlBusParams: Invalid handle."));
      return SCS_ERROR_INVALID_HANDLE;
      }
      
   switch (controlCommand)
      {
      case USB_START_REMOTE_WAKEUP:
         SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_CTRL_SET_REG, \
                               TD1120_DEV_CTRL_RESUME_XCEIVER);
      break;
      case USB_END_REMOTE_WAKEUP:
         /* Nothing to do for TD1120 */
      break;
      case USB_FORCE_FULL_SPEED:
         SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_CTRL_SET_REG, \
                               TD1120_DEV_CTRL_FORCE_FULL_SPEED);
      break;
      case USB_SET_SELF_POWERED_MODE:
         /* Nothing to do for TD1120 */
      break;
      case USB_SET_TEST_MODE:
         TD1120P_SetTestMode(pc, testMode);
      break;
      case USB_PERIPH_CONNECT:
         TD1120P_Connect(pc);
      break;
      case USB_PERIPH_DISCONNECT:
         TD1120P_Disconnect(pc);
      break;
      default:
      break;
      }
   return (SCS_SUCCESS);
   }


/*------------------------------------------------------------------------------
Name      : TD1120P_SetAddress
Purpose   : Set Address
Arguments : pc - peripheral controller handle
            address - address of the device
Returns   : None
Notes     : 
-----------------------------------------------------------------------------*/
SctStatus TD1120P_SetAddress(TdiPeriphController* pc, U32 address)
   {
   if (pc == NULL)
      {
      PRINTF1(("\nTD1120P_SetAddress: Invalid handle."));
      return SCS_ERROR_INVALID_HANDLE;
      }
      
   SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_ADDRESS_REG, address);
   
   return (SCS_SUCCESS);
   }
   
/*------------------------------------------------------------------------------
Name      : TD1120P_EpSetStall
Purpose   : Stall an endpoint
Arguments : pc - peripheral controller handle
            ep - endpoint structure poiniter
            setOrClear - set or clear 
Returns   : None
Notes     : General Stall ep funtion.
-----------------------------------------------------------------------------*/
SctStatus TD1120P_EpSetStall(TdiPeriphController* pc, TdiPeriphEp *ep, \
                             SctBool setOrClear)
   {
   if (setOrClear)
      {
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_IN_STALLS_SET_REG, \
         (1 << ep->epNumber));
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_STALLS_SET_REG, \
         (1 << ep->epNumber));
      }
   else
      {
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_IN_STALLS_CLEAR_REG, \
         (1 << ep->epNumber));
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_STALLS_CLEAR_REG, \
         (1 << ep->epNumber));
      }
   return (SCS_SUCCESS);
   }

/*------------------------------------------------------------------------------
Name      : TD1120P_EpHaltSet
Purpose   : Set STALL on ep
Arguments : pc - peripheral controller handle
            ep - endpoint structure poiniter
Returns   : SctStatus.
Notes     :
-----------------------------------------------------------------------------*/
SctStatus TD1120P_EpHaltSet(TdiPeriphController* pc, TdiPeriphEp* ep)
   {
   if (ep == NULL)
      {
      PRINTF1(("\nTD1120P_EpHaltSet: Invalid handle."));
      return SCS_EP_INVALID;
      }
            
   TD1120P_EpSetStall(pc, ep, SCC_TRUE); 
   return (SCS_SUCCESS);
   }
   
/*------------------------------------------------------------------------------
Name      : TD1120P_EpHaltClear
Purpose   : Clear STALL on ep
Arguments : pc - peripheral controller handle
            ep - endpoint structure poiniter
Returns   : SctStatus.
Notes     :
-----------------------------------------------------------------------------*/
SctStatus TD1120P_EpHaltClear(TdiPeriphController* pc, TdiPeriphEp* ep)
   {
   if (ep == NULL)
      {
      PRINTF1(("\nTD1120P_EpHaltClear: Invalid handle."));
      return SCS_EP_INVALID;
      }
            
   TD1120P_EpSetStall(pc, ep, SCC_FALSE); 
   return (SCS_SUCCESS);
   }

/*------------------------------------------------------------------------------
Name      : TD1120P_EpInitialize
Purpose   : Initialize the EP just created.
Arguments : pc - peripheral controller handle
            ep - endpoint structure poiniter
Returns   : SctStatus
-----------------------------------------------------------------------------*/
SctStatus TD1120P_EpInitialize(TdiPeriphController *pc, TdiPeriphEp *ep)
   {
   U32 fifoStart, fifoEnd;
   
   fifoStart = pc->controllerResourceAddress;
   
   /* Set the FIFO size to be 2*MPS */
   if (ep->type == USB_CONTROL_ENDPOINT) 
      { 
      /* control endpoint */
      ep->controllerBufferSize = (ep->maxPktSize << 1);
      }
   else
      {
      ep->controllerBufferSize = ep->maxPktSize;
      }
   
   ep->controllerBufferSize += (ep->controllerBufferSize % 8);
       
   pc->controllerResourceAddress = (fifoStart + ep->controllerBufferSize);

   /* control endpoint fifo start is zero.  Since MPS for control endpoint is always 
      DWORD aligned, the fifo end address will always be >= 4.  For non-control 
      endpoint, the fifo does not start at address zero, the fifo size is >= 8, hence
      the fifo end will be >= 4 */
   fifoEnd = (pc->controllerResourceAddress - 4);
   
   /* Clear the EpToggle */   
   if (ep->type == USB_CONTROL_ENDPOINT)
      {
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_IN_TOGGLE_BITS_CLEAR_REG, \
         (1 << ep->epNumber));
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_TOGGLE_BITS_CLEAR_REG, \
         (1 << ep->epNumber));
      }
   else
      {
      if (ep->direction)
         {
         SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_IN_TOGGLE_BITS_CLEAR_REG, \
            (1 << ep->epNumber));
         }
      else
         {
         SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_TOGGLE_BITS_CLEAR_REG, \
            (1 << ep->epNumber));
         }
      }
   
   /* Initialize the  endpoint. Ensure that the FIFO size, the FIFO 
   ** threshold are DWord aligned  
   */
   TD1120P_Configure_EP(pc->ioBase, ep->epNumber, ep->type, ep->direction, \
      ep->maxPktSize, (ep->maxPktSize + (ep->maxPktSize % 4)), fifoStart, fifoEnd); 

   /* EP Enables */
   if (ep->type == USB_CONTROL_ENDPOINT)
      {
      /* Enable IN EP */
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_IN_EN_SET_REG, \
         (1 << ep->epNumber));
      /* Enable OUT EP */
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_EN_SET_REG, \
         (1 << ep->epNumber));
      /* Set OUT NAK so that we don't receive anything before the software wants to */
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_NAK_SET_REG, \
         (1 << ep->epNumber));
      }
   else
      {
      if (ep->direction == USB_REQUEST_DIRECTION_IN)
         {
         /* Enable IN EP */
         SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_IN_EN_SET_REG, \
            (1 << ep->epNumber));
         }
      else
         {
         /* Enable OUT EP */
         SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_EN_SET_REG, \
            (1 << ep->epNumber));
     
         /* Set OUT NAK so that we don't receive anything before the software wants to */
         SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_OUT_NAK_SET_REG, \
            (1 << ep->epNumber));
         }
      }
   return (SCS_SUCCESS);
   }


/*------------------------------------------------------------------------------
Name      : TD1120P_CreateEp
Purpose   : Create EP structure and initialize the endpoint
Arguments : pc - peripheral controller handle 
            epAddr - endpoint address 
            epDirectoin - direction of transfer
            epType - type of endpoint (e.g. control, iso, etc)
            epMPS - endpoint MPS
            periphEp - pointer to TDI endpoint structure pointer
Returns   : DEndpoint *
Notes     :   
-----------------------------------------------------------------------------*/
SctStatus TD1120P_CreateEp(TdiPeriphController* pc, U32 epAddr, U32 epDirection, \
                           U32 epType, U32 epMps, TdiPeriphEp** periphEp)
   {
   TdiPeriphEp* ep;
   
   if (pc == NULL)
      {
      PRINTF1(("\nTD1120P_CreateEp: Invalid handle."));
      return SCS_ERROR_INVALID_HANDLE;
      }

   if ((epAddr & USB_MASK_ENDPOINT_ADDRESS) > TD1120_DEV_MAX_EP) 
      {
      PRINTF1(("\nTD1120P_CreateEp: Invalid Ep handle."));
      return SCS_EP_INVALID;
      }

   ep = SYS_Malloc(sizeof(TdiPeriphEp));
   if (ep == NULL)
      {
      return NULL;
      }

   SYS_MemSet(ep, 0, sizeof(TdiPeriphEp));

   ep->maxPktSize = epMps;
   ep->transferThreshold = epMps;
   ep->epNumber = (epAddr & USB_MASK_ENDPOINT_ADDRESS); 
   ep->type = epType;
   ep->direction = epDirection;
   
   pc->ep[ep->epNumber] = ep;
   
   if (SCS_SUCCESS != TD1120P_EpInitialize(pc, ep))
      {
      PRINTF1(("\nTD1120P_CreateEp: Insufficient resources."));
      return (SCS_INSUFFICIENT_RESOURCE);
      }
   
   *periphEp = ep;
   
   return (SCS_SUCCESS);
   }
   
/*------------------------------------------------------------------------------
Name      : TD1120P_EpCleanup
Purpose   : Clean up the ep
Arguments : pc - peripheral controller handle
            ep - endpoint structure pointer
Returns   : None
Notes     :
-----------------------------------------------------------------------------*/
void TD1120P_EpDeinitialize(TdiPeriphController* pc, TdiPeriphEp *ep)
   {
   if (ep->direction == USB_REQUEST_DIRECTION_IN)
      {
      /* Disable IN EP */
      SYS_Write1120P_Register(pc->ioBase, TD1120_DEV_EP_IN_EN_CLEAR_REG, \
         (1 << ep->epNumber));
      }
   else
      {

⌨️ 快捷键说明

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