Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 12 additions & 44 deletions bsp/efm32/drv_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,49 +128,6 @@ void rt_hw_rtc_isr(rt_device_t device)
RTC->IFC = _RTC_IFC_MASK;
}

/***************************************************************************//**
* @brief
* Register RTC device
*
* @details
*
* @note
*
* @param[in] device
* Pointer to device descriptor
*
* @param[in] name
* Device name
*
* @param[in] flag
* Configuration flags
*
* @return
* Error code
******************************************************************************/
rt_err_t rt_hw_rtc_register(
rt_device_t device,
const char *name,
rt_uint32_t flag)
{
RT_ASSERT(device != RT_NULL);

device->type = RT_Device_Class_RTC;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
device->init = RT_NULL;
device->open = rt_rtc_open;
device->close = RT_NULL;
device->read = rt_rtc_read;
device->write = RT_NULL;
device->control = rt_rtc_control;
device->user_data = RT_NULL; /* no private */

/* register a character device */
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
}


/***************************************************************************//**
* @brief
* Initialize all RTC module related hardware and register RTC device to kernel
Expand Down Expand Up @@ -224,7 +181,18 @@ void rt_hw_rtc_init(void)
}

/* register rtc device */
rt_hw_rtc_register(&rtc, RT_RTC_NAME, EFM32_NO_DATA);
rtc.type = RT_Device_Class_RTC;
rtc.rx_indicate = RT_NULL;
rtc.tx_complete = RT_NULL;
rtc.init = RT_NULL;
rtc.open = rt_rtc_open;
rtc.close = RT_NULL;
rtc.read = rt_rtc_read;
rtc.write = RT_NULL;
rtc.control = rt_rtc_control;
rtc.user_data = RT_NULL; /* no private */

rt_device_register(&rtc, RT_RTC_NAME, RT_DEVICE_FLAG_RDWR | EFM32_NO_DATA);
}

