summaryrefslogtreecommitdiff
path: root/components/drivers/rtc/rtc_dm.c
blob: 7aa1ce6cc0cab30b91fffb7f9a9f1e774772687c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-12-06     GuEe-GUI     first version
 */

#include <rtatomic.h>
#include "rtc_dm.h"

#define DBG_TAG "rtc.dm"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

int rtc_dev_set_name(struct rt_device *rtc_dev)
{
    int id;
    static volatile rt_atomic_t uid = 1;

    RT_ASSERT(rtc_dev != RT_NULL)

    if (rt_device_find("rtc"))
    {
        id = (int)rt_atomic_add(&uid, 1);

        return rt_dm_dev_set_name(rtc_dev, "rtc%u", id);
    }
    else
    {
        return rt_dm_dev_set_name(rtc_dev, "rtc");
    }
}

time_t rtc_wkalarm_to_timestamp(struct rt_rtc_wkalarm *alarm)
{
    struct tm tm_time;
    time_t current_time;

    current_time = time(RT_NULL);
    localtime_r(&current_time, &tm_time);

    tm_time.tm_sec = alarm->tm_sec;
    tm_time.tm_min = alarm->tm_min;
    tm_time.tm_hour = alarm->tm_hour;

    return timegm(&tm_time);
}

void rtc_timestamp_to_wkalarm(time_t timestamp, struct rt_rtc_wkalarm *alarm)
{
    struct tm tm_time;

    localtime_r(&timestamp, &tm_time);

    alarm->tm_sec = tm_time.tm_sec;
    alarm->tm_min = tm_time.tm_min;
    alarm->tm_hour = tm_time.tm_hour;
}