[rtc] add comments

This commit is contained in:
Meco Man 2021-07-30 03:55:18 +08:00
parent 94a9332915
commit a25667a7e5
3 changed files with 39 additions and 36 deletions

View File

@ -265,8 +265,8 @@ static rt_err_t stm32_rtc_set_secs(void *args)
static const struct rt_rtc_ops stm32_rtc_ops = static const struct rt_rtc_ops stm32_rtc_ops =
{ {
stm32_rtc_init, stm32_rtc_init,
stm32_rtc_get_secs, /* get_secs */ stm32_rtc_get_secs,
stm32_rtc_set_secs, /* set secs */ stm32_rtc_set_secs,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,

View File

@ -6,7 +6,8 @@
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2012-10-10 aozima first version. * 2012-10-10 aozima first version.
* 2021-06-11 iysheng implement RTC v2.0 * 2021-06-11 iysheng implement RTC framework V2.0
* 2021-07-30 Meco Man move rtc_core.h to rtc.h
*/ */
#ifndef __RTC_H__ #ifndef __RTC_H__
@ -38,7 +39,7 @@ typedef struct rt_rtc_device
const struct rt_rtc_ops *ops; const struct rt_rtc_ops *ops;
} rt_rtc_dev_t; } rt_rtc_dev_t;
rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc, rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
const char *name, const char *name,
rt_uint32_t flag, rt_uint32_t flag,
void *data); void *data);

View File

@ -10,7 +10,8 @@
* 2012-04-16 aozima add scheduler lock for set_date and set_time. * 2012-04-16 aozima add scheduler lock for set_date and set_time.
* 2018-02-16 armink add auto sync time by NTP * 2018-02-16 armink add auto sync time by NTP
* 2021-05-09 Meco Man remove NTP * 2021-05-09 Meco Man remove NTP
* 2021-06-11 iysheng implement RTC v2.0 * 2021-06-11 iysheng implement RTC framework V2.0
* 2021-07-30 Meco Man move rtc_core.c to rtc.c
*/ */
#include <time.h> #include <time.h>
@ -21,13 +22,10 @@
#ifdef RT_USING_RTC #ifdef RT_USING_RTC
#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;
/* /*
* This function initializes rtc_core * This function initializes rtc_core
*/ */
static rt_err_t rt_rtc_core_init(struct rt_device *dev) static rt_err_t rt_rtc_init(struct rt_device *dev)
{ {
rt_rtc_dev_t *rtc_core; rt_rtc_dev_t *rtc_core;
@ -38,71 +36,75 @@ static rt_err_t rt_rtc_core_init(struct rt_device *dev)
return (rtc_core->ops->init()); return (rtc_core->ops->init());
} }
return (-RT_ENOSYS); return -RT_ENOSYS;
} }
static rt_err_t rt_rtc_core_open(struct rt_device *dev, rt_uint16_t oflag) static rt_err_t rt_rtc_open(struct rt_device *dev, rt_uint16_t oflag)
{ {
return (RT_EOK); return RT_EOK;
} }
static rt_err_t rt_rtc_core_close(struct rt_device *dev) static rt_err_t rt_rtc_close(struct rt_device *dev)
{ {
/* Add close member function in rt_rtc_ops when need, /* Add close member function in rt_rtc_ops when need,
* then call that function here. * then call that function here.
* */ * */
return (RT_EOK); return RT_EOK;
} }
static rt_err_t rt_rtc_core_control(struct rt_device *dev, static rt_err_t rt_rtc_control(struct rt_device *dev, int cmd, void *args)
int cmd,
void *args)
{ {
rt_rtc_dev_t *rtc_core; #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_err_t ret = -RT_EINVAL;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
rtc_core = (rt_rtc_dev_t *)dev; rtc_device = (rt_rtc_dev_t *)dev;
switch (cmd) switch (cmd)
{ {
case RT_DEVICE_CTRL_RTC_GET_TIME: case RT_DEVICE_CTRL_RTC_GET_TIME:
ret = TRY_DO_RTC_FUNC(rtc_core, get_secs, args); ret = TRY_DO_RTC_FUNC(rtc_device, get_secs, args);
break; break;
case RT_DEVICE_CTRL_RTC_SET_TIME: case RT_DEVICE_CTRL_RTC_SET_TIME:
ret = TRY_DO_RTC_FUNC(rtc_core, set_secs, args); ret = TRY_DO_RTC_FUNC(rtc_device, set_secs, args);
break; break;
case RT_DEVICE_CTRL_RTC_GET_TIME_US: case RT_DEVICE_CTRL_RTC_GET_TIME_US:
ret = TRY_DO_RTC_FUNC(rtc_core, get_usecs, args); ret = TRY_DO_RTC_FUNC(rtc_device, get_usecs, args);
break; break;
case RT_DEVICE_CTRL_RTC_SET_TIME_US: case RT_DEVICE_CTRL_RTC_SET_TIME_US:
ret = TRY_DO_RTC_FUNC(rtc_core, set_usecs, args); ret = TRY_DO_RTC_FUNC(rtc_device, set_usecs, args);
break; break;
case RT_DEVICE_CTRL_RTC_GET_ALARM: case RT_DEVICE_CTRL_RTC_GET_ALARM:
ret = TRY_DO_RTC_FUNC(rtc_core, get_alarm, args); ret = TRY_DO_RTC_FUNC(rtc_device, get_alarm, args);
break; break;
case RT_DEVICE_CTRL_RTC_SET_ALARM: case RT_DEVICE_CTRL_RTC_SET_ALARM:
ret = TRY_DO_RTC_FUNC(rtc_core, set_alarm, args); ret = TRY_DO_RTC_FUNC(rtc_device, set_alarm, args);
break; break;
default: default:
break; break;
} }
return ret; return ret;
#undef TRY_DO_RTC_FUNC
} }
#ifdef RT_USING_DEVICE_OPS #ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops rtc_core_ops = const static struct rt_device_ops rtc_core_ops =
{ {
rt_rtc_core_init, rt_rtc_init,
rt_rtc_core_open, rt_rtc_open,
rt_rtc_core_close, rt_rtc_close,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
rt_rtc_core_control, rt_rtc_control,
}; };
#endif #endif /* RT_USING_DEVICE_OPS */
rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc, rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
const char *name, const char *name,
rt_uint32_t flag, rt_uint32_t flag,
void *data) void *data)
@ -119,13 +121,13 @@ rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
#ifdef RT_USING_DEVICE_OPS #ifdef RT_USING_DEVICE_OPS
device->ops = &rtc_core_ops; device->ops = &rtc_core_ops;
#else #else
device->init = rt_rtc_core_init; device->init = rt_rtc_init;
device->open = rt_rtc_core_open; device->open = rt_rtc_open;
device->close = rt_rtc_core_close; device->close = rt_rtc_close;
device->read = RT_NULL; device->read = RT_NULL;
device->write = RT_NULL; device->write = RT_NULL;
device->control = rt_rtc_core_control; device->control = rt_rtc_control;
#endif #endif /* RT_USING_DEVICE_OPS */
device->user_data = data; device->user_data = data;
/* register a character device */ /* register a character device */