summaryrefslogtreecommitdiff
path: root/bsp/airm2m/air105/libraries/rt_drivers/drv_rtc.c
blob: 9c21eb248c8033baa7c0344c84fb419ae4396a55 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-02-22     airm2m       first version
 */


#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include <sys/time.h>

#ifdef BSP_USING_ONCHIP_RTC

#ifndef RTC_BKP_DR1
#define RTC_BKP_DR1 RT_NULL
#endif

//#define DRV_DEBUG
#define LOG_TAG             "drv.rtc"
#include <drv_log.h>

static rt_err_t air105_rtc_init(void)
{
    RTC_GlobalInit();
    return RT_EOK;
}

static rt_err_t air105_rtc_get_secs(void *args)
{
    *(rt_uint32_t *) args = RTC_GetUTC();
    LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);

    return RT_EOK;
}

static rt_err_t air105_rtc_set_secs(void *args)
{
    rt_err_t result = RT_EOK;
    rt_uint32_t stamp = *(rt_uint32_t *)args;
    RTC_SetStamp(stamp);
    LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);

    return result;
}

static rt_err_t air105_rtc_get_timeval(void *args)
{
    struct timeval *tv = (struct timeval *) args;
    tv->tv_sec = RTC_GetUTC();
    return RT_EOK;
}

static const struct rt_rtc_ops air105_rtc_ops =
{
    air105_rtc_init,
    air105_rtc_get_secs,
    air105_rtc_set_secs,
    RT_NULL,
    RT_NULL,
    air105_rtc_get_timeval,
    RT_NULL,
};

static rt_rtc_dev_t air105_rtc_dev;

static int rt_hw_rtc_init(void)
{
    rt_err_t result;

    air105_rtc_dev.ops = &air105_rtc_ops;
    result = rt_hw_rtc_register(&air105_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
    if (result != RT_EOK)
    {
        LOG_E("rtc register err code: %d", result);
        return result;
    }
    LOG_D("rtc init success");

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif /* BSP_USING_ONCHIP_RTC */