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.
Fork of nrf51_rtc by
nrf51_rtc.cpp@1:5917157d58c6, 2015-03-02 (annotated)
- Committer:
- lionello
- Date:
- Mon Mar 02 08:54:40 2015 +0000
- Revision:
- 1:5917157d58c6
- Parent:
- 0:3677a016109b
- Child:
- 2:e1479e6ffc37
Made update_rtc() an actual static method
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fxschumacher | 0:3677a016109b | 1 | #include "nrf51_rtc.h" |
fxschumacher | 0:3677a016109b | 2 | |
lionello | 1:5917157d58c6 | 3 | |
lionello | 1:5917157d58c6 | 4 | nrf51_rtc rtc; |
lionello | 1:5917157d58c6 | 5 | |
lionello | 1:5917157d58c6 | 6 | |
fxschumacher | 0:3677a016109b | 7 | int nrf51_rtc::set_time(time_t rawtime) { |
fxschumacher | 0:3677a016109b | 8 | // set the current time from a parameter |
fxschumacher | 0:3677a016109b | 9 | time_base = rawtime; |
fxschumacher | 0:3677a016109b | 10 | rtc_previous = int (NRF_RTC1->COUNTER) / ticks_per_second; |
fxschumacher | 0:3677a016109b | 11 | return 0; |
fxschumacher | 0:3677a016109b | 12 | } |
fxschumacher | 0:3677a016109b | 13 | |
fxschumacher | 0:3677a016109b | 14 | time_t nrf51_rtc::time() { |
fxschumacher | 0:3677a016109b | 15 | // get the current time... and update the underlying variables for tracking time |
fxschumacher | 0:3677a016109b | 16 | // ...this routine must be called regularly, before the 24b RTC register can wrap around: |
fxschumacher | 0:3677a016109b | 17 | // t < size of register / (LFCLK_FREQUENCY / (NRF_RTC1->PRESCALER + 1)) |
fxschumacher | 0:3677a016109b | 18 | // size of register = 2^24 ticks |
fxschumacher | 0:3677a016109b | 19 | // LFCLK_FREQUENCY = 2^15 cycles/sec |
fxschumacher | 0:3677a016109b | 20 | // NRF_RTC1->PRESCALER = 0 -- as (currently) set by mbed library! |
fxschumacher | 0:3677a016109b | 21 | // = (2^24)/(2^15/1) = 2^9 seconds = 512 seconds, ~ 8.5 minutes |
fxschumacher | 0:3677a016109b | 22 | unsigned int rtc_now = (NRF_RTC1->COUNTER) / ticks_per_second; |
fxschumacher | 0:3677a016109b | 23 | unsigned int delta_seconds = ((rtc_now + counter_size_in_seconds) - rtc_previous) % counter_size_in_seconds; |
fxschumacher | 0:3677a016109b | 24 | time_base = time_base + (time_t) delta_seconds; |
fxschumacher | 0:3677a016109b | 25 | rtc_previous = rtc_now; |
fxschumacher | 0:3677a016109b | 26 | return time_base; |
fxschumacher | 0:3677a016109b | 27 | } |
fxschumacher | 0:3677a016109b | 28 | |
fxschumacher | 0:3677a016109b | 29 | void nrf51_rtc::update_rtc() { |
fxschumacher | 0:3677a016109b | 30 | // for use as interrupt routine, same as "time" but doesn't return a value (as req'd for interrupt) |
lionello | 1:5917157d58c6 | 31 | rtc.time(); |
fxschumacher | 0:3677a016109b | 32 | } |
fxschumacher | 0:3677a016109b | 33 | |
fxschumacher | 0:3677a016109b | 34 | nrf51_rtc::nrf51_rtc() { |
fxschumacher | 0:3677a016109b | 35 | rtc_previous=0; |
fxschumacher | 0:3677a016109b | 36 | time_base=0; |
fxschumacher | 0:3677a016109b | 37 | #define LFCLK_FREQUENCY 0x8000 |
fxschumacher | 0:3677a016109b | 38 | #define RTC_COUNTER_SIZE 0x1000000 |
fxschumacher | 0:3677a016109b | 39 | ticks_per_second = LFCLK_FREQUENCY / (NRF_RTC1->PRESCALER + 1); |
fxschumacher | 0:3677a016109b | 40 | counter_size_in_seconds = RTC_COUNTER_SIZE / ticks_per_second; |
fxschumacher | 0:3677a016109b | 41 | |
fxschumacher | 0:3677a016109b | 42 | // Ticker inside the method doesn't work: the interrupt attachment to the ticker messes up the world (code goes to the weeds) |
fxschumacher | 0:3677a016109b | 43 | // ... not needed if "rtc.time()" is called frequently enough (once per 512 seconds) |
fxschumacher | 0:3677a016109b | 44 | // ... this scheme (ticker+interrupt routine to update rtc) works correctly when implemented at the next level up |
fxschumacher | 0:3677a016109b | 45 | //Ticker rtc_ticker; |
fxschumacher | 0:3677a016109b | 46 | //#define RTC_UPDATE 1 |
fxschumacher | 0:3677a016109b | 47 | //rtc_ticker.attach(this,&nrf51_rtc::update_rtc, RTC_UPDATE); // update the time regularly (must be < duration of 24b timer) |
fxschumacher | 0:3677a016109b | 48 | } |