cancel
Showing results for 
Search instead for 
Did you mean: 

Info about receiving buffer in usb cdc vcp application with stm32cube

valerio2
Associate II
Posted on April 10, 2015 at 17:43

Hi everybody,

I am a newbie trying to develop a simple application for an stm32f429 discovery board with freertos, STM32Cube_FW_F4_V1.5.0and vcp over usb capabilities. I used stm32cubemx to generate the project that works fine. I can run a simple echo program via terminal with the micro connected to a pc with windows7.

However there are some things that are very unclear to me and I want to ask for an explanation. I unsuccesfullygoogled a lot, gave a look at the examples in STM32Cube_FW_F4_V1.5.0 and read theUser manualUM1734 (STM32Cube USB device library)

Particularly, what I don't get is related to usbd_cdc_if.c. In the version generated by stm32cubemx there are

/* Define size for the receive and transmit buffer over CDC */

/* It's up to user to redefine and/or remove those define */

#define APP_RX_DATA_SIZE 4

.

.

.

/* Create buffer for reception and transmission */

/* It's up to user to redefine and/or remove those define */

/* Received Data over USB are stored in this buffer */

uint8_t UserRxBufferHS[APP_RX_DATA_SIZE];

/* Send Data over USB CDC are stored in this buffer */

uint8_t UserTxBufferHS[APP_TX_DATA_SIZE];

.

.

.

/**

* @brief CDC_Init_HS

* Initializes the CDC media low layer over the USB HS IP

* @param None

* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL

*/

static

int8_t CDC_Init_HS(

void

)

{

hUsbDevice_1 = &hUsbDeviceHS;

/* USER CODE BEGIN 8 */

/* Set Application Buffers */

USBD_CDC_SetTxBuffer(hUsbDevice_1, UserTxBufferHS, 0);

USBD_CDC_SetRxBuffer(hUsbDevice_1, UserRxBufferHS);

return

(USBD_OK);

/* USER CODE END 8 */

}

.

.

.

static

int8_t CDC_Receive_HS (uint8_t* Buf, uint32_t *Len)

{

/* USER CODE BEGIN 11 */

return

(USBD_OK);

/* USER CODE END 11 */

}

The (I think defenitely wrong) idea I got in my mind is that in UserRxBufferHS is stored by the lower layer what I type over the terminal (that is what arrives at the endpoint out). The lower layer also calls CDC_Receive_HS. Basicly Buf, which is defined by the interface ofCDC_Receive_HS,is copied toUserRxBufferHS. I noticed that *Len is always 1 whatever I type and that inUserRxBufferHS there are always the last character I typed and two more odd characters (like interrupt characters, e.g. 0@). That is if I type ''t'' then I get ''t0@'' inUserRxBufferHS.

According to this I wonder why one would set APP_RX_DATA_SIZE to wathever value greater than 3 since inUserRxBufferHS there always are 3 elements.

Is this right? Can anybody give me a better insight?

Thank you and regards.

2 REPLIES 2
chadowryudan
Associate
Posted on April 21, 2015 at 17:44

Hi Thy,

I've generated a VCP project using CubeMX on F0. I had to do minor changes to make it work:

USBD_CDC_if.c:

- USBD_CDC_SetTxBuffer, argument ''UserTxBufferFS'' substituted by ''Buf''.

USBD_CDC.c:

- added function:

USBD_LL_PrepareReceive(pdev, epnum, hcdc->RxBuffer, CDC_DATA_FS_OUT_PACKET_SIZE);

In the block:

if(pdev->ClassData != NULL){

((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Receive(hcdc->RxBuffer, hcdc->RxLength);

USBD_LL_PrepareReceive(pdev, epnum, hcdc->RxBuffer,

CDC_DATA_FS_OUT_PACKET_SIZE

);

return USBD_OK;

}

Find attached the generated project in IAR, hope it will help you.

________________

Attachments :

VCP_fixed.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzV5&d=%2Fa%2F0X0000000bMj%2FIn2yN9raChNjZ0GxVVpMm.P0OAS2mYfxIbvWnmFcfBk&asPdf=false
valerio2
Associate II
Posted on April 22, 2015 at 11:42

Thank you for your reply. I will give a look at your code. Anyway I've managed to make my code working, and also it works fine (you know, black box approach). However the software architecture underneath is still something obscure to me and, even if I am not anymore focused on VCP and USB, I would like to have a better reference than those provided by ST.

Regards.