#include "nrf_calendar.h" #include "nrf.h" #include "drv_rtc.h"
static struct tm time_struct, m_tm_return_time; static time_t m_time, m_last_calibrate_time = 0; static float m_calibrate_factor = 0.0f; static drv_rtc_t m_rtc_inst = DRV_RTC_INSTANCE(1);
#define NRF_CLOCK_LFCLK_RC CLOCK_LFCLKSRC_SRC_RC
void nrf_cal_set_time(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t minute, uint32_t second) { static time_t uncal_difftime, difftime, newtime; time_struct.tm_year = year - 1900; time_struct.tm_mon = month; time_struct.tm_mday = day; time_struct.tm_hour = hour; time_struct.tm_min = minute; time_struct.tm_sec = second; newtime = mktime(&time_struct); //CAL_RTC->TASKS_CLEAR = 1;
// Calculate the calibration offset if(m_last_calibrate_time != 0) { difftime = newtime - m_last_calibrate_time; uncal_difftime = m_time - m_last_calibrate_time; m_calibrate_factor = (float)difftime / (float)uncal_difftime; }
// Assign the new time to the local time variables m_time = m_last_calibrate_time = newtime; }
struct tm *nrf_cal_get_time(void) { time_t return_time; return_time = m_time + drv_rtc_counter_get(&m_rtc_inst) / 16384; m_tm_return_time = *localtime(&return_time); return &m_tm_return_time; }
struct tm *nrf_cal_get_time_calibrated(void) { time_t uncalibrated_time, calibrated_time; if(m_calibrate_factor != 0.0f) { uncalibrated_time = m_time + drv_rtc_counter_get(&m_rtc_inst) / 16384; calibrated_time = m_last_calibrate_time + (time_t)((float)(uncalibrated_time - m_last_calibrate_time) * m_calibrate_factor + 0.5f); m_tm_return_time = *localtime(&calibrated_time); return &m_tm_return_time; } else return nrf_cal_get_time(); }
char *nrf_cal_get_time_string(bool calibrated) { static char cal_string[80]; strftime(cal_string, 80, "%x - %H:%M:%S", (calibrated ? nrf_cal_get_time_calibrated() : nrf_cal_get_time())); return cal_string; }
|