fsl_rtc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_RTC_H_
  31. #define _FSL_RTC_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup rtc
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. #define FSL_RTC_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  43. /*@}*/
  44. /*! @brief List of RTC interrupts */
  45. typedef enum _rtc_interrupt_enable
  46. {
  47. kRTC_TimeInvalidInterruptEnable = RTC_IER_TIIE_MASK, /*!< Time invalid interrupt.*/
  48. kRTC_TimeOverflowInterruptEnable = RTC_IER_TOIE_MASK, /*!< Time overflow interrupt.*/
  49. kRTC_AlarmInterruptEnable = RTC_IER_TAIE_MASK, /*!< Alarm interrupt.*/
  50. kRTC_SecondsInterruptEnable = RTC_IER_TSIE_MASK /*!< Seconds interrupt.*/
  51. } rtc_interrupt_enable_t;
  52. /*! @brief List of RTC flags */
  53. typedef enum _rtc_status_flags
  54. {
  55. kRTC_TimeInvalidFlag = RTC_SR_TIF_MASK, /*!< Time invalid flag */
  56. kRTC_TimeOverflowFlag = RTC_SR_TOF_MASK, /*!< Time overflow flag */
  57. kRTC_AlarmFlag = RTC_SR_TAF_MASK /*!< Alarm flag*/
  58. } rtc_status_flags_t;
  59. #if (defined(FSL_FEATURE_RTC_HAS_OSC_SCXP) && FSL_FEATURE_RTC_HAS_OSC_SCXP)
  60. /*! @brief List of RTC Oscillator capacitor load settings */
  61. typedef enum _rtc_osc_cap_load
  62. {
  63. kRTC_Capacitor_2p = RTC_CR_SC2P_MASK, /*!< 2 pF capacitor load */
  64. kRTC_Capacitor_4p = RTC_CR_SC4P_MASK, /*!< 4 pF capacitor load */
  65. kRTC_Capacitor_8p = RTC_CR_SC8P_MASK, /*!< 8 pF capacitor load */
  66. kRTC_Capacitor_16p = RTC_CR_SC16P_MASK /*!< 16 pF capacitor load */
  67. } rtc_osc_cap_load_t;
  68. #endif /* FSL_FEATURE_SCG_HAS_OSC_SCXP */
  69. /*! @brief Structure is used to hold the date and time */
  70. typedef struct _rtc_datetime
  71. {
  72. uint16_t year; /*!< Range from 1970 to 2099.*/
  73. uint8_t month; /*!< Range from 1 to 12.*/
  74. uint8_t day; /*!< Range from 1 to 31 (depending on month).*/
  75. uint8_t hour; /*!< Range from 0 to 23.*/
  76. uint8_t minute; /*!< Range from 0 to 59.*/
  77. uint8_t second; /*!< Range from 0 to 59.*/
  78. } rtc_datetime_t;
  79. /*!
  80. * @brief RTC config structure
  81. *
  82. * This structure holds the configuration settings for the RTC peripheral. To initialize this
  83. * structure to reasonable defaults, call the RTC_GetDefaultConfig() function and pass a
  84. * pointer to your config structure instance.
  85. *
  86. * The config struct can be made const so it resides in flash
  87. */
  88. typedef struct _rtc_config
  89. {
  90. bool wakeupSelect; /*!< true: Wakeup pin outputs the 32 KHz clock;
  91. false:Wakeup pin used to wakeup the chip */
  92. bool updateMode; /*!< true: Registers can be written even when locked under certain
  93. conditions, false: No writes allowed when registers are locked */
  94. bool supervisorAccess; /*!< true: Non-supervisor accesses are allowed;
  95. false: Non-supervisor accesses are not supported */
  96. uint32_t compensationInterval; /*!< Compensation interval that is written to the CIR field in RTC TCR Register */
  97. uint32_t compensationTime; /*!< Compensation time that is written to the TCR field in RTC TCR Register */
  98. } rtc_config_t;
  99. /*******************************************************************************
  100. * API
  101. ******************************************************************************/
  102. #if defined(__cplusplus)
  103. extern "C" {
  104. #endif
  105. /*!
  106. * @name Initialization and deinitialization
  107. * @{
  108. */
  109. /*!
  110. * @brief Ungates the RTC clock and configures the peripheral for basic operation.
  111. *
  112. * This function issues a software reset if the timer invalid flag is set.
  113. *
  114. * @note This API should be called at the beginning of the application using the RTC driver.
  115. *
  116. * @param base RTC peripheral base address
  117. * @param config Pointer to the user's RTC configuration structure.
  118. */
  119. void RTC_Init(RTC_Type *base, const rtc_config_t *config);
  120. /*!
  121. * @brief Stops the timer and gate the RTC clock.
  122. *
  123. * @param base RTC peripheral base address
  124. */
  125. static inline void RTC_Deinit(RTC_Type *base)
  126. {
  127. /* Stop the RTC timer */
  128. base->SR &= ~RTC_SR_TCE_MASK;
  129. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  130. /* Gate the module clock */
  131. CLOCK_DisableClock(kCLOCK_Rtc0);
  132. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  133. }
  134. /*!
  135. * @brief Fills in the RTC config struct with the default settings.
  136. *
  137. * The default values are as follows.
  138. * @code
  139. * config->wakeupSelect = false;
  140. * config->updateMode = false;
  141. * config->supervisorAccess = false;
  142. * config->compensationInterval = 0;
  143. * config->compensationTime = 0;
  144. * @endcode
  145. * @param config Pointer to the user's RTC configuration structure.
  146. */
  147. void RTC_GetDefaultConfig(rtc_config_t *config);
  148. /*! @}*/
  149. /*!
  150. * @name Current Time & Alarm
  151. * @{
  152. */
  153. /*!
  154. * @brief Sets the RTC date and time according to the given time structure.
  155. *
  156. * The RTC counter must be stopped prior to calling this function because writes to the RTC
  157. * seconds register fail if the RTC counter is running.
  158. *
  159. * @param base RTC peripheral base address
  160. * @param datetime Pointer to the structure where the date and time details are stored.
  161. *
  162. * @return kStatus_Success: Success in setting the time and starting the RTC
  163. * kStatus_InvalidArgument: Error because the datetime format is incorrect
  164. */
  165. status_t RTC_SetDatetime(RTC_Type *base, const rtc_datetime_t *datetime);
  166. /*!
  167. * @brief Gets the RTC time and stores it in the given time structure.
  168. *
  169. * @param base RTC peripheral base address
  170. * @param datetime Pointer to the structure where the date and time details are stored.
  171. */
  172. void RTC_GetDatetime(RTC_Type *base, rtc_datetime_t *datetime);
  173. /*!
  174. * @brief Sets the RTC alarm time.
  175. *
  176. * The function checks whether the specified alarm time is greater than the present
  177. * time. If not, the function does not set the alarm and returns an error.
  178. *
  179. * @param base RTC peripheral base address
  180. * @param alarmTime Pointer to the structure where the alarm time is stored.
  181. *
  182. * @return kStatus_Success: success in setting the RTC alarm
  183. * kStatus_InvalidArgument: Error because the alarm datetime format is incorrect
  184. * kStatus_Fail: Error because the alarm time has already passed
  185. */
  186. status_t RTC_SetAlarm(RTC_Type *base, const rtc_datetime_t *alarmTime);
  187. /*!
  188. * @brief Returns the RTC alarm time.
  189. *
  190. * @param base RTC peripheral base address
  191. * @param datetime Pointer to the structure where the alarm date and time details are stored.
  192. */
  193. void RTC_GetAlarm(RTC_Type *base, rtc_datetime_t *datetime);
  194. /*! @}*/
  195. /*!
  196. * @name Interrupt Interface
  197. * @{
  198. */
  199. /*!
  200. * @brief Enables the selected RTC interrupts.
  201. *
  202. * @param base RTC peripheral base address
  203. * @param mask The interrupts to enable. This is a logical OR of members of the
  204. * enumeration ::rtc_interrupt_enable_t
  205. */
  206. static inline void RTC_EnableInterrupts(RTC_Type *base, uint32_t mask)
  207. {
  208. base->IER |= mask;
  209. }
  210. /*!
  211. * @brief Disables the selected RTC interrupts.
  212. *
  213. * @param base RTC peripheral base address
  214. * @param mask The interrupts to enable. This is a logical OR of members of the
  215. * enumeration ::rtc_interrupt_enable_t
  216. */
  217. static inline void RTC_DisableInterrupts(RTC_Type *base, uint32_t mask)
  218. {
  219. base->IER &= ~mask;
  220. }
  221. /*!
  222. * @brief Gets the enabled RTC interrupts.
  223. *
  224. * @param base RTC peripheral base address
  225. *
  226. * @return The enabled interrupts. This is the logical OR of members of the
  227. * enumeration ::rtc_interrupt_enable_t
  228. */
  229. static inline uint32_t RTC_GetEnabledInterrupts(RTC_Type *base)
  230. {
  231. return (base->IER & (RTC_IER_TIIE_MASK | RTC_IER_TOIE_MASK | RTC_IER_TAIE_MASK | RTC_IER_TSIE_MASK));
  232. }
  233. /*! @}*/
  234. /*!
  235. * @name Status Interface
  236. * @{
  237. */
  238. /*!
  239. * @brief Gets the RTC status flags.
  240. *
  241. * @param base RTC peripheral base address
  242. *
  243. * @return The status flags. This is the logical OR of members of the
  244. * enumeration ::rtc_status_flags_t
  245. */
  246. static inline uint32_t RTC_GetStatusFlags(RTC_Type *base)
  247. {
  248. return (base->SR & (RTC_SR_TIF_MASK | RTC_SR_TOF_MASK | RTC_SR_TAF_MASK));
  249. }
  250. /*!
  251. * @brief Clears the RTC status flags.
  252. *
  253. * @param base RTC peripheral base address
  254. * @param mask The status flags to clear. This is a logical OR of members of the
  255. * enumeration ::rtc_status_flags_t
  256. */
  257. void RTC_ClearStatusFlags(RTC_Type *base, uint32_t mask);
  258. /*! @}*/
  259. /*!
  260. * @name Timer Start and Stop
  261. * @{
  262. */
  263. /*!
  264. * @brief Starts the RTC time counter.
  265. *
  266. * After calling this function, the timer counter increments once a second provided SR[TOF] or
  267. * SR[TIF] are not set.
  268. *
  269. * @param base RTC peripheral base address
  270. */
  271. static inline void RTC_StartTimer(RTC_Type *base)
  272. {
  273. base->SR |= RTC_SR_TCE_MASK;
  274. }
  275. /*!
  276. * @brief Stops the RTC time counter.
  277. *
  278. * RTC's seconds register can be written to only when the timer is stopped.
  279. *
  280. * @param base RTC peripheral base address
  281. */
  282. static inline void RTC_StopTimer(RTC_Type *base)
  283. {
  284. base->SR &= ~RTC_SR_TCE_MASK;
  285. }
  286. /*! @}*/
  287. #if (defined(FSL_FEATURE_RTC_HAS_OSC_SCXP) && FSL_FEATURE_RTC_HAS_OSC_SCXP)
  288. /*!
  289. * @brief This function sets the specified capacitor configuration for the RTC oscillator.
  290. *
  291. * @param base RTC peripheral base address
  292. * @param capLoad Oscillator loads to enable. This is a logical OR of members of the
  293. * enumeration ::rtc_osc_cap_load_t
  294. */
  295. static inline void RTC_SetOscCapLoad(RTC_Type *base, uint32_t capLoad)
  296. {
  297. uint32_t reg = base->CR;
  298. reg &= ~(RTC_CR_SC2P_MASK | RTC_CR_SC4P_MASK | RTC_CR_SC8P_MASK | RTC_CR_SC16P_MASK);
  299. reg |= capLoad;
  300. base->CR = reg;
  301. }
  302. #endif /* FSL_FEATURE_SCG_HAS_OSC_SCXP */
  303. /*!
  304. * @brief Performs a software reset on the RTC module.
  305. *
  306. * This resets all RTC registers except for the SWR bit and the RTC_WAR and RTC_RAR
  307. * registers. The SWR bit is cleared by software explicitly clearing it.
  308. *
  309. * @param base RTC peripheral base address
  310. */
  311. static inline void RTC_Reset(RTC_Type *base)
  312. {
  313. base->CR |= RTC_CR_SWR_MASK;
  314. base->CR &= ~RTC_CR_SWR_MASK;
  315. /* Set TSR register to 0x1 to avoid the timer invalid (TIF) bit being set in the SR register */
  316. base->TSR = 1U;
  317. }
  318. #if defined(FSL_FEATURE_RTC_HAS_MONOTONIC) && (FSL_FEATURE_RTC_HAS_MONOTONIC)
  319. /*!
  320. * @name Monotonic counter functions
  321. * @{
  322. */
  323. /*!
  324. * @brief Reads the values of the Monotonic Counter High and Monotonic Counter Low and returns
  325. * them as a single value.
  326. *
  327. * @param base RTC peripheral base address
  328. * @param counter Pointer to variable where the value is stored.
  329. */
  330. void RTC_GetMonotonicCounter(RTC_Type *base, uint64_t *counter);
  331. /*!
  332. * @brief Writes values Monotonic Counter High and Monotonic Counter Low by decomposing
  333. * the given single value.
  334. *
  335. * @param base RTC peripheral base address
  336. * @param counter Counter value
  337. */
  338. void RTC_SetMonotonicCounter(RTC_Type *base, uint64_t counter);
  339. /*!
  340. * @brief Increments the Monotonic Counter by one.
  341. *
  342. * Increments the Monotonic Counter (registers RTC_MCLR and RTC_MCHR accordingly) by setting
  343. * the monotonic counter enable (MER[MCE]) and then writing to the RTC_MCLR register. A write to the
  344. * monotonic counter low that causes it to overflow also increments the monotonic counter high.
  345. *
  346. * @param base RTC peripheral base address
  347. *
  348. * @return kStatus_Success: success
  349. * kStatus_Fail: error occurred, either time invalid or monotonic overflow flag was found
  350. */
  351. status_t RTC_IncrementMonotonicCounter(RTC_Type *base);
  352. /*! @}*/
  353. #endif /* FSL_FEATURE_RTC_HAS_MONOTONIC */
  354. #if defined(__cplusplus)
  355. }
  356. #endif
  357. /*! @}*/
  358. #endif /* _FSL_RTC_H_ */