rtc_api.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /** \addtogroup hal */
  17. /** @{*/
  18. #ifndef MBED_RTC_API_H
  19. #define MBED_RTC_API_H
  20. #include "device.h"
  21. #include <time.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /**
  26. * \defgroup hal_rtc RTC hal
  27. *
  28. * The RTC hal provides a low level interface to the Real Time Counter (RTC) of a
  29. * target.
  30. *
  31. * # Defined behaviour
  32. * * The function ::rtc_init is safe to call repeatedly - Verified by test ::rtc_init_test.
  33. * * RTC accuracy is at least 10% - Verified by test ::rtc_accuracy_test.
  34. * * Init/free doesn't stop RTC from counting - Verified by test ::rtc_persist_test.
  35. * * Software reset doesn't stop RTC from counting - Verified by test ::rtc_reset_test.
  36. * * Sleep modes don't stop RTC from counting - Verified by test ::rtc_sleep_test.
  37. * * Shutdown mode doesn't stop RTC from counting - Not verified.
  38. * * The functions ::rtc_write/::rtc_read provides availability to set/get RTC time
  39. * - Verified by test ::rtc_write_read_test.
  40. * * The functions ::rtc_isenabled returns 1 if the RTC is counting and the time has been set,
  41. * 0 otherwise - Verified by test ::rtc_enabled_test.
  42. *
  43. * # Undefined behaviour
  44. * * Calling any function other than ::rtc_init before the initialisation of the RTC
  45. *
  46. * # Potential bugs
  47. * * Incorrect overflow handling - Verified by ::rtc_range_test
  48. * * Glitches due to ripple counter - Verified by ::rtc_glitch_test
  49. *
  50. * @see hal_rtc_tests
  51. *
  52. * @{
  53. */
  54. /**
  55. * \defgroup hal_rtc_tests RTC hal tests
  56. * The RTC test validate proper implementation of the RTC hal.
  57. *
  58. * To run the RTC hal tests use the command:
  59. *
  60. * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-rtc*
  61. */
  62. /** Initialize the RTC peripheral
  63. *
  64. * Powerup the RTC in perpetration for access. This function must be called
  65. * before any other RTC functions ares called. This does not change the state
  66. * of the RTC. It just enables access to it.
  67. *
  68. * @note This function is safe to call repeatedly - Tested by ::rtc_init_test
  69. *
  70. * Example Implementation Pseudo Code:
  71. * @code
  72. * void rtc_init()
  73. * {
  74. * // Enable clock gate so processor can read RTC registers
  75. * POWER_CTRL |= POWER_CTRL_RTC_Msk;
  76. *
  77. * // See if the RTC is already setup
  78. * if (!(RTC_STATUS & RTC_STATUS_COUNTING_Msk)) {
  79. *
  80. * // Setup the RTC clock source
  81. * RTC_CTRL |= RTC_CTRL_CLK32_Msk;
  82. * }
  83. * }
  84. * @endcode
  85. */
  86. void rtc_init(void);
  87. /** Deinitialize RTC
  88. *
  89. * Powerdown the RTC in preparation for sleep, powerdown or reset. That should only
  90. * affect the CPU domain and not the time keeping logic.
  91. * After this function is called no other RTC functions should be called
  92. * except for ::rtc_init.
  93. *
  94. * @note This function does not stop the RTC from counting - Tested by ::rtc_persist_test
  95. *
  96. * Example Implementation Pseudo Code:
  97. * @code
  98. * void rtc_free()
  99. * {
  100. * // Disable clock gate since processor no longer needs to read RTC registers
  101. * POWER_CTRL &= ~POWER_CTRL_RTC_Msk;
  102. * }
  103. * @endcode
  104. */
  105. void rtc_free(void);
  106. /** Check if the RTC has the time set and is counting
  107. *
  108. * @retval 0 The time reported by the RTC is not valid
  109. * @retval 1 The time has been set the RTC is counting
  110. *
  111. * Example Implementation Pseudo Code:
  112. * @code
  113. * int rtc_isenabled()
  114. * {
  115. * if (RTC_STATUS & RTC_STATUS_COUNTING_Msk) {
  116. * return 1;
  117. * } else {
  118. * return 0;
  119. * }
  120. * }
  121. * @endcode
  122. */
  123. int rtc_isenabled(void);
  124. /** Get the current time from the RTC peripheral
  125. *
  126. * @return The current time in seconds
  127. *
  128. * @note Some RTCs are not synchronized with the main clock. If
  129. * this is the case with your RTC then you must read the RTC time
  130. * in a loop to prevent reading the wrong time due to a glitch.
  131. * The test ::rtc_glitch_test is intended to catch this bug.
  132. *
  133. * Example implementation for an unsynchronized ripple counter:
  134. * @code
  135. * time_t rtc_read()
  136. * {
  137. * uint32_t val;
  138. * uint32_t last_val;
  139. *
  140. * // Loop until the same value is read twice
  141. * val = RTC_SECONDS;
  142. * do {
  143. * last_val = val;
  144. * val = RTC_SECONDS;
  145. * } while (last_val != val);
  146. *
  147. * return (time_t)val;
  148. * }
  149. * @endcode
  150. */
  151. time_t rtc_read(void);
  152. /** Write the current time in seconds to the RTC peripheral
  153. *
  154. * @param t The current time to be set in seconds.
  155. *
  156. * Example Implementation Pseudo Code:
  157. * @code
  158. * void rtc_write(time_t t)
  159. * {
  160. * RTC_SECONDS = t;
  161. * }
  162. * @endcode
  163. */
  164. void rtc_write(time_t t);
  165. /**@}*/
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169. #endif
  170. /** @}*/