hpm_rtc_drv.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2021-2023 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. #include <time.h>
  19. /**
  20. * @brief RTC alarm configuration
  21. */
  22. typedef struct {
  23. uint16_t index; /**< RTC alarm index */
  24. uint16_t type; /**< Alarm type */
  25. time_t period; /**< ALarm period */
  26. } rtc_alarm_config_t;
  27. /**
  28. * @brief RTC Alarm type
  29. */
  30. #define RTC_ALARM_TYPE_ONE_SHOT (0U) /**< The RTC alarm will be triggered only once */
  31. #define RTC_ALARM_TYPE_PERIODIC (1U) /**< The RTC alarm will be triggered periodically */
  32. #define RTC_ALARM_TYPE_ABSOLUTE_TIME_ONE_SHOT (2U) /**< The RTC alarm will be triggered via the absolute time provided via period */
  33. /**
  34. * @brief Typical RTC alarm period definitions
  35. */
  36. #define ALARM_PERIOD_ONE_SEC (1UL) /**< Alarm period: 1 second */
  37. #define ALARM_PERIOD_ONE_MIN (60UL) /**< Alarm period: 1 minute */
  38. #define ALARM_PERIOD_ONE_HOUR (3600U) /**< Alarm period: 1 hour */
  39. #define ALARM_PERIOD_ONE_DAY (ALARM_PERIOD_ONE_HOUR * 24UL) /**< Alarm period: 1 day */
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * @brief Configure the RTC time
  45. * @param [in] base RTC base address
  46. * @param [in] time seconds since 1970.1.1, 0:0:0
  47. * @retval API execution status status_success or status_invalid_argument
  48. */
  49. hpm_stat_t rtc_config_time(RTC_Type *base, time_t time);
  50. /**
  51. * @brief Configure RTC Alarm
  52. * @param [in] base RTC base address
  53. * @param [in] config RTC alarm configuration pointer
  54. * @retval API execution status status_success or status_invalid_arugment;
  55. */
  56. hpm_stat_t rtc_config_alarm(RTC_Type *base, rtc_alarm_config_t *config);
  57. /**
  58. * @brief Get the time returned by RTC module
  59. * @param [in] base RTC base address
  60. * @retval RTC time
  61. */
  62. time_t rtc_get_time(RTC_Type *base);
  63. /**
  64. * @brief Enable RTC alarm interrupt
  65. * @param [in] base RTC base address
  66. * @param [in] index RTC alarm index, valid value is 0 or 1
  67. * @param [in] enable RTC alarm enable flag
  68. * @arg true Enable specified RTC alarm
  69. * @arg false Disable specified RTC alarm
  70. */
  71. static inline void rtc_enable_alarm_interrupt(RTC_Type *base, uint32_t index, bool enable)
  72. {
  73. if (index > 1) {
  74. return;
  75. }
  76. uint32_t mask = (index == 0U) ? RTC_ALARM_EN_ENABLE0_MASK : RTC_ALARM_EN_ENABLE1_MASK;
  77. if (enable) {
  78. base->ALARM_EN |= mask;
  79. } else {
  80. base->ALARM_EN &= ~mask;
  81. }
  82. }
  83. /**
  84. * @brief Clear RTC alarm flag
  85. * @param [in] base RTC base address
  86. * @param [in] index RTC alarm index, valid value is 0 or 1
  87. */
  88. static inline void rtc_clear_alarm_flag(RTC_Type *base, uint32_t index)
  89. {
  90. if (index > 1) {
  91. return;
  92. }
  93. uint32_t mask = (index == 0U) ? RTC_ALARM_FLAG_ALARM0_MASK : RTC_ALARM_FLAG_ALARM1_MASK;
  94. base->ALARM_FLAG = mask;
  95. }
  96. /**
  97. * @brief Check whether RTC alarm flag is set or not
  98. * @param [in] base RTC base address
  99. * @param [in] index RTC alarm index, valid value is 0 or 1
  100. * @retval RTC alarm flag. Valid value is true or false
  101. */
  102. static inline bool rtc_is_alarm_flag_asserted(RTC_Type *base, uint32_t index)
  103. {
  104. if (index > 1) {
  105. return false;
  106. }
  107. uint32_t mask = (index == 0U) ? RTC_ALARM_FLAG_ALARM0_MASK : RTC_ALARM_FLAG_ALARM1_MASK;
  108. return IS_HPM_BITMASK_SET(base->ALARM_FLAG, mask);
  109. }
  110. #ifdef __cplusplus
  111. }
  112. #endif
  113. /**
  114. * @}
  115. *
  116. */
  117. #endif /* HPM_RTC_DRV_H */