2024-05-31

This commit is contained in:
2584532475@qq.com committed 2024-05-31 16:57:54 +08:00
1 parent f8591b8b99
commit 4a963bd78c
27 files changed
+2956 -229

No files matched your search

@@ -0,0 +1,609 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2012-10-27 heyuanjie87 first version.
* 2013-05-17 aozima initial alarm event & mutex in system init.
*/
#include <rtthread.h>
#include <rtdevice.h>
#define RT_RTC_YEARS_MAX 137
#define RT_ALARM_DELAY 2
#define RT_ALARM_STATE_INITED 0x02
#define RT_ALARM_STATE_START 0x01
#define RT_ALARM_STATE_STOP 0x00
#if (defined(RT_USING_RTC) && defined(RT_USING_ALARM))
static struct rt_alarm_container _container;
rt_inline rt_uint32_t alarm_mkdaysec(struct tm *time)
{
rt_uint32_t sec;
sec = time->tm_sec;
sec += time->tm_min * 60;
sec += time->tm_hour * 3600;
return (sec);
}
static rt_err_t alarm_set(struct rt_alarm *alarm)
{
rt_device_t device;
struct rt_rtc_wkalarm wkalarm;
rt_err_t ret;
device = rt_device_find("rtc");
if (device == RT_NULL)
{
return (RT_ERROR);
}
if (alarm->flag & RT_ALARM_STATE_START)
wkalarm.enable = RT_TRUE;
else
wkalarm.enable = RT_FALSE;
wkalarm.tm_sec = alarm->wktime.tm_sec;
wkalarm.tm_min = alarm->wktime.tm_min;
wkalarm.tm_hour = alarm->wktime.tm_hour;
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_ALARM, &wkalarm);
if ((ret == RT_EOK) && wkalarm.enable)
{
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_ALARM, &wkalarm);
if (ret == RT_EOK)
{
/*
some RTC device like RX8025,it's alarms precision is 1 minute.
in this case,low level RTC driver should set wkalarm->tm_sec to 0.
*/
alarm->wktime.tm_sec = wkalarm.tm_sec;
alarm->wktime.tm_min = wkalarm.tm_min;
alarm->wktime.tm_hour = wkalarm.tm_hour;
}
}
return (ret);
}
static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
{
rt_uint32_t sec_alarm, sec_now;
rt_bool_t wakeup = RT_FALSE;
time_t timestamp;
sec_alarm = alarm_mkdaysec(&alarm->wktime);
sec_now = alarm_mkdaysec(now);
if (alarm->flag & RT_ALARM_STATE_START)
{
switch (alarm->flag & 0xFF00)
{
case RT_ALARM_ONESHOT:
{
sec_alarm = mktime(&alarm->wktime);
sec_now = mktime(now);
if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm))
{
/* stop alarm */
alarm->flag &= ~RT_ALARM_STATE_START;
alarm_set(alarm);
wakeup = RT_TRUE;
}
}
break;
case RT_ALARM_DAILY:
{
if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm))
wakeup = RT_TRUE;
}
break;
case RT_ALARM_WEEKLY:
{
/* alarm at wday */
sec_alarm += alarm->wktime.tm_wday * 24 * 3600;
sec_now += now->tm_wday * 24 * 3600;
if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm))
wakeup = RT_TRUE;
}
break;
case RT_ALARM_MONTHLY:
{
/* monthly someday generate alarm signals */
if (alarm->wktime.tm_mday == now->tm_mday)
{
if ((sec_now - sec_alarm) <= RT_ALARM_DELAY)
wakeup = RT_TRUE;
}
}
break;
case RT_ALARM_YAERLY:
{
if ((alarm->wktime.tm_mday == now->tm_mday) && \
(alarm->wktime.tm_mon == now->tm_mon))
{
if ((sec_now - sec_alarm) <= RT_ALARM_DELAY)
wakeup = RT_TRUE;
}
}
break;
}
if ((wakeup == RT_TRUE) && (alarm->callback != RT_NULL))
{
timestamp = time(RT_NULL);
alarm->callback(alarm, timestamp);
}
}
}
static void alarm_update(rt_uint32_t event)
{
struct rt_alarm *alm_prev = RT_NULL, *alm_next = RT_NULL;
struct rt_alarm *alarm;
rt_int32_t sec_now, sec_alarm, sec_tmp;
rt_int32_t sec_next = 24 * 3600, sec_prev = 0;
time_t timestamp;
struct tm now;
rt_list_t *next;
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
if (!rt_list_isempty(&_container.head))
{
/* get time of now */
timestamp = time(RT_NULL);
localtime_r(&timestamp, &now);
for (next = _container.head.next; next != &_container.head; next = next->next)
{
alarm = rt_list_entry(next, struct rt_alarm, list);
/* check the overtime alarm */
alarm_wakeup(alarm, &now);
}
timestamp = time(RT_NULL);
localtime_r(&timestamp, &now);
sec_now = alarm_mkdaysec(&now);
for (next = _container.head.next; next != &_container.head; next = next->next)
{
alarm = rt_list_entry(next, struct rt_alarm, list);
/* calculate seconds from 00:00:00 */
sec_alarm = alarm_mkdaysec(&alarm->wktime);
if ((alarm->flag & RT_ALARM_STATE_START) && (alarm != _container.current))
{
sec_tmp = sec_alarm - sec_now;
if (sec_tmp > 0)
{
/* find alarm after now(now to 23:59:59) and the most recent */
if (sec_tmp < sec_next)
{
sec_next = sec_tmp;
alm_next = alarm;
}
}
else
{
/* find alarm before now(00:00:00 to now) and furthest from now */
if (sec_tmp < sec_prev)
{
sec_prev = sec_tmp;
alm_prev = alarm;
}
}
}
}
/* enable the alarm after now first */
if (sec_next < 24 * 3600)
{
if (alarm_set(alm_next) == RT_EOK)
_container.current = alm_next;
}
else if (sec_prev < 0)
{
/* enable the alarm before now */
if (alarm_set(alm_prev) == RT_EOK)
_container.current = alm_prev;
}
}
rt_mutex_release(&_container.mutex);
}
static rt_uint32_t days_of_year_month(int tm_year, int tm_mon)
{
rt_uint32_t ret, year;
year = tm_year + 1900;
if (tm_mon == 1)
{
ret = 28 + ((!(year % 4) && (year % 100)) || !(year % 400));
}
else if (((tm_mon <= 6) && (tm_mon % 2 == 0)) || ((tm_mon > 6) && (tm_mon % 2 == 1)))
{
ret = 31;
}
else
{
ret = 30;
}
return (ret);
}
static rt_bool_t is_valid_date(struct tm *date)
{
if ((date->tm_year < 0) || (date->tm_year > RT_RTC_YEARS_MAX))
{
return (RT_FALSE);
}
if ((date->tm_mon < 0) || (date->tm_mon > 11))
{
return (RT_FALSE);
}
if ((date->tm_mday < 1) || \
(date->tm_mday > days_of_year_month(date->tm_year, date->tm_mon)))
{
return (RT_FALSE);
}
return (RT_TRUE);
}
static rt_err_t alarm_setup(rt_alarm_t alarm, struct tm *wktime)
{
rt_err_t ret = RT_ERROR;
time_t timestamp;
struct tm *setup, now;
setup = &alarm->wktime;
*setup = *wktime;
timestamp = time(RT_NULL);
localtime_r(&timestamp, &now);
/* if these are a "don't care" value,we set them to now*/
if ((setup->tm_sec > 59) || (setup->tm_sec < 0))
setup->tm_sec = now.tm_sec;
if ((setup->tm_min > 59) || (setup->tm_min < 0))
setup->tm_min = now.tm_min;
if ((setup->tm_hour > 23) || (setup->tm_hour < 0))
setup->tm_hour = now.tm_hour;
switch (alarm->flag & 0xFF00)
{
case RT_ALARM_DAILY:
{
/* do nothing but needed */
}
break;
case RT_ALARM_ONESHOT:
{
/* if these are "don't care" value we set them to now */
if (setup->tm_year == RT_ALARM_TM_NOW)
setup->tm_year = now.tm_year;
if (setup->tm_mon == RT_ALARM_TM_NOW)
setup->tm_mon = now.tm_mon;
if (setup->tm_mday == RT_ALARM_TM_NOW)
setup->tm_mday = now.tm_mday;
/* make sure the setup is valid */
if (!is_valid_date(setup))
goto _exit;
}
break;
case RT_ALARM_WEEKLY:
{
/* if tm_wday is a "don't care" value we set it to now */
if ((setup->tm_wday < 0) || (setup->tm_wday > 6))
setup->tm_wday = now.tm_wday;
}
break;
case RT_ALARM_MONTHLY:
{
/* if tm_mday is a "don't care" value we set it to now */
if ((setup->tm_mday < 1) || (setup->tm_mday > 31))
setup->tm_mday = now.tm_mday;
}
break;
case RT_ALARM_YAERLY:
{
/* if tm_mon is a "don't care" value we set it to now */
if ((setup->tm_mon < 0) || (setup->tm_mon > 11))
setup->tm_mon = now.tm_mon;
if (setup->tm_mon == 1)
{
/* tm_mon is February */
/* tm_mday should be 1~29.otherwise,it's a "don't care" value */
if ((setup->tm_mday < 1) || (setup->tm_mday > 29))
setup->tm_mday = now.tm_mday;
}
else if (((setup->tm_mon <= 6) && (setup->tm_mon % 2 == 0)) || \
((setup->tm_mon > 6) && (setup->tm_mon % 2 == 1)))
{
/* Jan,Mar,May,Jul,Aug,Oct,Dec */
/* tm_mday should be 1~31.otherwise,it's a "don't care" value */
if ((setup->tm_mday < 1) || (setup->tm_mday > 31))
setup->tm_mday = now.tm_mday;
}
else
{
/* tm_mday should be 1~30.otherwise,it's a "don't care" value */
if ((setup->tm_mday < 1) || (setup->tm_mday > 30))
setup->tm_mday = now.tm_mday;
}
}
break;
default:
{
goto _exit;
}
}
if ((setup->tm_hour == 23) && (setup->tm_min == 59) && (setup->tm_sec == 59))
{
/*
for insurance purposes, we will generate an alarm
signal two seconds ahead of.
*/
setup->tm_sec = 60 - RT_ALARM_DELAY;
}
/* set initialized state */
alarm->flag |= RT_ALARM_STATE_INITED;
ret = RT_EOK;
_exit:
return (ret);
}
/** \brief send a rtc alarm event
*
* \param dev pointer to RTC device(currently unused,you can ignore it)
* \param event RTC event(currently unused)
* \return none
*/
void rt_alarm_update(rt_device_t dev, rt_uint32_t event)
{
rt_event_send(&_container.event, 1);
}
/** \brief modify the alarm setup
*
* \param alarm pointer to alarm
* \param cmd control command
* \param arg argument
*/
rt_err_t rt_alarm_control(rt_alarm_t alarm, int cmd, void *arg)
{
rt_err_t ret = RT_ERROR;
RT_ASSERT(alarm != RT_NULL);
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
switch (cmd)
{
case RT_ALARM_CTRL_MODIFY:
{
struct rt_alarm_setup *setup;
RT_ASSERT(arg != RT_NULL);
setup = arg;
rt_alarm_stop(alarm);
alarm->flag = setup->flag & 0xFF00;
alarm->wktime = setup->wktime;
ret = alarm_setup(alarm, &alarm->wktime);
}
break;
}
rt_mutex_release(&_container.mutex);
return (ret);
}
/** \brief start an alarm
*
* \param alarm pointer to alarm
* \return RT_EOK
*/
rt_err_t rt_alarm_start(rt_alarm_t alarm)
{
rt_int32_t sec_now, sec_old, sec_new;
rt_err_t ret = RT_ERROR;
time_t timestamp;
struct tm now;
if (alarm == RT_NULL)
return (ret);
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
if (!(alarm->flag & RT_ALARM_STATE_INITED))
{
if (alarm_setup(alarm, &alarm->wktime) != RT_EOK)
goto _exit;
}
if ((alarm->flag & 0x01) == RT_ALARM_STATE_STOP)
{
timestamp = time(RT_NULL);
localtime_r(&timestamp, &now);
alarm->flag |= RT_ALARM_STATE_START;
/* set alarm */
if (_container.current == RT_NULL)
{
ret = alarm_set(alarm);
}
else
{
sec_now = alarm_mkdaysec(&now);
sec_old = alarm_mkdaysec(&_container.current->wktime);
sec_new = alarm_mkdaysec(&alarm->wktime);
if ((sec_new < sec_old) && (sec_new > sec_now))
{
ret = alarm_set(alarm);
}
else if ((sec_new > sec_now) && (sec_old < sec_now))
{
ret = alarm_set(alarm);
}
else if ((sec_new < sec_old) && (sec_old < sec_now))
{
ret = alarm_set(alarm);
}
else
{
ret = RT_EOK;
goto _exit;
}
}
if (ret == RT_EOK)
{
_container.current = alarm;
}
}
_exit:
rt_mutex_release(&_container.mutex);
return (ret);
}
/** \brief stop an alarm
*
* \param alarm pointer to alarm
* \return RT_EOK
*/
rt_err_t rt_alarm_stop(rt_alarm_t alarm)
{
rt_err_t ret = RT_ERROR;
if (alarm == RT_NULL)
return (ret);
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
if (!(alarm->flag & RT_ALARM_STATE_START))
goto _exit;
/* stop alarm */
alarm->flag &= ~RT_ALARM_STATE_START;
if (_container.current == alarm)
{
ret = alarm_set(alarm);
_container.current = RT_NULL;
}
if (ret == RT_EOK)
alarm_update(0);
_exit:
rt_mutex_release(&_container.mutex);
return (ret);
}
/** \brief delete an alarm
*
* \param alarm pointer to alarm
* \return RT_EOK
*/
rt_err_t rt_alarm_delete(rt_alarm_t alarm)
{
rt_err_t ret = RT_ERROR;
if (alarm == RT_NULL)
return (ret);
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
/* stop the alarm */
alarm->flag &= ~RT_ALARM_STATE_START;
if (_container.current == alarm)
{
ret = alarm_set(alarm);
_container.current = RT_NULL;
/* set new alarm if necessary */
alarm_update(0);
}
rt_list_remove(&alarm->list);
rt_free(alarm);
rt_mutex_release(&_container.mutex);
return (ret);
}
/** \brief create an alarm
*
* \param flag set alarm mode e.g: RT_ALARM_DAILY
* \param setup pointer to setup infomation
*/
rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback, struct rt_alarm_setup *setup)
{
struct rt_alarm *alarm;
if (setup == RT_NULL)
return (RT_NULL);
alarm = rt_malloc(sizeof(struct rt_alarm));
if (alarm == RT_NULL)
return (RT_NULL);
rt_list_init(&alarm->list);
alarm->wktime = setup->wktime;
alarm->flag = setup->flag & 0xFF00;
alarm->callback = callback;
rt_mutex_take(&_container.mutex, RT_WAITING_FOREVER);
rt_list_insert_after(&_container.head, &alarm->list);
rt_mutex_release(&_container.mutex);
return (alarm);
}
/** \brief rtc alarm service thread entry
*
*/
static void rt_alarmsvc_thread_init(void *param)
{
rt_uint32_t recv;
_container.current = RT_NULL;
while (1)
{
if (rt_event_recv(&_container.event, 0xFFFF,
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &recv) == RT_EOK)
{
alarm_update(recv);
}
}
}
/** \brief initialize alarm service system
*
* \param none
* \return none
*/
void rt_alarm_system_init(void)
{
rt_thread_t tid;
rt_list_init(&_container.head);
rt_event_init(&_container.event, "alarmsvc", RT_IPC_FLAG_FIFO);
rt_mutex_init(&_container.mutex, "alarmsvc", RT_IPC_FLAG_FIFO);
tid = rt_thread_create("alarmsvc",
rt_alarmsvc_thread_init, RT_NULL,
512, 8, 1);
if (tid != RT_NULL)
rt_thread_startup(tid);
}
#endif
@@ -0,0 +1,254 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2012-01-29 aozima first version.
* 2012-04-12 aozima optimization: find rtc device only first.
* 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 <string.h>
#include <rtthread.h>
#ifdef RT_USING_RTC
/* 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 */
/**
* Set system date(time not modify).
*
* @param rt_uint32_t year e.g: 2012.
* @param rt_uint32_t month e.g: 12 (1~12).
* @param rt_uint32_t day e.g: 31.
*
* @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)
{
time_t now;
struct tm *p_tm;
struct tm tm_new;
rt_device_t device;
rt_err_t ret = -RT_ERROR;
/* get current time */
now = time(RT_NULL);
/* lock scheduler. */
rt_enter_critical();
/* converts calendar time time into local time. */
p_tm = localtime(&now);
/* copy the statically located variable */
memcpy(&tm_new, p_tm, sizeof(struct tm));
/* unlock scheduler. */
rt_exit_critical();
/* update date. */
tm_new.tm_year = year - 1900;
tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
tm_new.tm_mday = day;
/* converts the local time in time to calendar time. */
now = mktime(&tm_new);
device = rt_device_find("rtc");
if (device == RT_NULL)
{
return -RT_ERROR;
}
/* update to RTC device. */
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
return ret;
}
/**
* Set system time(date not modify).
*
* @param rt_uint32_t hour e.g: 0~23.
* @param rt_uint32_t minute e.g: 0~59.
* @param rt_uint32_t second e.g: 0~59.
*
* @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)
{
time_t now;
struct tm *p_tm;
struct tm tm_new;
rt_device_t device;
rt_err_t ret = -RT_ERROR;
/* get current time */
now = time(RT_NULL);
/* lock scheduler. */
rt_enter_critical();
/* converts calendar time time into local time. */
p_tm = localtime(&now);
/* copy the statically located variable */
memcpy(&tm_new, p_tm, sizeof(struct tm));
/* unlock scheduler. */
rt_exit_critical();
/* update time. */
tm_new.tm_hour = hour;
tm_new.tm_min = minute;
tm_new.tm_sec = second;
/* converts the local time in time to calendar time. */
now = mktime(&tm_new);
device = rt_device_find("rtc");
if (device == RT_NULL)
{
return -RT_ERROR;
}
/* update to RTC device. */
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
return ret;
}
#ifdef RTC_SYNC_USING_NTP
static void ntp_sync_thread_enrty(void *param)
{
extern time_t ntp_sync_to_rtc(const char *host_name);
/* first sync delay for network connect */
rt_thread_delay(RTC_NTP_FIRST_SYNC_DELAY * RT_TICK_PER_SECOND);
while (1)
{
ntp_sync_to_rtc(NULL);
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;
return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_rtc_ntp_sync_init);
#endif /* RTC_SYNC_USING_NTP */
#ifdef RT_USING_FINSH
#include <finsh.h>
#include <rtdevice.h>
void list_date(void)
{
time_t now;
now = time(RT_NULL);
rt_kprintf("%s\n", ctime(&now));
}
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_time, set time. e.g: set_time(23,59,59))
#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
static void date(uint8_t argc, char **argv)
{
if (argc == 1)
{
time_t now;
/* output current time */
now = time(RT_NULL);
rt_kprintf("%s", ctime(&now));
}
else if (argc >= 7)
{
/* set time and date */
uint16_t year;
uint8_t month, day, hour, min, sec;
year = atoi(argv[1]);
month = atoi(argv[2]);
day = atoi(argv[3]);
hour = atoi(argv[4]);
min = atoi(argv[5]);
sec = atoi(argv[6]);
if (year > 2099 || year < 2000)
{
rt_kprintf("year is out of range [2000-2099]\n");
return;
}
if (month == 0 || month > 12)
{
rt_kprintf("month is out of range [1-12]\n");
return;
}
if (day == 0 || day > 31)
{
rt_kprintf("day is out of range [1-31]\n");
return;
}
if (hour > 23)
{
rt_kprintf("hour is out of range [0-23]\n");
return;
}
if (min > 59)
{
rt_kprintf("minute is out of range [0-59]\n");
return;
}
if (sec > 59)
{
rt_kprintf("second is out of range [0-59]\n");
return;
}
set_time(hour, min, sec);
set_date(year, month, day);
}
else
{
rt_kprintf("please input: date [year month day hour min sec] or date\n");
rt_kprintf("e.g: date 2018 01 01 23 59 59 or date\n");
}
}
MSH_CMD_EXPORT(date, get date and time or set [year month day hour min sec]);
#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
#endif /* RT_USING_FINSH */
#endif /* RT_USING_RTC */
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2012-10-10 aozima first version.
*/
#ifndef __RTC_H__
#define __RTC_H__
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
int rt_soft_rtc_init(void);
int rt_rtc_ntp_sync_init(void);
#endif /* __RTC_H__ */
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-01-30 armink the first version
*/
#include <time.h>
#include <string.h>
#include <rtthread.h>
#include <drivers/rtc.h>
#ifdef RT_USING_SOFT_RTC
/* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */
#define RTC_TIME_INIT(year, month, day, hour, minute, second) \
{.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second}
#ifndef SOFT_RTC_TIME_DEFAULT
#define SOFT_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0)
#endif
static struct rt_device soft_rtc_dev;
static rt_tick_t init_tick;
static time_t init_time;
static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
{
time_t *time;
struct tm time_temp;
RT_ASSERT(dev != RT_NULL);
memset(&time_temp, 0, sizeof(struct tm));
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
time = (time_t *) args;
*time = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
{
time = (time_t *) args;
init_time = *time - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
break;
}
}
return RT_EOK;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops soft_rtc_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
soft_rtc_control
};
#endif
int rt_soft_rtc_init(void)
{
static rt_bool_t init_ok = RT_FALSE;
struct tm time_new = SOFT_RTC_TIME_DEFAULT;
if (init_ok)
{
return 0;
}
/* make sure only one 'rtc' device */
RT_ASSERT(!rt_device_find("rtc"));
init_tick = rt_tick_get();
init_time = mktime(&time_new);
soft_rtc_dev.type = RT_Device_Class_RTC;
/* register rtc device */
#ifdef RT_USING_DEVICE_OPS
soft_rtc_dev.ops = &soft_rtc_ops;
#else
soft_rtc_dev.init = RT_NULL;
soft_rtc_dev.open = RT_NULL;
soft_rtc_dev.close = RT_NULL;
soft_rtc_dev.read = RT_NULL;
soft_rtc_dev.write = RT_NULL;
soft_rtc_dev.control = soft_rtc_control;
#endif
/* no private */
soft_rtc_dev.user_data = RT_NULL;
rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
init_ok = RT_TRUE;
return 0;
}
INIT_DEVICE_EXPORT(rt_soft_rtc_init);
#endif /* RT_USING_SOFT_RTC */