cancel
Showing results for 
Search instead for 
Did you mean: 

USART Loopback with STM32F107

loic
Associate II
Posted on May 20, 2015 at 11:35

Hi,

I would make a loopback with my board STM3210C using USART2. I use the code

/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Recieve%20more%20then%201%20Byte%20over%20USART&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=3862

. Here is my main code :

while(1)
{
//Wait until a byte is received
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
RXBUFF[j] = USART_ReceiveData(USART2);
if (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != RESET)
USART_SendData(USART2, RXBUFF[j]);
j++;
if (j >= 1)
j = 0;
 } 

When I use the Hyperterminal to show the data exchange, I have a value returned only all the 4 or 5 values send. I just would send a number between 0 and 9, and receveid the same to verify the connexion. Thanks for your help Best regards Loïc
13 REPLIES 13
loic
Associate II
Posted on May 22, 2015 at 09:02

Thank you for the time you give to my problem.

I have tried your code, there is no problems with the serial port, but when I use the VCP configuration with this code, It's impossible to send data (Putty etablishes the connexion, but it shows nothing when I type on the keyboard). The problem comes from USART_Cmd(), so I have commented it and came back at the same point than yesterday. EDIT : I have made a simple test and I obtain this result in Putty : 1234abab 12345abab 123456abab 123ababab. All the numbers are typed by me.

while
(1)
{
uint16_t data;
while
(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); 
// Wait for Character
data = USART_ReceiveData(USART2);
USART_SendData(USART2, 
'a'
);
Tempo(1000);
while
(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); 
// Wait for Empty
USART_SendData(USART2, data); 
// Echo
USART_SendData(USART2, 
'b'
);
Tempo(1000);
}

loic
Associate II
Posted on May 22, 2015 at 13:53

I started a

new project from

the VCP exemple here : http://www.st.com/web/en/catalog/tools/FM147/CL1794/SC961/SS1743/LN1734/PF257882 --> STM32_USB-Host-Device_Lib_V2.1.0\Project\USB_Device_Examples\VCP. I just add this in the main function (file app.c) and obtain the same result in my hyperterminal as earlier...

/* Main loop */
while
(1)
{
if
(i++ == 0x100000)
{

uint16_t data;

STM_EVAL_LEDToggle(LED1);
STM_EVAL_LEDToggle(LED2);
STM_EVAL_LEDToggle(LED3);
STM_EVAL_LEDToggle(LED4);
i = 0;

while
(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); 
// Wait for Character

data = USART_ReceiveData(USART2);

while
(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); 
// Wait for Empty

USART_SendData(USART2, data); 
// Echo

}
}

Posted on May 22, 2015 at 14:50

You can't do this

USART_SendData(USART2, data); 
// Echo
USART_SendData(USART2, 
'b'
);

The 'b' destroys the prior register content, you have to wait for TXE each time. I don't quite understand the point of the echo in the VCP example. Why wouldn't you do that in the IRQ handler? If you're interrupting on the RXNE signal, that would be the place to do this.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
loic
Associate II
Posted on May 27, 2015 at 11:59

I do this in the main function because I need to use ''data'' in order to make a Switch with it.

Have you an idea why there is no good results in the HyperTerminal ? Problem of buffer size ?

Thanks for your replies.