Skip to main content
christoph239955_stm1_st
Associate
August 26, 2009
Question

Firmware Lib problem calc BRR register for usart

  • August 26, 2009
  • 3 replies
  • 704 views
Posted on August 26, 2009 at 12:24

Firmware Lib problem calc BRR register for usart

    This topic has been closed for replies.

    3 replies

    varun
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    Hi Christoph,

    How did you found that your resulting baud is not correct? I mean are you using some debugger or simulator to do that?

    First ensure that you have modified the oscillator specification as per your hardware

    #define HSE_Value ((u32)8000000) /* change here in stm32f10x_conf.h */

    In case you are using keil simulator, ensure that Xtal specification in Target option is same as HSE_Value.

    Also, the keil simulator some time misbehaves for the peripheral clock values shown in dialog box. To ensure this doesn't happen, first open,

    peripherals -> power, reset and clock control window..... close it then open the respective peripheral windows!! :-]

    Regards,

    Varun

    christoph239955_stm1_st
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    Hello,

    hopefully anyone can help me. I use the STM32F107VCT6 on the STM3210c-Eval board.

    is it possible that the library function ''USART_Init'' works not correct?

    I setup the USART 2 and use the lib-function and the Usart-init struct.

    when I try to get the USART2 work with 9600 baud with the lib-function it doesn’t work.

    But if I calculate the BRR Register values ''by hand'' it works fine!

    I tried to get the system clocks with the lib-function ''RCC_GetClocksFreq'' but this function gives strange frequencies!

    so maybe this function works not correct?

    here the usart configuration:

    -----------------------------

    void SetUSART(){

    /* The following example illustrates how to configure the USART1 */

    /* Resets the USART1 registers to their default reset value */

    RCC_ClocksTypeDef RCC_ClocksStatus; // for test

    USART_DeInit(USART2);

    USART_Cmd(USART2, ENABLE); // Enable USART 2

    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_Tx | USART_Mode_Rx;

    RCC_GetClocksFreq(&RCC_ClocksStatus); //for test

    USART_Init(USART2, &USART_InitStructure); // init USART 2 with init structure

    USART_ITConfig(USART2, USART_IT_RXNE , ENABLE); // Enables the USART2 Receive interrupt

    USART_ITConfig(USART2, USART_IT_TXE, ENABLE); // Enables the USART2 Transmission Complete interrupt

    /* Write to USART BRR */

    USART2->BRR = 0x146; //20,36d

    }

    here the clock configuration:

    -----------------------------

    void SetCLK(){

    ErrorStatus HSEStartUpStatus;

    // RCC = Reset Clock Control

    /* RCC system reset(for debug purpose) */

    RCC_DeInit();

    /* Enable HSE (>High speed external clock here: 25Mhz)*/

    RCC_HSEConfig(RCC_HSE_ON); // switch to external oscillator

    /* Disable Internal High Speed oscillator */

    RCC_HSICmd(DISABLE);

    /* Wait till HSE is ready */

    HSEStartUpStatus = RCC_WaitForHSEStartUp(); // check stable clock source

    if(HSEStartUpStatus == SUCCESS){

    /* HCLK = SYSCLK */

    RCC_HCLKConfig(RCC_SYSCLK_Div1); // System Clock

    /* PCLK2 = HCLK */

    RCC_PCLK2Config(RCC_HCLK_Div1); // peripheral clock 2

    /* PCLK1 = HCLK/2 */

    RCC_PCLK1Config(RCC_HCLK_Div2); // peripheral clock 1

    /* Flash 2 wait state */

    FLASH_SetLatency(FLASH_Latency_2);

    /* Enable Prefetch Buffer */

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* PLLCLK = 25MHz * 2 = 50 MHz */

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_2); // PLL clock

    /* Enable PLL */

    RCC_PLLCmd(ENABLE); // enable the pll clock

    /* Wait till PLL is ready */

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){} // wait until PLL is ready

    /* Select PLL as system clock source */

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);// system clock = PLLCLOCK

    /* Wait till PLL is used as system clock source */

    while(RCC_GetSYSCLKSource() != 0x08){} // wait for ready clock

    /* Configure HCLK such as HCLK = SYSCLK (AHB prescaler)*/

    RCC_HCLKConfig(RCC_SYSCLK_Div1);

    /* Configure PCLK1 such as PCLK1 = HCLK/16 (APB1 Prescaler)*/

    // PCLK1 for: TIM2 and USART2

    RCC_PCLK1Config(RCC_HCLK_Div16);

    }

    /* Enable GPIO and USART1 clock */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);

    /* Enable Timer2..7 clock */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL, ENABLE);

    }

    thanks for any helpnullhere the clock configuration:

    christoph239955_stm1_st
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:21

    thanks Varun,

    I use the Ride7 with the Rlink to debug the Evalboard.

    I can see the clock frequencies in the watchwindow.

    Thanks for the tip in the *conf.h i forgot... ;)

    Now the frequencies are correct.

    I added the sourcecode files *usart.c and *rcc.c and all

    works fine. :D

    thanks for your help