Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nrf51_rtc by
nrf51_rtc.cpp
- Committer:
- lionello
- Date:
- 2015-03-18
- Revision:
- 2:e1479e6ffc37
- Parent:
- 1:5917157d58c6
- Child:
- 3:9be93d7808ca
File content as of revision 2:e1479e6ffc37:
#include "nrf51_rtc.h"
#include <mbed.h>
nrf51_rtc rtc;
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)
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)
}
