dev_rtc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 __DEV_RTC_H__
  14. #define __DEV_RTC_H__
  15. #include <rtdef.h>
  16. #include <sys/time.h>
  17. /**
  18. * @defgroup group_RTC RTC
  19. * @brief RTC driver api
  20. * @ingroup group_device_driver
  21. *
  22. * <b>Example</b>
  23. * @code {.c}
  24. *
  25. * #include <rtthread.h>
  26. * #include <rtdevice.h>
  27. *
  28. * #define RTC_NAME "rtc"
  29. *
  30. * static int rtc_sample(int argc, char *argv[])
  31. * {
  32. * rt_err_t ret = RT_EOK;
  33. * time_t now;
  34. * rt_device_t device = RT_NULL;
  35. *
  36. * device = rt_device_find(RTC_NAME);
  37. * if (!device)
  38. * {
  39. * LOG_E("find %s failed!", RTC_NAME);
  40. * return -RT_ERROR;
  41. * }
  42. *
  43. * if(rt_device_open(device, 0) != RT_EOK)
  44. * {
  45. * LOG_E("open %s failed!", RTC_NAME);
  46. * return -RT_ERROR;
  47. * }
  48. *
  49. * ret = set_date(2018, 12, 3);
  50. * if (ret != RT_EOK)
  51. * {
  52. * rt_kprintf("set RTC date failed\n");
  53. * return ret;
  54. * }
  55. *
  56. * ret = set_time(11, 15, 50);
  57. * if (ret != RT_EOK)
  58. * {
  59. * rt_kprintf("set RTC time failed\n");
  60. * return ret;
  61. * }
  62. *
  63. * rt_thread_mdelay(3000);
  64. *
  65. * now = time(RT_NULL);
  66. * rt_kprintf("%s\n", ctime(&now));
  67. *
  68. * return ret;
  69. * }
  70. * MSH_CMD_EXPORT(rtc_sample, rtc sample);
  71. * @endcode
  72. */
  73. /*!
  74. * @addtogroup group_RTC
  75. * @{
  76. */
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. #define RT_DEVICE_CTRL_RTC_GET_TIME (RT_DEVICE_CTRL_BASE(RTC) + 0x01) /**< get second time */
  81. #define RT_DEVICE_CTRL_RTC_SET_TIME (RT_DEVICE_CTRL_BASE(RTC) + 0x02) /**< set second time */
  82. #define RT_DEVICE_CTRL_RTC_GET_TIMEVAL (RT_DEVICE_CTRL_BASE(RTC) + 0x03) /**< get timeval for gettimeofday */
  83. #define RT_DEVICE_CTRL_RTC_SET_TIMEVAL (RT_DEVICE_CTRL_BASE(RTC) + 0x04) /**< set timeval for gettimeofday */
  84. #define RT_DEVICE_CTRL_RTC_GET_ALARM (RT_DEVICE_CTRL_BASE(RTC) + 0x05) /**< get alarm */
  85. #define RT_DEVICE_CTRL_RTC_SET_ALARM (RT_DEVICE_CTRL_BASE(RTC) + 0x06) /**< set alarm */
  86. #define RT_DEVICE_CTRL_RTC_GET_TIMESPEC (RT_DEVICE_CTRL_BASE(RTC) + 0x07) /**< get timespec for clock_gettime */
  87. #define RT_DEVICE_CTRL_RTC_SET_TIMESPEC (RT_DEVICE_CTRL_BASE(RTC) + 0x08) /**< set timespec for clock_settime */
  88. #define RT_DEVICE_CTRL_RTC_GET_TIMERES (RT_DEVICE_CTRL_BASE(RTC) + 0x09) /**< get resolution for clock_getres */
  89. /**
  90. * @brief RTC alarm structure
  91. */
  92. struct rt_rtc_wkalarm
  93. {
  94. rt_bool_t enable; /* 0 = alarm disabled, 1 = alarm enabled */
  95. rt_int32_t tm_sec; /* alarm at tm_sec */
  96. rt_int32_t tm_min; /* alarm at tm_min */
  97. rt_int32_t tm_hour; /* alarm at tm_hour */
  98. rt_int32_t tm_mday; /* alarm at tm_mday */
  99. rt_int32_t tm_mon; /* alarm at tm_mon */
  100. rt_int32_t tm_year; /* alarm at tm_year */
  101. };
  102. /**
  103. * @brief RTC operations
  104. */
  105. struct rt_rtc_ops
  106. {
  107. rt_err_t (*init)(void);
  108. rt_err_t (*get_secs)(time_t *sec);
  109. rt_err_t (*set_secs)(time_t *sec);
  110. rt_err_t (*get_alarm)(struct rt_rtc_wkalarm *alarm);
  111. rt_err_t (*set_alarm)(struct rt_rtc_wkalarm *alarm);
  112. rt_err_t (*get_timeval)(struct timeval *tv);
  113. rt_err_t (*set_timeval)(struct timeval *tv);
  114. };
  115. /**
  116. * @brief RTC device structure
  117. */
  118. typedef struct rt_rtc_device
  119. {
  120. struct rt_device parent;
  121. const struct rt_rtc_ops *ops;
  122. } rt_rtc_dev_t;
  123. /**
  124. * @brief Register a RTC device
  125. *
  126. * @param rtc RTC device
  127. * @param name RTC device name
  128. * @param flag RTC device flag
  129. * @param data RTC device data
  130. * @return rt_err_t error code
  131. */
  132. rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
  133. const char *name,
  134. rt_uint32_t flag,
  135. void *data);
  136. /**
  137. * @brief set date
  138. *
  139. * @param year year
  140. * @param month month
  141. * @param day day
  142. * @return rt_err_t error code
  143. */
  144. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
  145. /**
  146. * @brief set time
  147. *
  148. * @param hour hour
  149. * @param minute minute
  150. * @param second second
  151. * @return rt_err_t error code
  152. */
  153. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
  154. /**
  155. * @brief set timestamp
  156. *
  157. * @param timestamp A pointer to time
  158. * @return rt_err_t error code
  159. */
  160. rt_err_t set_timestamp(time_t timestamp);
  161. /**
  162. * @brief get timestamp
  163. *
  164. * @param timestamp A secondary pointer to time
  165. * @return rt_err_t error code
  166. */
  167. rt_err_t get_timestamp(time_t *timestamp);
  168. #ifdef RT_USING_SYSTEM_WORKQUEUE
  169. rt_err_t rt_soft_rtc_sync(void);
  170. rt_err_t rt_soft_rtc_set_source(const char *name);
  171. #endif
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. /*! @}*/
  176. #endif /* __DEV_RTC_H__ */