Skip to main content
Lukasz Przenioslo
Associate III
December 2, 2015
Question

Get more resolution from RTC clock

Posted on December 02, 2015 at 10:28

Hello there,

I was wondering either it is possible to count with microseconds resolution isntead of seconds resolution using RTC 32k clock? I am using HAL lib. In theRTC_TimeTypeDef time structure there are 2 members I dont quite understand:

/*!< Specifies the RTC_SSR RTC Sub Second register content.
This parameter corresponds to a time unit range between [0-1] Second
with [1 Sec / SecondFraction +1] granularity */
uint32_t SubSeconds; 
/*!< Specifies the range or granularity of Sub Second register content
corresponding to Synchronous pre-scaler factor value (PREDIV_S)
This parameter corresponds to a time unit range between [0-1] Second
with [1 Sec / SecondFraction +1] granularity.
This field will be used only by HAL_RTC_GetTime function */
uint32_t SecondFraction;

At the begining I thought I would be able to use them somehow to specify the time with more precision, but those values dont change. I would apreciate all help in this case.
    This topic has been closed for replies.

    14 replies

    thomfischer
    Senior
    December 7, 2015
    Posted on December 07, 2015 at 11:37

    Hello,

    hardware ? ,cpu ?, library version ?

    I use STM32F429 , LSE with 32.768Khz crystal

    void

    SystemClock_Config

    (

    void

    )

    {

    …

    PeriphClkInitStruct

    .PeriphClockSelection = RCC_PERIPHCLK_SAI_PLLI2S|RCC_PERIPHCLK_RTC;

    PeriphClkInitStruct

    .PLLI2S.PLLI2SN = 192;

    PeriphClkInitStruct

    .PLLI2S.PLLI2SQ = 2;

    PeriphClkInitStruct

    .PLLI2SDivQ = 2;

    PeriphClkInitStruct

    .

    RTCClockSelection

    = RCC_RTCCLKSOURCE_LSE;

    HAL_RCCEx_PeriphCLKConfig(&

    PeriphClkInitStruct

    );

    …

    }

    hrtc.

    Init

    .

    AsynchPrediv

    = 31;

    hrtc.

    Init

    .

    SynchPrediv

    = 1023;

    with LSI = 32.000Khz crystal, you should use

    hrtc.

    Init

    .

    AsynchPrediv

    = 31;

    hrtc.

    Init

    .

    SynchPrediv

    = 999;

    I use following code

    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

    msec = 999 - (sTime.

    SubSeconds

    * 999 / sTime.

    SecondFraction

    );

    test code

    ...

    RTC_TimeTypeDef

    sTime;

    RTC_DateTypeDef

    sDate;

    tsprintf(

    ''RCC->BDSR=0x%04x RTCEN:0x%1x RTCSEL:0x%1x (1=LSE,2=LSI,3=HSE)\r\n''

    ,RCC->BDCR,((RCC->BDCR)>>15)&0x01,((RCC->BDCR)>>8)&0x03);

    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

    tsprintf(

    ''stime.SecondFraction=%d \r\n''

    ,sTime.

    SecondFraction

    );

    tsprintf(

    ''here we start\r\n''

    );

    os

    Delay(10);

    // we use freeRtos else use HAL_Delay()

    tsprintf(

    ''after 10

    ms

    delay\r\n''

    );

    osDelay(20);

    tsprintf(

    ''after 20

    ms

    delay\r\n''

    );

    osDelay(50);

    tsprintf(

    ''after 50

    ms

    delay\r\n''

    );

    os

    Delay(100);

    tsprintf(

    ''after 100

    ms

    delay\r\n''

    );

    osDelay(200);

    tsprintf(

    ''after 200

    ms

    delay\r\n''

    );

    osDelay(500);

    tsprintf(

    ''after 500

    ms

    delay\r\n''

    );

    osDelay(1000);

    tsprintf(

    ''after 1000

    ms

    delay\r\n''

    );

    this prints with LSE

    ...

    2015-12-07 11:02:58.005 RCC->BDSR=0x8103 RTCEN:0x1 RTCSEL:0x1 (1=LSE,2=LSI,3=HSE)

    2015-12-07 11:02:58.005 stime.SecondFraction=1023

    2015-12-07 11:02:58.005 here we start

    2015-12-07 11:02:58.016 after 10 ms delay

    2015-12-07 11:02:58.036 after 20 ms delay

    2015-12-07 11:02:58.085 after 50 ms delay

    2015-12-07 11:02:58.186 after 100 ms delay

    2015-12-07 11:02:58.386 after 200 ms delay

    2015-12-07 11:02:58.886 after 500 ms delay

    2015-12-07 11:02:59.886 after 1000 ms delay

    …

    with LSI

    ...

    2015-12-07 11:24:41.005 RCC->BDSR=0x8203 RTCEN:0x1 RTCSEL:0x2 (1=LSE,2=LSI,3=HSE)

    2015-12-07 11:24:41.005 stime.SecondFraction=999

    2015-12-07 11:24:41.005 here we start

    2015-12-07 11:24:41.017 after 10 ms delay

    2015-12-07 11:24:41.038 after 20 ms delay

    2015-12-07 11:24:41.092 after 50 ms delay

    2015-12-07 11:24:41.200 after 100 ms delay

    2015-12-07 11:24:41.415 after 200 ms delay

    2015-12-07 11:24:41.954 after 500 ms delay

    2015-12-07 11:24:43.030 after 1000 ms delay

    ...

    Tesla DeLorean
    Guru
    December 7, 2015
    Posted on December 07, 2015 at 16:59

    Clive, you example is using SPL which has different struct for RTC than HAL.

    Indeed, because I'm not using HAL, mainly because it's a very thick, and often broken, PoS, but that's a whole other discussion. My goal here however, is to illustrate that the HW works just fine, so any issues you may be having with it are in the SW domain, either yours or your choice of abstraction. I think the math and methods I have addressed will port to the HAL paradigm.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Lukasz Przenioslo
    Associate III
    December 7, 2015
    Posted on December 07, 2015 at 18:09

    Thank you guys, I understand how to do it now but it still seems to give me wrong output, I will post my code soon.

    Lukasz Przenioslo
    Associate III
    December 8, 2015
    Posted on December 08, 2015 at 11:20

    After closer look everything works. Thank you very much for help guys.