An attempt to provide a Real Time Clock for the nRF51-DK. This code provides rtc.time and rtc.set_time replacements for the similarly named C standard methods (unimplemented for nRF51-DK last I checked). Not very well tested, but it seems to work for simple applications.

Dependents:   nRF51_rtc_example LoRa_PIR BLE_Lightning_sensor BLE_AlarmWatch ... more

Files at this revision

API Documentation at this revision

Comitter:
fxschumacher
Date:
Sun Feb 08 01:28:47 2015 +0000
Commit message:
Initial revision -- library to implement Real Time Clock (time and set_time) for nRF51-DK.

Changed in this revision

nrf51_rtc.cpp Show annotated file Show diff for this revision Revisions of this file
nrf51_rtc.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 3677a016109b nrf51_rtc.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nrf51_rtc.cpp	Sun Feb 08 01:28:47 2015 +0000
@@ -0,0 +1,46 @@
+#include "nrf51_rtc.h"
+
+int nrf51_rtc::set_time(time_t rawtime) {
+    // set the current time from a parameter
+    time_base = rawtime;
+    rtc_previous = int (NRF_RTC1->COUNTER) / ticks_per_second;
+    return 0;
+}
+
+time_t nrf51_rtc::time() {
+    // get the current time... and update the underlying variables for tracking time
+    //  ...this routine must be called regularly, before the 24b RTC register can wrap around:  
+    //  t < size of register / (LFCLK_FREQUENCY / (NRF_RTC1->PRESCALER + 1))
+    //    size of register = 2^24 ticks 
+    //    LFCLK_FREQUENCY = 2^15 cycles/sec 
+    //    NRF_RTC1->PRESCALER = 0 -- as (currently) set by mbed library!
+    //  = (2^24)/(2^15/1) = 2^9 seconds = 512 seconds, ~ 8.5 minutes
+    unsigned int rtc_now = (NRF_RTC1->COUNTER) / ticks_per_second;
+    unsigned int delta_seconds = ((rtc_now + counter_size_in_seconds) - rtc_previous) % counter_size_in_seconds;
+    time_base = time_base + (time_t) delta_seconds;
+    rtc_previous = rtc_now;
+    return time_base;
+}
+
+void nrf51_rtc::update_rtc() {
+    // for use as interrupt routine, same as "time" but doesn't return a value (as req'd for interrupt)
+    //  ...but this is useless since the compiler seems to be unable to correctly attach a member function to the ticker interrupt
+    //  ...the exact same interrupt routine attached to a ticker works fine when declared outside the method
+    nrf51_rtc::time();
+}
+
+nrf51_rtc::nrf51_rtc() {
+    rtc_previous=0;
+    time_base=0;
+    #define LFCLK_FREQUENCY 0x8000
+    #define RTC_COUNTER_SIZE 0x1000000
+    ticks_per_second = LFCLK_FREQUENCY / (NRF_RTC1->PRESCALER + 1);
+    counter_size_in_seconds = RTC_COUNTER_SIZE / ticks_per_second;
+    
+    // Ticker inside the method doesn't work:  the interrupt attachment to the ticker messes up the world (code goes to the weeds)
+    //  ... not needed if "rtc.time()" is called frequently enough (once per 512 seconds)
+    //  ... this scheme (ticker+interrupt routine to update rtc) works correctly when implemented at the next level up
+    //Ticker rtc_ticker;
+    //#define RTC_UPDATE 1
+    //rtc_ticker.attach(this,&nrf51_rtc::update_rtc, RTC_UPDATE); // update the time regularly (must be < duration of 24b timer)
+}
diff -r 000000000000 -r 3677a016109b nrf51_rtc.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nrf51_rtc.h	Sun Feb 08 01:28:47 2015 +0000
@@ -0,0 +1,22 @@
+#ifndef NRF51_RTC_H
+#define NRF51_RTC_H
+
+#include "mbed.h"
+
+static class nrf51_rtc {
+    // class to create equivalent of time() and set_time()
+    //   ...depends upon RTC1 to be running and to never stop -- a byproduct of instantiation of mbed library's "ticker"
+    public:
+    nrf51_rtc();
+    time_t time();
+    int set_time(time_t rawtime);
+    
+    // these should be private
+    private:
+    time_t time_base;
+    unsigned int rtc_previous;
+    unsigned int ticks_per_second, counter_size_in_seconds;
+    void update_rtc(); // similar to "time" but doesn't return the value, just updates underlying variables
+
+} rtc;
+#endif
\ No newline at end of file