lpcware

ReadEp does not work on custom USB class

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by giedrius on Fri Jul 19 05:22:19 MST 2013
Hello,
Hopefully I'm posting in the right section.

I am trying to do double CDC support for the LPC11U37 device. To my understanding the device can support only one CDC class by itself, so I have to build a custom CDC class handler. The first CDC instance is configured to be handled by the hardware, while the second one I am trying to implement as a custom class myself.

Up to this point everything is working (I see two virtual COM ports on PC, I can send and receive data), but the problem comes when I try to read data in control messages for the custom class. Calling ReadEp function in USB_EVT_OUT setup always return 0, although USB analyzer clearly shows that the data have been sent and acknowledged (ACK).

I have attached EP0 handler using
pUsbApi->core->RegisterClassHandler(hUsb, EP0_hdlr, NULL);


The code for EP0_hdlr is this:

ErrorCode_t EP0_hdlr(USBD_HANDLE_T hUsb, void* data, uint32_t event) {
evnts[numEvt++] = event;

if (event == USB_EVT_SETUP) {
USB_SETUP_PACKET packet;
uint32_t len = pUsbApi->hw->ReadSetupPkt(hUsb, USB_ENDPOINT_OUT(0), (uint32_t*)&packet);

if ((packet.bmRequestType.B & 0x7F) == 0x21) { // Type=Class, Recipient=Interface
if (packet.wIndex.W == 2) { // OSC CDC CIF interface (2)
switch (packet.bRequest) {
case 0x20: { // SET_LINE_CODING
if (packet.wLength != 7)
return ERR_USBD_INVALID_REQ;

return LPC_OK;
}
case 0x21: { // GET_LINE_CODING
if (packet.wLength != 7)
return ERR_USBD_INVALID_REQ;

uint8_t lcs[] = { 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
pUsbApi->hw->WriteEP(pUsbHandle, USB_ENDPOINT_IN(0), lcs, 7);
return LPC_OK;
}
case 0x22: { // SET_CONTROL_LINE_STATE
if (packet.wLength != 0)
return ERR_USBD_INVALID_REQ;

pUsbApi->hw->WriteEP(pUsbHandle, USB_ENDPOINT_IN(0), NULL, 0);
return LPC_OK;
}
}
}
}
} else if (event == USB_EVT_OUT) {
numEvtOut++;
lenEvtOut += pUsbApi->hw->ReadEP(hUsb, USB_ENDPOINT_OUT(0), (uint8_t*)tmpStpBuf);
} else if (event == USB_EVT_IN) {

}

return ERR_USBD_UNHANDLED;
}


As you can see I have few debug variables: evnts - which logs the incoming events, numEvnt - event count, numEvntOut - number of OUT events and lenEvtOut - ammount of data received in OUT events.

For the evnts I get array [1, 3, 1, 3, 1, 3, 1, 2, 1, 3, 1, 3, 1, 3, 1, 2, 1, 3] which matches the USB analyzer log: 4*GET_LINE_CODING(Setup received(1) + IN sent(3)) 1*SET_LINE_CODING(Setup received + OUT received) 3*GET_LINE_CODING 1*SET_LINE_CODING 1*GET_LINE_CODING.
For the numEvntOut I get, as expected, 2. However lenEvtOut is 0, although it should be 14 (2*7) as two SET_LINE_CODING messages arrived, which had 7 bytes of data each.

Any help is appreciated. Thanks!

Outcomes