AnsweredAssumed Answered

Using USB module code generated by KDS processor expert cannot enum my device,why?

Question asked by haibo deng on Jun 11, 2015

Hi:

 

We just have a board with the processor type MK64FN1M0VLQ12. And we use the KDS processor expert to generate the module low level drivers.

But it has a problem as follows.

When we want to test the USB interface,we call the functions supported by the USB1.c just like the help document says,but I found it can not enum

my device.

I can receive the first setup packet from the host .It request a device descriptor,and I send my descriptor to the host. Next to it ,I found the host reset

my device and request a device descriptor again. I send my descriptor again,and the host reset my device.

The host reset my device 4 times and the suspend it.Why?

 

I think the host will send a set address request following the device descriport request,but I never receive it.

 

Can anyone help me?Thanks very much.

 

Here is my usb.c and debug information:

/*

* usb.c

*

*  Created on: 2015年4月15日

*  Author: Denghaibo

*/

#include "Cpu.h"

#include "Events.h"

#include "Pins1.h"

#include "USB1.h"

 

 

/* Including shared modules, which are used for whole project */

#include "PE_Types.h"

#include "PE_Error.h"

#include "PE_Const.h"

#include "IO_Map.h"

#include "PDD_Includes.h"

#include "Init_Config.h"

#include "stdio.h"

#include "common.h"

 

TMyDevData MyDevData;

bool CallbackCalled = FALSE;

 

 

void Callback(LDD_TDeviceData *pDevData, LDD_USB_Device_TTD* pTD)

{

  word Sn;

  printStr("DataTDPtr done!\r\n");

  pTD->CallbackFnPtr = 0;

}

void Callback1(LDD_TDeviceData *pDevData, LDD_USB_Device_TTD* pTD)

{

  word Sn;

  printStr("HskTDPtr done!\r\n");

   pTD->CallbackFnPtr = 0;

}

 

extern LDD_USB_TDevDescriptor MyUSBDevDescriptor;

extern uint8_t USB_Config_Descriptor[9+9+7+7];

extern LDD_USB_TEpDescriptor USB_Ep1OutDescriptor;

extern LDD_USB_TEpDescriptor USB_Ep1InDescriptor;

extern uint8_t Language[4];

extern unsigned char ManufacturerStr[22];

extern unsigned char ProductStr[16];

extern unsigned char SerialNumberStr[26];

 

LDD_TError USER_USB1_DevInitEndpoint(LDD_TUserData *UserDataPtr)

{

  LDD_TError error = ERR_OK;

  TMyDevData *tMyDevData = (TMyDevData *)UserDataPtr;

  error = USB1_DeviceInitEndpoint((LDD_TDeviceData *)tMyDevData->USB1_LDDDevDataPtr, (LDD_USB_TEpDescriptor *)&MyUSBDevDescriptor, 0x00);

  return error;

}

/*

** ==================================================================

**     Method      :  USER_USB_init (void)

*/

/*!

**     @brief

**         Call the driver function USB1_Init(),then enable the pull-up

**         resister, wait for the host to config the USBFS.

**     @param

**         UserDataPtr     - NULL

**     @return

**                         - NULL

*/

/* ===================================================================*/

 

LDD_TError USER_USB_init(void)

{

  LDD_TError error = ERR_OK;

   MyDevData.USB1_LDDDevDataPtr = USB1_Init((LDD_TUserData *)&MyDevData);         /* Initialize the device */

      USB0_USBCTRL=0x40;

   error = USB1_Enable(MyDevData.USB1_LDDDevDataPtr);

   return error;

}

/*

* 标准请求

*/

#define USB_DEV_GET_STATUS 0x00

#define USB_DEV_CLR_FEATURE 0x01

#define USB_DEV_SET_FEATURE 0x03

#define USB_DEV_SET_ADDRESS 0x05

#define USB_DEV_GET_DESCRIPTOR 0x06

#define USB_DEV_SET_DESCRIPTOR 0x07

#define USB_DEV_GET_CONFIG   0x08

#define USB_DEV_SET_CONFIG   0x09

#define USB_DEV_GET_INTERFACE   0x0A

