hpm_rtc_drv.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2021-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_RTC_DRV_H
  8. #define HPM_RTC_DRV_H
  9. /**
  10. * @brief RTC driver APIs
  11. * @defgroup rtc_interface RTC driver APIs
  12. * @ingroup io_interfaces
  13. * @{
  14. *
  15. */
  16. #include "hpm_common.h"
  17. #include "hpm_rtc_regs.h"
  18. #ifndef __ICCRISCV__
  19. #include <sys/time.h>
  20. #endif
  21. #include <time.h>
  22. /**
  23. * @brief RTC alarm configuration
  24. */
  25. typedef struct {
  26. uint16_t index; /**< RTC alarm index */
  27. uint16_t type; /**< Alarm type */
  28. time_t period; /**< Alarm period */
  29. } rtc_alarm_config_t;
  30. /**
  31. * @brief RTC Alarm type
  32. */
  33. #define RTC_ALARM_TYPE_ONE_SHOT (0U) /**< The RTC alarm will be triggered only once */
  34. #define RTC_ALARM_TYPE_PERIODIC (1U) /**< The RTC alarm will be triggered periodically */
  35. #define RTC_ALARM_TYPE_ABSOLUTE_TIME_ONE_SHOT (2U) /**< The RTC alarm will be triggered via the absolute time provided via period */
  36. /**
  37. * @brief Typical RTC alarm period definitions
  38. */
  39. #define ALARM_PERIOD_ONE_SEC (1UL) /**< Alarm period: 1 second */
  40. #define ALARM_PERIOD_ONE_MIN (60UL) /**< Alarm period: 1 minute */
  41. #define ALARM_PERIOD_ONE_HOUR (3600U) /**< Alarm period: 1 hour */
  42. #define ALARM_PERIOD_ONE_DAY (ALARM_PERIOD_ONE_HOUR * 24UL) /**< Alarm period: 1 day */
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @brief Configure the RTC time
  48. * @param [in] base RTC base address
  49. * @param [in] time seconds since 1970.1.1, 0:0:0
  50. * @retval API execution status status_success or status_invalid_argument
  51. */
  52. hpm_stat_t rtc_config_time(RTC_Type *base, time_t time);
  53. /**
  54. * @brief Configure RTC Alarm
  55. * @param [in] base RTC base address
  56. * @param [in] config RTC alarm configuration pointer
  57. * @retval API execution status status_success or status_invalid_argument;
  58. */
  59. hpm_stat_t rtc_config_alarm(RTC_Type *base, rtc_alarm_config_t *config);
  60. /**
  61. * @brief Get the time returned by RTC module
  62. * @param [in] base RTC base address
  63. * @retval RTC time
  64. */
  65. time_t rtc_get_time(RTC_Type *base);
  66. /**
  67. * @brief Get accurate time return by RTC module
  68. * @param [in] base RTC base address
  69. *
  70. * @return accurate time(including second and subsecond)
  71. */
  72. struct timeval rtc_get_timeval(RTC_Type *base);
  73. /**
  74. * @brief Enable RTC alarm interrupt
  75. * @param [in] base RTC base address
  76. * @param [in] index RTC alarm index, valid value is 0 or 1
  77. * @param [in] enable RTC alarm enable flag
  78. * @arg true Enable specified RTC alarm
  79. * @arg false Disable specified RTC alarm
  80. */
  81. static inline void rtc_enable_alarm_interrupt(RTC_Type *base, uint32_t index, bool enable)
  82. {
  83. if (index > 1) {
  84. return;
  85. }
  86. uint32_t mask = (index == 0U) ? RTC_ALARM_EN_ENABLE0_MASK : RTC_ALARM_EN_ENABLE1_MASK;
  87. if (enable) {
  88. base->ALARM_EN |= mask;
  89. } else {
  90. base->ALARM_EN &= ~mask;
  91. }
  92. }
  93. /**
  94. * @brief Clear RTC alarm flag based on alarm index
  95. * @param [in] base RTC base address
  96. * @param [in] index RTC alarm index, valid value is 0 or 1
  97. */
  98. static inline void rtc_clear_alarm_flag(RTC_Type *base, uint32_t index)
  99. {
  100. if (index > 1) {
  101. return;
  102. }
  103. uint32_t mask = (index == 0U) ? RTC_ALARM_FLAG_ALARM0_MASK : RTC_ALARM_FLAG_ALARM1_MASK;
  104. base->ALARM_FLAG = mask;
  105. }
  106. /**
  107. * @brief Clear RTC alarm flags based on flag masks
  108. * @param [in] base RTC base address
  109. * @param [in] masks RTC alarm masks
  110. */
  111. static inline void rtc_clear_alarm_flags(RTC_Type *base, uint32_t masks)
  112. {
  113. base->ALARM_FLAG = masks;
  114. }
  115. /**
  116. * @brief Check whether RTC alarm flag is set or not
  117. * @param [in] base RTC base address
  118. * @param [in] index RTC alarm index, valid value is 0 or 1
  119. * @retval RTC alarm flag. Valid value is true or false
  120. */
  121. static inline bool rtc_is_alarm_flag_asserted(RTC_Type *base, uint32_t index)
  122. {
  123. if (index > 1) {
  124. return false;
  125. }
  126. uint32_t mask = (index == 0U) ? RTC_ALARM_FLAG_ALARM0_MASK : RTC_ALARM_FLAG_ALARM1_MASK;
  127. return IS_HPM_BITMASK_SET(base->ALARM_FLAG, mask);
  128. }
  129. /**
  130. * @brief Get the RTC alarm flags
  131. * @param [in] base RTC base address
  132. * @return RTC alarm flags
  133. */
  134. static inline uint32_t rtc_get_alarm_flags(RTC_Type *base)
  135. {
  136. return base->ALARM_FLAG;
  137. }
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. /**
  142. * @}
  143. *
  144. */
  145. #endif /* HPM_RTC_DRV_H */