2019-10-24 17:56:09 +08:00
|
|
|
/*
|
2021-03-17 02:26:35 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2019-10-24 17:56:09 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2021-02-07 21:08:41 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <sys/time.h>
|
2021-03-17 02:26:35 +08:00
|
|
|
#include "drv_rtc.h"
|
|
|
|
#include "fsl_common.h"
|
2019-10-24 17:56:09 +08:00
|
|
|
#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
|
|
|
|
|
2021-03-17 02:26:35 +08:00
|
|
|
static time_t get_timestamp(void)
|
2019-10-24 17:56:09 +08:00
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
struct tm tm_new = {0};
|
|
|
|
rtc_datetime_t rtcDate;
|
|
|
|
|
2019-10-24 17:56:09 +08:00
|
|
|
/* Get date time */
|
|
|
|
RTC_GetDatetime(RTC, &rtcDate);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
|
|
|
tm_new.tm_sec = rtcDate.second;
|
|
|
|
tm_new.tm_min = rtcDate.minute;
|
2019-10-24 17:56:09 +08:00
|
|
|
tm_new.tm_hour = rtcDate.hour;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
|
|
|
tm_new.tm_mday = rtcDate.day;
|
|
|
|
tm_new.tm_mon = rtcDate.month - 1;
|
|
|
|
tm_new.tm_year = rtcDate.year - 1900;
|
2019-10-24 17:56:09 +08:00
|
|
|
|
2021-02-07 21:08:41 +08:00
|
|
|
return timegm(&tm_new);
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int set_timestamp(time_t timestamp)
|
|
|
|
{
|
2022-06-22 13:41:06 +08:00
|
|
|
struct tm now;
|
2021-03-17 02:26:35 +08:00
|
|
|
rtc_datetime_t rtcDate;
|
|
|
|
|
2022-06-22 13:41:06 +08:00
|
|
|
gmtime_r(×tamp, &now);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2022-06-22 13:41:06 +08:00
|
|
|
rtcDate.second = now.tm_sec ;
|
|
|
|
rtcDate.minute = now.tm_min ;
|
|
|
|
rtcDate.hour = now.tm_hour;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2022-06-22 13:41:06 +08:00
|
|
|
rtcDate.day = now.tm_mday;
|
|
|
|
rtcDate.month = now.tm_mon + 1;
|
|
|
|
rtcDate.year = now.tm_year + 1900;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2019-10-24 17:56:09 +08:00
|
|
|
/* RTC time counter has to be stopped before setting the date & time in the TSR register */
|
|
|
|
RTC_StopTimer(RTC);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2019-10-24 17:56:09 +08:00
|
|
|
/* Set RTC time to default */
|
|
|
|
RTC_SetDatetime(RTC, &rtcDate);
|
|
|
|
|
|
|
|
/* Start the RTC time counter */
|
|
|
|
RTC_StartTimer(RTC);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2019-10-24 17:56:09 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t lpc_rtc_init(rt_device_t dev)
|
|
|
|
{
|
|
|
|
/* Init RTC */
|
|
|
|
RTC_Init(RTC);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2019-10-24 17:56:09 +08:00
|
|
|
/* Start the RTC time counter */
|
|
|
|
RTC_StartTimer(RTC);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t lpc_rtc_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
return RT_EOK;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
|
2021-03-17 02:26:35 +08:00
|
|
|
static rt_err_t lpc_rtc_close(rt_device_t dev)
|
2019-10-24 17:56:09 +08:00
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
2019-10-24 17:56:09 +08:00
|
|
|
|
|
|
|
static rt_size_t lpc_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
|
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
return 0;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_size_t lpc_rtc_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
|
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
return 0;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t lpc_rtc_control(rt_device_t dev, int cmd, void *args)
|
|
|
|
{
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2019-10-24 17:56:09 +08:00
|
|
|
switch(cmd)
|
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
case RT_DEVICE_CTRL_RTC_GET_TIME:
|
2019-10-24 17:56:09 +08:00
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
*(uint32_t *)args = get_timestamp();
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
break;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_RTC_SET_TIME:
|
2019-10-24 17:56:09 +08:00
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
set_timestamp(*(time_t *)args);
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
break;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
2019-10-24 17:56:09 +08:00
|
|
|
default:
|
2021-03-17 02:26:35 +08:00
|
|
|
return RT_EINVAL;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
2021-03-17 02:26:35 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
|
|
|
|
2021-03-17 02:26:35 +08:00
|
|
|
static struct rt_device device =
|
2019-10-24 17:56:09 +08:00
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
.type = RT_Device_Class_RTC,
|
|
|
|
.init = lpc_rtc_init,
|
|
|
|
.open = lpc_rtc_open,
|
|
|
|
.close = lpc_rtc_close,
|
2019-10-24 17:56:09 +08:00
|
|
|
.read = lpc_rtc_read,
|
|
|
|
.write = lpc_rtc_write,
|
2021-03-17 02:26:35 +08:00
|
|
|
.control = lpc_rtc_control,
|
2019-10-24 17:56:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int rt_hw_rtc_init(void)
|
|
|
|
{
|
|
|
|
rt_err_t ret = RT_EOK;
|
2021-03-17 02:26:35 +08:00
|
|
|
|
|
|
|
ret = rt_device_register(&device, "rtc", RT_DEVICE_FLAG_RDWR);
|
2019-10-24 17:56:09 +08:00
|
|
|
if(ret != RT_EOK)
|
|
|
|
{
|
2021-03-17 02:26:35 +08:00
|
|
|
return ret;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
2021-03-17 02:26:35 +08:00
|
|
|
|
|
|
|
rt_device_open(&device, RT_DEVICE_OFLAG_RDWR);
|
|
|
|
|
|
|
|
return RT_EOK;
|
2019-10-24 17:56:09 +08:00
|
|
|
}
|
2021-03-17 02:26:35 +08:00
|
|
|
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
|
2019-10-24 17:56:09 +08:00
|
|
|
|
|
|
|
#endif /*RT_USING_RTC */
|