#define USB_DEV_SET_INTERFACE   0x0B

#define USB_SYNCH_FRAME   0x0C

 

uint8_t bAsciiBuffer[100];

 

void USER_USB1_OnDeviceSetupPacket(LDD_TUserData *UserDataPtr, uint8_t EpNum)

{

  TMyDevData *MyDevDataPtr = (TMyDevData *)UserDataPtr;

  LDD_TDeviceData *USB_LDDDevDataPtr = MyDevDataPtr->USB1_LDDDevDataPtr;

  LDD_USB_TSDP                    *pSDP = &MyDevDataPtr->SDP;

 

 

  LDD_USB_Device_TTD              *DataTDPtr = &MyDevDataPtr->DataTD;//

  LDD_USB_Device_TTD              *HskTDPtr = &MyDevDataPtr->HskTD;

  LDD_TError                       res = ERR_OK;

  int                              DestIdx;

  uint8_t* pSetupData;

  uint8_t i;

  uint16_t bBytesSend,error;

 

  USB1_DeviceReadSetupData(USB_LDDDevDataPtr,EpNum,pSDP);

 

  DataTDPtr->Head.EpNum      = 0x00u;

  DataTDPtr->Head.BufferPtr  = NULL;

  DataTDPtr->Head.BufferSize = 0x00u;

  DataTDPtr->Head.Flags      = 0x00;

 

  charChangeToAscii((byte*)pSDP,(byte*)bAsciiBuffer,8);

  AS1_SendBlock((byte*)&bAsciiBuffer[0],26,&bBytesSend);

 

   if ((pSDP->bmRequestType & LDD_USB_REQ_RECP_MASK) == LDD_USB_REQ_RECP_DEVICE) {

      /* Request for Device */

      switch(*((uint8*)&(pSDP->bRequest))) { /* Depend on request type call do: */

 

      case USB_DEV_GET_DESCRIPTOR: //获取描述符

 

      if(((pSDP->wValue >> 8)&0xff) == LDD_USB_DT_DEVICE)

      {

   DataTDPtr->Head.EpNum      = EpNum;

   DataTDPtr->Head.BufferPtr  = (LDD_TData*)&MyUSBDevDescriptor;

   DataTDPtr->Head.BufferSize = MyUSBDevDescriptor.bLength;

   DataTDPtr->Head.Flags      = LDD_USB_DEVICE_TRANSFER_FLAG_EXT_PARAM;

   DataTDPtr->CallbackFnPtr   = Callback;

   DataTDPtr->ParamPtr = 0;

      }

      else if(((pSDP->wValue >> 8)&0xff) == LDD_USB_DT_CONFIGURATION)

      {

   DataTDPtr->Head.EpNum      = EpNum;

   DataTDPtr->Head.BufferPtr  = (LDD_TData*)&USB_Config_Descriptor;

   DataTDPtr->Head.BufferSize = pSDP->wLength;

   DataTDPtr->Head.Flags      = 0;//LDD_USB_DEVICE_TRANSFER_FLAG_EXT_PARAM;

   DataTDPtr->CallbackFnPtr   = Callback;

   DataTDPtr->ParamPtr = 0;

      }

      else if(((pSDP->wValue >> 8)&0xff) == LDD_USB_DT_STRING)

      {

  if((pSDP->wValue&0xff) == 0)

  {

   DataTDPtr->Head.EpNum      = EpNum;

   DataTDPtr->Head.BufferPtr  = (LDD_TData*)&Language;

   DataTDPtr->Head.BufferSize = 0x04;

  }

  else if((pSDP->wValue&0xff) == 1)//索引1

  {

   DataTDPtr->Head.EpNum      = EpNum;

   DataTDPtr->Head.BufferPtr  = (LDD_TData*)&ManufacturerStr;

   DataTDPtr->Head.BufferSize = sizeof(ManufacturerStr);

  }

  else if((pSDP->wValue&0xff) == 2)//索引2

  {

   DataTDPtr->Head.EpNum      = EpNum;

   DataTDPtr->Head.BufferPtr  = (LDD_TData*)&ProductStr;

   DataTDPtr->Head.BufferSize = sizeof(ProductStr);

  }

  else if((pSDP->wValue&0xff) == 3)//索引3

  {

   DataTDPtr->Head.EpNum      = EpNum;

   DataTDPtr->Head.BufferPtr  = (LDD_TData*)&SerialNumberStr;

   DataTDPtr->Head.BufferSize = sizeof(SerialNumberStr);

  }

      }

      break;

        case USB_DEV_SET_ADDRESS:

          /* Note: USB State of the device is changed after successful status stage of transaction */

          /* Send zero-length data packet and set callback for this packet */

      DataTDPtr->Head.EpNum   = EpNum;

          DataTDPtr->Head.BufferSize    = 0x00;

 

 

          USB_PDD_WriteAddressReg(USB0_BASE_PTR, (((LDD_USB_TSetAddressRequest*)pSDP)->DeviceAddress));      /* Set device address to the 0 value */

          break;

        case USB_DEV_SET_CONFIG:

      DataTDPtr->Head.EpNum   = EpNum;

          DataTDPtr->Head.BufferSize    = 0x00;

      break;

        default:

          res = ERR_NOTAVAIL;

        break;

      }

    } else if ((pSDP->bmRequestType & LDD_USB_REQ_RECP_MASK) == LDD_USB_REQ_RECP_INTERFACE) {

      /* Request for Interface */

      DestIdx = (uint8_t)pSDP->wIndex;

  #define MAX_INTERFACE_NUMBER 1

      if (DestIdx > MAX_INTERFACE_NUMBER) {

        res = ERR_FAILED;                /* Interface number too big */

      } else {

 

      }

    } else if ((pSDP->bmRequestType & LDD_USB_REQ_RECP_MASK) == LDD_USB_REQ_RECP_ENDPOINT) {

      /* Request for Endpoint */

      DestIdx = ((uint8_t)pSDP->wIndex << 0x01) & 0xFF;

      if (pSDP->wIndex & 0x80) { /* IN(Tx) EP? */

        DestIdx |= 0x01;

      }

  #define MAX_EP_IDX 1

      if (DestIdx > MAX_EP_IDX) {

        res = ERR_FAILED;                /* Endpoint number too big */

      } else {

 

 

      }

    } else {

      /* Request for Other */

      res = ERR_NOTAVAIL;

    }

 

    if (res == ERR_OK) {

      HskTDPtr->Head.BufferSize = 0x00;

      HskTDPtr->Head.EpNum   = EpNum;

      HskTDPtr->Head.Flags      = LDD_USB_DEVICE_TRANSFER_FLAG_EXT_PARAM;

      HskTDPtr->CallbackFnPtr   = Callback1;

      HskTDPtr->ParamPtr   = 0;

      printStr("Return data:");

 

  charChangeToAscii((byte*) DataTDPtr->Head.BufferPtr,(byte*)bAsciiBuffer,DataTDPtr->Head.BufferSize);

  AS1_SendBlock((byte*)&bAsciiBuffer[0],DataTDPtr->Head.BufferSize * 3 + 2,&bBytesSend);

 

      if (pSDP->wLength == 0x00) {

        /* No data */

        DataTDPtr->Head.BufferSize = 0x00u;                   /* Send Zero-length data packet */

        (void)USB1_DeviceSendData(USB_LDDDevDataPtr, DataTDPtr);

      } else if (pSDP->bmRequestType & LDD_USB_DIR_IN) {

        /* Control read */

        error = USB1_DeviceSendData(USB_LDDDevDataPtr, DataTDPtr);

        error = USB1_DeviceRecvData(USB_LDDDevDataPtr, HskTDPtr);

      } else {

        /* Control write */

        (void)USB1_DeviceRecvData(USB_LDDDevDataPtr, DataTDPtr);

        (void)USB1_DeviceSendData(USB_LDDDevDataPtr, HskTDPtr);

      }

    }

    if (res != ERR_OK) {

      (void)USB1_DeviceStallEndpoint(USB_LDDDevDataPtr, 0x00, 0x00);

      /* Command is not handled or command error*/

    }

}

 

!

Original Attachment has been moved to: MK64-0606.rar

Outcomes