cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Virtual Com Port problem [solved]

chief
Associate II
Posted on October 30, 2015 at 13:30

STM32CubeMX generate that code for my STM32F4Discovery (STM32F407VG):

/**
* @brief CDC_Receive_FS
* 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 operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
return (USBD_OK);
/* USER CODE END 6 */ 
}
/**
* @brief CDC_Transmit_FS
* Data send over USB IN endpoint are sent over CDC interface 
* through this function. 
* @note
* 
* 
* @param Buf: Buffer of data to be send
* @param Len: Number of data to be send (in bytes)
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
*/
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
uint8_t result = USBD_OK;
/* USER CODE BEGIN 7 */ 
USBD_CDC_SetTxBuffer(hUsbDevice_0, Buf, Len); 
result = USBD_CDC_TransmitPacket(hUsbDevice_0);
/* USER CODE END 7 */ 
return result;
}

TheCDC_Receive_FS function looks strange and (so sudenly) don't work. Transmite from MCU to PC works fine. How to use Virtual Com Port for reciving in MCU ?
7 REPLIES 7
Posted on October 30, 2015 at 17:14

Hi RiseOfDeath,

usbd_cdc_if.c is a driver for user application, so you can add your own code in CDC_Receive_FS function.

For more details, refer to 

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00108129.pdf?s_searchtype=keyword

 that explains more the use of STM32Cube USb device library.

-Shahrzad-

Rosiney Silva
Associate II
Posted on October 30, 2015 at 22:22

I was also having problems, but with the tip of Shahrzad and the sample

''(...) STM32Cube\Repository\STM32Cube_FW_F4_V1.9.0\Projects\STM32469I_EVAL\Applications\USB_Device\CDC_Standalone'' got it to work.

If you want to try something fast by following the instructions in ''CDC_Receive_FS'':

// Example Null Modem

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)

{

  /* USER CODE BEGIN 6 */

  CDC_Transmit_FS(Buf, *Len);

  USBD_CDC_ReceivePacket(hUsbDevice_0);

  return (USBD_OK);

  /* USER CODE END 6 */

}

chief
Associate II
Posted on November 03, 2015 at 07:55

Thx, it's work.

stm32cube-t
Senior III
Posted on March 22, 2016 at 19:48

Dear user,

Some helper code has been added in STM32CubeMX release 4.13.

Best regards
taraben
Senior
Posted on March 27, 2016 at 22:53

Now with CubeMX 4.14 and F4-1.11 the receive CDC is broken.

it inserts commands SetRxBuffer and ReceivePacket. There are following problems:

1) SetRxBuffer is used to assign the buffer used in the ISR, so reallocating the same buffer is useless. The buffer (max 64Bytes for FS) is ALREADY asigned in the CDC_Init_FS function.

2) calling ReceivePacket will reenable the receive for next packet. But the data will get lost. As we are still in ISR context and USB transfer rates are high, we can not assume application will handle the wast ammount of data here

All we need to do is calling a user defined function to inform about the incoming data. Once application has read that data, the application is responsible to recall ReceivePacket in order to continue reading.

The commenting of this functions is wrong:

This function will block any OUT packet reception on USB endpoint

untill exiting this function.

Correct is: The function must exit immeditely. OUT packet reception is blocked until calling ReceivePacket function.

Reagrds, Adib.

--

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)

{

  /* USER CODE BEGIN 6 */

  // USBD_CDC_SetRxBuffer(hUsbDevice_0, &Buf[0]);

  // USBD_CDC_ReceivePacket(hUsbDevice_0);

  usb_cdc_rx_handler(Buf, *Len);

  return (USBD_OK);

  /* USER CODE END 6 */

}

Nesrine M_O
Lead II
Posted on March 28, 2016 at 11:45

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/CubeMx%204.14%20F4%201.11%20Virtual%20COM%20Port%20Problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=10]CubeMx 4.14 F4 1.11 Virtual COM Port Problem

taraben
Senior
Posted on March 28, 2016 at 18:52

Hello Sylva.risiney,

your given code IS WRONG:

// Example Null Modem

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)

{

  /* USER CODE BEGIN 6 */

  CDC_Transmit_FS(Buf, *Len);

  USBD_CDC_ReceivePacket(hUsbDevice_0);

  return (USBD_OK);

  /* USER CODE END 6 */

}

The given buffer is the buffer from receive proccess.

You push the buffer to be transmitted.

Now you must block the buffer. The buffer must not be written until the Transmit procccess has been completed.

What you do in your code is to give the buffer the same time to the receive proccess again.

That could mean that the buffer will be overwritten by an other received packet before it is transmitted. And this resulty in a repeated string that is not the string originally received.

What you have to do is: to wait until the tx has been completed AND THEN call USBD_CDC_ReceivePacket.

This is a tricky part because there is no callback once the tx has been completed.

Regrads, Adib.

--