dev_rtc.h 5.0 KB

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