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
Posted on September 25, 2015 at 14:25

Does the same problem occur if you use the LSI clock?

You can't put a scope probe directly on LSE, you'll alter the characteristics of the circuit too much, you must observe indirectly, either via the TAMPER pin, or measuring it with a timer, there's an example of that in the firmware library.

The LSE is very fussy, it needs a 6-7pF crystal, not the more common 9 to 12pF ones.

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 25, 2015 at 15:10

I changed from LSE to LSI

void RTC_Config(void)

{

unsigned char ErrorStatus;

  /* Enable the PWR clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Allow access to RTC */

  PWR_BackupAccessCmd(ENABLE);

  /* Reset RTC Domain */

  RCC_BackupResetCmd(ENABLE);

RCC_BackupResetCmd(DISABLE);

  /* Enable the LSI OSC */

  RCC_LSICmd(ENABLE);

  /* Wait till LSI is ready */

  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)

  {

  }

  /* Select the RTC Clock Source */

  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

/* Configure the RTC data register and RTC prescaler */

  RTC_InitStructure.RTC_AsynchPrediv = 0x7F;

  RTC_InitStructure.RTC_SynchPrediv =  0xCB;

  RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;

  ErrorStatus=RTC_Init(&RTC_InitStructure);

  

  /* Set the time to 01h 02mn 00s AM */

  RTC_TimeStruct.RTC_H12     = RTC_H12_AM;

  RTC_TimeStruct.RTC_Hours   = 0x01;

  RTC_TimeStruct.RTC_Minutes = 0x02;

  RTC_TimeStruct.RTC_Seconds = 0x00;  

  ErrorStatus=RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct);

}

THE RESULT IS THE SAME

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

Posted on September 25, 2015 at 15:42

Can you try without the reset commands?

What debugger are you using?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 25, 2015 at 16:39

Check in debugger, that PWR_CR.DBP is set indeed.

JW
cdb1702
Associate II
Posted on September 25, 2015 at 17:11

I have made a try without the reset command

  /* Reset RTC Domain */

  //RCC_BackupResetCmd(ENABLE);

  //RCC_BackupResetCmd(DISABLE);

but no result

The debugger is ST-LINK with SWD

cdb1702
Associate II
Posted on September 25, 2015 at 17:26

hello JW

PWR_BackupAccessCmd(ENABLE) set it and I verified that it is set

#define PWR_OFFSET               (PWR_BASE - PERIPH_BASE)

#define CR_OFFSET                (PWR_OFFSET + 0x00)

#define DBP_BitNumber            0x08

#define CR_DBP_BB                (PERIPH_BB_BASE + (CR_OFFSET * 32) + (DBP_BitNumber * 4))

void RTC_Config(void)

{

unsigned char ErrorStatus;

unsigned long app;

  /* Enable the PWR clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Allow access to RTC */

  PWR_BackupAccessCmd(ENABLE);

  app=*(__IO uint32_t *) CR_DBP_BB;

app=1 

Posted on September 25, 2015 at 17:38

By itself, or running something like CooCox or GDB?

Anyway, all very odd, identical code functions on a STM32F429I-DISCO with LSE circuitry added. STM32F429ZIT6

I see hours of fun ahead..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 25, 2015 at 20:29

Let's try another tack.

Does you board have a USART hooked up? Which USART, which pins?

Is SWO (PB3) connected on your debug interface?

What is the external HSE frequency? What speed is the board running at?
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 10:09

I used

PB10:UART3 TX

PB11:UART3 RX

PB3 is free

Debug Interface with only 2 pin

PA13 : SWDIO

PA14: SWCLK

Board running at 168 Mhz starting from an oscillator of 8 Mhz

I saw that  in this forum at least 3 other people had the same problem and I was wondering if these people had then resolved the issue

Thanks

Carlo De Bonis

Posted on September 28, 2015 at 10:25

> I saw that  in this forum at least 3 other people had the same problem and I was wondering if these people had then resolved the issue

Some posters never come back to report of their success; others are tinkerers who give up.

Let's step back - tell us, what exactly is your problem and how do you diagnose it.

As I said above, you should have started your own thread.

JW