cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 USART - PC receives wrong data

verdano
Associate II
Posted on June 08, 2016 at 11:45

Hi guys,

I've implemented the code to send data from STM32f103 to PC with USART interface, but the data that I receive are wrong. This is the code that I've implemented :

void
RCC_Configuration( 
void
)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig( RCC_HSE_ON );
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if
( HSEStartUpStatus == SUCCESS )
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable );
/* Flash 2 wait state */
FLASH_SetLatency( FLASH_Latency_2 );
/* HCLK = SYSCLK */
RCC_HCLKConfig( RCC_SYSCLK_Div1 );
//Define AHB prescale @72Mhz
/* PCLK2 = HCLK */
RCC_PCLK2Config( RCC_HCLK_Div1 ); 
//Define APB2 prescale @72Mhz
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config( RCC_HCLK_Div2 ); 
//Define APB1 prescale @36Mhz
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig( RCC_PCLK2_Div6 ); 
//Max 14 Mhz now @12Mhz
/* PLLCLK = 8MHz*9 = 72MHz */
RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
/*Set USB clock @48Mhz*/
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
/* Enable PLL */
RCC_PLLCmd( ENABLE );
/* Wait till PLL is ready */
while
( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET )
{;}
/* Select PLL as system clock source */
RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
/* Wait till PLL is used as system clock source */
while
( RCC_GetSYSCLKSource() != 0x08 )
{;}
}
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable SPI1 clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1 , ENABLE );
/* Enable GPIOs clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
}
void
USART_init(
void
)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* USARTx configured as follow:
- BaudRate = 115200 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 = 115200;
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;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
}
void
USART_putc(
char
c)
{
/* e.g. write a character to the USART */
USART_SendData(USART1, c);
/* Loop until the end of transmission */
while
(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ;
}
void
USART_puts(
const
char
*s)
{
int
i;
for
(i=0; s[i]!=0; i++)
{
USART_putc(s[i]);
}
}
void
printf1(
const
char
*format, ...)
{
va_list list;
va_start(list, format);
int
len = vsnprintf(0, 0, format, list);
char
*s;
s = (
char
*)malloc(len + 1);
vsprintf(s, format, list);
USART_puts(s);
free(s);
va_end(list);
return
;
}

But if I try to send ''Hello world\n'', I receive this: 0690X0000060395QAA.jpg I use RS232USB converte with three cables for connect the STM32F103 to PC. Some suggestion? P.S. With another code (I have only the .hex) the communication works fine.
16 REPLIES 16
Nesrine M_O
Lead II
Posted on June 08, 2016 at 11:54

Hi verdano.gianmichele,

I suggest you to try the UART_Printf example under the STM32F1 cube firmware package it may be helpful.

STM32Cube_FW_F1_V1.4.0\Projects\STM3210E_EVAL\Examples\UART\UART_Printf

-Syrine-

Posted on June 08, 2016 at 12:47

Its pretty apparent the OP isn't using Cube

Unfortunately the code provided is incomplete, try to provide some complete context. You are using a specific board, cite some links or names for that. You specify you have a working .hex, where did that come from? Provides a cite.

Perhaps the board doesn't use an 8 MHz crystal and thus the timing assumptions are off?

I'd front check for TXE rather than back check for TC.

Do you have a debugger? Can you use that?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
verdano
Associate II
Posted on June 08, 2016 at 14:08

Hi, thanks for your answer. I've a custom board with STM32 onboard. I'm using for developer the Ride 7 software with is specific debugger. The .hex comes from one of the examples provided from the Ride7 software. What can I see in debug mode to fix my issue in the USART interface?  

AvaTar
Lead
Posted on June 08, 2016 at 14:30

Are the baud rate settings identical (start/stop bits, parity) ?

A scope is recommended, to check that the outgoing signals actually have the assumed baud rate.

> Perhaps the board doesn't use an 8 MHz crystal and thus the timing assumptions are off?

 

That is an often recurring reason for the baud rate being off.

verdano
Associate II
Posted on June 08, 2016 at 14:34

> Perhaps the board doesn't use an 8 MHz crystal and thus the timing assumptions are off?

Probably this's my problem. I'm cheking...

Posted on June 08, 2016 at 14:42

The system clock is conveyed via HSE_VALUE define, the PLL settings will also need to change. Some debuggers can provide a peripheral view showing configuration settings.

One could also send a stream of 'U' characters and confirm baud timing with an oscilloscope.

Internal clocking could also be check by programming PA8 in MCO mode.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
verdano
Associate II
Posted on June 08, 2016 at 14:43

How can I check the HSE value via software? 

verdano
Associate II
Posted on June 08, 2016 at 14:57

This is what I see in debug mode. Watching this values and the prescale that I've set seems that the in an 8Mhz oscillator...right?

0690X000006039AQAQ.jpg

verdano
Associate II
Posted on June 08, 2016 at 15:21

I've modified the code in this manner and for me the oscillator is an 8Mhz. Other ideas?

void
RCC_Configuration( 
void
)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig( RCC_HSE_ON );
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if
( HSEStartUpStatus == SUCCESS )
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable );
/* Flash 2 wait state */
FLASH_SetLatency( FLASH_Latency_2 );
/* HCLK = SYSCLK */
RCC_HCLKConfig( RCC_SYSCLK_Div1 );
//Define AHB prescale @72Mhz
/* PCLK2 = HCLK */
RCC_PCLK2Config( RCC_HCLK_Div1 ); 
//Define APB2 prescale @72Mhz
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config( RCC_HCLK_Div2 ); 
//Define APB1 prescale @36Mhz
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig( RCC_PCLK2_Div6 ); 
//Max 14 Mhz now @12Mhz
/* PLLCLK = 8MHz*9 = 72MHz */
//RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
/*Set USB clock @48Mhz*/
//RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
/* Enable PLL */
RCC_PLLCmd( ENABLE );
/* Wait till PLL is ready */
while
( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET )
{;}
/* Select PLL as system clock source */
RCC_SYSCLKConfig( RCC_SYSCLKSource_HSE );
/* Wait till PLL is used as system clock source */
//while ( RCC_GetSYSCLKSource() != 0x08 )
// {;}
}
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable SPI1 clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1 , ENABLE );
/* Enable GPIOs clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
}

0690X000006039FQAQ.jpg