This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Nordic Calendar example (https://github.com/NordicPlayground/nrf5-calendar-example) related issue

I wanted to implement a time sync feature on nRF-52840 DK. I took a reference from this example, but there were certain things unclear to me.

Can somebody explain to me what is calibrated and uncalibrated time here? and what exactly are we doing here in this example?

  • Hi

    On second thought you should get decent millisecond accuracy using the RTC. For some reason I thought about microseconds, not milliseconds, when I replied to you earlier...

    If you change the prescaler to 0 in the calendar example the RTC will run at the full 32.768kHz clock speed, and if you divide the RTC counter value by 32.768 you should get the current time in milliseconds. 

    The important factor is that you need to update all references to the CAL_RTC->COUNTER register, since this value will now be much higher (rather than increase by 8 every second it will increase by 32768 every second). 

    Also, the maximum time the RTC can run without being cleared will be much shorter, since the 24-bit RTC counter will overflow in 512 seconds when the prescaler is 0. 

    Best regards
    Torbjørn

  • omg i learned so much about rtc from you guys thanks and sorry for keep bugging you i grasped everything about that nrf calender except one thing why are  you using this function and what is the purpose of this function

    void nrf_cal_set_callback(void (*callback)(void), uint32_t interval)
    {
    // Set the calendar callback, and set the callback interval in seconds
    cal_event_callback = callback;
    m_rtc_increment = interval;
    m_time += CAL_RTC->COUNTER / 8;
    CAL_RTC->TASKS_CLEAR = 1;
    CAL_RTC->CC[0] = interval * 8;
    },

    and also how can i print something like this int count =CAL_RTC->COUNTER / 32.768;

    printf("%d",count);;is this correct.I want to see the values myselfs before sending it over ble

    and also what happens when overflow occurs and how can i rectify that

    REGARDS

    MANIKANDAN V

  • Hi Manikandan

    I am happy I could be of help Slight smile

    The purpose of this function is to provide a callback function to the library that will be called at a regular interval, which is useful if for instance you want to log the time regularly in your code, or if you want to display the time somewhere at regular intervals. 

    If you want to schedule some activity at a certain time in the future you can also use this callback to check the current time once in a while, and perform some activity once you reach a certain point in time. 

    As an example, if you create a void(void) function in your code and forward it to this function with the interval set to 60, your function will be called once every minute:

    void do_something(void)
    {
        // This code will run once every minute
    }
    
    .
    .
    
    nrf_cal_set_callback(do_something, 60);

     

    manikandan said:

    and also how can i print something like this int count =CAL_RTC->COUNTER / 32.768;

    printf("%d",count);;is this correct.I want to see the values myselfs before sending it over ble

    In order to divide the value by 32.768 you would have to convert the COUNTER value to float first, like this for instance:

    int count_ms = (int)((float)CAL_RTC->COUNTER / 32.768f);

    It is also possible to rewrite the formula and perform it all in integers:

    int count_ms = (CAL_RTC->COUNTER * 250 + 125) / 8192;

    Please note I haven't tested any of these code snippets, so take them with a grain of salt ;) 

    manikandan said:
    and also what happens when overflow occurs and how can i rectify that

    When an overflow occurs the RTC COUNTER value will be reset back to 0, and since the code is not set up to handle this it will lead to incorrect time updates. 

    To avoid this simply call the nrf_cal_set_callback(..) function with an interval lower than 512 seconds.

    Then the  RTC IRQ handler will be called before the overflow happens, and you should not have any issues. 

    Best regards
    Torbjørn 

  • hello, thanks for your help  i can be able to get milliseconds and run a 24 hours clock successfully now i want to do time sync up that day you told if we are developing our own app it is possible to get a timestamp in the app and send it over a proprietary service to the nRF52 device every time i connect.

    1)indeed we are developing our own app can you elaborate on how can i get that timestamp in program and set that time in nrf_cal_set_time(..) ??

    2)where do i get my timestamp in ble_app_uart program?? in nus_data_handler(..) function uh??

    thanks and regards

    Manikandan V

  • Hi Manikandan

    1) Not sure which mobile platform you are targeting, but on Android there is a LocalTime library for getting time and date information, and I am sure something similar exists for iOS. 

    The important thing is that you get an integer timestamp that can be passed over the BLE link. 

    2) That is correct. By default the ble_app_uart example uses the nus_data_handler(..) function to process incoming data from the app. 

    Best regards
    Torbjørn

Related