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

Using RTC to get real time for nRF9160DK

Hi Dev team,

I had an issue regarding getting the real time using RTC. I tried to follow the below dev zone blog but the CAL_RTC does not work in my code.

As such, I am unable to use this to build my application. Does nR9160DK not support "CAL_RTC" like "NRF_CLOCK" .

Can you let me know how I could use the below code to build my app for nRF9160DK.

https://devzone.nordicsemi.com/f/nordic-q-a/51781/rtc-31st-date-problem-in-nrf52840

Also, a question regarding RTC and LFCLK. How can I view the real time value using RTC and LFCLK.

I can see in the debugger that they are running but I need to know the real time through it. Some help in explaining this would be appreciated :).

Regards,

Adeel.

Parents
  • Hi Adeel

    How does this case relate to the other one you opened recently?
    https://devzone.nordicsemi.com/f/nordic-q-a/64931/using-rtc-to-control-the-real-timestamp

    For simplicity I will handle both. 

    CAL_RTC is simply a define in nrf_calendar.h, and is defined to NRF_RTC0 by default. 

    Please be aware that this example is not developed for NCS/Zephyr, so I am not sure how well it works there. Ideally it would have to be modified to use the nrfx drivers, which are supported both in the nRF5 SDK and in NCS. 

    Best regards
    Torbjørn

  • Hi Tobjorn,

    I am basically stuck in an issue wherein I need to get the real time and schedule certain events at that time.

    The idea here was to use LFCLK and RTC to try and obtain the real time and schedule events and interrupts based on that.

    For that, I needed an idea in order to get and schedules events and interrupts through RTC and LFCLK.

    Regards,

    Adeel.

  • Hi Adeel

    The LFCLK and RTC can't help in getting the real time, all they can do is keep track of time once you know what it is.

    I don't know what kind of resolution you need, but one method would be to use the time provided by the NTP server as explained in the other case, and then run an RTC timer or a Zephyr timer from that point on. 

    Then you update the local time at a fixed interval, and use this interrupt to check if you should run any scheduled events. 

    Using a Zephyr timer is probably the easiest, but I have to do some checking internally to figure out if the Zephyr timer is accurate enough to be used for time tracking. 

    If this is not the case then you should use an RTC timer through the nrfx_rtc library. 

    Best regards
    Torbjørn

  • Hi Tobjorn,

    Thanks for the clarification. I wanted to run the device in a very low power mode and therefore calling NTP at regular intervals would really consume a lot of current. So, instead I was looking for some alternate solutions to get time through the hardware clock as such.

    Also, regarding the interrupt, I tried to work with this snippet given from Nordic:

    https://github.com/andenore/NordicSnippets/blob/master/examples/rtc/main.c

    This explains very well the procedure of the code but I am unable to trigger the interrupt via this code. I was hoping to trigger the interrupt every second for now and kept the CC[0] register at 32768.

    Could you please have a look and let me know how to visualize if the interrupt is working or not in this case. I tried to see it in the debug mode and the register values vary at breakpoints set by me , but I would like to know how to see if the interrupt is working here. 

    Regards,

    Adeel.

Reply
  • Hi Tobjorn,

    Thanks for the clarification. I wanted to run the device in a very low power mode and therefore calling NTP at regular intervals would really consume a lot of current. So, instead I was looking for some alternate solutions to get time through the hardware clock as such.

    Also, regarding the interrupt, I tried to work with this snippet given from Nordic:

    https://github.com/andenore/NordicSnippets/blob/master/examples/rtc/main.c

    This explains very well the procedure of the code but I am unable to trigger the interrupt via this code. I was hoping to trigger the interrupt every second for now and kept the CC[0] register at 32768.

    Could you please have a look and let me know how to visualize if the interrupt is working or not in this case. I tried to see it in the debug mode and the register values vary at breakpoints set by me , but I would like to know how to see if the interrupt is working here. 

    Regards,

    Adeel.

Children
  • Hi Adeel

    One of my colleagues wrote a number of small examples showing how to use various peripherals in the nRF9160, including this one for the RTC:
    https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/nrfx/rtc

    I tried to integrate it in my own example, and got it working quite easily. The only change I did was to replace the toggling of LED's with a printk() call. 

    The critical thing is to include the following line from the prj.conf file:

    CONFIG_COMPILER_OPT="-DNRFX_RTC_ENABLED=1 -DNRFX_RTC0_ENABLED=1"

    And the following line from the Kconfig file:

    config FORCE_NRFX_MODULES
    	bool "Test to force nrfx enabled"
    	select NRFX_RTC
    	default y

    You can also consider using a Zephyr timer object, which is provided as a part of the Zephyr RTOS:
    https://docs.zephyrproject.org/1.12.0/kernel/timing/timers.html

    I confirmed internally that the Zephyr timers should be as accurate as the 32kHz clock you are using (RC or XO), which means if you use an external 32kHz crystal as the source they can be quite accurate.  

    Best regards
    Torbjørn

  • Hi Tobjorn,

    Yes, I had tried this basic RTC example and it worked for me. I was working on the other example as it was more of what I wanted when working with an interrupt.

    https://github.com/andenore/NordicSnippets/blob/master/examples/rtc/main.c

    The code works and I was trying to generate an interrupt on a compare event and that does not happen.

    I want to generate a compare event every 10 secs and so I set my CC[0] value to 327680. I want to trigger an interrupt as soon as the compare event is achieved and the interrupt should join back to the original program after 10 secs.

    When I run the code and view it in debug mode, I see that the counter as well as the compare register CC[0] resets to zero after the compare is achieved and the interrupt is not triggered. I assume that the code has to execute the interrupt handler RTC0_IRQHandler() after the compare event is done and the CC[0] value should not be reset to 0 automatically after the compare event.

    I have used the same code above and just changed the CC[0] value to 327680.

    Can you help me in identifying the issue here. 

    Regards,

    Adeel.

  • Hi Adeel

    Would the k_timer approach I suggested in one of the other cases be an alternative?

    It shouldn't be necessary to use a dedicated RTC for timing, since you have Zephyr timers available (similar to the app_timer module in the nRF5 SDK, if you have any experience with that). 

    Best regards
    Torbjørn

  • Hi Tobjorn,

    Yes. It could be and I am able to use the k_timers for generating timer interrupts after a certain defined time interval. But I also wanted to generate interrupts through an interrupt handler function that caters to the interrupts instantly through a handler.

    I tried it in the example mentioned above to try and enable an interrupt through NVIC_EnableIRQ (RTC0_IRQn). As per my understanding , this enabling of the interrupt should transfer the program to its handler function : void RTC0_IRQHandler(void) wherein I have a certain task written inside the handler function.

    Is there another example with the interrupt handler functions wherein I could check its functionality. I am basically trying to work on high priority interrupts.   

    Regards,

    Adeel.

  • Hi Adeel

    The NVIC API is not supported in Zephyr. 

    To enable an interrupt you have to do the following (using the RTC1 interrupt as an example):

    IRQ_DIRECT_CONNECT(RTC1_IRQn, 0, RTC1_IRQHandler, 0);
    irq_enable(RTC1_IRQn);

    Best regards
    Torbjørn

Related