[DeviceDriver] Add NTP auto sync time for RTC device.

This commit is contained in:
armink 2018-02-16 12:58:48 +08:00
parent c4f4710b43
commit ce6b0a6efb
1 changed files with 73 additions and 14 deletions

View File

@ -22,16 +22,31 @@
* 2012-01-29 aozima first version. * 2012-01-29 aozima first version.
* 2012-04-12 aozima optimization: find rtc device only first. * 2012-04-12 aozima optimization: find rtc device only first.
* 2012-04-16 aozima add scheduler lock for set_date and set_time. * 2012-04-16 aozima add scheduler lock for set_date and set_time.
* 2018-02-16 armink add auto sync time by NTP
*/ */
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <rtthread.h> #include <rtthread.h>
/** \brief returns the current time. /* Using NTP auto sync RTC time */
#ifdef RTC_SYNC_USING_NTP
/* NTP first sync delay time for network connect, unit: second */
#ifndef RTC_NTP_FIRST_SYNC_DELAY
#define RTC_NTP_FIRST_SYNC_DELAY (30)
#endif
/* NTP sync period, unit: second */
#ifndef RTC_NTP_SYNC_PERIOD
#define RTC_NTP_SYNC_PERIOD (1L*60L*60L)
#endif
#endif /* RTC_SYNC_USING_NTP */
/**
* Returns the current time.
* *
* \param time_t * t the timestamp pointer, if not used, keep NULL. * @param time_t * t the timestamp pointer, if not used, keep NULL.
* \return time_t return timestamp current. *
* @return time_t return timestamp current.
* *
*/ */
/* for IAR 6.2 later Compiler */ /* for IAR 6.2 later Compiler */
@ -70,12 +85,14 @@ time_t time(time_t *t)
return time_now; return time_now;
} }
/** \brief set system date(time not modify). /**
* Set system date(time not modify).
* *
* \param rt_uint32_t year e.g: 2012. * @param rt_uint32_t year e.g: 2012.
* \param rt_uint32_t month e.g: 12 (1~12). * @param rt_uint32_t month e.g: 12 (1~12).
* \param rt_uint32_t day e.g: e.g: 31. * @param rt_uint32_t day e.g: 31.
* \return rt_err_t if set success, return RT_EOK. *
* @return rt_err_t if set success, return RT_EOK.
* *
*/ */
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
@ -118,12 +135,14 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
return ret; return ret;
} }
/** \brief set system time(date not modify). /**
* Set system time(date not modify).
* *
* \param rt_uint32_t hour e.g: 0~23. * @param rt_uint32_t hour e.g: 0~23.
* \param rt_uint32_t minute e.g: 0~59. * @param rt_uint32_t minute e.g: 0~59.
* \param rt_uint32_t second e.g: 0~59. * @param rt_uint32_t second e.g: 0~59.
* \return rt_err_t if set success, return RT_EOK. *
* @return rt_err_t if set success, return RT_EOK.
* *
*/ */
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
@ -166,6 +185,45 @@ rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
return ret; return ret;
} }
#ifdef RTC_SYNC_USING_NTP
static void ntp_sync_thread_enrty(void *param)
{
extern time_t ntp_sync_to_rtc(void);
/* first sync delay for network connect */
rt_thread_delay(RTC_NTP_FIRST_SYNC_DELAY * RT_TICK_PER_SECOND);
while (1)
{
ntp_sync_to_rtc();
rt_thread_delay(RTC_NTP_SYNC_PERIOD * RT_TICK_PER_SECOND);
}
}
int rt_rtc_ntp_sync_init(void)
{
static rt_bool_t init_ok = RT_FALSE;
rt_thread_t thread;
if (init_ok)
{
return 0;
}
thread = rt_thread_create("ntp_sync", ntp_sync_thread_enrty, RT_NULL, 1536, 26, 2);
if (thread)
{
rt_thread_startup(thread);
}
else
{
return -RT_ENOMEM;
}
init_ok = RT_TRUE;
}
INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init);
#endif /* RTC_SYNC_USING_NTP */
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
#include <rtdevice.h> #include <rtdevice.h>
@ -181,4 +239,5 @@ FINSH_FUNCTION_EXPORT(list_date, show date and time.)
FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28)) FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59)) FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
#endif
#endif /* RT_USING_FINSH */