cancel
Showing results for 
Search instead for 
Did you mean: 

HAL CDC Extra byte in CDC_Receive_FS buffer

vladsol2009
Associate II
Posted on August 07, 2017 at 20:40

I have created a simple project in cubemx, with usb fs device (CDC).

But i see an strange behavior: The buffer of CDC_Receive_FS contains (bytes sent from PC + 1 extra byte) 

On example, when sending 0x32 0x33 in buffer i got 0x32 0x33 0xee

0x32 0x34 ->  0x32 0x34 0xaf

0x32 0x35 -> 0x32 0x35 0x6e

etc..

Why can this happen?

3 REPLIES 3
Posted on August 08, 2017 at 01:03

Hello !!

The CDC_Receive_FS function you mention, is called from lower layers and contains as parameter a pointer to received length.

Is this received *length equal to 3? or as you wrote before , just contained in buffer?

Your hardware is unknown to us, provide some information about it

Posted on August 08, 2017 at 04:48

MCU is STM32F103R8T6

UserRxBufferFS size is 64

USB_OTG_MAX_EP0_SIZE is 64

USB_OTG_FS_MAX_PACKET_SIZE is 64

Received *Len contains ''5'', but *Buf contains 6 bytes.

0690X00000603zpQAA.jpg

With even number of bytes there is no problems:

1 byte sent, 2 bytes in buffer, Len is 1

2 bytes sent, 2 bytes in buffer, Len is 2 == OK

3 bytes sent, 4 bytes in buffer, Len is 3

4 bytes sent, 4 bytes in buffer, Len is 4

 == OK

5 bytes sent, 6 bytes in buffer, Len is 5

6 bytes sent, 6 bytes in buffer, Len is 6

 == OK

7 bytes sent, 8 bytes in buffer, Len is 7

8 bytes sent, 8 bytes in buffer, Len is 8

 == OK

9 bytes sent, 10 bytes in buffer, Len is 9

10 bytes sent, 10 bytes in buffer, len is 10

 == OK

Posted on August 08, 2017 at 13:35

Hello again!!

You wrote... '1 byte sent, 2 bytes in buffer, Len is 1

2 bytes sent, 2 bytes in buffer, Len is 2 == OK

3 bytes sent, 4 bytes in buffer, Len is 3'

Two bytes in a 64 byte size buffer means nothing. the (buffer allways contains 64 bytes)

Usually the buffers has as initial values per byte ,0x00

With a 'normal' debuger is posible to see oly the bytes>0x20 . But this does not mean that the rest bytes don't exist.

The internal structure of USB CDC modules is 'unknown' to the highest layer user.

The knowledge of the user  must be , only , the number of bytes received and the buffer pointer to take theese determined bytes. The remaining of the buffer is not guaranteed to conforms with something determined or not.

So, this the normal behaviour of this callback function.