#endif
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/drv_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ static rt_err_t stm32_rtc_set_secs(void *args)
static const struct rt_rtc_ops stm32_rtc_ops =
{
stm32_rtc_init,
stm32_rtc_get_secs, /* get_secs */
stm32_rtc_set_secs, /* set secs */
stm32_rtc_get_secs,
stm32_rtc_set_secs,
RT_NULL,
RT_NULL,
RT_NULL,
Expand All @@ -280,7 +280,7 @@ static int rt_hw_rtc_init(void)
rt_err_t result;

stm32_rtc_dev.ops = &stm32_rtc_ops;
result = rt_rtc_dev_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
result = rt_hw_rtc_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
if (result != RT_EOK)
{
LOG_E("rtc register err code: %d", result);
Expand Down
34 changes: 32 additions & 2 deletions components/drivers/include/drivers/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,43 @@
* Change Logs:
* Date Author Notes
* 2012-10-10 aozima first version.
* 2021-06-11 iysheng implement RTC framework V2.0
* 2021-07-30 Meco Man move rtc_core.h to rtc.h
*/

#ifndef __RTC_H__
#define __RTC_H__

#include <rtconfig.h>
#include <drivers/rtc_core.h>
#include <rtdef.h>

#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */
#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */
#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */
#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */

struct rt_rtc_ops
{
rt_err_t (*init)(void);
rt_err_t (*get_secs)(void *arg);
rt_err_t (*set_secs)(void *arg);
rt_err_t (*get_alarm)(void *arg);
rt_err_t (*set_alarm)(void *arg);
rt_err_t (*get_usecs)(void *arg);
rt_err_t (*set_usecs)(void *arg);
};

typedef struct rt_rtc_device
{
struct rt_device parent;
const struct rt_rtc_ops *ops;
} rt_rtc_dev_t;

rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
const char *name,
rt_uint32_t flag,
void *data);

rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
Expand Down
45 changes: 0 additions & 45 deletions components/drivers/include/drivers/rtc_core.h

This file was deleted.

2 changes: 1 addition & 1 deletion components/drivers/rtc/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CPPPATH = [cwd + '/../include']
group = []

if GetDepend(['RT_USING_RTC']):
src = src + ['rtc.c', 'rtc_core.c']
src = src + ['rtc.c']
if GetDepend(['RT_USING_ALARM']):
src = src + ['alarm.c']
if GetDepend(['RT_USING_SOFT_RTC']):
Expand Down
132 changes: 114 additions & 18 deletions components/drivers/rtc/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* 2012-04-16 aozima add scheduler lock for set_date and set_time.
* 2018-02-16 armink add auto sync time by NTP
* 2021-05-09 Meco Man remove NTP
* 2021-06-11 iysheng implement RTC framework V2.0
* 2021-07-30 Meco Man move rtc_core.c to rtc.c
*/

#include <time.h>
Expand All @@ -20,7 +22,117 @@

#ifdef RT_USING_RTC

/*
* This function initializes rtc_core
*/
static rt_err_t rt_rtc_init(struct rt_device *dev)
{
rt_rtc_dev_t *rtc_core;

RT_ASSERT(dev != RT_NULL);
rtc_core = (rt_rtc_dev_t *)dev;
if (rtc_core->ops->init)
{
return (rtc_core->ops->init());
}

return -RT_ENOSYS;
}

static rt_err_t rt_rtc_open(struct rt_device *dev, rt_uint16_t oflag)
{
return RT_EOK;
}

static rt_err_t rt_rtc_close(struct rt_device *dev)
{
/* Add close member function in rt_rtc_ops when need,
* then call that function here.
* */
return RT_EOK;
}

static rt_err_t rt_rtc_control(struct rt_device *dev, int cmd, void *args)
{
#define TRY_DO_RTC_FUNC(rt_rtc_dev, func_name, args) \
rt_rtc_dev->ops->func_name ? rt_rtc_dev->ops->func_name(args) : -RT_EINVAL;

rt_rtc_dev_t *rtc_device;
rt_err_t ret = -RT_EINVAL;

RT_ASSERT(dev != RT_NULL);
rtc_device = (rt_rtc_dev_t *)dev;

switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
ret = TRY_DO_RTC_FUNC(rtc_device, get_secs, args);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
ret = TRY_DO_RTC_FUNC(rtc_device, set_secs, args);
break;
case RT_DEVICE_CTRL_RTC_GET_TIME_US:
ret = TRY_DO_RTC_FUNC(rtc_device, get_usecs, args);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME_US:
ret = TRY_DO_RTC_FUNC(rtc_device, set_usecs, args);
break;
case RT_DEVICE_CTRL_RTC_GET_ALARM:
ret = TRY_DO_RTC_FUNC(rtc_device, get_alarm, args);
break;
case RT_DEVICE_CTRL_RTC_SET_ALARM:
ret = TRY_DO_RTC_FUNC(rtc_device, set_alarm, args);
break;
default:
break;
}

return ret;

#undef TRY_DO_RTC_FUNC
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops rtc_core_ops =
{
rt_rtc_init,
rt_rtc_open,
rt_rtc_close,
RT_NULL,
RT_NULL,
rt_rtc_control,
};
#endif /* RT_USING_DEVICE_OPS */

rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
const char *name,
rt_uint32_t flag,
void *data)
{
struct rt_device *device;
RT_ASSERT(rtc != RT_NULL);

device = &(rtc->parent);

device->type = RT_Device_Class_RTC;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;

#ifdef RT_USING_DEVICE_OPS
device->ops = &rtc_core_ops;
#else
device->init = rt_rtc_init;
device->open = rt_rtc_open;
device->close = rt_rtc_close;
device->read = RT_NULL;
device->write = RT_NULL;
device->control = rt_rtc_control;
#endif /* RT_USING_DEVICE_OPS */
device->user_data = data;

/* register a character device */
return rt_device_register(device, name, flag);
}

/**
* Set system date(time not modify, local timezone).
Expand All @@ -30,27 +142,19 @@
* @param rt_uint32_t day e.g: 31.
*
* @return rt_err_t if set success, return RT_EOK.
*
*/
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
{
time_t now;
struct tm *p_tm;
struct tm tm_new;
rt_device_t device;
rt_err_t ret = -RT_ERROR;

/* get current time */
now = time(RT_NULL);

/* lock scheduler. */
rt_enter_critical();
/* converts calendar time into local time. */
p_tm = localtime(&now);
/* copy the statically located variable */
rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
/* unlock scheduler. */
rt_exit_critical();
localtime_r(&now, &tm_new);

/* update date. */
tm_new.tm_year = year - 1900;
Expand Down Expand Up @@ -80,27 +184,19 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
* @param rt_uint32_t second e.g: 0~59.
*
* @return rt_err_t if set success, return RT_EOK.
*
*/
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
{
time_t now;
struct tm *p_tm;
struct tm tm_new;
rt_device_t device;
rt_err_t ret = -RT_ERROR;

/* get current time */
now = time(RT_NULL);

/* lock scheduler. */
rt_enter_critical();
/* converts calendar time into local time. */
p_tm = localtime(&now);
/* copy the statically located variable */
rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
/* unlock scheduler. */
rt_exit_critical();
localtime_r(&now, &tm_new);

/* update time. */
tm_new.tm_hour = hour;
Expand Down
Loading