cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USB device : First data byte always missing after device initialization and enumeration

ritesh unde
Associate
Posted on October 25, 2017 at 08:47

Hi,

I am using STM32F401RE. For my USB device application I used DFU class stack from STM32cube mx.

I have ported stack to my application,It is working fine but I always misses first byte received after initialization of USB device and subsequent enumeration.

HAL_PCD_EP_GetRxCount returns correct number bytes but HAL_PCD_EP_Receive gives no data.

Following is code snippet of DATA out stage :

volatile unsigned char rite_14[68];

volatile unsigned int rite_11=0;

USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev , uint8_t epnum, uint8_t *pdata)

{

USBD_EndpointTypeDef *pep;

unsigned int rite_12=0;

if(epnum == 0)

{

pep = &pdev->ep_out[0];

if ( pdev->ep0_state == USBD_EP0_DATA_OUT)

{

if(pep->rem_length > pep->maxpacket)

{

pep->rem_length -= pep->maxpacket;

USBD_CtlContinueRx (pdev,

pdata,

MIN(pep->rem_length ,pep->maxpacket));

}

else

{

if((pdev->pClass->EP0_RxReady != NULL)&&

(pdev->dev_state == USBD_STATE_CONFIGURED))

{

pdev->pClass->EP0_RxReady(pdev);

}

USBD_CtlSendStatus(pdev);

}

}

}

else if((pdev->pClass->DataOut != NULL)&&

(pdev->dev_state == USBD_STATE_CONFIGURED))

{

//pdev->pClass->DataOut(pdev, epnum); not requred

if(epnum == 0x02)

// My application endpoint number

{

rite_11 = HAL_PCD_EP_GetRxCount(pdev->pData, epnum);

rite_12 = HAL_PCD_EP_Receive(pdev->pData, epnum, rite_14, rite_11);

EP2_Handler(rite_14, rite_11);

// appliaction layer processing

}

}

return USBD_OK;

}

#usb-device #stm32_usb_device_library #usb-implementation #usb-fs #usb-stm32f4
1 ACCEPTED SOLUTION

Accepted Solutions
ritesh unde
Associate
Posted on October 30, 2017 at 06:07

Hi,

It has been solved while porting I have not added HAL_PCD_EP_Receive in USBD_SetClassConfig function, which as per @ben k initiates data reception.

Thank you

View solution in original post

2 REPLIES 2
Ben K
Senior III
Posted on October 28, 2017 at 10:49

The HAL_PCD_EP_Receive() API initiates data reception through an endpoint to the specified RAM location with the maximum length. The HAL_PCD_EP_GetRxCount() API returns the actually received data's size after the completion of the endpoint transfer.

You first have to initiate a reception, then when the DataOutStage is called, you already have the data in your buffer, you only need to determine the actual length to process the data. After it is done, you can initiate another reception.

ritesh unde
Associate
Posted on October 30, 2017 at 06:07

Hi,

It has been solved while porting I have not added HAL_PCD_EP_Receive in USBD_SetClassConfig function, which as per @ben k initiates data reception.

Thank you