2020-10-23 07:03 AM
I'm trying to make a vcp implementation with STM32f407 and freertos.
I have no problem sending data from STM to my pc.
When I send data from PC to STM, when I send a few tens of bytes of data, the CDC_Receive_FS function is triggered several times and I cannot receive any processable data.
I tried to write a loopback application for testing.
I have edited the following codes in usbd_cdc_if.c file, one of the codes produced by CubeMX.
USBD_CDC_LineCodingTypeDef LineCoding =
{
115200, / * baud rate * /
0x00, / * stop bits-1 * /
0x00, / * parity - none * /
0x08 / * nb. of bits 8 * /
};
.....
static int8_t CDC_Control_FS (uint8_t cmd, uint8_t * pbuf, uint16_t length)
{
/ * USER CODE BEGIN 5 * /
switch (cmd)
{
case CDC_SEND_ENCAPSULATED_COMMAND:
break;
case CDC_GET_ENCAPSULATED_RESPONSE:
break;
case CDC_SET_COMM_FEATURE:
break;
case CDC_GET_COMM_FEATURE:
break;
case CDC_CLEAR_COMM_FEATURE:
break;
/ ************************************************* ****************************** /
/ * Line Coding Structure * /
/ * ------------------------------------------------ ----------------------------- * /
/ * Offset | Field | You | Value | Description * /
/ * 0 | dwDTERate | 4 | Number | Data terminal rate, in bits per second * /
/ * 4 | bCharFormat | 1 | Number | Stop bits * /
/ * 0 - 1 Stop bit * /
/ * 1 - 1.5 Stop bits * /
/ * 2 - 2 Stop bits * /
/ * 5 | bParityType | 1 | Number | Parity * /
/ * 0 - None * /
/ * 1 - Odd * /
/ * 2 - Even * /
/ * 3 - Mark * /
/ * 4 - Space * /
/ * 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). * /
/ ************************************************* ****************************** /
case CDC_SET_LINE_CODING:
LineCoding.bitrate = (uint32_t) (pbuf [0] | (pbuf [1] << 8) | \
(pobuf [2] << 16) | (pbuf [3] << 24));
LineCoding.format = pbuf [4];
LineCoding.paritytype = pbuf [5];
LineCoding.datatype = pbuf [6];
/ * Set the new configuration * /
// ComPort_Config ();
break;
case CDC_GET_LINE_CODING:
pbuf [0] = (uint8_t) (LineCoding.bitrate);
pbuf [1] = (uint8_t) (LineCoding.bitrate >> 8);
pbuf [2] = (uint8_t) (LineCoding.bitrate >> 16);
pbuf [3] = (uint8_t) (LineCoding.bitrate >> 24);
pbuf [4] = LineCoding.format;
pbuf [5] = LineCoding.paritytype;
pbuf [6] = LineCoding.datatype;
break;
case CDC_SET_CONTROL_LINE_STATE:
break;
case CDC_SEND_BREAK:
break;
default:
break;
}
return (USBD_OK);
/ * USER CODE END 5 * /
}
static int8_t CDC_Receive_FS (uint8_t * Buf, uint32_t * Len)
{
/ * USER CODE BEGIN 6 * /
USBD_CDC_SetRxBuffer (& hUsbDeviceFS, & Buf [0]);
USBD_CDC_ReceivePacket (& hUsbDeviceFS);
CDC_Transmit_FS (Buf, Len)); //send back incoming data
return (USBD_OK);
/ * USER CODE END 6 * /
}
When I send a File from Serial port to STM something goes back like the following
Copyright (cf ***********) 2020 ST ´Sight (cf *********** Microelectronics.
* r ***********
* All ri „nics.
* r *********** ght All ri „nics.
* r *********** s reserved. </ cente "Q *********** r> </h2>
C „/ cente" Q *********** *
* This softw ™ *********** are compon * softw ™ *********** e € �?compon * softw ™ ******* **** nt icompon * softw ™ *********** s licensed by ST un7 *********** der Ultimate Liberty ******* **** license
* SLAËVL *********** 00ê [nape
* SLAËVL *********** 44, the "^ & * SLAËVL *********** License"; ] * SLAËVL *********** You may not use th *********** is file EDGE use th ******** *** xcept in compliance w T ******* ith
* tb§pliance w T ******* he License. You may obtain a *** copy of the açemay obtain a *** License a6 açemay obtain a *** t:
* NÛ openemay obtain a *** şàobtain a *** wá şàobtain a *** ww.st.com/SLA0(Û shoobtain a *** 044
*
RA0 (Û shoobtai
Does anyone know how I can fix this situation
2020-10-23 05:51 PM
In the CDC_Receive_FS function you cannot just call CDC_Transmit_FS. It cannot be called only if the input pipe is busy.
And it becomes busy until the host turns around and sends IN token.
Add some buffering to your loopback test.
-- pa