cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4Discovery Virtual COM port

jurij
Associate II
Posted on July 10, 2012 at 22:00

Hello!

I would like to send simple data from STM32F4Discovery to PC (''Hello World!'' every second would be perfect for beginning).

I am pretty lost here. How do I create virtual COM port? When I connect PC to the board via Mini USB, everything is normal - board gets detected and I can use it and upload programs normally. But when I connect also micro USB port, nothing happens on computer (only LD7 green light goes ON on board). Is there any simple tutorial on how to do this? Thanks!
18 REPLIES 18
Posted on July 11, 2012 at 13:37

The max baud rate depends on the clock speed of the APB to which the USART/UART is attached. ie 1/16 of 168 MHz or 84 MHz.

Any ground will do, but ideally you want one close to the driven pins.

// STM32 USART3 (Tx PB.10, Rx PB.11) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); // USART3_TX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); // USART3_RX
}
/**************************************************************************************/
void USART3_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART3_Configuration();
while(1)
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART3, 0x49); // Send 'I'
}
while(1); // Don't want to exit
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jurij
Associate II
Posted on July 11, 2012 at 15:35

I see ... but there is definitely a mistake in data sheet ... it should say 5.25 Mbit/s, not bit/s ...

jlchoobs
Associate II
Posted on July 11, 2012 at 20:26

If you go to the Projects Page Link below :

[[this link/image has been flagged as malicious by our security scan software and has been deleted]]

then scroll down to 'Usb Virtual Com Port'.

This is a simple Virtual Com Port Project for the Stm32f4Discovery Board.

The Code Download Link is listed there.

Check this discussion for the Driver:

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/New%20Project%20Download%20-%20Virtual%20Com%20with%20Cmd%20Shell%20and%20ADC%20Reading

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/New%20Project%20Download%20-%20Virtual%20Com%20with%20Cmd%20Shell%20and%20ADC%20Reading

jurij
Associate II
Posted on July 16, 2012 at 20:12

Hi!

I managed to hook up STM32F4Discovery with Arduino Mega2560 ... and it is working nicely, sending messages from Discovery via Arduino to PC works OK (I am using baud rate 256000).

Now I have two questions:

1) This code:

if (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET) { // Wait for empty

USART_SendData(USART3, 0x38); //ASCII number does not matter here

}

... does not work. If I use just this piece of code to transmit, the condition USART_FLAG_TXE = RESET never gets TRUE, hence the program does not enter ''if'' statement. However, If I send data using just USART_SendData(USART3, 0x38); before the ''if'' statement, the condition USART_FLAG_TXE = RESET does get true occasionally ... however, I don't know exactly when.

So I am using delays between sending chars and it works fine:

USART_SendData(USART3,

'T'

);

Delay(500);

USART_SendData(USART3,

'E'

);

Delay(500);

USART_SendData(USART3,

'S'

);

Delay(500);

USART_SendData(USART3,

'T'

);

Delay(500);

Any idea about the condition - when does it go true?

2) I would like to receive data from PC ... any quick example?

Many thanks!

Posted on July 16, 2012 at 20:34

It's empty when it's HIGH, not when it's LOW thus you want :

if  (USART_GetFlagStatus(USART3, USART_FLAG_TXE) != RESET) // Wait for empty

{

  USART_SendData(USART3, 0x38); //ASCII number does not matter here

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jurij
Associate II
Posted on July 16, 2012 at 21:03

OK, thanks!

How do I store 2 numbers in a row sent over UART? Let's say I want to send number 10 (in ASCII) ... numbers 0x31 and 0x30 are going to be sent ... however, if I want to receive them like this:

int i = USART_ReceiveData(USART3);

int j = USART_ReceiveData(USART3);

... number 0x30 (the last digit sent - digit 0 in ASCII) is stored in both integers i & j ... why?

Posted on July 16, 2012 at 21:55

Because you need for RXNE to go high for EACH byte you receive.

If you don't read data quickly enough it will be over written by the next byte coming down the wire. You may also observe an overrun error, which you'll need to clear.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jurij
Associate II
Posted on July 17, 2012 at 11:50

I tried like this:

int

 

i = USART_ReceiveData(USART3);

 

 

while

(USART_GetFlagStatus(USART3, USART_FLAG_RXNE == RESET)) {

//wait in the loop until byte is received - wait until RXNE is high

}

int

j = USART_ReceiveData(USART3);

while

(USART_GetFlagStatus(USART3, USART_FLAG_RXNE == RESET)) {

//wait in the loop until byte is received - wait until RXNE is high

}

... But it does not work ... any suggestion what am I doing wrong? Thanks again!

Posted on July 17, 2012 at 15:59

... But it does not work ... any suggestion what am I doing wrong? Thanks again!

Dunno, probably assuming you can grab characters from the past. Your code without any real context is hard to analyze. Simple Echoing based on the framework already published

while(1)
{
if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET) // Character Ready
{
uint16_t ch;
ch = USART_ReceiveData(USART3); // Grab it
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART3, ch); // Echo it
}
#define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
if (USART_GetFlagStatus(USART3, USART_FLAG_ERRORS) != RESET) // Errors?
USART_ReceiveData(USART3); // Clear Them
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..