fsl_rtc.h 17 KB

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