AnsweredAssumed Answered

STM32L1 RTC incorrect rate

Question asked by b.tom on Jun 24, 2016
Hi everyone!

I have a problem getting the RTC of a STM32L151C8 device to work properly. I am using a custom board with an 8MHz external oscillator and the SPL which works fine so far.

I used the instructions and guidelines directly from ST (datasheet, application notes, examples) and some other web sources but the RTC counter always seems to run with a wrong (to high) frequency. As RTC clock source i tried the external HSE (divided by 8) as well as the internal LSI oscillator, both show similar behavior. Basic setup seems to be fine as RTC runs and registers (when readable in debugger) contain the set values.

Here is my (condensed) initialization code based on the SPL examples:

    RTC_InitTypeDef RTC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    PWR_RTCAccessCmd(ENABLE);
#if defined (RTC_USE_CLOCK_LSI)
    RCC_LSICmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
    RTC_InitStructure.RTC_SynchPrediv = 0x127;
    RTC_InitStructure.RTC_AsynchPrediv = 0x7C;
#elif defined (RTC_USE_CLOCK_HSE8)
    RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div8);
    RTC_InitStructure.RTC_SynchPrediv = 7999;
    RTC_InitStructure.RTC_AsynchPrediv = 124;
#endif
    RCC_RTCCLKCmd(ENABLE);
    RTC_WaitForSynchro();
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
    if (RTC_Init(&RTC_InitStructure) == ERROR) {
        while (1);
    }

As mentioned above, the counter always runs too fast. The minute register increments lets say every 10 seconds. Interestingly, this is on the one hand not constant, which means that this increment rate might change when reflashing the firmware. One the other hand, looking on the output (led) and the debugger view of the specific register, the time 'jumps' (non constant increment).

So IMHO it looks more like a problem with oscillator setup, but as it also does not work with the HSE source i am not sure about that.
Does anyone have an idea what i could try to get this fixed? Maybe i just overlooked something in the examples.

Thank you and best regards

Outcomes