rtc.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-10-10 aozima first version.
  9. * 2021-06-11 iysheng implement RTC framework V2.0
  10. * 2021-07-30 Meco Man move rtc_core.h to rtc.h
  11. * 2022-04-05 tyx add timestamp function
  12. */
  13. #ifndef __RTC_H__
  14. #define __RTC_H__
  15. #include <rtdef.h>
  16. #include <sys/time.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #define RT_DEVICE_CTRL_RTC_GET_TIME (RT_DEVICE_CTRL_BASE(RTC) + 0x01) /**< get second time */
  21. #define RT_DEVICE_CTRL_RTC_SET_TIME (RT_DEVICE_CTRL_BASE(RTC) + 0x02) /**< set second time */
  22. #define RT_DEVICE_CTRL_RTC_GET_TIMEVAL (RT_DEVICE_CTRL_BASE(RTC) + 0x03) /**< get timeval for gettimeofday */
  23. #define RT_DEVICE_CTRL_RTC_SET_TIMEVAL (RT_DEVICE_CTRL_BASE(RTC) + 0x04) /**< set timeval for gettimeofday */
  24. #define RT_DEVICE_CTRL_RTC_GET_ALARM (RT_DEVICE_CTRL_BASE(RTC) + 0x05) /**< get alarm */
  25. #define RT_DEVICE_CTRL_RTC_SET_ALARM (RT_DEVICE_CTRL_BASE(RTC) + 0x06) /**< set alarm */
  26. #define RT_DEVICE_CTRL_RTC_GET_TIMESPEC (RT_DEVICE_CTRL_BASE(RTC) + 0x07) /**< get timespec for clock_gettime */
  27. #define RT_DEVICE_CTRL_RTC_SET_TIMESPEC (RT_DEVICE_CTRL_BASE(RTC) + 0x08) /**< set timespec for clock_settime */
  28. #define RT_DEVICE_CTRL_RTC_GET_TIMERES (RT_DEVICE_CTRL_BASE(RTC) + 0x09) /**< get resolution for clock_getres */
  29. /* used for alarm function */
  30. struct rt_rtc_wkalarm
  31. {
  32. rt_bool_t enable; /* 0 = alarm disabled, 1 = alarm enabled */
  33. rt_int32_t tm_sec; /* alarm at tm_sec */
  34. rt_int32_t tm_min; /* alarm at tm_min */
  35. rt_int32_t tm_hour; /* alarm at tm_hour */
  36. rt_int32_t tm_mday; /* alarm at tm_mday */
  37. rt_int32_t tm_mon; /* alarm at tm_mon */
  38. rt_int32_t tm_year; /* alarm at tm_year */
  39. };
  40. struct rt_rtc_ops
  41. {
  42. rt_err_t (*init)(void);
  43. rt_err_t (*get_secs)(time_t *sec);
  44. rt_err_t (*set_secs)(time_t *sec);
  45. rt_err_t (*get_alarm)(struct rt_rtc_wkalarm *alarm);
  46. rt_err_t (*set_alarm)(struct rt_rtc_wkalarm *alarm);
  47. rt_err_t (*get_timeval)(struct timeval *tv);
  48. rt_err_t (*set_timeval)(struct timeval *tv);
  49. };
  50. typedef struct rt_rtc_device
  51. {
  52. struct rt_device parent;
  53. const struct rt_rtc_ops *ops;
  54. } rt_rtc_dev_t;
  55. rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
  56. const char *name,
  57. rt_uint32_t flag,
  58. void *data);
  59. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
  60. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
  61. rt_err_t set_timestamp(time_t timestamp);
  62. rt_err_t get_timestamp(time_t *timestamp);
  63. #ifdef RT_USING_SYSTEM_WORKQUEUE
  64. rt_err_t rt_soft_rtc_sync();
  65. rt_err_t rt_soft_rtc_set_source(const char *name);
  66. #endif
  67. #ifdef __cplusplus
  68. }
  69. #endif
  70. #endif /* __RTC_H__ */