reformat the coding style
using -RT_ERROR instead of RT_ERROR for the return value git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2311 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
2aab960999
commit
ec1f35670d
|
@ -1,173 +1,174 @@
|
||||||
/*
|
/*
|
||||||
* File : rtc.c
|
* File : rtc.c
|
||||||
* This file is part of RT-Thread RTOS
|
* This file is part of RT-Thread RTOS
|
||||||
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* The license and distribution terms for this file may be
|
||||||
* found in the file LICENSE in this distribution or at
|
* found in the file LICENSE in this distribution or at
|
||||||
* http://www.rt-thread.org/license/LICENSE
|
* http://www.rt-thread.org/license/LICENSE
|
||||||
*
|
*
|
||||||
* Change Logs:
|
* Change Logs:
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
|
|
||||||
/** \brief returns the current time.
|
/** \brief 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 */
|
||||||
#if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
|
#if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
|
||||||
#pragma module_name = "?time"
|
#pragma module_name = "?time"
|
||||||
time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
|
time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
|
||||||
#else
|
#else
|
||||||
time_t time(time_t* t)
|
time_t time(time_t *t)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
static rt_device_t device = RT_NULL;
|
static rt_device_t device = RT_NULL;
|
||||||
time_t time_now = 0;
|
time_t time_now = 0;
|
||||||
|
|
||||||
/* optimization: find rtc device only first. */
|
/* optimization: find rtc device only first. */
|
||||||
if (device == RT_NULL)
|
if (device == RT_NULL)
|
||||||
{
|
{
|
||||||
device = rt_device_find("rtc");
|
device = rt_device_find("rtc");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read timestamp from RTC device. */
|
/* read timestamp from RTC device. */
|
||||||
if (device != RT_NULL)
|
if (device != RT_NULL)
|
||||||
{
|
{
|
||||||
if (rt_device_open(device, 0) == RT_EOK)
|
if (rt_device_open(device, 0) == RT_EOK)
|
||||||
{
|
{
|
||||||
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
|
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
|
||||||
rt_device_close(device);
|
rt_device_close(device);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if t is not NULL, write timestamp to *t */
|
/* if t is not NULL, write timestamp to *t */
|
||||||
if (t != RT_NULL)
|
if (t != RT_NULL)
|
||||||
{
|
{
|
||||||
*t = time_now;
|
*t = time_now;
|
||||||
}
|
}
|
||||||
|
|
||||||
return time_now;
|
return time_now;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief set system date(time not modify).
|
/** \brief 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: 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)
|
||||||
{
|
{
|
||||||
time_t now;
|
time_t now;
|
||||||
struct tm * p_tm;
|
struct tm *p_tm;
|
||||||
struct tm tm_new;
|
struct tm tm_new;
|
||||||
rt_device_t device;
|
rt_device_t device;
|
||||||
rt_err_t ret = RT_ERROR;
|
rt_err_t ret = -RT_ERROR;
|
||||||
|
|
||||||
/* get current time */
|
/* get current time */
|
||||||
now = time(RT_NULL);
|
now = time(RT_NULL);
|
||||||
|
|
||||||
/* lock scheduler. */
|
/* lock scheduler. */
|
||||||
rt_enter_critical();
|
rt_enter_critical();
|
||||||
/* converts calendar time time into local time. */
|
/* converts calendar time time into local time. */
|
||||||
p_tm = localtime(&now);
|
p_tm = localtime(&now);
|
||||||
/* copy the statically located variable */
|
/* copy the statically located variable */
|
||||||
memcpy(&tm_new, p_tm, sizeof(struct tm));
|
memcpy(&tm_new, p_tm, sizeof(struct tm));
|
||||||
/* unlock scheduler. */
|
/* unlock scheduler. */
|
||||||
rt_exit_critical();
|
rt_exit_critical();
|
||||||
|
|
||||||
/* update date. */
|
/* update date. */
|
||||||
tm_new.tm_year = year - 1900;
|
tm_new.tm_year = year - 1900;
|
||||||
tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
|
tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
|
||||||
tm_new.tm_mday = day;
|
tm_new.tm_mday = day;
|
||||||
|
|
||||||
/* converts the local time in time to calendar time. */
|
/* converts the local time in time to calendar time. */
|
||||||
now = mktime(&tm_new);
|
now = mktime(&tm_new);
|
||||||
|
|
||||||
device = rt_device_find("rtc");
|
device = rt_device_find("rtc");
|
||||||
if (device == RT_NULL)
|
if (device == RT_NULL)
|
||||||
{
|
{
|
||||||
return RT_ERROR;
|
return -RT_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
|
/* update to RTC device. */
|
||||||
|
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
|
||||||
/* update to RTC device. */
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief set system time(date not modify).
|
/** \brief 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)
|
||||||
{
|
{
|
||||||
time_t now;
|
time_t now;
|
||||||
struct tm * p_tm;
|
struct tm *p_tm;
|
||||||
struct tm tm_new;
|
struct tm tm_new;
|
||||||
rt_device_t device;
|
rt_device_t device;
|
||||||
rt_err_t ret = RT_ERROR;
|
rt_err_t ret = -RT_ERROR;
|
||||||
|
|
||||||
/* get current time */
|
/* get current time */
|
||||||
now = time(RT_NULL);
|
now = time(RT_NULL);
|
||||||
|
|
||||||
/* lock scheduler. */
|
/* lock scheduler. */
|
||||||
rt_enter_critical();
|
rt_enter_critical();
|
||||||
/* converts calendar time time into local time. */
|
/* converts calendar time time into local time. */
|
||||||
p_tm = localtime(&now);
|
p_tm = localtime(&now);
|
||||||
/* copy the statically located variable */
|
/* copy the statically located variable */
|
||||||
memcpy(&tm_new, p_tm, sizeof(struct tm));
|
memcpy(&tm_new, p_tm, sizeof(struct tm));
|
||||||
/* unlock scheduler. */
|
/* unlock scheduler. */
|
||||||
rt_exit_critical();
|
rt_exit_critical();
|
||||||
|
|
||||||
/* update time. */
|
/* update time. */
|
||||||
tm_new.tm_hour = hour;
|
tm_new.tm_hour = hour;
|
||||||
tm_new.tm_min = minute;
|
tm_new.tm_min = minute;
|
||||||
tm_new.tm_sec = second;
|
tm_new.tm_sec = second;
|
||||||
|
|
||||||
/* converts the local time in time to calendar time. */
|
/* converts the local time in time to calendar time. */
|
||||||
now = mktime(&tm_new);
|
now = mktime(&tm_new);
|
||||||
|
|
||||||
device = rt_device_find("rtc");
|
device = rt_device_find("rtc");
|
||||||
if (device == RT_NULL)
|
if (device == RT_NULL)
|
||||||
{
|
{
|
||||||
return RT_ERROR;
|
return -RT_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
|
/* update to RTC device. */
|
||||||
|
ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
|
||||||
/* update to RTC device. */
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RT_USING_FINSH
|
#ifdef RT_USING_FINSH
|
||||||
#include <finsh.h>
|
#include <finsh.h>
|
||||||
#include <rtdevice.h>
|
#include <rtdevice.h>
|
||||||
void list_date(void)
|
|
||||||
{
|
void list_date(void)
|
||||||
time_t now;
|
{
|
||||||
|
time_t now;
|
||||||
now = time(RT_NULL);
|
|
||||||
rt_kprintf("%s\n", ctime(&now));
|
now = time(RT_NULL);
|
||||||
}
|
rt_kprintf("%s\n", ctime(&now));
|
||||||
FINSH_FUNCTION_EXPORT(list_date, show date and time.)
|
}
|
||||||
|
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))
|
FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
|
||||||
#endif
|
FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue