cancel
Showing results for 
Search instead for 
Did you mean: 

USB CDC VCP help to receive Data from PC

maxyork145
Associate
Posted on July 09, 2013 at 20:09

Hello everybody (This about USB VCP)

i started an application to send and receive data from PC to STM32F4 discovery

i used the titorial provided by STmicro, but the problem i'm not able to sent to the STM32F4Discovery (PC to STM32), only from Discovery to PC

i use this function to send to PC, she work fine, but i can't find how receive data from PC, anything can help me.

The function blow provide to send Data to PC

----------------------------------------------------------------------------------------------------------------

static uint16_t VCP_DataTx (uint8_t* Buf, uint32_t Len)

 

{

 

  if (linecoding.datatype == 7)

 

  {

 

    APP_Rx_Buffer[APP_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1) & 0x7F;

 

  }

 

  else if (linecoding.datatype == 8)

 

  {

 

    APP_Rx_Buffer[APP_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1);

 

  }

 

 

 

  APP_Rx_ptr_in++;

 

 

 

  /* To avoid buffer overflow */

 

  if(APP_Rx_ptr_in == APP_RX_DATA_SIZE)

 

  {

 

    APP_Rx_ptr_in = 0;

 

  } 

 

 

 

  return USBD_OK;

 

}

#usb-vcp-need-receive-function
2 REPLIES 2
Posted on July 09, 2013 at 20:23

As I recall I just had the VCP_DataRx() routine send data out the USART, but it could just as easily put it in a buffer.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gvigelet2
Associate II
Posted on July 17, 2013 at 20:32

Clive is correct it is the following function that receives the data from the PC (via usb) and passes the data to the UART.

/**

  * @brief  VCP_DataRx

  *         Data received over USB OUT endpoint are sent over CDC interface 

  *         through this function.

  *           

  *         @note

  *         This function will block any OUT packet reception on USB endpoint 

  *         untill exiting this function. If you exit this function before transfer

  *         is complete on CDC interface (ie. using DMA controller) it will result 

  *         in receiving more data while previous ones are still not sent.

  *                 

  * @param  Buf: Buffer of data to be received

  * @param  Len: Number of data received (in bytes)

  * @retval Result of the opeartion: USBD_OK if all operations are OK else VCP_FAIL

  */

static uint16_t VCP_DataRx (uint8_t* Buf, uint32_t Len)

{

  uint32_t i;

  

  for (i = 0; i < Len; i++)

  {

    USART_SendData(EVAL_COM1, *(Buf + i) );

    while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET); 

  } 

 

  return USBD_OK;

}

If you are looking for the trail the data travels, the data comes from the PC over usb which is handled by the USBD_CDC library through function usbd_cdc_DataOut 

/**

  * @brief  usbd_cdc_DataOut

  *         Data received on non-control Out endpoint

  * @param  pdev: device instance

  * @param  epnum: endpoint number

  * @retval status

  */

static uint8_t  usbd_cdc_DataOut (void *pdev, uint8_t epnum)

{      

  uint16_t USB_Rx_Cnt;

  

  /* Get the received data buffer and update the counter */

  USB_Rx_Cnt = ((USB_OTG_CORE_HANDLE*)pdev)->dev.out_ep[epnum].xfer_count;

  

  /* USB data will be immediately processed, this allow next USB traffic being 

     NAKed till the end of the application Xfer */

  APP_FOPS.pIf_DataRx(USB_Rx_Buffer, USB_Rx_Cnt);

  

  /* Prepare Out endpoint to receive next packet */

  DCD_EP_PrepareRx(pdev,

                   CDC_OUT_EP,

                   (uint8_t*)(USB_Rx_Buffer),

                   CDC_DATA_OUT_PACKET_SIZE);

  return USBD_OK;

}

the APP_FOPS.pIf_DataRx(USB_Rx_Buffer, USB_Rx_Cnt); command passes it to your defined handler which is configured with a define in your usbd_conf.h file

#define APP_FOPS                        VCP_fops

Hopefully this helps enough.