lpcware

USB_Configure_Event called by USBD_API->hw->Init

Discussion created by lpcware Employee on Jun 15, 2016
Content originally posted in LPCWare by namorada on Thu Dec 10 02:54:29 MST 2015
Hello,

I'm working on a new design. I have troubleshooting on it. indeed, i start to work from bwtest example from nxp libusb.
I integrate it in my code:

g_pUsbApi = (const USBD_API_T *) LPC_ROM_API->usbdApiBase;

memset((void *) &usb_param, 0, sizeof(USBD_API_INIT_PARAM_T));

usb_param.usb_reg_base = LPC_USB_BASE;
usb_param.max_num_ep = 4;
usb_param.mem_base = USB_STACK_MEM_BASE;
usb_param.mem_size = USB_STACK_MEM_SIZE;

usb_param.USB_Configure_Event = USB_ConfigureEvent;

desc.device_desc = (u8 *) USB_DeviceDescriptor;
desc.string_desc = (u8 *) USB_StringDescriptor;

desc.high_speed_desc = USB_HsConfigDescriptor;
desc.full_speed_desc = USB_FsConfigDescriptor;
desc.device_qualifier = (u8 *) USB_DeviceQualifier;

EP2.bLength = USB_ENDPOINT_DESC_SIZE;
EP2.bDescriptorType = USB_ENDPOINT_DESCRIPTOR_TYPE;
EP2.bEndpointAddress = USB_IN_EP;
EP2.bmAttributes = USB_ENDPOINT_TYPE_INTERRUPT;
EP2.wMaxPacketSize = 64;
EP2.bInterval = 0x5;

retour = USBD_API->hw->Init(&g_hUsb, &desc, &usb_param);
if (retour == LPC_OK)  // Initalisation validée?
{

g_u8_ConfigUSB = 1;
// Déclaration de l'Endpoint0
pCtrl = (USB_CORE_CTRL_T *) g_hUsb;/* convert the handle to control structure */
g_Ep0BaseHdlr = pCtrl->ep_event_hdlr[0];/* retrieve the default EP0_OUT handler */
pCtrl->ep_event_hdlr[0] = EP0_Hdlr;/* set our patch routine as EP0_OUT handler */

/* allocate TDs for bandwidth test in USB accessable memory*/
while (usb_param.mem_base & 0x1F) {
usb_param.mem_base++; usb_param.mem_size--;
}
// Allocation des DTD
//g_dtdPool[0] = (DTD_T *) usb_param.mem_base;  // EP1_OUT
//usb_param.mem_base += (8 * sizeof(DTD_T));// EP1_OUT
//g_dtdPool[1] = (DTD_T *) usb_param.mem_base;// EP1_IN
//usb_param.mem_base += (8 * sizeof(DTD_T));// EP1_IN
//g_dtdPool[2] = (DTD_T *) usb_param.mem_base;// EP2_OUT
//usb_param.mem_base += (8 * sizeof(DTD_T));// EP2_OUT
g_dtdPool[3] = (DTD_T *) usb_param.mem_base;// EP2_IN
usb_param.mem_base += (8 * sizeof(DTD_T));// EP2_IN
usb_param.mem_size -= (4 * 8 * sizeof(DTD_T));

ep_indx = ((EP2_IN & 0x0F) << 1) + 1;// = 5 pour EP2_IN = 0x82
retour = USBD_API->core->RegisterEpHandler(g_hUsb, ep_indx, EP2_Hdlr, &g_dtdPool[3]);
if(retour == LPC_OK)
{
IT_NVIC_AutoriseInt(USB0_IRQn);

// Connect USB
USBD_API->hw->Connect(g_hUsb, 1);

}

}


And, here is my "USB_Configure_Event" functions:

static ErrorCode_t USB_ConfigureEvent(USBD_HANDLE_T hUsb)
{

static USB_CORE_CTRL_T *pCtrl;


USB_Prog_DTD(EP2_IN_INDEX, EP2_IN_BIT);/* EP2_IN */
pCtrl = (USB_CORE_CTRL_T *) hUsb;
if(pCtrl->config_value == 1)
{
USBD_API->hw->ConfigEP(g_hUsb, &EP2);
USBD_API->hw->EnableEP(g_hUsb, USB_IN_EP);
USBD_API->hw->ResetEP(g_hUsb, USB_IN_EP);
g_u32_NbOctetEP2 = USBD_API->hw->WriteEP(g_hUsb, USB_IN_EP, (u8*)g_tu8_TabEcg, 0);
}
return LPC_OK;
}


void USB_Prog_DTD(u32 epIndex, u32 epBit)
{

u8 u8_i;
u32 dtd_phys;
dtd_phys = 1;
DQH_T *ep_QH = (DQH_T *) LPC_USB->ENDPOINTLISTADDR;
// Récupération des adresse physiques
dtd_phys = (u32) (&g_dtdPool[epIndex - 2][0]);

// Récupération de DTD pour l'endpoint passé en param.
DTD_T *ep_TD = &g_dtdPool[epIndex - 2][0];

// Initialisation
memset((void *) dtd_phys, 0, 8 * sizeof(DTD_T));

for (u8_i = 0; u8_i < 8; u8_i++)
{
ep_TD[u8_i].next_dTD = (u32) (dtd_phys + ((1 + u8_i) * sizeof(DTD_T)));
ep_TD[u8_i].total_bytes = (0x5000 << 16) | TD_IOC | 0x80;
ep_TD[u8_i].buffer0 = USB_DEST_ADDR;
ep_TD[u8_i].buffer1 = (USB_DEST_ADDR + 0x1000);
ep_TD[u8_i].buffer2 = (USB_DEST_ADDR + 0x2000);
ep_TD[u8_i].buffer3 = (USB_DEST_ADDR + 0x3000);
ep_TD[u8_i].buffer4 = (USB_DEST_ADDR + 0x4000);
ep_TD[u8_i].reserved = 0;
}

ep_TD[7].next_dTD = 0x01;

// Mise à jour de la file des endpoint
ep_QH[epIndex].next_dTD = dtd_phys;
ep_QH[epIndex].total_bytes &= (~0xC0);
// Activation de l'endpoint
LPC_USB->ENDPTPRIME |= epBit;

}


My problem is that the "USB_Configure_Event" is call directly by USBD_API->hw->Init, who will call a memset on a array not initialized which will cause an hardfault.
If i tried with only the example, the "USB_Configure_Event" is called only after USBD_API->hw->Connect(g_hUsb, 1), which is totally normal.

Seems that in my code, the host already see the device without calling USBD_API->hw->Connect(g_hUsb, 1);...

Anyt idea?

Outcomes