cancel
Showing results for 
Search instead for 
Did you mean: 

Need help initializing RTC on STM32F407

jimmieh85
Associate
Posted on July 18, 2012 at 22:11

Hi I am trying to write date and time to the RTC which I later will read.

But something in my RTC configuration doesn’t work and reading the values in debug mode shows that the RTC registers that I try to set never get the new values.

Here is my code:

void Set_RTC()

{

/* Enable the PWR clock */

RCC_APB1ENR.PWREN = 1;

/* Allow access to RTC */

PWR_CR.DBP = 1;

/* LSE used as RTC source clock */

RCC_BDCR.LSEON = 1;    //External low-speed oscillator enable

 /* Wait till LSE is ready */

while(RCC_BDCR.LSERDY != 1)

{

}

/* Select the RTC Clock Source to LSE */

RCC_BDCR.F8 = 1;

RCC_BDCR.F9 = 0;

/* Wait for RTC APB registers synchronisation */

while(RTC_ISR.RSF != 1)

{

}

RCC_BDCR.RTCEN = 1;   //Enable RTC clock  <<<

RTC_WPR = 0xCA;   //unlock write protection

RTC_WPR = 0x53;   //unlock write protection

/* Configure the RTC prescaler */

RTC_PRER = 0x7f00ff;  //  set   SynchPrediv to FF and AsynchPrediv to 7F

//RCC_BDCR.RTCEN = 1;   //Enable RTC clock  <<<

RTC_ISR.INIT = 1;     //enter initialization mode <<<<<< THIS REGISTER NEVER GETS THE NEW VALUE

while(RTC_ISR.INITF != 1)   //poll INITF

{

}

/* Configure the RTC PRER */

 RTC_PRER = 0x7F;

 RTC_PRER |= 0xFF << 16;

 RTC_TR = 0x123500; //setting time to 12.35.00

 RTC_DR = 0x126718;  // set date to  2012-07-18

 RTC_CR.F6 = 0; // set FMT 24H format

 RTC_ISR.INIT = 0;     //exit initialization mode

  /* Enable the write protection for RTC registers */

  RTC_WPR = 0xFF;

}

I am using a STM32F407 @ 140Mz internal HSI oscillator.

What am I doing wrong?

42 REPLIES 42
cdb1702
Associate II
Posted on September 28, 2015 at 11:14

The problem is in this function

/**

  * @brief  Enters the RTC Initialization mode.

  * @note   The RTC Initialization mode is write protected, use the 

  *         RTC_WriteProtectionCmd(DISABLE) before calling this function.    

  * @param  None

  * @retval An ErrorStatus enumeration value:

  *          - SUCCESS: RTC is in Init mode

  *          - ERROR: RTC is not in Init mode  

  */

ErrorStatus RTC_EnterInitMode(void)

{

  __IO uint32_t initcounter = 0x00;

  ErrorStatus status = ERROR;

  uint32_t initstatus = 0x00;

  /* Check if the Initialization mode is set */

  if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)

  {

    /* Set the Initialization mode */

RTC->ISR = (uint32_t)RTC_INIT_MASK;

    /* Wait till RTC is in INIT state and if Time out is reached exit */

    do

    {

      initstatus = RTC->ISR & RTC_ISR_INITF;

      initcounter++;  

    } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));

    

    if ((RTC->ISR & RTC_ISR_INITF) != RESET)

    {

      status = SUCCESS;

    }

    else

    {

      status = ERROR;

    }        

  }

  else

  {

    status = SUCCESS;  

  } 

    

  return (status);  

}

NOTES

in the do...while

 initstatus = RTC->ISR & RTC_ISR_INITF;

ALWAYS IS 0

/* Wait till RTC is in INIT statet */

RTC does not go to INIT state and the return status is ERROR 

Posted on September 28, 2015 at 14:55

The problem is observable there, the real issue is likely upstream.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cdb1702
Associate II
Posted on September 28, 2015 at 15:15

Where do you think there is the cause?

Hw problem?

I USED FOR QUARTZ 32 KHZ 10 PF PF instead of 7pf?

The oscillation is present and it is at correct frequenzy.

Posted on September 28, 2015 at 18:03

You can't probe the crystal directly.

Try attached test code, outputs via USART3 at 9600 8N1, post back the output

The LSE clock should be output via PA8 (MCO1), you should scope that.

________________

Attachments :

USART3-LSE-FORUM-REL1.hex : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0RD&d=%2Fa%2F0X0000000bi9%2FcmRw9ukRTFimvhsM.FdlcKWSdjvDKF40t67qe8vRYrM&asPdf=false
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cdb1702
Associate II
Posted on September 29, 2015 at 14:59

The attached file is

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Attachments/58211/USART3-LSE-FORUM-REL1.hex

and I cannot insert in my project

I am creating code to use PA8 to view LSE and I will reply as soon as possible

Posted on September 29, 2015 at 15:30

You don't need to add it to your project, just download it into the processor (using for example the ST-Link Utility) and run.

JW
cdb1702
Associate II
Posted on September 29, 2015 at 15:34

   /* With this code I see on PA8 pin 32.7 Khz Clock  */

  /* Enable the GPIO_Clock */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  /* Configure the GPIO_pin */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);

       RCC_MCO1Config(RCC_MCO1Source_LSE , RCC_MCO1Div_1);

Do you have other suggestions?

Carlo

Posted on September 29, 2015 at 17:32

Do you have other suggestions?

I'd like to see the output from the code I provided.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cdb1702
Associate II
Posted on September 30, 2015 at 09:37

HELLO,

I HAVE DOWNLOADED THE CODE IN MY BOARD TROUGH THE PROGRAM ''FLASH LOADER DEMO'' BUT UNFORTUNATELY 2 THINGS HAVE HAPPENED.

- I HAD NOT ANSWERS FROM THE BOARD

- I AM NOT ABLE TO LOAD THE PREVIOUS PROGRAM

WHAT KIND OF ANSWERS YOU EXPECT FROM THE SERIAL AND WHICH INFORMATION WE COULD GIVE?

- THEREFORE  I EQUIP TO PREPARE A NEW CARD

Posted on September 30, 2015 at 13:15

You should be able to set BOOT0 High, and none of this code should run. And presumably you can JTAG/SWD into it and erase it too.

I'm trying out a couple of working theories about why your system isn't working, I understand all the code that's built in and the tools used to build it, so I'm not trying to troubleshoot the issue with a keyhole perspective.

It should initialize the board based on your clocks, pins and USART, and it should test and time the LSE wrt the internal references. Based on the totality of the output, it might provide some clues about where things are broken.

The code fragment you have provided, and others have provided, has been tested on working hardware, it works, so there's clearly some other system level issue at play here. I'm not looking to play 20-Questions to do a system review and figure out the hardware/software interactions here.

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