ex ELLA / nrf51_rtc

Fork of nrf51_rtc by Francis Schumacher

Committer:
lionello
Date:
Wed Mar 18 09:35:02 2015 +0000
Revision:
2:e1479e6ffc37
Parent:
1:5917157d58c6
Child:
3:9be93d7808ca
Move #include mbed to cpp file; header only needs time.h

Who changed what in which revision?

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