/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_device_register_service
* Returned Value : USB_OK or error code
* Comments       :
*     Registers a callback routine for a specified event or endpoint.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_register_service
   (
      /* [IN] Handle to the USB device */
      _usb_device_handle         handle,
      
      /* [IN] type of event or endpoint number to service */
      uint_8                     type,
      
      /* [IN] Pointer to the service's callback function */
      void(_CODE_PTR_ service)(pointer, uint_8, boolean, uint_8, uint_8_ptr, uint_32, uint_8)
   )
{ /* Body */
   USB_DEV_STATE_STRUCT_PTR   usb_dev_ptr;
   SERVICE_STRUCT_PTR         service_ptr;
   SERVICE_STRUCT_PTR _PTR_   search_ptr;
   int                        lockKey;
 
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   /* Needs mutual exclusion */
   lockKey = USB_lock();
   
   /* Search for an existing entry for type */
   for (search_ptr = &usb_dev_ptr->SERVICE_HEAD_PTR;
      *search_ptr;
      search_ptr = &(*search_ptr)->NEXT) 
   {
      if ((*search_ptr)->TYPE == type) 
      {
         /* Found an existing entry */
         USB_unlock(lockKey);
         USB_printf("_usb_device_register_service, service %d already opened\n");
         return USBERR_OPEN_SERVICE;
      } /* Endif */
   } /* Endfor */
   
   /* No existing entry found - create a new one */
   service_ptr = (SERVICE_STRUCT_PTR)USB_memalloc(sizeof(SERVICE_STRUCT));
   if (!service_ptr) 
   {
      USB_unlock(lockKey);
      USB_printf("_usb_device_register_service, malloc for %d bytes failed\n", 
                    sizeof(SERVICE_STRUCT));
      return USBERR_ALLOC;
   } /* Endif */

   service_ptr->TYPE = type;
   service_ptr->SERVICE = service;
   service_ptr->NEXT = NULL;
   *search_ptr = service_ptr;
   
   USB_unlock(lockKey);

   return USB_OK;
} /* EndBody */
void    mvUsbCh9GetStatus(_usb_device_handle handle, boolean setup,
                         SETUP_STRUCT* ctrl_req)
{ /* Body */
    uint_8                  endpoint, direction;
    uint_16                 usb_status;
    USB_DEV_STATE_STRUCT*   usb_dev_ptr = (USB_DEV_STATE_STRUCT*)handle;

    ARC_DEBUG_TRACE(ARC_DEBUG_FLAG_SETUP, "%s: setup=%d\n", __FUNCTION__, (int)setup);

    if(!setup)
        return;

    switch (ctrl_req->REQUESTTYPE)
    {
       case (REQ_DIR_IN | REQ_RECIP_DEVICE):
          /* Device request */
          _usb_device_get_status(handle, ARC_USB_STATUS_DEVICE, &usb_status);
          break;

       case (REQ_DIR_IN | REQ_RECIP_INTERFACE):
          /* Interface request */
          _usb_device_get_status(handle, ARC_USB_STATUS_INTERFACE, &usb_status);
          break;

       case (REQ_DIR_IN | REQ_RECIP_ENDPOINT):
          /* Endpoint request */
          endpoint = ctrl_req->INDEX & ARC_USB_STATUS_ENDPOINT_NUMBER_MASK;
          if( (ctrl_req->INDEX & (1 << REQ_DIR_OFFSET)) == REQ_DIR_IN)
            direction = ARC_USB_SEND;
          else
            direction = ARC_USB_RECV;

          usb_status = _usb_device_is_endpoint_stalled(handle, endpoint, direction);
          break;

       default:
          /* Unknown request */
           USB_printf("GetStatus: Unknown request type 0x%x\n", ctrl_req->REQUESTTYPE);
          _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
          return;
    } /* Endswitch */

    /* Send the requested data */
    *usb_dev_ptr->STATUS_PTR = USB_16BIT_LE(usb_status);
    _usb_device_send_data(handle, 0, (uint_8_ptr)usb_dev_ptr->STATUS_PTR, sizeof(uint_16));

    /* status phase */
    _usb_device_recv_data(handle, 0, NULL, 0);

    return;
} /* Endbody */
/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_device_unregister_service
* Returned Value : USB_OK or error code
* Comments       :
*     Unregisters a callback routine for a specified event or endpoint.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_unregister_service
   (
      /* [IN] Handle to the USB device */
      _usb_device_handle         handle,

      /* [IN] type of event or endpoint number to service */
      uint_8                     type
   )
{ /* Body */
   USB_DEV_STATE_STRUCT_PTR   usb_dev_ptr;
   SERVICE_STRUCT_PTR         service_ptr;
   SERVICE_STRUCT_PTR _PTR_   search_ptr;
   int                        lockKey;
 
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   /* Needs mutual exclusion */
   lockKey = USB_lock();
   
   /* Search for an existing entry for type */
   for (search_ptr = &usb_dev_ptr->SERVICE_HEAD_PTR;
      *search_ptr;
      search_ptr = &(*search_ptr)->NEXT) 
   {
      if ((*search_ptr)->TYPE == type) {
         /* Found an existing entry - delete it */
         break;
      } /* Endif */
   } /* Endfor */
   
   /* No existing entry found */
   if (!*search_ptr) 
   {
      USB_unlock(lockKey);
      USB_printf("_usb_device_unregister_service, no service found\n");
      return USBERR_CLOSED_SERVICE;
   } /* Endif */
   
   service_ptr = *search_ptr;
   *search_ptr = service_ptr->NEXT;

   USB_memfree((pointer)service_ptr);
   
   USB_unlock(lockKey);

   return USB_OK;

} /* EndBody */
/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_device_shutdown
*  Returned Value : USB_OK or error code
*  Comments       :
*        Shutdown an initialized USB device
*
*END*-----------------------------------------------------------------*/
void _usb_device_shutdown(_usb_device_handle handle)
{ /* Body */
    USB_DEV_STATE_STRUCT_PTR     usb_dev_ptr;
    SERVICE_STRUCT_PTR           service_ptr;
    int                          ep;

    ARC_DEBUG_TRACE(ARC_DEBUG_FLAG_CTRL, "shutdown\n");
   
    usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;

    for(ep=0; ep<(usb_dev_ptr->MAX_ENDPOINTS); ep++)
    {
        /* Cancel all transfers on all endpoints */
        while(_usb_device_get_transfer_status(handle, ep, ARC_USB_RECV) != 
                                                    ARC_USB_STATUS_IDLE)
        {
            _usb_device_cancel_transfer(handle, ep, ARC_USB_RECV);
        }
        while(_usb_device_get_transfer_status(handle, ep, ARC_USB_SEND) != 
                                                    ARC_USB_STATUS_IDLE)
        {
            _usb_device_cancel_transfer(handle, ep, ARC_USB_SEND);
        }
    }
    _usb_dci_vusb20_shutdown(usb_dev_ptr);
   
    /* Free all the Callback function structure memory */
    for( service_ptr = usb_dev_ptr->SERVICE_HEAD_PTR; service_ptr;
         service_ptr = service_ptr->NEXT) 
    {
        USB_printf("_usb_device_shutdown: free service_ptr = 0x%x\n", 
                            service_ptr);
        USB_memfree(service_ptr);
    }
    usb_dev_ptr->SERVICE_HEAD_PTR = NULL;

    _usb_device_cleanup(usb_dev_ptr);
} /* EndBody */
void    mvUsbCh9ClearFeature(_usb_device_handle handle, boolean setup,
                            SETUP_STRUCT* setup_ptr)
{ /* Body */
    uint_8   endpoint, direction;
    uint_16  usb_status;

    ARC_DEBUG_TRACE(ARC_DEBUG_FLAG_SETUP, "%s: setup=%d\n", __FUNCTION__, (int)setup);

    _usb_device_get_status(handle, ARC_USB_STATUS_DEVICE_STATE, &usb_status);
    if ((usb_status != ARC_USB_STATE_CONFIG) && (usb_status != ARC_USB_STATE_ADDRESS))
    {
        USB_printf("ClearFeature: Wrong USB state %d\n", usb_status);
        _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
        return;
    } /* Endif */

    if(!setup)
        return;

    switch (setup_ptr->REQUESTTYPE)
    {
        case (REQ_DIR_OUT | REQ_RECIP_DEVICE):
            /* DEVICE */
            switch(setup_ptr->VALUE)
            {
                case DEVICE_REMOTE_WAKEUP:
                    /* clear remote wakeup */
                    _usb_device_get_status(handle, ARC_USB_STATUS_DEVICE, &usb_status);
                    usb_status &= ~ARC_USB_REMOTE_WAKEUP;
                    _usb_device_set_status(handle, ARC_USB_STATUS_DEVICE, usb_status);
                    USB_printf("Clear REMOTE_WAKEUP feature\n");
                    break;

                case DEVICE_TEST_MODE:
                    /* Exit Test Mode */
                    _usb_device_set_status(handle, ARC_USB_STATUS_TEST_MODE, 0);
                    break;

                default:
                    USB_printf("ClearFeature: Unknown Device feature %d\n",
                                setup_ptr->VALUE);
                    _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
                    return;
            } /* Endif */
            break;

        case (REQ_DIR_OUT | REQ_RECIP_ENDPOINT):
            /* ENDPOINT */
            if (setup_ptr->VALUE != ENDPOINT_HALT)
            {
                USB_printf("ClearFeature: Wrong Endpoint feature %d\n",
                            setup_ptr->VALUE);
                _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
                return;
            } /* Endif */

            endpoint = setup_ptr->INDEX & ARC_USB_STATUS_ENDPOINT_NUMBER_MASK;
            if( (setup_ptr->INDEX & (1 << REQ_DIR_OFFSET)) == REQ_DIR_IN)
                direction = ARC_USB_SEND;
            else
                direction = ARC_USB_RECV;

            _usb_device_unstall_endpoint(handle, endpoint, direction);
            break;

        default:
            USB_printf("ClearFeature: Unknown REQUEST_TYPE %d\n",
                                setup_ptr->REQUESTTYPE);

            _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
            return;
    } /* Endswitch */

    /* status phase */
    _usb_device_send_data(handle, 0, 0, 0);
}
void    mvUsbCh9SetFeature(_usb_device_handle handle, boolean setup,
                          SETUP_STRUCT* setup_ptr)
{
   uint_16                  usb_status;
   uint_8                   endpoint, direction;
   USB_DEV_STATE_STRUCT*    usb_dev_ptr = (USB_DEV_STATE_STRUCT*)handle;

   ARC_DEBUG_TRACE(ARC_DEBUG_FLAG_SETUP, "%s: setup=%d\n", __FUNCTION__, (int)setup);

   if (setup)
   {
      switch (setup_ptr->REQUESTTYPE)
      {
         case (REQ_DIR_OUT | REQ_RECIP_DEVICE):
            /* DEVICE */
            switch (setup_ptr->VALUE)
            {
               case DEVICE_REMOTE_WAKEUP:
                  /* set remote wakeup */
                  _usb_device_get_status(handle, ARC_USB_STATUS_DEVICE, &usb_status);
                  usb_status |= ARC_USB_REMOTE_WAKEUP;
                  _usb_device_set_status(handle, ARC_USB_STATUS_DEVICE, usb_status);
                  USB_printf("Set REMOTE_WAKEUP feature\n");
                  break;

               case DEVICE_TEST_MODE:
                  /* Test Mode */
                  if( (setup_ptr->INDEX & 0x00FF) || (usb_dev_ptr->SPEED != ARC_USB_SPEED_HIGH) )
                  {
                     USB_printf("SetFeature: Wrong Test mode parameters: mode=%d, speed=%d\n",
                                (setup_ptr->INDEX & 0x00FF), usb_dev_ptr->SPEED);
                     _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
                     return;
                  } /* Endif */

                  _usb_device_get_status(handle, ARC_USB_STATUS_DEVICE_STATE, &usb_status);
                  if( (usb_status == ARC_USB_STATE_CONFIG)  ||
                      (usb_status == ARC_USB_STATE_ADDRESS) ||
                      (usb_status == ARC_USB_STATE_DEFAULT))
                  {
                      /* wait with Set Test mode */
                      ENTER_TEST_MODE = TRUE;
                      test_mode_index = (setup_ptr->INDEX & 0xFF00);
                      USB_printf("SetFeature: Prepare for Test mode 0x%x\n", test_mode_index);
                  }
                  else
                  {
                     USB_printf("SetFeature: Wrong USB state for Test mode: state=%d\n",
                                usb_status);
                     _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
                     return;
                  } /* Endif */
                  break;

               default:
                    USB_printf("SetFeature: Unknown Device feature %d\n",
                                setup_ptr->VALUE);
                  _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
                  return;
            } /* Endswitch */
            break;

         case (REQ_DIR_OUT | REQ_RECIP_ENDPOINT):
            /* ENDPOINT */
            if (setup_ptr->VALUE != ENDPOINT_HALT)
            {
                USB_printf("SetFeature: Unknown Endpoint feature %d\n",
                            setup_ptr->VALUE);
                _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
                return;
            } /* Endif */

            endpoint = setup_ptr->INDEX & ARC_USB_STATUS_ENDPOINT_NUMBER_MASK;
            if( (setup_ptr->INDEX & (1 << REQ_DIR_OFFSET)) == REQ_DIR_IN)
                direction = ARC_USB_SEND;
            else
                direction = ARC_USB_RECV;

            _usb_device_stall_endpoint(handle, endpoint, direction);
            break;

         default:
            USB_printf("SetFeature: Unknown REQUEST_TYPE %d\n",
                       setup_ptr->REQUESTTYPE);

            _usb_device_stall_endpoint(handle, 0, ARC_USB_RECV);
            return;
      } /* Endswitch */

      /* status phase */
      _usb_device_send_data(handle, 0, 0, 0);
   }
   else
   {
      if (ENTER_TEST_MODE)
      {
         /* Enter Test Mode */
          USB_printf("SetFeature: Activate Test mode 0x%x\n", test_mode_index);
         _usb_device_set_status(handle, ARC_USB_STATUS_TEST_MODE, test_mode_index);
      } /* Endif */
   } /* Endif */
}
Exemplo n.º 7
0
/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_dci_vusb20_cancel_transfer
*  Returned Value : USB_OK or error code
*  Comments       :
*        Cancels a transfer
*
*END*-----------------------------------------------------------------*/
uint_8 _usb_dci_vusb20_cancel_transfer
   (
      /* [IN] the USB_dev_initialize state structure */
      _usb_device_handle         handle,
     
      /* [IN] the Endpoint number */
      uint_8                     ep_num,
            
      /* [IN] direction */
      uint_8                     direction
   )
{ /* Body */
   USB_DEV_STATE_STRUCT_PTR             usb_dev_ptr;
   VUSB20_REG_STRUCT_PTR                dev_ptr;
   VUSB20_EP_TR_STRUCT_PTR              dTD_ptr, check_dTD_ptr;
   VUSB20_EP_QUEUE_HEAD_STRUCT_PTR      ep_queue_head_ptr;
   XD_STRUCT_PTR                        xd_ptr;
   uint_32                              temp, bit_pos;
   volatile unsigned long               timeout, status_timeout;
   
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   dev_ptr = (VUSB20_REG_STRUCT_PTR)usb_dev_ptr->DEV_PTR;
   
   bit_pos = (1 << (16 * direction + ep_num));
   temp = (2*ep_num + direction);
   
   ep_queue_head_ptr = (VUSB20_EP_QUEUE_HEAD_STRUCT_PTR)usb_dev_ptr->EP_QUEUE_HEAD_PTR + temp;
   
   /* Unlink the dTD */
   dTD_ptr = usb_dev_ptr->EP_DTD_HEADS[temp];
   
   if (dTD_ptr) 
   {
      ARC_DEBUG_CODE(ARC_DEBUG_FLAG_STATS, (usb_dev_ptr->STATS.usb_cancel_count++));

      check_dTD_ptr = (VUSB20_EP_TR_STRUCT_PTR)USB_DTD_PHYS_TO_VIRT(usb_dev_ptr, 
                            ((uint_32)USB_32BIT_LE(dTD_ptr->NEXT_TR_ELEM_PTR) & VUSBHS_TD_ADDR_MASK));

      if (USB_32BIT_LE(dTD_ptr->SIZE_IOC_STS) & VUSBHS_TD_STATUS_ACTIVE) 
      {
         /* Flushing will halt the pipe */
         /* Write 1 to the Flush register */
         dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTFLUSH = USB_32BIT_LE(bit_pos);

         /* Wait until flushing completed */
         timeout = 0x1000000;
         while (USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTFLUSH) & bit_pos) 
         {
            /* ENDPTFLUSH bit should be cleared to indicate this operation is complete */
            timeout--;
            if(timeout == 0)
            {
                USB_printf("USB Cancel: - TIMEOUT for ENDPTFLUSH=0x%x, bit_pos=0x%x \n", 
                      (unsigned)USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTFLUSH), 
                      (unsigned)bit_pos);
                break;
            }
         } /* EndWhile */
         status_timeout = 0x100000;
         while (USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTSTATUS) & bit_pos) 
         {
            status_timeout--;
            if(status_timeout == 0)
            {
                USB_printf("USB Cancel: - TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n", 
                      (unsigned)USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTSTATUS), 
                      (unsigned)bit_pos);
                break;
            }

            /* Write 1 to the Flush register */
            dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTFLUSH = USB_32BIT_LE(bit_pos);
         
            /* Wait until flushing completed */
            timeout = 0x1000000;
            while (USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTFLUSH) & bit_pos) 
            {
               /* ENDPTFLUSH bit should be cleared to indicate this operation is complete */
               timeout--;
               if(timeout == 0)
                {
                    USB_printf("USB Cancel: - TIMEOUT for ENDPTFLUSH=0x%x, ENDPTSTATUS=0x%x, bit_pos=0x%x\n", 
                            (unsigned)USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTFLUSH), 
                            (unsigned)USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTSTATUS),
                            (unsigned)bit_pos);
                    break;
                }
            } /* EndWhile */
         } /* EndWhile */
      } /* Endif */
      
      /* Retire the current dTD */
      dTD_ptr->SIZE_IOC_STS = 0;
      dTD_ptr->NEXT_TR_ELEM_PTR = USB_32BIT_LE(VUSBHS_TD_NEXT_TERMINATE);
      
      /* The transfer descriptor for this dTD */
      xd_ptr = (XD_STRUCT_PTR)dTD_ptr->SCRATCH_PTR->XD_FOR_THIS_DTD;
      dTD_ptr->SCRATCH_PTR->PRIVATE = (pointer)usb_dev_ptr;

      ARC_DEBUG_TRACE(ARC_DEBUG_FLAG_TRANSFER,
                      "cncl_%d: fri=0x%x, ep=%d%s, buf=%p, size=%d, xd=%p, dTD=%p %p, bit=0x%x\n",
                       usb_dev_ptr->STATS.usb_cancel_count & 0xFFFF, 
                       USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.USB_FRINDEX), 
                       ep_num, direction ? "in" : "out", 
                       xd_ptr->WSTARTADDRESS, xd_ptr->WTOTALLENGTH, xd_ptr, 
                       dTD_ptr, check_dTD_ptr, bit_pos);
       
      /* Free the dTD */
      _usb_dci_vusb20_free_dTD((pointer)dTD_ptr);
      
      /* Update the dTD head and tail for specific endpoint/direction */
      if (!check_dTD_ptr) 
      {
         usb_dev_ptr->EP_DTD_HEADS[temp] = NULL;
         usb_dev_ptr->EP_DTD_TAILS[temp] = NULL;
         if (xd_ptr) 
         {
            xd_ptr->SCRATCH_PTR->PRIVATE = (pointer)usb_dev_ptr;
            /* Free the transfer descriptor */
            _usb_device_free_XD((pointer)xd_ptr);
         } /* Endif */
         /* No other transfers on the queue */
         ep_queue_head_ptr->NEXT_DTD_PTR = USB_32BIT_LE(VUSB_EP_QUEUE_HEAD_NEXT_TERMINATE);
         ep_queue_head_ptr->SIZE_IOC_INT_STS = 0;
      } 
      else 
      {
         usb_dev_ptr->EP_DTD_HEADS[temp] = check_dTD_ptr;
            
         if (xd_ptr) 
         {
            if ((uint_32)check_dTD_ptr->SCRATCH_PTR->XD_FOR_THIS_DTD != (uint_32)xd_ptr) 
            {
               xd_ptr->SCRATCH_PTR->PRIVATE = (pointer)usb_dev_ptr;
               /* Free the transfer descriptor */
               _usb_device_free_XD((pointer)xd_ptr);
            } /* Endif */
         } /* Endif */
         
         if (USB_32BIT_LE(check_dTD_ptr->SIZE_IOC_STS) & VUSBHS_TD_STATUS_ACTIVE) 
         {         
            /* Start CR 1015 */
            /* Prime the Endpoint */
            dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTPRIME = USB_32BIT_LE(bit_pos);

            if (!(USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTSTATUS) & bit_pos)) 
            {
               timeout = 0x100000;
               while (USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTPRIME) & bit_pos) 
               {
                  /* Wait for the ENDPTPRIME to go to zero */
                  timeout--;
                  if(timeout == 0)
                  {
                      USB_printf("USB Cancel: - TIMEOUT for ENDPTPRIME=0x%x, bit_pos=0x%x\n", 
                                (unsigned)USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTPRIME), 
                                (unsigned)bit_pos);
                      break;
                  }
               } /* EndWhile */

               if (USB_32BIT_LE(dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTSTATUS) & bit_pos) 
               {
                  /* The endpoint was not not primed so no other transfers on 
                  ** the queue 
                  */
                  goto done;
               } /* Endif */
            } 
            else 
            {
               goto done;
            } /* Endif */

            /* No other transfers on the queue */
            ep_queue_head_ptr->NEXT_DTD_PTR = (uint_32)USB_32BIT_LE((uint_32)check_dTD_ptr);
            ep_queue_head_ptr->SIZE_IOC_INT_STS = 0;
   
            /* Prime the Endpoint */
            dev_ptr->REGISTERS.OPERATIONAL_DEVICE_REGISTERS.ENDPTPRIME = USB_32BIT_LE(bit_pos);
         } /* Endif */
      } /* Endif */
   } /* Endif */
   
