rtc.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2006-2021, 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. /* used for alarm function */
  27. struct rt_rtc_wkalarm
  28. {
  29. rt_bool_t enable; /* 0 = alarm disabled, 1 = alarm enabled */
  30. rt_int32_t tm_sec; /* alarm at tm_sec */
  31. rt_int32_t tm_min; /* alarm at tm_min */
  32. rt_int32_t tm_hour; /* alarm at tm_hour */
  33. };
  34. struct rt_rtc_ops
  35. {
  36. rt_err_t (*init)(void);
  37. rt_err_t (*get_secs)(time_t *sec);
  38. rt_err_t (*set_secs)(time_t *sec);
  39. rt_err_t (*get_alarm)(struct rt_rtc_wkalarm *alarm);
  40. rt_err_t (*set_alarm)(struct rt_rtc_wkalarm *alarm);
  41. rt_err_t (*get_timeval)(struct timeval *tv);
  42. rt_err_t (*set_timeval)(struct timeval *tv);
  43. };
  44. typedef struct rt_rtc_device
  45. {
  46. struct rt_device parent;
  47. const struct rt_rtc_ops *ops;
  48. } rt_rtc_dev_t;
  49. rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
  50. const char *name,
  51. rt_uint32_t flag,
  52. void *data);
  53. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
  54. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
  55. rt_err_t set_timestamp(time_t timestamp);
  56. rt_err_t get_timestamp(time_t *timestamp);
  57. #ifdef __cplusplus
  58. }
  59. #endif
  60. #endif /* __RTC_H__ */