I know that nrf_cal_set_time() from the nrf calendar example is NOT meant to be called on a regular basis / should only be called to update the time being kept locally to recalibrate after drift in the LfClk. But please humor me for a second -- I want to confirm that the reason this is problem is because of latency in getting the current time from a CTS central.
Notes on the following code:
- the RTC is set to trigger a COMPARE0 event every 1 min
- read_time() calls ble_cts_c_current_time_read()
- the callback event from ble_cts_c_current_time_read() calls nrf_cal_set_time()
- nrf_cal_set_time() updates the local time m_time (m_time was originally initialized to 0)
- dostuff() prints out m_time.
In summary, every 1 minute, this code in theory gets the time from the CTS and prints it out.
void rtc_handler(nrf_drv_rtc_int_type_t int_type) { if (int_type == NRF_DRV_RTC_INT_COMPARE0) { read_time(); // get the time dostuff(); // print the time //Increment CC so it'll trigger again in COMPARE_COUNTERTIME seconds rtc.p_reg->CC[0] += COMPARE_COUNTERTIME * 8; //Re-enable COMPARE0 event & interrupt nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE0_MASK); } }
In reality, it seems like the time is being printed before it is retrieved from the CTS. The first time this runs (after 1 minute), dostuff() should print the time returned from CTS, but it instead prints 0, as if read_time() were never called. The next minute (minute 2), it prints the time from minute 1.
My questions:
1. Is it right that this is happening because the latency in actually generating a CURRENT_TIME event?
2. Is this why there is latency: After read_time() is called, control is returned to rtc_handler() to execute dostuff() just after the gatt read request is put on the SoftDevice queue. So this means that dostuff() is executed while the GATT read request might still be on the queue, and thus no time has yet been retrieved from CTS.
3. I assume the queue is being processed by the softdevice in parallel with continuing to execute the application code? How many threads can run in parallel?