done:

   /* End CR 1015 */  
   return USB_OK;
} /* EndBody */
Exemplo n.º 8
0
void USB_DEBUG_RADIO(void)
{
	int16_t ACCELDATA[3];
	int16_t GYRODATA[3];
//	int16_t TEMPDATA;
	int16_t MAGDATA[3];
	
	float ACCELDATA_F[3];
	float GYRODATA_F[3];
	float MAGDATA_F[3];
//	float TEMP_F;
	
	uint16_t ADCTEMP;

	static uint16_t MS5611_PROM[7];
	static float MS5611_TEMP_NoOffset[2],MS5611_TaPAfterOffset[2],PRESSUREDATA;
	
	MS5611_PROM_READ(MS5611_PROM);
	MS5611_GetTempture(CMD_MS5611_D2_OSR_4096, MS5611_PROM, MS5611_TEMP_NoOffset);
	MS5611_GetPressure(CMD_MS5611_D1_OSR_4096, MS5611_PROM, MS5611_TEMP_NoOffset, MS5611_TaPAfterOffset);
	PRESSUREDATA = MS5611_TaPAfterOffset[1] * 0.01f;
	
	if(BBSYSTEM.NRFONLINE==1)
		USB_printf("NRF is online!\n");
	
	if(BBSYSTEM.MPU9250ONLINE==1)	
		USB_printf("MPU checking pass!\n");
	
	READ_MPU9250_ACCEL_RAW(ACCELDATA);
	READ_MPU9250_GYRO_RAW(GYRODATA);
	READ_MPU9250_Bypass_MAG_RAW(MAGDATA);
//	TEMPDATA=READ_MPU9250_TEMP_RAW();
	
	ACCELDATA_F[0]=ACCELDATA[0]/ACCELLSB;
	ACCELDATA_F[1]=ACCELDATA[1]/ACCELLSB;
	ACCELDATA_F[2]=ACCELDATA[2]/ACCELLSB;	
	GYRODATA_F[0]=GYRODATA[0]/GYROLSB;
	GYRODATA_F[1]=GYRODATA[1]/GYROLSB;
	GYRODATA_F[2]=GYRODATA[2]/GYROLSB;
	MAGDATA_F[0]=MAGDATA[0]*MAGLSB;
	MAGDATA_F[1]=MAGDATA[1]*MAGLSB;
	MAGDATA_F[2]=MAGDATA[2]*MAGLSB;
//TEMP_F=TEMPDATA/340.0 + 36.53;
	
	ADCTEMP = adcBatteryConversion();
  USB_printf("The BatVolt is: %d mv\n",ADCTEMP);
	USB_printf("The Pressure is: %0.4f mbar\n", PRESSUREDATA);
	USB_printf("X axis Acceleration is: %0.4f g\n",ACCELDATA_F[0]);
	USB_printf("Y axis Acceleration is: %0.4f g\n",ACCELDATA_F[1]);
	USB_printf("Z axis Acceleration is: %0.4f g\n",ACCELDATA_F[2]);
	USB_printf("X axis Gyro is: %0.3f deg/s\n",GYRODATA_F[0]);
	USB_printf("Y axis Gyro is: %0.3f deg/s\n",GYRODATA_F[1]);
	USB_printf("Z axis Gyro is: %0.3f deg/s\n",GYRODATA_F[2]);
	USB_printf("X axis Mag is: %0.3f uT\n",MAGDATA_F[0]);
	USB_printf("Y axis Mag is: %0.3f uT\n",MAGDATA_F[1]);
	USB_printf("Z axis Mag is: %0.3f uT\n",MAGDATA_F[2]);
	USB_printf("==================================\n");
	
	//USB_TxWrite(NRF24L01_RXDATA,strlen(NRF24L01_RXDATA));
	//USB_printf("\n");
}
Exemplo n.º 9
0
/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_device_send_data
*  Returned Value : USB_OK or error code
*  Comments       :
*        Sends data on a specified endpoint.
*
*END*-----------------------------------------------------------------*/
uint_8 _usb_device_send_data
   (
      /* [IN] the USB_USB_dev_initialize state structure */
      _usb_device_handle         handle,
            
      /* [IN] the Endpoint number */
      uint_8                     ep_num,
            
      /* [IN] buffer to send */
      uint_8_ptr                 buff_ptr,
            
      /* [IN] length of the transfer */
      uint_32                    size
   )
{ /* Body */
   int 	                        lockKey;
   uint_8                       error = 0;
   XD_STRUCT_PTR                xd_ptr;
   USB_DEV_STATE_STRUCT_PTR     usb_dev_ptr;
   boolean                      toSend = TRUE;

   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;

   ARC_DEBUG_TRACE(ARC_DEBUG_FLAG_TX,
       "send_data: handle=%p, ep=%d, pBuf=0x%x, size=%d, EP_QH=%p\n", 
       handle, ep_num, (unsigned)buff_ptr, (int)size, usb_dev_ptr->EP_QUEUE_HEAD_PTR);

   ARC_DEBUG_CODE(ARC_DEBUG_FLAG_STATS, (usb_dev_ptr->STATS.usb_send_count++));
      
   lockKey = USB_lock();

   if (!usb_dev_ptr->XD_ENTRIES) 
   {
      USB_unlock(lockKey);
      USB_printf("_usb_device_send_data, transfer in progress\n");
      return ARC_USB_STATUS_TRANSFER_IN_PROGRESS;
   } /* Endif */

#if defined(USB_UNDERRUN_WA)
    {
        int 			                head;
       	VUSB20_EP_QUEUE_HEAD_STRUCT* 	ep_queue_head_ptr;

		ep_queue_head_ptr = (VUSB20_EP_QUEUE_HEAD_STRUCT_PTR)usb_dev_ptr->EP_QUEUE_HEAD_PTR + 
                       						                  2*ep_num + ARC_USB_SEND;

        if( ((ep_queue_head_ptr->MAX_PKT_LENGTH >> 16) & 0x7FF) > global_wa_threshold) 
        {
            /* Only Endpoints with maxPktSize more than 128 bytes need special processing */
            if( (size > global_wa_threshold) || 
                (usbSendQueue.num != 0) )
            {
/*
                USB_printf("_usb_device_send_data: ep_num=%d, maxPktSize=%d, size=%d\n",
                        ep_num, (ep_queue_head_ptr->MAX_PKT_LENGTH >> 16) & 0x7FF, size);
*/
                /* Check if usbSendQueue is not Full */
                if(usbSendQueue.num == MAX_XDS_FOR_TR_CALLS)
                {
                    USB_printf("ep=%d: usbSendQueue is FULL\n", ep_num);
                    USB_unlock(lockKey);
                    return USBERR_TX_FAILED;
                }

                /* Add to usbSendQueu */
                head = usbSendQueue.head;

                usbSendQueue.num++;
		        usbSendQueue.num_dma++;
                usbSendQueue.size[head] = size;
                usbSendQueue.buff_ptr[head] = buff_ptr;
                usbSendQueue.ep_num[head] = ep_num;

                head++;
                if(head == MAX_XDS_FOR_TR_CALLS)
                    head = 0;

                usbSendQueue.head = head;

                /* Process first usbSendQueue element if possible */
                if(usbSendQueue.num == 1)
                {
		            error = _usb_prepare_to_send(handle);
		        }
		        toSend = FALSE;
            }
        }
    }
#endif /* USB_UNDERRUN_WA */

    if(toSend == TRUE)
    {
        /* Get a transfer descriptor */
        USB_XD_QGET(usb_dev_ptr->XD_HEAD, usb_dev_ptr->XD_TAIL, xd_ptr);

        usb_dev_ptr->XD_ENTRIES--;

        if(buff_ptr != NULL)
            USB_dcache_flush((pointer)buff_ptr, size);   

        /* Initialize the new transfer descriptor */      
        xd_ptr->EP_NUM = ep_num;
        xd_ptr->BDIRECTION = ARC_USB_SEND;
        xd_ptr->WTOTALLENGTH = size;
        xd_ptr->WSOFAR = 0;
        xd_ptr->WSTARTADDRESS = buff_ptr;   
        xd_ptr->BSTATUS = ARC_USB_STATUS_TRANSFER_ACCEPTED;

        error = _usb_dci_vusb20_add_dTD(handle, xd_ptr);
    }
    USB_unlock(lockKey);
   
    if (error) 
    {
        USB_printf("_usb_device_send_data, transfer failed\n");
        return USBERR_TX_FAILED;
    } /* Endif */
    return error;

} /* EndBody */
Exemplo n.º 10
0
/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : usbSendComplete
*  Returned Value : None
*  Comments       :
*        Callback for send transfer complete event.
*
*END*-----------------------------------------------------------------*/
void    usbSendComplete(void* handle, uint_8 type, boolean setup, uint_8 dir, 
                        uint_8_ptr buffer, uint_32 length, uint_8 error)
{
    /* Check if this complete is one from the sendQueue */
    if( (usbSendQueue.ep_num[usbSendQueue.tail] == type) &&
        (usbSendQueue.num > 0) )
    {
        USB_DEV_STATE_STRUCT_PTR    usb_dev_ptr;
        uint_8*                     buff_ptr;
        uint_32                     size;
        int                         num, tail;

        usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;

        tail = usbSendQueue.tail;
        num = usbSendQueue.num;
        buff_ptr = usbSendQueue.buff_ptr[tail];
        size = usbSendQueue.size[tail];
/*
        USB_printf("usbSendComplete: num=%d, tail=%d, usbSentSize=%d, type=%d, length=%d (%d), buff=%p (%p)\n", 
                num, tail, usbSentSize, type, length, usbSendQueue.size[tail], 
                buffer, usbSendQueue.buff_ptr[tail]);
*/
        usbSentSize += length;

	    /* if the buffer was on the SRAM */
	    if( ((unsigned)buffer >= (unsigned)usbSramBase) &&
	    ((unsigned)buffer < ((unsigned)usbSramBase + (usbSramPartSize * global_wa_sram_parts))) )
	    {
	        sram_parts[sent_index] = S_FREE;
            sent_index++;
            if(sent_index == global_wa_sram_parts)
                sent_index = 0;
	    }

        if(usbSentSize >= usbSendQueue.size[tail])
        {
            /* Remove from the usbSendQueues */
            num--;
            tail++;
            if(tail == MAX_XDS_FOR_TR_CALLS)
                tail = 0;

            usbSendQueue.tail = tail;
            usbSendQueue.num = num;
            usbSentSize = 0;

            /* Call complete callback */
            _usb_device_call_service(handle, type, setup, dir, 
                         buff_ptr, size, error);

            if(num == 0)
                return;
        }

	    error = _usb_prepare_to_send(handle);	
        if (error) 
        {
            USB_printf("usbSendComplete, add_dTD failed\n");
        }	

    }
    else
    {
        /* Call complete callback */
        _usb_device_call_service(handle, type, setup, dir, 
                        buffer, length, error);
    }
}
Exemplo n.º 11
0
/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_device_recv_data
*  Returned Value : USB_OK or error code
*  Comments       :
*        Receives data on a specified endpoint.
*
*END*-----------------------------------------------------------------*/
uint_8 _usb_device_recv_data
   (
      /* [IN] the USB_USB_dev_initialize state structure */
      _usb_device_handle         handle,
            
      /* [IN] the Endpoint number */
      uint_8                     ep_num,
            
      /* [IN] buffer to receive data */
      uint_8_ptr                 buff_ptr,
            
      /* [IN] length of the transfer */
      uint_32                    size
   )
{ /* Body */
    int                              lockKey;
    uint_8                           error = USB_OK;
    XD_STRUCT_PTR                    xd_ptr;
    USB_DEV_STATE_STRUCT_PTR         usb_dev_ptr;
   
    usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;

    ARC_DEBUG_TRACE(ARC_DEBUG_FLAG_RX, "recv_data: ep=%d, buf_ptr=0x%x, size=%d\n",
                                       ep_num, (unsigned)buff_ptr, (int)size);

    ARC_DEBUG_CODE(ARC_DEBUG_FLAG_STATS, (usb_dev_ptr->STATS.usb_recv_count++));

    if(buff_ptr != NULL)
        USB_dcache_inv((pointer)buff_ptr,size);   
    
    lockKey = USB_lock();

    if (!usb_dev_ptr->XD_ENTRIES) 
    {
        USB_unlock(lockKey);
        USB_printf("_usb_device_recv_data, transfer in progress\n");
        return ARC_USB_STATUS_TRANSFER_IN_PROGRESS;
    } /* Endif */

    /* Get a transfer descriptor for the specified endpoint 
    ** and direction 
    */
    USB_XD_QGET(usb_dev_ptr->XD_HEAD, usb_dev_ptr->XD_TAIL, xd_ptr);
   
    usb_dev_ptr->XD_ENTRIES--;

    /* Initialize the new transfer descriptor */      
    xd_ptr->EP_NUM = ep_num;
    xd_ptr->BDIRECTION = ARC_USB_RECV;
    xd_ptr->WTOTALLENGTH = size;
    xd_ptr->WSOFAR = 0;
    xd_ptr->WSTARTADDRESS = buff_ptr;
   
    xd_ptr->BSTATUS = ARC_USB_STATUS_TRANSFER_ACCEPTED;

    error = _usb_dci_vusb20_add_dTD(handle, xd_ptr);

    USB_unlock(lockKey);
   
    if (error) 
    {
        USB_printf("_usb_device_recv_data, receive failed\n");
        return USBERR_RX_FAILED;
    } /* Endif */

    return error;

} /* EndBody */
Exemplo n.º 12
0
void pin_test(void) {
	uint32_t pin_counts[32] = { 0 };
	uint32_t data;
	uint32_t count = 0;

	USB_printf("************* sampling...\r\n");
	while (1) {
		// ZX_D0..ZX_D7
		data = GPIOB->IDR;
		if (data & 1)   pin_counts[0]++;
		if (data & 2)   pin_counts[1]++;
		if (data & 4)   pin_counts[2]++;
		if (data & 8)   pin_counts[3]++;
		if (data & 16)  pin_counts[4]++;
		if (data & 32)  pin_counts[5]++;
		if (data & 64)  pin_counts[6]++;
		if (data & 128) pin_counts[7]++;

		// ZX_A0..ZX_A15
		data = GPIOE->IDR;
		if (data & 1)     pin_counts[8]++;
		if (data & 2)     pin_counts[9]++;
		if (data & 4)     pin_counts[10]++;
		if (data & 8)     pin_counts[11]++;
		if (data & 16)    pin_counts[12]++;
		if (data & 32)    pin_counts[13]++;
		if (data & 64)    pin_counts[14]++;
		if (data & 128)   pin_counts[15]++;
		if (data & 256)   pin_counts[16]++;
		if (data & 512)   pin_counts[17]++;
		if (data & 1024)  pin_counts[18]++;
		if (data & 2048)  pin_counts[19]++;
		if (data & 4096)  pin_counts[20]++;
		if (data & 8192)  pin_counts[21]++;
		if (data & 16384) pin_counts[22]++;
		if (data & 32768) pin_counts[23]++;

		// control lines on port D
		data = GPIOD->IDR;
		if (data & ZX_RESET_Pin) pin_counts[24]++;
		if (data & ZX_MEMREQ_Pin) pin_counts[25]++;
		if (data & ZX_IOREQ_Pin) pin_counts[26]++;
		if (data & ZX_RD_Pin) pin_counts[27]++;
		if (data & ZX_WR_Pin) pin_counts[28]++;

		// control lines on port A
		data = GPIOA->IDR;
		if (data & ZX_ROMCS_Pin) pin_counts[29]++;
		if (data & ZX_WAIT_Pin) pin_counts[30]++;

		if (++count == 1000000) {
			// blink LED
			HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
			// dump pin counts
			USB_printf("************* systick=%ld\r\n", HAL_GetTick());
			report_pin("D0", pin_counts[0], count);
			report_pin("D1", pin_counts[1], count);
			report_pin("D2", pin_counts[2], count);
			report_pin("D3", pin_counts[3], count);
			report_pin("D4", pin_counts[4], count);
			report_pin("D5", pin_counts[5], count);
			report_pin("D6", pin_counts[6], count);
			report_pin("D7", pin_counts[7], count);
			report_pin("A0", pin_counts[8], count);
			report_pin("A1", pin_counts[9], count);
			report_pin("A2", pin_counts[10], count);
			report_pin("A3", pin_counts[11], count);
			report_pin("A4", pin_counts[12], count);
			report_pin("A5", pin_counts[13], count);
			report_pin("A6", pin_counts[14], count);
			report_pin("A7", pin_counts[15], count);
			report_pin("A8", pin_counts[16], count);
			report_pin("A9", pin_counts[17], count);
			report_pin("A10", pin_counts[18], count);
			report_pin("A11", pin_counts[19], count);
			report_pin("A12", pin_counts[20], count);
			report_pin("A13", pin_counts[21], count);
			report_pin("A14", pin_counts[22], count);
			report_pin("A15", pin_counts[23], count);
			report_pin("RESET", pin_counts[24], count);
			report_pin("MEMREQ", pin_counts[25], count);
			report_pin("IOREQ", pin_counts[26], count);
			report_pin("RD", pin_counts[27], count);
			report_pin("WR", pin_counts[28], count);
			report_pin("ROMCS", pin_counts[29], count);
			report_pin("WAIT", pin_counts[30], count);
			USB_printf("************* sampling...\r\n");

			count = 0;

			// zero pin counters
			for (int i = 0; i < 32; i++) {
				pin_counts[i] = 0;
			}
		}
	}

}
Exemplo n.º 13
0
void report_pin(const char *pin_name, uint32_t high_count, uint32_t total_samples_count) {
	uint32_t pct = 1000*high_count/total_samples_count;
	USB_printf("Pin %6s samples high: %7ld %3ld.%01ld%%\r\n", pin_name, high_count, pct/10, pct % 10);
}
Exemplo n.º 14
0
/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_device_init
*  Returned Value : USB_OK or error code
*  Comments       :
*        Initializes the USB device specific data structures and calls 
*  the low-level device controller chip initialization routine.
*
*END*-----------------------------------------------------------------*/
uint_8 _usb_device_init
   (
      /* [IN] the USB device controller to initialize */
      uint_8                    devnum,

      /* [OUT] the USB_USB_dev_initialize state structure */
      _usb_device_handle*       handle
   )
{ /* Body */
   USB_DEV_STATE_STRUCT_PTR         usb_dev_ptr;
   XD_STRUCT_PTR                    xd_ptr;
   uint_8                           i, error;
   SCRATCH_STRUCT_PTR               temp_scratch_ptr;

   /* global_import_funcs must be initailized before */
   if(global_import_funcs == NULL)
       return USBERR_INIT_FAILED;

   if (devnum > MAX_USB_DEVICES) 
   {
        USB_printf("_usb_device_init, error invalid device number");
        return USBERR_INVALID_DEVICE_NUM;
   } /* Endif */
   
   /* Allocate memory for the state structure */
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)USB_memalloc(sizeof(USB_DEV_STATE_STRUCT));      
   if (usb_dev_ptr == NULL) 
   {
        USB_printf("_usb_device_init, malloc of %d bytes for USB_DEV_STATE_STRUCT failed\n", 
                    sizeof(USB_DEV_STATE_STRUCT));
        return USBERR_ALLOC_STATE;
   } /* Endif */
   
   /* Zero out the internal USB state structure */
   USB_memzero(usb_dev_ptr, sizeof(USB_DEV_STATE_STRUCT));
    
   usb_dev_ptr->DEV_NUM = devnum;

   /* Multiple devices will have different base addresses and 
   ** interrupt vectors (For future)
   */   
   usb_dev_ptr->USB_STATE = ARC_USB_STATE_UNKNOWN;
   
   /* Allocate MAX_XDS_FOR_TR_CALLS */
   xd_ptr = (XD_STRUCT_PTR)USB_memalloc(sizeof(XD_STRUCT) * MAX_XDS_FOR_TR_CALLS);      
   if (xd_ptr == NULL) 
   {
        _usb_device_cleanup(usb_dev_ptr);
        USB_printf("_usb_device_init, malloc of %d bytes for %d XD_STRUCT failed\n", 
                        sizeof(XD_STRUCT) * MAX_XDS_FOR_TR_CALLS, MAX_XDS_FOR_TR_CALLS);
        return USBERR_ALLOC_TR;
   } /* Endif */
   
   usb_dev_ptr->XD_BASE = xd_ptr;

   _usb_clear_stats(usb_dev_ptr);

   USB_memzero(xd_ptr, sizeof(XD_STRUCT) * MAX_XDS_FOR_TR_CALLS);

   /* Allocate memory for internal scratch structure */   
   usb_dev_ptr->XD_SCRATCH_STRUCT_BASE = (SCRATCH_STRUCT_PTR)
                    USB_memalloc(sizeof(SCRATCH_STRUCT) * MAX_XDS_FOR_TR_CALLS);
   if (usb_dev_ptr->XD_SCRATCH_STRUCT_BASE == NULL) 
   {
        _usb_device_cleanup(usb_dev_ptr);
        USB_printf("_usb_device_init, malloc of %d bytes for %d XD_STRUCT failed\n", 
                        sizeof(SCRATCH_STRUCT) * MAX_XDS_FOR_TR_CALLS, MAX_XDS_FOR_TR_CALLS);
        return USBERR_ALLOC;
   } /* Endif */

   temp_scratch_ptr = usb_dev_ptr->XD_SCRATCH_STRUCT_BASE;
   usb_dev_ptr->XD_HEAD = NULL;
   usb_dev_ptr->XD_TAIL = NULL;
   usb_dev_ptr->XD_ENTRIES = 0;

   /* Enqueue all the XDs */   
   for (i=0;i<MAX_XDS_FOR_TR_CALLS;i++) 
   {
      xd_ptr->SCRATCH_PTR = temp_scratch_ptr;
      xd_ptr->SCRATCH_PTR->FREE = _usb_device_free_XD;
      xd_ptr->SCRATCH_PTR->PRIVATE = (pointer)usb_dev_ptr;
      _usb_device_free_XD((pointer)xd_ptr);
      xd_ptr++;
      temp_scratch_ptr++;
   } /* Endfor */

   usb_dev_ptr->TEMP_XD_PTR = (XD_STRUCT_PTR)USB_memalloc(sizeof(XD_STRUCT));
   if(usb_dev_ptr->TEMP_XD_PTR == NULL)
   {
        USB_printf("_usb_device_init, malloc of %d bytes for TEMP_XD_STRUCT failed\n", 
                        sizeof(XD_STRUCT));
        _usb_device_cleanup(usb_dev_ptr);
        return USBERR_ALLOC;
   }
   USB_memzero(usb_dev_ptr->TEMP_XD_PTR, sizeof(XD_STRUCT));

   /* Allocate 2 bytes for USB_STATUS to be sent over USB, so Cache line aligned */
   usb_dev_ptr->STATUS_UNAIGNED_PTR = (uint_8*)USB_memalloc(sizeof(uint_16) + PSP_CACHE_LINE_SIZE);
   if(usb_dev_ptr->STATUS_UNAIGNED_PTR == NULL)
   {
        USB_printf("_usb_device_init, malloc of %d bytes for USB_STATUS failed\n", 
                        sizeof(uint_16) + PSP_CACHE_LINE_SIZE);
        _usb_device_cleanup(usb_dev_ptr);
        return USBERR_ALLOC;
   }
   USB_memzero(usb_dev_ptr->STATUS_UNAIGNED_PTR, sizeof(uint_16) + PSP_CACHE_LINE_SIZE);
   usb_dev_ptr->STATUS_PTR = (uint_16*)USB_CACHE_ALIGN((uint_32)usb_dev_ptr->STATUS_UNAIGNED_PTR);

   /* Allocate 53 bytes for USB Test packet to be sent over USB, so Cache line aligned */
   usb_dev_ptr->TEST_PKT_UNAIGNED_PTR = (uint_8*)USB_memalloc(USB_TEST_MODE_TEST_PACKET_LENGTH + PSP_CACHE_LINE_SIZE);
   if(usb_dev_ptr->TEST_PKT_UNAIGNED_PTR == NULL)
   {
        USB_printf("_usb_device_init, malloc of %d bytes for USB Test packet failed\n", 
                        USB_TEST_MODE_TEST_PACKET_LENGTH + PSP_CACHE_LINE_SIZE);
        _usb_device_cleanup(usb_dev_ptr);
        return USBERR_ALLOC;
   }
   USB_memzero(usb_dev_ptr->TEST_PKT_UNAIGNED_PTR, USB_TEST_MODE_TEST_PACKET_LENGTH + PSP_CACHE_LINE_SIZE);
   usb_dev_ptr->TEST_PKT_PTR = (uint_8*)USB_CACHE_ALIGN((uint_32)usb_dev_ptr->TEST_PKT_UNAIGNED_PTR);

   /* Initialize the USB controller chip */
   error = _usb_dci_vusb20_init(devnum, usb_dev_ptr);
   if (error) 
   {
        _usb_device_cleanup(usb_dev_ptr);
        USB_printf("_usb_device_init, init failed");
        return USBERR_INIT_FAILED;
   } /* Endif */

   USB_printf("device_init: pDev=0x%x, pXD(%d)=0x%x, pSCRATCH(%d)=0x%x, pTempXD=0x%x\n",
                (unsigned)usb_dev_ptr, MAX_XDS_FOR_TR_CALLS, (unsigned)usb_dev_ptr->XD_BASE,
                MAX_XDS_FOR_TR_CALLS, (unsigned)usb_dev_ptr->XD_SCRATCH_STRUCT_BASE,
                (unsigned)usb_dev_ptr->TEMP_XD_PTR);

   *handle = usb_dev_ptr;
   return USB_OK;   
} /* EndBody */