Author Topic: stm32 bulk transfer fail, but enumeration is OK  (Read 20556 times)

lanmanck

  • Member
  • ***
  • Posts: 6
stm32 bulk transfer fail, but enumeration is OK
« on: May 21, 2013, 03:12:48 am »
Hi,Guys:
I am using STM32L152, and want to change VirtualComPort project to a simple Bulk transfer project.
I modify the descriptor, use endpoint 0x81 and 0x03. Remove Sof_callback and some uart xfer function.
Now Windows 7 X64 can regonize the device, and driver can be loaded(WinUSB or WinDriver).
But when I perform PIPE write/read in WinDriver(easy to debug), it fail , same thing happened in WinUSB.
Use USBLyzer , it shows:
Stall PID

Here is the endp callback:

Code: [Select]
void EP1_IN_Callback (void)
{ USB_SIL_Write(ENDP1,USART_Rx_Buffer, 64);
SetEPTxValid(ENDP1);
}

void EP3_OUT_Callback(void)
{
  uint16_t USB_Rx_Cnt;

  /* Get the received data buffer and update the counter */
  USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, USB_Rx_Buffer);

  /* USB data will be immediately processed, this allow next USB traffic being
  NAKed till the end of the USART Xfer */

  //USB_To_USART_Send_Data(USB_Rx_Buffer, USB_Rx_Cnt);

  /* Enable the receive of data on EP3,stall */
  SetEPRxValid(ENDP3);
}

and here is the description:

Code: [Select]
/* USB Standard Device Descriptor */
const uint8_t Virtual_Com_Port_DeviceDescriptor[] =
  {
    0x12,   /* bLength=18 */
    USB_DEVICE_DESCRIPTOR_TYPE,     /* bDescriptorType */
    0x00,
    0x02,   /* bcdUSB = 2.00 (USB2.0) */
    0x00,   /* bDeviceClass: CDC=2,HID=3,DISPLAY=4,2->0(device) */
    0x00,   /* bDeviceSubClass */
    0x00,   /* bDeviceProtocol*/
    0x40,   /* bMaxPacketSize064 */
    0x16,
    0x40,   /* idVendor = 0x4016 ->4000 163580 */
    0x80,
    0x35,   /* idProduct = 0x3580 */
    0x00,
    0x01,   /* bcdDevice 1.00 */
    1,      /* Index of string descriptor describing manufacturerString_Descriptor[1] */
    2,      /* Index of string descriptor describing product */
    3,      /* Index of string descriptor describing the device's serial number */
    0x01    /* bNumConfigurations */
  };

const uint8_t Virtual_Com_Port_ConfigDescriptor[] =
  {
    /*Configuration Descriptor*/
    0x09,   /* bLength: Configuration Descriptor size */
    USB_CONFIGURATION_DESCRIPTOR_TYPE,      /* bDescriptorType: Configuration */
    VIRTUAL_COM_PORT_SIZ_CONFIG_DESC,       /*wTotalLength:no of returned bytes */
    0x00,
    0x01,   /* bNumInterfaces: 2 interface2->1 */
    0x01,   /* bConfigurationValue: Configuration value */
    0x00,   /* iConfiguration */
    0xC0,   /* bmAttributes: self powered(b7 - buspwr, b6 - selfpwr, b5 - rwu) */
    0x32,   /* MaxPower 100 mA */

    /* Interface Descriptor */
    0x09,   /* bLength: Interface Descriptor size */
    USB_INTERFACE_DESCRIPTOR_TYPE,  /* bDescriptorType: Interface */
    /* Interface descriptor type */
    0x00,   /* bInterfaceNumber: Number of Interface */
    0x00,   /* bAlternateSetting: Alternate setting */
    0x02,   /* bNumEndpoints: One endpoints used1->2 */
    0xFF,   /* bInterfaceClass: Communication Interface Class2->0xFF */
    0xFF,   /* bInterfaceSubClass: Abstract Control Model2->0xFF */
    0xFF,   /* bInterfaceProtocol: Common AT commands1->0xFF */
    0x00,   /* iInterface: */

    /*Endpoint 3 Descriptor*/
    0x07,   /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: Endpoint */
    0x03,   /* bEndpointAddress: (OUT3) */
    0x02,   /* bmAttributes: Bulk,0=CTRL,1=ISO,3=INT */
    0x40,   /* wMaxPacketSize: */
    0x00,
    0x00,   /* bInterval: ignore for Bulk transfer */

    /*Endpoint 1 Descriptor*/
    0x07,   /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: Endpoint */
    0x81,   /* bEndpointAddress: (IN1) */
    0x02,   /* bmAttributes: Bulk */
    0x40,   /* wMaxPacketSize: */
    0x00,
    0x00    /* bInterval */
  };

