cancel
Showing results for 
Search instead for 
Did you mean: 

UART Baudrate / ST LIB

kjepsen9
Associate II
Posted on October 08, 2008 at 11:14

UART Baudrate / ST LIB

#uart-baud-rate-hse_value-hsi
6 REPLIES 6
kjepsen9
Associate II
Posted on May 17, 2011 at 12:47

kjepsen9
Associate II
Posted on May 17, 2011 at 12:47

Hmm...

Hello,

I have made a user board with 16Mhz xtal instead 8Mhz.

When i configure the UART i get double speed.

I have set the frequency in the Keil enviroment.

I have also set in the lib config file:

/* In the following line adjust the value of External High Speed oscillator (HSE)

used in your application */

#define HSE_Value ((u32)16000000) /* Value of the External oscillator in Hz*/

but i still have the error.

my clock setup:

/* 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);

/* PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */

RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 MHz */

RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_4);

/* 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 GPIOx and AFIO clocks */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOx | RCC_APB2Periph_AFIO, ENABLE);

and my serial setup:

USART_InitStructure.USART_BaudRate = 4800;

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 the USARTx */

USART_Init(USARTx, &USART_InitStructure);

/* Enable the USARTx */

USART_Cmd(USARTx, ENABLE);

BR

Kasper

andreas2
Associate II
Posted on May 17, 2011 at 12:47

I'm guessing you're using some Keil provided precompiled FWLib?

Really not a good idea, since the HSE_Value define is used inside the RCC_GetClocksFreq() which in turn is called from USART_Init() to calculate the baud rate register. So you changing the the HSE_value in stm32f10x_conf.h has no effect at all if the build system doesn't detect it and rebuilds _all_ source, including libraries, that depends on it.

kjepsen9
Associate II
Posted on May 17, 2011 at 12:47

Hello,

You are right, lesson learned :)

Kasper

hossam
Associate II
Posted on November 26, 2014 at 20:54

Hi,

I have a problem like this one

I have configured the UART to work at 19200 but when I test it using my board with external crystal 8MHz I found that data is sent @ 9600 baud rate

and when I try the same code on Nucleo STM32F302 board with internal clock HSI tit works at 19200 as required

I am using eclipse with GNU ARM Eclipse plugins and STM32 STD peripheral library .

When I try to change the HSE_VALUE in the environment Symbols of eclipse or in stm32f30x.h it has no effect on the system even after making clean and rebuild the project

and here's my initialization code

USART_InitTypeDef USART_InitStructure;

  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIOA clock */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);

  /* Enable USART1 APB clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  /* USART1 Pins configuration ************************************************/

  /* Connect pin to Periph */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);

  /* Configure pins as AF pushpull */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* USART1 configured as follow:

  - BaudRate = 19200 baud

  - Word Length = 8 Bits

  - Stop Bit = 1 Stop Bit

  - Parity = Even Parity

  - Hardware flow control disabled (RTS and CTS signals)

  - Receive and transmit enabled

  */

  USART_DeInit(USART1);

  USART_InitStructure.USART_BaudRate = 19200;

  USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_Even;   //USART_Parity_No;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_Init(USART1, &USART_InitStructure);

  /* NVIC configuration */

   NVIC_Configuration();

   /* Enable USART1 Receive and Transmit interrupts */

   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

  // USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

  /* USART enable */

  USART_Cmd(USART1, ENABLE);

  /* Wait until Receive enable acknowledge flag is set */

  while(USART_GetFlagStatus(USART1, USART_FLAG_REACK) == RESET)

  {}

  /* Wait until Transmit enable acknowledge flag is set */

  while(USART_GetFlagStatus(USART1, USART_FLAG_TEACK) == RESET)

  {}

Posted on November 26, 2014 at 21:23

Congratulations on posting against a six year old thread, for a different processor.

Suggest you check your clock and PLL configuration code. I'm not sure if HSE_VALUE is an environment variable as much as a DEFINE. Does the Nucleo need to use the BYPASS HSE setting?

You could print out the value of HSE_VALUE from your code, and the configuration of the F3's internal register states. You could also review the library source, or output internal clocks via the MCO pin (PA8)

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