cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F334 and RTC Setup

Manu Abraham
Senior
Posted on March 28, 2017 at 10:35

Hi,

I have a STM32F334 with a cylindrical watch 32.768kHz xtal connected as a LSE oscillator. LSE now works. Folks on the forum, were kind enough to point out to me about possible PCB related issues.Originally, I've had a EPSON SMD crystal in there.

Found a multitude of problems in there after digging into the issue, the first problem was a dead 32.768kHz crystal. Many trials later, tried the same crystal connected to a HEF4060 on a veroboard to test the crystal, which verified that the crystal was dead. Tried a cylindrical watch crystal with the HEF4060 and it worked. Tried the same cylindrical crystal in the same position, which did not work at all. The PCB design issue came to my mind, eventually stuck the crystal on top of the F334 and soldered the pins directly on to the F334.

Unfortunately, this also did not help. After pulling out some hair, I found that for the LSE to be turned ON, the RTC domain had to be reset, which allowed the LSE to be turned ON.

This now works, I can turn ON the LSE, can see the MCO output as well as the RTC_CALIB_OUT.

This but raises yet another concern. Everytime, I reset the CPU, the LSE is turned OFF/ON, as a result the RTC setting gets messed up. I am wondering how to avoid the RTC getting messed up.

Any thoughts/suggestions ?

Thanks

void rtc_setup(void)

{

    RTC_InitTypeDef rtc;

    __PWR_CLK_ENABLE();                /* Enable the PWR clock */

    /**

     * After reset, the Backup Domain Registers (RCC BDCR Register,

     * RTC registers and RTC backup registers) are protected against

     * possible stray write accesses.

     * To access the Backup domain, it must be explicitly enabled

     * The change takes effect, only at RESET and hence must be

     * RESET cycled.

     */

    __RTC_DOMAIN_WR_ENABLE();            /* Allow access to RTC */

    __RTC_DOMAIN_RST_ENABLE();            /* Reset RTC Domain */

    __RTC_DOMAIN_RST_DISABLE();

    __RTC_LSEBYPASS_DISABLE();            /* disable LSE bypass    */

    __LSE_ON();                    /* and switch LSE on    */

    while (!__LSE_RDY());                /* Wait until LSE is ready */

    __RTC_CLKSRC_LSE();                /* Select the RTC Clock Source */

    __RTC_CLK_ENABLE();                /* Clock enable        */

    RTC_WaitForSynchro();                /* Wait for RTC APB registers synchronisation */

    RCC_MCOConfig(RCC_MCOSource_LSE, RCC_MCOPrescaler_128);    /* Configure MCO clock source */

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

    rtc.RTC_AsynchPrediv = 0x7F;

    rtc.RTC_SynchPrediv  = 0xFF;

    rtc.RTC_HourFormat   = RTC_HourFormat_24;

    RTC_Init(&rtc);

    __RTC_REGWR_ENA();

    __RTC_CALIBOUT_512Hz();                /* RTC_CALIB Selection (1Hz or 512Hz)*/

    __RTC_CALIBOUT_ENA();                /* RTC clock output */

    __RTC_REGWR_DIS();

}

int main(void)

{    

    RTC_TimeTypeDef t;

    SysTick_Config(SystemCoreClock / 1000);        /* SysTick event @1ms        */

    USART1_Init();

    putstr(USART1, '\r\n========================\r');

    putstr(USART1, '   RTC Debug\r');

    putstr(USART1, '========================\r\n');

    rtc_setup();

    HRTIM_Config_MasterPWM();

    __TOGGLE(LED1_PORT, LED1);

    __ADC_CONV_START(ADC1);

    while (1) {

            RTC_GetTime(RTC_Format_BIN, &t);

    }

}
1 ACCEPTED SOLUTION

Accepted Solutions
plekkerkerker9
Associate II
Posted on March 28, 2017 at 12:38

Seems like you reset the RTC yourself during startup in the rtc_setup() function.

you could just put the following statement at the start of your rct_setup() function:

if( __LSE_RDY() ) return;

This will check if the LSE is already started, if it is, just leave it running.

That's basically what I do, and it works without any problem.

View solution in original post

7 REPLIES 7
plekkerkerker9
Associate II
Posted on March 28, 2017 at 12:38

Seems like you reset the RTC yourself during startup in the rtc_setup() function.

you could just put the following statement at the start of your rct_setup() function:

if( __LSE_RDY() ) return;

This will check if the LSE is already started, if it is, just leave it running.

That's basically what I do, and it works without any problem.

Manu Abraham
Senior
Posted on March 28, 2017 at 12:48

Hi,

The LSE is supposed to clock the RTC right ?

My mind is stuck in this following loop:

At CPU startup, LSE is not enabled, you need to enable LSE.

(One more issue persists in my mind .. How does the RTC prevent loosing time, when the LSE is not running ?)

To enable LSE you need to reset the RTC domain, or is my understanding incorrect ?

Once I reset the RTC domain, the RTC contents is also reset.

Am I completely missing something ?

plekkerkerker9
Associate II
Posted on March 28, 2017 at 13:08

Yes, LSE (Low Speed External) is normally the clock source for the RTC.

But as long as there is power on the Vbat pin, the LSE keeps on running no matter what happens to the rest of the CPU. And this is indeed so the RTC can keep track of time.

See Page 82 in the STM32F334 manual:

6.1.2 Battery backup domain

To retain the content of the Backup registers and supply the RTC function when VDD is

turned off, VBAT pin can be connected to an optional standby voltage supplied by a battery

or by another source.

The VBAT pin powers the RTC unit, the LSE oscillator and the PC13 to PC15 I/Os, allowing

the RTC to operate even when the main power supply is turned off. The switch to the VBAT

supply is controlled by the Power Down Reset embedded in the Reset block.

In my case I power Vbat via a 47mF ultracap, and that will keep the RTC going for 10 hours or so, which is enough for my needs. But once Vbat is to low, the LSE stops, and the RTC will loose time.

Posted on March 28, 2017 at 13:08

That does the trick. Verified MCO and CALIB_OUT. LSE state appears not changed until a power cycle; not affected by a RESET though. Which probably explains my other question too.. That looks like a nifty hack, not allowing LSE/RTC to change except for a power cycle. I guess I missed this behaviour while reading the datasheet. Need to re-read the data sheet, where it explains this behaviour.

Thanks!

Posted on March 28, 2017 at 13:13

TA, for pasting that section of the manual, I was just about to search for it. :)

The ultracap seems to do it's job very well. Do you have a link to the ultracap product ?

Posted on March 28, 2017 at 13:27

This is the Ultracap I use:

https://octopart.com/search?q=FC0H473ZFTBR24

 

But there are probably better options out there. I didn't gave it a lot of thought.

You could also consider a CR2032 coin cell. This will keep the RTC running for months or even years.

Posted on March 28, 2017 at 13:35

CR2032 was quite big considering the PCB size, during the breakout board design time. That was the only reason, avoided the battery on it. Anyways, for most applications a week's backup would be more than sufficient.