fsl_rtc.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2016, 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. /*! @file */
  38. /*******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41. /*! @name Driver version */
  42. /*@{*/
  43. #define FSL_RTC_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  44. /*@}*/
  45. /*! @brief List of RTC interrupts */
  46. typedef enum _rtc_interrupt_enable
  47. {
  48. kRTC_AlarmInterruptEnable = RTC_CTRL_ALARMDPD_EN_MASK, /*!< Alarm interrupt.*/
  49. kRTC_WakeupInterruptEnable = RTC_CTRL_WAKEDPD_EN_MASK /*!< Wake-up interrupt.*/
  50. } rtc_interrupt_enable_t;
  51. /*! @brief List of RTC flags */
  52. typedef enum _rtc_status_flags
  53. {
  54. kRTC_AlarmFlag = RTC_CTRL_ALARM1HZ_MASK, /*!< Alarm flag*/
  55. kRTC_WakeupFlag = RTC_CTRL_WAKE1KHZ_MASK /*!< 1kHz wake-up timer flag*/
  56. } rtc_status_flags_t;
  57. /*! @brief Structure is used to hold the date and time */
  58. typedef struct _rtc_datetime
  59. {
  60. uint16_t year; /*!< Range from 1970 to 2099.*/
  61. uint8_t month; /*!< Range from 1 to 12.*/
  62. uint8_t day; /*!< Range from 1 to 31 (depending on month).*/
  63. uint8_t hour; /*!< Range from 0 to 23.*/
  64. uint8_t minute; /*!< Range from 0 to 59.*/
  65. uint8_t second; /*!< Range from 0 to 59.*/
  66. } rtc_datetime_t;
  67. /*******************************************************************************
  68. * API
  69. ******************************************************************************/
  70. #if defined(__cplusplus)
  71. extern "C" {
  72. #endif
  73. /*!
  74. * @name Initialization and deinitialization
  75. * @{
  76. */
  77. /*!
  78. * @brief Ungates the RTC clock and enables the RTC oscillator.
  79. *
  80. * @note This API should be called at the beginning of the application using the RTC driver.
  81. *
  82. * @param base RTC peripheral base address
  83. */
  84. void RTC_Init(RTC_Type *base);
  85. /*!
  86. * @brief Stop the timer and gate the RTC clock
  87. *
  88. * @param base RTC peripheral base address
  89. */
  90. static inline void RTC_Deinit(RTC_Type *base)
  91. {
  92. /* Stop the RTC timer */
  93. base->CTRL &= ~RTC_CTRL_RTC_EN_MASK;
  94. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  95. /* Gate the module clock */
  96. CLOCK_DisableClock(kCLOCK_Rtc);
  97. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  98. }
  99. /*! @}*/
  100. /*!
  101. * @name Current Time & Alarm
  102. * @{
  103. */
  104. /*!
  105. * @brief Sets the RTC date and time according to the given time structure.
  106. *
  107. * The RTC counter must be stopped prior to calling this function as writes to the RTC
  108. * seconds register will fail if the RTC counter is running.
  109. *
  110. * @param base RTC peripheral base address
  111. * @param datetime Pointer to structure where the date and time details to set are stored
  112. *
  113. * @return kStatus_Success: Success in setting the time and starting the RTC
  114. * kStatus_InvalidArgument: Error because the datetime format is incorrect
  115. */
  116. status_t RTC_SetDatetime(RTC_Type *base, const rtc_datetime_t *datetime);
  117. /*!
  118. * @brief Gets the RTC time and stores it in the given time structure.
  119. *
  120. * @param base RTC peripheral base address
  121. * @param datetime Pointer to structure where the date and time details are stored.
  122. */
  123. void RTC_GetDatetime(RTC_Type *base, rtc_datetime_t *datetime);
  124. /*!
  125. * @brief Sets the RTC alarm time
  126. *
  127. * The function checks whether the specified alarm time is greater than the present
  128. * time. If not, the function does not set the alarm and returns an error.
  129. *
  130. * @param base RTC peripheral base address
  131. * @param alarmTime Pointer to structure where the alarm time is stored.
  132. *
  133. * @return kStatus_Success: success in setting the RTC alarm
  134. * kStatus_InvalidArgument: Error because the alarm datetime format is incorrect
  135. * kStatus_Fail: Error because the alarm time has already passed
  136. */
  137. status_t RTC_SetAlarm(RTC_Type *base, const rtc_datetime_t *alarmTime);
  138. /*!
  139. * @brief Returns the RTC alarm time.
  140. *
  141. * @param base RTC peripheral base address
  142. * @param datetime Pointer to structure where the alarm date and time details are stored.
  143. */
  144. void RTC_GetAlarm(RTC_Type *base, rtc_datetime_t *datetime);
  145. /*! @}*/
  146. /*!
  147. * @brief Enable the RTC high resolution timer and set the wake-up time.
  148. *
  149. * @param base RTC peripheral base address
  150. * @param wakeupValue The value to be loaded into the RTC WAKE register
  151. */
  152. static inline void RTC_SetWakeupCount(RTC_Type *base, uint16_t wakeupValue)
  153. {
  154. /* Enable the 1kHz RTC timer */
  155. base->CTRL |= RTC_CTRL_RTC1KHZ_EN_MASK;
  156. /* Set the start count value into the wake-up timer */
  157. base->WAKE = wakeupValue;
  158. }
  159. /*!
  160. * @brief Read actual RTC counter value.
  161. *
  162. * @param base RTC peripheral base address
  163. */
  164. static inline uint16_t RTC_GetWakeupCount(RTC_Type *base)
  165. {
  166. /* Read wake-up counter */
  167. return RTC_WAKE_VAL(base->WAKE);
  168. }
  169. /*!
  170. * @name Interrupt Interface
  171. * @{
  172. */
  173. /*!
  174. * @brief Enables the selected RTC interrupts.
  175. *
  176. * @param base RTC peripheral base address
  177. * @param mask The interrupts to enable. This is a logical OR of members of the
  178. * enumeration ::rtc_interrupt_enable_t
  179. */
  180. static inline void RTC_EnableInterrupts(RTC_Type *base, uint32_t mask)
  181. {
  182. uint32_t reg = base->CTRL;
  183. /* Clear flag bits to prevent accidentally clearing anything when writing back */
  184. reg &= ~(RTC_CTRL_ALARM1HZ_MASK | RTC_CTRL_WAKE1KHZ_MASK);
  185. reg |= mask;
  186. base->CTRL = reg;
  187. }
  188. /*!
  189. * @brief Disables the selected RTC interrupts.
  190. *
  191. * @param base RTC peripheral base address
  192. * @param mask The interrupts to enable. This is a logical OR of members of the
  193. * enumeration ::rtc_interrupt_enable_t
  194. */
  195. static inline void RTC_DisableInterrupts(RTC_Type *base, uint32_t mask)
  196. {
  197. uint32_t reg = base->CTRL;
  198. /* Clear flag bits to prevent accidentally clearing anything when writing back */
  199. reg &= ~(RTC_CTRL_ALARM1HZ_MASK | RTC_CTRL_WAKE1KHZ_MASK | mask);
  200. base->CTRL = reg;
  201. }
  202. /*!
  203. * @brief Gets the enabled RTC interrupts.
  204. *
  205. * @param base RTC peripheral base address
  206. *
  207. * @return The enabled interrupts. This is the logical OR of members of the
  208. * enumeration ::rtc_interrupt_enable_t
  209. */
  210. static inline uint32_t RTC_GetEnabledInterrupts(RTC_Type *base)
  211. {
  212. return (base->CTRL & (RTC_CTRL_ALARMDPD_EN_MASK | RTC_CTRL_WAKEDPD_EN_MASK));
  213. }
  214. /*! @}*/
  215. /*!
  216. * @name Status Interface
  217. * @{
  218. */
  219. /*!
  220. * @brief Gets the RTC status flags
  221. *
  222. * @param base RTC peripheral base address
  223. *
  224. * @return The status flags. This is the logical OR of members of the
  225. * enumeration ::rtc_status_flags_t
  226. */
  227. static inline uint32_t RTC_GetStatusFlags(RTC_Type *base)
  228. {
  229. return (base->CTRL & (RTC_CTRL_ALARM1HZ_MASK | RTC_CTRL_WAKE1KHZ_MASK));
  230. }
  231. /*!
  232. * @brief Clears the RTC status flags.
  233. *
  234. * @param base RTC peripheral base address
  235. * @param mask The status flags to clear. This is a logical OR of members of the
  236. * enumeration ::rtc_status_flags_t
  237. */
  238. static inline void RTC_ClearStatusFlags(RTC_Type *base, uint32_t mask)
  239. {
  240. uint32_t reg = base->CTRL;
  241. /* Clear flag bits to prevent accidentally clearing anything when writing back */
  242. reg &= ~(RTC_CTRL_ALARM1HZ_MASK | RTC_CTRL_WAKE1KHZ_MASK);
  243. /* Write 1 to the flags we wish to clear */
  244. reg |= mask;
  245. base->CTRL = reg;
  246. }
  247. /*! @}*/
  248. /*!
  249. * @name Timer Start and Stop
  250. * @{
  251. */
  252. /*!
  253. * @brief Starts the RTC time counter.
  254. *
  255. * After calling this function, the timer counter increments once a second provided SR[TOF] or
  256. * SR[TIF] are not set.
  257. *
  258. * @param base RTC peripheral base address
  259. */
  260. static inline void RTC_StartTimer(RTC_Type *base)
  261. {
  262. base->CTRL |= RTC_CTRL_RTC_EN_MASK;
  263. }
  264. /*!
  265. * @brief Stops the RTC time counter.
  266. *
  267. * RTC's seconds register can be written to only when the timer is stopped.
  268. *
  269. * @param base RTC peripheral base address
  270. */
  271. static inline void RTC_StopTimer(RTC_Type *base)
  272. {
  273. base->CTRL &= ~RTC_CTRL_RTC_EN_MASK;
  274. }
  275. /*! @}*/
  276. /*!
  277. * @brief Performs a software reset on the RTC module.
  278. *
  279. * This resets all RTC registers to their reset value. The bit is cleared by software explicitly clearing it.
  280. *
  281. * @param base RTC peripheral base address
  282. */
  283. static inline void RTC_Reset(RTC_Type *base)
  284. {
  285. base->CTRL |= RTC_CTRL_SWRESET_MASK;
  286. base->CTRL &= ~RTC_CTRL_SWRESET_MASK;
  287. }
  288. #if defined(__cplusplus)
  289. }
  290. #endif
  291. /*! @}*/
  292. #endif /* _FSL_RTC_H_ */