Hi,
I'm trying to implement a slow timer which is supposed to be triggered about every 30 minutes or even less frequent, but I'm kind of clueless how to solve this issue.
I tried implementing the app_timer first, as described in this post: https://devzone.nordicsemi.com/nordic/short-range-guides/b/software-development-kit/posts/application-timer-tutorial
but unfortunately I did not find a function or parameter to change the frequency or prescaler of this timer, so for this use-case the app_timer was not the best choice.
(Unless I would catch every interrupt and count them up, which I like to avoid)
As this is a low power application, I want to create as few interrupts as possible and after I read that you can use the RTC to create timers up to 8 hours, I tried to implement that.
Now, I'm basically left with 2 questions:
-
Is there something I need to pay attention to? Like for example messing with the clock configuration. (And I already figured you cannot use instance 0 of the RTC, with this example)
-
And how do I set up the compare interrupt, in a way, that it triggers repeatably?
Here are the steps I've taken so far:
My code is based on the LPN Example from the Mesh SDK.
I attempted to implement the RTC as demonstrated in the SDK\examples\peripheral\rtc example.
static void rtc_handler(nrf_drv_rtc_int_type_t int_type) { if (int_type == NRF_DRV_RTC_INT_COMPARE0) { __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "COMPARE0 match\n"); } else if (int_type == NRF_DRV_RTC_INT_TICK) { __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "TICK event\n"); } } static void lfclk_config(void) { ret_code_t err_code = nrf_drv_clock_init(); APP_ERROR_CHECK(err_code); nrf_drv_clock_lfclk_request(NULL); } /** @brief Function initialization and configuration of RTC driver instance. */ static void rtc_config(void) { uint32_t err_code; //Initialize RTC instance nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG; config.prescaler = 4095; err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler); APP_ERROR_CHECK(err_code); //Enable tick event & interrupt nrf_drv_rtc_tick_enable(&rtc,true); //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true); APP_ERROR_CHECK(err_code); //Power on RTC instance nrf_drv_rtc_enable(&rtc); } int main(void) { initialize(); start(); // lfclk_config(); <- causing Error 133 rtc_config(); for (;;) { (void)sd_app_evt_wait(); } }
On the first run I got Mesh error 133, wich I think is caused because the function initialize() is probably using the clock somewhere else.
Therefore I used the rtc_config() without calling lfclk_config() first, which seems to work.
It is working in the sense, that ticks is called permanently, but compare is only called once after a certain number of ticks (which makes sense, I just expected it to trigger again after a wrap around of the RTC).
This behaves the same, even when I'm trying to set up a new compare in the rtc_handler.
Since I'm not sure if that is even the proper way of doing things, it would be great to get some advice: How I can start/stop and modify the RTC and if it's fine ignoring the set up of the clock (in this example), or how to do that correctly.
Sincerely,
Heiner