And, when I perform PIPE read/write, the CTR_LP() can not enter, it seems no transition Interrupt happen.
Any response is appreciated.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: stm32 bulk transfer fail, but enumeration is OK
« Reply #1 on: May 21, 2013, 09:37:29 am »
USBlyzer seems to be saying that an endpoint is returning STALL instead of the expected response (data, ACK, etc.)

Look at your endpoint's settings and what happens on receiving an IN or OUT token packet. Are your callbacks being called, and if not, why not?

lanmanck

  • Member
  • ***
  • Posts: 6
Re: stm32 bulk transfer fail, but enumeration is OK
« Reply #2 on: May 21, 2013, 09:29:57 pm »
Hi,Jan:
I finally read data from device successfully.
Because for IN endp, user should first fill a packet to IN endp, so the SIE will wait for PC's IN request, otherwise the EP_IN_Callback will not occur.
EP_in_callback just tell user last packet is transfered OK, but without last filling, this interrupt will never happen. Damn ST.
So next is the OUT endp and CONTROL endp read/write sequence,Could you share your experiment about the endp read/ write? here is my endp config:

Code: [Select]
void Virtual_Com_Port_Reset(void)
{
uint8_t buffer[64];

  /* Set Virtual_Com_Port DEVICE as not configured */
  pInformation->Current_Configuration = 0;

  /* Current Feature initialization */
  pInformation->Current_Feature = Virtual_Com_Port_ConfigDescriptor[7];

  /* Set Virtual_Com_Port DEVICE with the default Interface*/
  pInformation->Current_Interface = 0;

  SetBTABLE(BTABLE_ADDRESS);

  /* Initialize Endpoint 0 */
  SetEPType(ENDP0, EP_CONTROL);
  SetEPTxStatus(ENDP0, EP_TX_STALL); /* EP_TX_STALL->NAK from MASS */
  SetEPRxAddr(ENDP0, ENDP0_RXADDR);
  SetEPTxAddr(ENDP0, ENDP0_TXADDR);
  Clear_Status_Out(ENDP0);
  SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
  SetEPRxValid(ENDP0);

  /* Initialize Endpoint 1 */
  SetEPType(ENDP1, EP_BULK);
  SetEPTxAddr(ENDP1, ENDP1_TXADDR);
  SetEPTxStatus(ENDP1, EP_TX_NAK);
  SetEPRxStatus(ENDP1, EP_RX_DIS);

/* pre-fill a packet. */
  USB_SIL_Write(ENDP1,buffer, 64);
SetEPTxValid(ENDP1);


  /* Initialize Endpoint 3 */
  SetEPType(ENDP3, EP_BULK);
  SetEPRxAddr(ENDP3, ENDP3_RXADDR);
  SetEPRxCount(ENDP3, VIRTUAL_COM_PORT_DATA_SIZE);
  SetEPRxStatus(ENDP3, EP_RX_VALID);
  SetEPTxStatus(ENDP3, EP_TX_DIS);

  /* Set this device to response on default address */
  SetDeviceAddress(0);

  bDeviceState = ATTACHED;
}

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: stm32 bulk transfer fail, but enumeration is OK
« Reply #3 on: May 21, 2013, 09:34:26 pm »
Yes, the data has to be ready to go out when the IN token packet arrives.

For OUT transactions, the endpoint must be configured to ACK received data. (How to do that varies with the processor's architecture.) Then firmware can retrieve the data and set up the endpoint to ACK the next packet.

In a quick look at your code, I don't see anything wrong.
« Last Edit: May 21, 2013, 09:37:51 pm by Jan Axelson »

lanmanck

  • Member
  • ***
  • Posts: 6
Re: stm32 bulk transfer fail, but enumeration is OK
« Reply #4 on: May 22, 2013, 05:08:57 am »
Hi,Jan:
Now I can perform PIPE write.
I have check the code, actually, We must use ENDP0 , ENDP1, ENDP2, not ENDP3. because in usb_core.c when it's setted address, the code use a for loop :
Code: [Select]
void SetDeviceAddress(uint8_t Val)
{
  uint32_t i;
  uint32_t nEP = Device_Table.Total_Endpoint;

  /* set address in every used endpoint */
  for (i = 0; i < nEP; i++)
  {
    _SetEPAddress((uint8_t)i, (uint8_t)i);
  } /* for */
  _SetDADDR(Val | DADDR_EF); /* set device address and enable function */
}

That makes me crazy, because I set  Device_Table.Total_Endpoint=2, So endp3 totally not set address.!!!
Well, honestly, This code from ST is really BAD.
OK, only one problem left, I Still cannot receive or write any data to PIPE0, Will you give me some more advice?
Thanks.

lanmanck

  • Member
  • ***
  • Posts: 6
Re: stm32 bulk transfer fail, but enumeration is OK
« Reply #5 on: May 22, 2013, 05:29:58 am »
I just test with bushound, it works now.
Thank you.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: stm32 bulk transfer fail, but enumeration is OK
« Reply #6 on: May 22, 2013, 10:23:20 am »
Glad you got it working!