drv_rtc.c 3.1 KB
Newer Older
N
nxp58695 已提交
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
N
nxp58695 已提交
3 4 5 6 7 8 9 10
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-03-15     Liuguang     the first version.
 * 2019-07-19     Magicoe      The first version for LPC55S6x
 */
mysterywolf's avatar
mysterywolf 已提交
11

12 13 14
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
mysterywolf's avatar
mysterywolf 已提交
15 16
#include "drv_rtc.h"
#include "fsl_common.h"
N
nxp58695 已提交
17 18 19 20 21 22 23 24
#include "fsl_rtc.h"

#ifdef RT_USING_RTC

#if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
    #error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
#endif

mysterywolf's avatar
mysterywolf 已提交
25
static time_t get_timestamp(void)
N
nxp58695 已提交
26
{
mysterywolf's avatar
mysterywolf 已提交
27 28 29
    struct tm tm_new = {0};
    rtc_datetime_t rtcDate;

N
nxp58695 已提交
30 31
    /* Get date time */
    RTC_GetDatetime(RTC, &rtcDate);
mysterywolf's avatar
mysterywolf 已提交
32 33 34

    tm_new.tm_sec  = rtcDate.second;
    tm_new.tm_min  = rtcDate.minute;
N
nxp58695 已提交
35
    tm_new.tm_hour = rtcDate.hour;
mysterywolf's avatar
mysterywolf 已提交
36 37 38 39

    tm_new.tm_mday = rtcDate.day;
    tm_new.tm_mon  = rtcDate.month - 1;
    tm_new.tm_year = rtcDate.year - 1900;
N
nxp58695 已提交
40

41
    return timegm(&tm_new);
N
nxp58695 已提交
42 43 44 45
}

static int set_timestamp(time_t timestamp)
{
46
    struct tm now;
mysterywolf's avatar
mysterywolf 已提交
47 48
    rtc_datetime_t rtcDate;

49
    gmtime_r(&timestamp, &now);
mysterywolf's avatar
mysterywolf 已提交
50

51 52 53
    rtcDate.second = now.tm_sec ;
    rtcDate.minute = now.tm_min ;
    rtcDate.hour   = now.tm_hour;
mysterywolf's avatar
mysterywolf 已提交
54

55 56 57
    rtcDate.day    = now.tm_mday;
    rtcDate.month  = now.tm_mon  + 1;
    rtcDate.year   = now.tm_year + 1900;
mysterywolf's avatar
mysterywolf 已提交
58

N
nxp58695 已提交
59 60
    /* RTC time counter has to be stopped before setting the date & time in the TSR register */
    RTC_StopTimer(RTC);
mysterywolf's avatar
mysterywolf 已提交
61

N
nxp58695 已提交
62 63 64 65 66
    /* Set RTC time to default */
    RTC_SetDatetime(RTC, &rtcDate);

    /* Start the RTC time counter */
    RTC_StartTimer(RTC);
mysterywolf's avatar
mysterywolf 已提交
67

N
nxp58695 已提交
68 69 70 71 72 73 74
    return RT_EOK;
}

static rt_err_t lpc_rtc_init(rt_device_t dev)
{
    /* Init RTC */
    RTC_Init(RTC);
mysterywolf's avatar
mysterywolf 已提交
75

N
nxp58695 已提交
76 77
    /* Start the RTC time counter */
    RTC_StartTimer(RTC);
mysterywolf's avatar
mysterywolf 已提交
78 79

    return RT_EOK;
N
nxp58695 已提交
80 81 82 83
}

static rt_err_t lpc_rtc_open(rt_device_t dev, rt_uint16_t oflag)
{
mysterywolf's avatar
mysterywolf 已提交
84
    return RT_EOK;
N
nxp58695 已提交
85 86
}

mysterywolf's avatar
mysterywolf 已提交
87
static rt_err_t lpc_rtc_close(rt_device_t dev)
N
nxp58695 已提交
88
{
mysterywolf's avatar
mysterywolf 已提交
89 90
    return RT_EOK;
}
N
nxp58695 已提交
91 92 93

static rt_size_t lpc_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
mysterywolf's avatar
mysterywolf 已提交
94
    return 0;
N
nxp58695 已提交
95 96 97 98
}

static rt_size_t lpc_rtc_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
mysterywolf's avatar
mysterywolf 已提交
99
    return 0;
N
nxp58695 已提交
100 101 102 103 104
}

static rt_err_t lpc_rtc_control(rt_device_t dev, int cmd, void *args)
{
    RT_ASSERT(dev != RT_NULL);
mysterywolf's avatar
mysterywolf 已提交
105

N
nxp58695 已提交
106 107
    switch(cmd)
    {
mysterywolf's avatar
mysterywolf 已提交
108
        case RT_DEVICE_CTRL_RTC_GET_TIME:
N
nxp58695 已提交
109
        {
mysterywolf's avatar
mysterywolf 已提交
110
            *(uint32_t *)args = get_timestamp();
N
nxp58695 已提交
111 112
        }
        break;
mysterywolf's avatar
mysterywolf 已提交
113 114

        case RT_DEVICE_CTRL_RTC_SET_TIME:
N
nxp58695 已提交
115
        {
mysterywolf's avatar
mysterywolf 已提交
116
            set_timestamp(*(time_t *)args);
N
nxp58695 已提交
117 118
        }
        break;
mysterywolf's avatar
mysterywolf 已提交
119

N
nxp58695 已提交
120
        default:
mysterywolf's avatar
mysterywolf 已提交
121
            return RT_EINVAL;
N
nxp58695 已提交
122
    }
mysterywolf's avatar
mysterywolf 已提交
123 124

    return RT_EOK;
N
nxp58695 已提交
125 126
}

mysterywolf's avatar
mysterywolf 已提交
127
static struct rt_device device =
N
nxp58695 已提交
128
{
mysterywolf's avatar
mysterywolf 已提交
129 130 131 132
    .type    = RT_Device_Class_RTC,
    .init    = lpc_rtc_init,
    .open    = lpc_rtc_open,
    .close   = lpc_rtc_close,
N
nxp58695 已提交
133 134
    .read    = lpc_rtc_read,
    .write   = lpc_rtc_write,
mysterywolf's avatar
mysterywolf 已提交
135
    .control = lpc_rtc_control,
N
nxp58695 已提交
136 137 138 139 140
};

int rt_hw_rtc_init(void)
{
    rt_err_t ret = RT_EOK;
mysterywolf's avatar
mysterywolf 已提交
141 142

    ret = rt_device_register(&device, "rtc", RT_DEVICE_FLAG_RDWR);
N
nxp58695 已提交
143 144
    if(ret != RT_EOK)
    {
mysterywolf's avatar
mysterywolf 已提交
145
        return ret;
N
nxp58695 已提交
146
    }
mysterywolf's avatar
mysterywolf 已提交
147 148 149 150

    rt_device_open(&device, RT_DEVICE_OFLAG_RDWR);

    return RT_EOK;
N
nxp58695 已提交
151
}
mysterywolf's avatar
mysterywolf 已提交
152
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
N
nxp58695 已提交
153 154

#endif /*RT_USING_RTC */