Skip to content

Commit a083fb9

Browse files
committed
[dm][rtc] support DM API for RTC
1. rtc_dev_set_name for RTC device init the name auto. 2. rtc_wkalarm_to_timestamp and rtc_timestamp_to_wkalarm for rt_rtc_wkalarm/time_t convert. Signed-off-by: GuEe-GUI <[email protected]>
1 parent 46bfd4d commit a083fb9

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

components/drivers/rtc/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ if GetDepend(['RT_USING_RTC']):
1313
if GetDepend(['RT_USING_SOFT_RTC']):
1414
src = src + ['dev_soft_rtc.c']
1515

16+
if GetDepend(['RT_USING_DM']):
17+
src += ['rtc_dm.c']
18+
1619
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_RTC'], CPPPATH = CPPPATH)
1720

1821
Return('group')

components/drivers/rtc/rtc_dm.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-12-06 GuEe-GUI first version
9+
*/
10+
11+
#include <rtatomic.h>
12+
#include "rtc_dm.h"
13+
14+
#define DBG_TAG "rtc.dm"
15+
#define DBG_LVL DBG_INFO
16+
#include <rtdbg.h>
17+
18+
int rtc_dev_set_name(struct rt_device *rtc_dev)
19+
{
20+
int id;
21+
static volatile rt_atomic_t uid = 1;
22+
23+
RT_ASSERT(rtc_dev != RT_NULL)
24+
25+
if (rt_device_find("rtc"))
26+
{
27+
id = (int)rt_atomic_add(&uid, 1);
28+
29+
return rt_dm_dev_set_name(rtc_dev, "rtc%u", id);
30+
}
31+
else
32+
{
33+
return rt_dm_dev_set_name(rtc_dev, "rtc");
34+
}
35+
}
36+
37+
time_t rtc_wkalarm_to_timestamp(struct rt_rtc_wkalarm *alarm)
38+
{
39+
struct tm tm_time;
40+
time_t current_time;
41+
42+
current_time = time(RT_NULL);
43+
localtime_r(&current_time, &tm_time);
44+
45+
tm_time.tm_sec = alarm->tm_sec;
46+
tm_time.tm_min = alarm->tm_min;
47+
tm_time.tm_hour = alarm->tm_hour;
48+
49+
return timegm(&tm_time);
50+
}
51+
52+
void rtc_timestamp_to_wkalarm(time_t timestamp, struct rt_rtc_wkalarm *alarm)
53+
{
54+
struct tm tm_time;
55+
56+
localtime_r(&timestamp, &tm_time);
57+
58+
alarm->tm_sec = tm_time.tm_sec;
59+
alarm->tm_min = tm_time.tm_min;
60+
alarm->tm_hour = tm_time.tm_hour;
61+
}

components/drivers/rtc/rtc_dm.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-12-06 GuEe-GUI first version
9+
*/
10+
11+
#ifndef __RTC_DM_H__
12+
#define __RTC_DM_H__
13+
14+
#include <rthw.h>
15+
#include <rtthread.h>
16+
#include <rtdevice.h>
17+
18+
#include <sys/time.h>
19+
20+
int rtc_dev_set_name(struct rt_device *rtc_dev);
21+
time_t rtc_wkalarm_to_timestamp(struct rt_rtc_wkalarm *alarm);
22+
void rtc_timestamp_to_wkalarm(time_t timestamp, struct rt_rtc_wkalarm *alarm);
23+
24+
#endif /* __RTC_DM_H__ */

0 commit comments

Comments
 (0)