1
\$\begingroup\$

I'm working with STM32F407VET6 board. And I'm trying to make communication between my PC and Micro Controller via sample which transmits what it receives through USB. I've created USB Device example via STM32CubeMX Version 4.16.0 and FW_F4 1.13.0.

My problem is very fantastic and I don't know how to google it. I'm using;

uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) 

and

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

functions.

I'm listening outputs from /dev/ttyACM0 via CuteCom. Also, sending commands through it. If I put some string to buffer, it doesn't send without noise. But it sends this caharacters correctly(!):

DEC   OCT    HEX    BINARY   CHARACTER   DESCRIPTION
-------------------------------------------------------
63    077    3F    00111111      ?       Question Mark
71    107    47    01000111      G       Uppercase G
75    113    4B    01001011      K       Uppercase K
83    123    53    01010011      S       Uppercase S
99    143    63    01100011      c       Lowercase c
111   157    6F    01101111      o       Lowercase o
119   167    77    01110111      w       Lowercase w

Also I read this characters on buffer if the string which I sent via CuteCom ends with one of them. If I only listen port, doesn't send anything, first string is changing and turns to tilde(~) character after several transmits. Here, I'm starting application, waiting, and sending string in order of: "w", "c", "asdfw", "a", "b", "c", "G" :

USB_Tx_buf: STRING_OF_TRANSMIT_BUFFER
USB_Rx_buf: STRING_OF_RECEIVE_BUFFER
-----------------------------------
USB_Tx_buf: E�L
USB_Rx_buf: 1�k
-----------------------------------
USB_Tx_buf: 0@�
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: A��
USB_Rx_buf: TA@
-----------------------------------
USB_Tx_buf:  Ag
USB_Rx_buf: E�L
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: A��
-----------------------------------
USB_Tx_buf: E�L
USB_Rx_buf: 1�k
-----------------------------------
USB_Tx_buf: 0@�
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: 
�z
USB_Rx_buf: 
�z
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: ~
-----------------------------------
USB_Tx_buf: ~
USB_Rx_buf: w
-----------------------------------
USB_Tx_buf: w
USB_Rx_buf: w
-----------------------------------
USB_Tx_buf: w
USB_Rx_buf: w
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: w
USB_Rx_buf: w
-----------------------------------
USB_Tx_buf: w
USB_Rx_buf: w
-----------------------------------
USB_Tx_buf: w
USB_Rx_buf: w
-----------------------------------
USB_Tx_buf: w
USB_Rx_buf: w
-----------------------------------
USB_Tx_buf: w
USB_Rx_buf: a�Wc~���CEIVE_BUFFER
-----------------------------------
USB_Tx_buf: a�Wc~���CEIVE_BUFFER
USB_Rx_buf: a�Wc~���CEIVE_BUFFER
-----------------------------------
USB_Tx_buf: a�Wc~���CEIVE_BUFFER
USB_Rx_buf: a�Wc~���CEIVE_BUFFER
-----------------------------------
USB_Tx_buf: b�Vc~���CEIVE_BUFFER
USB_Rx_buf: b�Vc~���CEIVE_BUFFER
-----------------------------------
USB_Tx_buf: b�Vc~���CEIVE_BUFFER
USB_Rx_buf: b�Vc~���CEIVE_BUFFER
-----------------------------------
USB_Tx_buf: b�Vc~���CEIVE_BUFFER
USB_Rx_buf: b�Vc~���CEIVE_BUFFER
-----------------------------------
USB_Tx_buf: b�Vc~���CEIVE_BUFFER
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: c
USB_Rx_buf: c
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
-----------------------------------
USB_Tx_buf: G
USB_Rx_buf: G
...

And here is communication part of code:

...
/* init code for USB_DEVICE */
MX_USB_DEVICE_Init();

/* USER CODE BEGIN startUSBTask */
uint8_t USB_TX_buf[MAX_COMMAND_STR_SIZE];
sprintf ( ( char * ) USB_TX_buf, "STRING_OF_TRANSMIT_BUFFER\r" );
sprintf ( USB_RX_BUF, "STRING_OF_RECEIVE_BUFFER" );
/* Infinite loop */
for ( ;; )
{
    CDC_Transmit_FS ( USB_TX_buf, ( uint16_t ) strlen ( ( char * ) USB_TX_buf ) );
    osDelay ( 10 );
    if (DEBUG) printf ( "USB_TX_buf: %s\nUSB_Rx_buf: %s\n-----------------------------------\n", ( char * ) USB_TX_buf, USB_RX_BUF );
}
/* USER CODE END startUSBTask */
....

Here is some lines of usbd_cdc_if.c, 84 to 89:

/* Received Data over USB are stored in this buffer       */
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
char USB_RX_BUF[APP_RX_DATA_SIZE];

/* Send Data over USB CDC are stored in this buffer       */
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

251 to 262:

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);
    sprintf(USB_RX_BUF, (char*)Buf);
    return (USBD_OK);
    /* USER CODE END 6 */ 
}

Where should I look for solution? Or How can i search it on Google?

\$\endgroup\$
  • 1
    \$\begingroup\$ This is characteristic of the baud rate and/or start/stop bits settings being wrong. \$\endgroup\$ – pjc50 Jul 29 '16 at 9:55
  • 3
    \$\begingroup\$ @pjc50 These settings do not matter for the USB CDC protocol (they have an effect only if the MCU firmware bridges to a 'real' serial port). \$\endgroup\$ – CL. Jul 29 '16 at 11:00
  • \$\begingroup\$ Thank You. I'm editing Question with parameters in usbd_conf.c file. \$\endgroup\$ – Orhan G. Hafif Jul 29 '16 at 11:01
  • \$\begingroup\$ Aw, just 14 seconds ago. OK, I'm not editing question :P \$\endgroup\$ – Orhan G. Hafif Jul 29 '16 at 11:01
2
\$\begingroup\$

Cutecom isn't actually cute software. It was the root of problem. After trying all possible fixes, I've just typed to terminal;

echo "CuteCom isn't actually cute software..." > /dev/ttyACM0

and it worked! I can read my string from buffer now. I don't know why but I faced a problem like this before. I'd be happy if someone will improve my answer.

\$\endgroup\$
  • 2
    \$\begingroup\$ Be careful: Typing to terminal is slow and equivalent to sending individual characters. You need to test with something that will send whole packets. As an alternative, try testing by sending an ASCII text file. I once reported a bug in a TI USB driver which they missed because they had simplified their testing in just the way you are doing. \$\endgroup\$ – Tut Aug 1 '16 at 14:47
  • \$\begingroup\$ Thank you. I've already planning to write C++ code for that. And I trust terminal more than the CuteCom:P \$\endgroup\$ – Orhan G. Hafif Aug 1 '16 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.