rtc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright 2019 SiFive, Inc. */
  2. /* SPDX-License-Identifier: Apache-2.0 */
  3. #ifndef METAL__RTC_H
  4. #define METAL__RTC_H
  5. #include <stdint.h>
  6. /*!
  7. * @file rtc.h
  8. * @brief API for Real-Time Clocks
  9. */
  10. struct metal_rtc;
  11. /*!
  12. * @brief List of RTC run behaviors
  13. */
  14. enum metal_rtc_run_option {
  15. METAL_RTC_STOP = 0,
  16. METAL_RTC_RUN,
  17. };
  18. struct metal_rtc_vtable {
  19. uint64_t (*get_rate)(const struct metal_rtc *const rtc);
  20. uint64_t (*set_rate)(const struct metal_rtc *const rtc, const uint64_t rate);
  21. uint64_t (*get_compare)(const struct metal_rtc *const rtc);
  22. uint64_t (*set_compare)(const struct metal_rtc *const rtc, const uint64_t compare);
  23. uint64_t (*get_count)(const struct metal_rtc *const rtc);
  24. uint64_t (*set_count)(const struct metal_rtc *const rtc, const uint64_t count);
  25. int (*run)(const struct metal_rtc *const rtc, const enum metal_rtc_run_option option);
  26. struct metal_interrupt *(*get_interrupt)(const struct metal_rtc *const rtc);
  27. int (*get_interrupt_id)(const struct metal_rtc *const rtc);
  28. };
  29. /*!
  30. * @brief Handle for a Real-Time Clock
  31. */
  32. struct metal_rtc {
  33. const struct metal_rtc_vtable *vtable;
  34. };
  35. /*!
  36. * @brief Get the rate of the RTC
  37. * @return The rate in Hz
  38. */
  39. inline uint64_t metal_rtc_get_rate(const struct metal_rtc *const rtc) {
  40. return rtc->vtable->get_rate(rtc);
  41. }
  42. /*!
  43. * @brief Set (if possible) the rate of the RTC
  44. * @return The new rate of the RTC (not guaranteed to be the same as requested)
  45. */
  46. inline uint64_t metal_rtc_set_rate(const struct metal_rtc *const rtc, const uint64_t rate) {
  47. return rtc->vtable->set_rate(rtc, rate);
  48. }
  49. /*!
  50. * @brief Get the compare value of the RTC
  51. * @return The compare value
  52. */
  53. inline uint64_t metal_rtc_get_compare(const struct metal_rtc *const rtc) {
  54. return rtc->vtable->get_compare(rtc);
  55. }
  56. /*!
  57. * @brief Set the compare value of the RTC
  58. * @return The set compare value (not guaranteed to be exactly the requested value)
  59. *
  60. * The RTC device might impose limits on the maximum compare value or the granularity
  61. * of the compare value.
  62. */
  63. inline uint64_t metal_rtc_set_compare(const struct metal_rtc *const rtc, const uint64_t compare) {
  64. return rtc->vtable->set_compare(rtc, compare);
  65. }
  66. /*!
  67. * @brief Get the current count of the RTC
  68. * @return The count
  69. */
  70. inline uint64_t metal_rtc_get_count(const struct metal_rtc *const rtc) {
  71. return rtc->vtable->get_count(rtc);
  72. }
  73. /*!
  74. * @brief Set the current count of the RTC
  75. * @return The set value of the count (not guaranteed to be exactly the requested value)
  76. *
  77. * The RTC device might impose limits on the maximum value of the count
  78. */
  79. inline uint64_t metal_rtc_set_count(const struct metal_rtc *const rtc, const uint64_t count) {
  80. return rtc->vtable->set_count(rtc, count);
  81. }
  82. /*!
  83. * @brief Start or stop the RTC
  84. * @return 0 if the RTC was successfully started/stopped
  85. */
  86. inline int metal_rtc_run(const struct metal_rtc *const rtc, const enum metal_rtc_run_option option) {
  87. return rtc->vtable->run(rtc, option);
  88. }
  89. /*!
  90. * @brief Get the interrupt handle for the RTC compare
  91. * @return The interrupt handle
  92. */
  93. inline struct metal_interrupt *metal_rtc_get_interrupt(const struct metal_rtc *const rtc) {
  94. return rtc->vtable->get_interrupt(rtc);
  95. }
  96. /*!
  97. * @brief Get the interrupt ID for the RTC compare
  98. * @return The interrupt ID
  99. */
  100. inline int metal_rtc_get_interrupt_id(const struct metal_rtc *const rtc) {
  101. return rtc->vtable->get_interrupt_id(rtc);
  102. }
  103. /*!
  104. * @brief Get the handle for an RTC by index
  105. * @return The RTC handle, or NULL if none is available at that index
  106. */
  107. struct metal_rtc *metal_rtc_get_device(int index);
  108. #endif