Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
RT-Thread
rt-thread
提交
8e2a456d
R
rt-thread
项目概览
RT-Thread
/
rt-thread
大约 1 年 前同步成功
通知
775
Star
8911
Fork
4735
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8e2a456d
编写于
4月 15, 2021
作者:
mysterywolf
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
implement set_timeval
上级
6d7dafe0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
127 addition
and
65 deletion
+127
-65
components/libc/compilers/common/sys/time.h
components/libc/compilers/common/sys/time.h
+4
-1
components/libc/compilers/common/time.c
components/libc/compilers/common/time.c
+119
-60
include/rtdef.h
include/rtdef.h
+4
-4
未找到文件。
components/libc/compilers/common/sys/time.h
浏览文件 @
8e2a456d
...
@@ -40,7 +40,7 @@ struct timeval {
...
@@ -40,7 +40,7 @@ struct timeval {
#endif
#endif
#endif
/* _TIMEVAL_DEFINED */
#endif
/* _TIMEVAL_DEFINED */
#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)) && !defined (__ICCARM__) && !defined (_WIN32)
#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)
/*GCC*/
) && !defined (__ICCARM__) && !defined (_WIN32)
struct
timespec
{
struct
timespec
{
time_t
tv_sec
;
/* seconds */
time_t
tv_sec
;
/* seconds */
long
tv_nsec
;
/* and nanoseconds */
long
tv_nsec
;
/* and nanoseconds */
...
@@ -56,6 +56,9 @@ int stime(const time_t *t);
...
@@ -56,6 +56,9 @@ int stime(const time_t *t);
time_t
timegm
(
struct
tm
*
const
t
);
time_t
timegm
(
struct
tm
*
const
t
);
int
gettimeofday
(
struct
timeval
*
tv
,
struct
timezone
*
tz
);
int
gettimeofday
(
struct
timeval
*
tv
,
struct
timezone
*
tz
);
int
settimeofday
(
const
struct
timeval
*
tv
,
const
struct
timezone
*
tz
);
int
settimeofday
(
const
struct
timeval
*
tv
,
const
struct
timezone
*
tz
);
#if defined(__ARMCC_VERSION) || defined (__ICCARM__)
struct
tm
*
gmtime_r
(
const
time_t
*
timep
,
struct
tm
*
r
);
#endif
#ifdef RT_USING_POSIX
#ifdef RT_USING_POSIX
#include <sys/types.h>
#include <sys/types.h>
...
...
components/libc/compilers/common/time.c
浏览文件 @
8e2a456d
...
@@ -68,6 +68,104 @@ static void num2str(char *c, int i)
...
@@ -68,6 +68,104 @@ static void num2str(char *c, int i)
c
[
1
]
=
i
%
10
+
'0'
;
c
[
1
]
=
i
%
10
+
'0'
;
}
}
/**
* Get time from RTC device (without timezone)
* @param tv: struct timeval
* @return -1 failure; 1 success
*/
static
int
get_timeval
(
struct
timeval
*
tv
)
{
#ifdef RT_USING_RTC
static
rt_device_t
device
=
RT_NULL
;
#endif
rt_err_t
rst
=
-
RT_ERROR
;
if
(
tv
==
RT_NULL
)
return
-
1
;
/* default is 0 */
tv
->
tv_sec
=
0
;
tv
->
tv_usec
=
0
;
#ifdef RT_USING_RTC
/* optimization: find rtc device only first */
if
(
device
==
RT_NULL
)
{
device
=
rt_device_find
(
"rtc"
);
}
/* read timestamp from RTC device */
if
(
device
!=
RT_NULL
)
{
if
(
rt_device_open
(
device
,
0
)
==
RT_EOK
)
{
rst
=
rt_device_control
(
device
,
RT_DEVICE_CTRL_RTC_GET_TIME
,
&
tv
->
tv_sec
);
rt_device_control
(
device
,
RT_DEVICE_CTRL_RTC_GET_TIME_US
,
&
tv
->
tv_usec
);
rt_device_close
(
device
);
}
}
else
{
/* LOG_W will cause a recursive printing if ulog timestamp function is turned on */
rt_kprintf
(
"Cannot find a RTC device to provide time!
\r\n
"
);
return
-
1
;
}
return
(
rst
<
0
)
?
-
1
:
1
;
#else
/* LOG_W will cause a recursive printing if ulog timestamp function is turned on */
rt_kprintf
(
"Cannot find a RTC device to provide time!
\r\n
"
);
return
-
1
;
#endif
/* RT_USING_RTC */
}
/**
* Set time to RTC device (without timezone)
* @param tv: struct timeval
* @return -1 failure; 1 success
*/
static
int
set_timeval
(
struct
timeval
*
tv
)
{
rt_err_t
rst
=
-
RT_ERROR
;
#ifdef RT_USING_RTC
static
rt_device_t
device
=
RT_NULL
;
#endif
if
(
tv
==
RT_NULL
)
return
-
1
;
#ifdef RT_USING_RTC
/* optimization: find rtc device only first */
if
(
device
==
RT_NULL
)
{
device
=
rt_device_find
(
"rtc"
);
}
/* read timestamp from RTC device */
if
(
device
!=
RT_NULL
)
{
if
(
rt_device_open
(
device
,
0
)
==
RT_EOK
)
{
rst
=
rt_device_control
(
device
,
RT_DEVICE_CTRL_RTC_SET_TIME
,
&
tv
->
tv_sec
);
rt_device_control
(
device
,
RT_DEVICE_CTRL_RTC_SET_TIME_US
,
&
tv
->
tv_usec
);
rt_device_close
(
device
);
}
}
else
{
LOG_W
(
"Cannot find a RTC device to provide time!"
);
return
-
1
;
}
return
(
rst
<
0
)
?
-
1
:
1
;
#else
LOG_W
(
"Cannot find a RTC device to provide time!"
);
return
-
1
;
#endif
/* RT_USING_RTC */
}
struct
tm
*
gmtime_r
(
const
time_t
*
timep
,
struct
tm
*
r
)
struct
tm
*
gmtime_r
(
const
time_t
*
timep
,
struct
tm
*
r
)
{
{
time_t
i
;
time_t
i
;
...
@@ -183,49 +281,6 @@ char* ctime(const time_t *tim_p)
...
@@ -183,49 +281,6 @@ char* ctime(const time_t *tim_p)
}
}
RTM_EXPORT
(
ctime
);
RTM_EXPORT
(
ctime
);
/*-1 failure; 1 success*/
static
int
get_timeval
(
struct
timeval
*
tv
)
{
if
(
tv
==
RT_NULL
)
return
-
1
;
/* default is not available */
tv
->
tv_sec
=
-
1
;
/* default is 0 */
tv
->
tv_usec
=
0
;
#ifdef RT_USING_RTC
static
rt_device_t
device
=
RT_NULL
;
/* optimization: find rtc device only first */
if
(
device
==
RT_NULL
)
{
device
=
rt_device_find
(
"rtc"
);
}
/* read timestamp from RTC device */
if
(
device
!=
RT_NULL
)
{
if
(
rt_device_open
(
device
,
0
)
==
RT_EOK
)
{
rt_device_control
(
device
,
RT_DEVICE_CTRL_RTC_GET_TIME
,
&
tv
->
tv_sec
);
rt_device_control
(
device
,
RT_DEVICE_CTRL_RTC_GET_TIME_US
,
&
tv
->
tv_usec
);
rt_device_close
(
device
);
}
}
#endif
/* RT_USING_RTC */
if
(
tv
->
tv_sec
==
(
time_t
)
-
1
)
{
/* LOG_W will cause a recursive printing if ulog timestamp function is turned on */
rt_kprintf
(
"Cannot find a RTC device to provide time!
\r\n
"
);
tv
->
tv_sec
=
0
;
return
-
1
;
}
return
1
;
}
/**
/**
* Returns the current time.
* Returns the current time.
*
*
...
@@ -239,7 +294,7 @@ RT_WEAK time_t time(time_t *t)
...
@@ -239,7 +294,7 @@ RT_WEAK time_t time(time_t *t)
{
{
struct
timeval
now
;
struct
timeval
now
;
if
(
get_timeval
(
&
now
)
>
0
)
if
(
get_timeval
(
&
now
)
>
0
)
{
{
if
(
t
)
if
(
t
)
{
{
...
@@ -263,28 +318,24 @@ RTM_EXPORT(clock);
...
@@ -263,28 +318,24 @@ RTM_EXPORT(clock);
int
stime
(
const
time_t
*
t
)
int
stime
(
const
time_t
*
t
)
{
{
#ifdef RT_USING_RTC
struct
timeval
tv
;
rt_device_t
device
;
/* read timestamp from RTC device. */
if
(
!
t
)
device
=
rt_device_find
(
"rtc"
);
{
if
(
rt_device_open
(
device
,
0
)
==
RT_EOK
)
errno
=
EFAULT
;
return
-
1
;
}
tv
.
tv_sec
=
*
t
;
if
(
set_timeval
(
&
tv
)
>
0
)
{
{
rt_device_control
(
device
,
RT_DEVICE_CTRL_RTC_SET_TIME
,
(
void
*
)
t
);
return
0
;
rt_device_close
(
device
);
}
}
else
else
{
{
LOG_W
(
"Cannot find a RTC device to provide time!"
);
errno
=
EFAULT
;
errno
=
EFAULT
;
return
-
1
;
return
-
1
;
}
}
return
0
;
#else
LOG_W
(
"Cannot find a RTC device to provide time!"
);
errno
=
EFAULT
;
return
-
1
;
#endif
/* RT_USING_RTC */
}
}
RTM_EXPORT
(
stime
);
RTM_EXPORT
(
stime
);
...
@@ -366,7 +417,7 @@ RTM_EXPORT(timegm);
...
@@ -366,7 +417,7 @@ RTM_EXPORT(timegm);
/* TODO: timezone */
/* TODO: timezone */
int
gettimeofday
(
struct
timeval
*
tv
,
struct
timezone
*
tz
)
int
gettimeofday
(
struct
timeval
*
tv
,
struct
timezone
*
tz
)
{
{
if
(
tv
!=
RT_NULL
&&
get_timeval
(
tv
)
>
0
)
if
(
tv
!=
RT_NULL
&&
get_timeval
(
tv
)
>
0
)
{
{
return
0
;
return
0
;
}
}
...
@@ -385,7 +436,15 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz)
...
@@ -385,7 +436,15 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
{
if
(
tv
->
tv_sec
>=
0
&&
tv
->
tv_usec
>=
0
)
if
(
tv
->
tv_sec
>=
0
&&
tv
->
tv_usec
>=
0
)
{
{
return
stime
((
const
time_t
*
)
&
tv
->
tv_sec
);
if
(
set_timeval
((
struct
timeval
*
)
tv
)
>
0
)
{
return
0
;
}
else
{
errno
=
EFAULT
;
return
-
1
;
}
}
}
else
else
{
{
...
...
include/rtdef.h
浏览文件 @
8e2a456d
...
@@ -964,10 +964,10 @@ enum rt_device_class_type
...
@@ -964,10 +964,10 @@ enum rt_device_class_type
#define RT_DEVICE_CTRL_MTD_FORMAT 0x10
/**< format a MTD device */
#define RT_DEVICE_CTRL_MTD_FORMAT 0x10
/**< format a MTD device */
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10
/**< get second time */
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10
/**< get second time */
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11
/**< set second time */
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11
/**< set second time */
#define RT_DEVICE_CTRL_RTC_GET_
ALARM 0x12
/**< get alarm
*/
#define RT_DEVICE_CTRL_RTC_GET_
TIME_US 0x12
/**< get microsecond time
*/
#define RT_DEVICE_CTRL_RTC_SET_
ALARM 0x13
/**< set alarm
*/
#define RT_DEVICE_CTRL_RTC_SET_
TIME_US 0x13
/**< set microsecond time
*/
#define RT_DEVICE_CTRL_RTC_GET_
TIME_US 0x14
/**< get microsecond time
*/
#define RT_DEVICE_CTRL_RTC_GET_
ALARM 0x14
/**< get alarm
*/
#define RT_DEVICE_CTRL_RTC_SET_
TIME_US 0x15
/**< set microsecond time
*/
#define RT_DEVICE_CTRL_RTC_SET_
ALARM 0x15
/**< set alarm
*/
typedef
struct
rt_device
*
rt_device_t
;
typedef
struct
rt_device
*
rt_device_t
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录