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
Posted on June 08, 2016 at 17:47

Ok, but what frequency part does your custom board actually USE, you're providing little insight.

Suggest you leave the board to run from the 8 MHZ HSI, and see if that works

void RCC_Configuration( void )
{
/* Running from HSI already, leave it alone */
/* 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);
}

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 09, 2016 at 08:46 Solved, the value of HSE oscillator was hard coded at 8Mhz in the code.

#if !defined (HSE_VALUE) 
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */

Posted on June 09, 2016 at 10:15

Ok, but for completeness what frequency external oscilator does your board actually use?

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 09, 2016 at 10:23

12 Mhz

AvaTar
Lead
Posted on June 09, 2016 at 10:40

> ... the value of HSE oscillator was hard coded at 8Mhz in the code.

 

That's not really true, as the line ''

#if !defined (HSE_VALUE)

'' in the header should tell you.

The

stm32xxx.h

headers are not supposed to be modified manually, and define a default value for HSE_VALUE, only if no one exists.

Personally, would would have preferred throwing an error (#error), instead of making wild assumptions.

However, the intended way is to define this value outside of the source code, either in the project settings of your IDE, or as a call parameter to the compiler (like ''-DHSE_VALUE=4194304'') in a makefile.

I think this is (or was) documented for the SPL. Not sure about the current state of (abandoned) affairs ...

verdano
Associate II
Posted on June 09, 2016 at 12:10

In the last version of SPL that I have this is the code and suppose a 8Mhz clock. I did a define in a makefile with the correct frequency. There was no errors at compile time...

#if !defined HSE_VALUE
#ifdef STM32F10X_CL 
#define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
#else 
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
#endif /* STM32F10X_CL */
#endif /* HSE_VALUE */

AvaTar
Lead
Posted on June 09, 2016 at 12:46

> There was no errors at compile time...

Sure, because the HSE_VALUE will be defined, one way or another. Basically, if no value is present, the header defines one appropriate for the most popular eval board at that time. Still, it would IMHO be better if the header would throw an error if not defined. The

''STM32F10X_CL'' 

define refers to originally to ''Connectivity Line'' - in difference to ''Value Line'' - a terminology long gone from ST's portfolio.