fsl_rtc.h 9.9 KB

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