fsl_snvs_hp.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright (c) 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_SNVS_HP_H_
  35. #define _FSL_SNVS_HP_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup snvs_hp
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. #define FSL_SNVS_HP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  47. /*@}*/
  48. /*! @brief List of SNVS interrupts */
  49. typedef enum _snvs_hp_interrupts
  50. {
  51. kSNVS_RTC_AlarmInterrupt = SNVS_HPCR_HPTA_EN_MASK, /*!< RTC time alarm */
  52. kSNVS_RTC_PeriodicInterrupt = SNVS_HPCR_PI_EN_MASK, /*!< RTC periodic interrupt */
  53. } snvs_hp_interrupts_t;
  54. /*! @brief List of SNVS flags */
  55. typedef enum _snvs_hp_status_flags
  56. {
  57. kSNVS_RTC_AlarmInterruptFlag = SNVS_HPSR_HPTA_MASK, /*!< RTC time alarm flag */
  58. kSNVS_RTC_PeriodicInterruptFlag = SNVS_HPSR_PI_MASK, /*!< RTC periodic interrupt flag */
  59. } snvs_hp_status_flags_t;
  60. /*! @brief Structure is used to hold the date and time */
  61. typedef struct _snvs_hp_rtc_datetime
  62. {
  63. uint16_t year; /*!< Range from 1970 to 2099.*/
  64. uint8_t month; /*!< Range from 1 to 12.*/
  65. uint8_t day; /*!< Range from 1 to 31 (depending on month).*/
  66. uint8_t hour; /*!< Range from 0 to 23.*/
  67. uint8_t minute; /*!< Range from 0 to 59.*/
  68. uint8_t second; /*!< Range from 0 to 59.*/
  69. } snvs_hp_rtc_datetime_t;
  70. /*!
  71. * @brief SNVS config structure
  72. *
  73. * This structure holds the configuration settings for the SNVS peripheral. To initialize this
  74. * structure to reasonable defaults, call the SNVS_GetDefaultConfig() function and pass a
  75. * pointer to your config structure instance.
  76. *
  77. * The config struct can be made const so it resides in flash
  78. */
  79. typedef struct _snvs_hp_rtc_config
  80. {
  81. bool rtcCalEnable; /*!< true: RTC calibration mechanism is enabled;
  82. false:No calibration is used */
  83. uint32_t rtcCalValue; /*!< Defines signed calibration value for nonsecure RTC;
  84. This is a 5-bit 2's complement value, range from -16 to +15 */
  85. uint32_t periodicInterruptFreq; /*!< Defines frequency of the periodic interrupt;
  86. Range from 0 to 15 */
  87. } snvs_hp_rtc_config_t;
  88. /*******************************************************************************
  89. * API
  90. ******************************************************************************/
  91. #if defined(__cplusplus)
  92. extern "C" {
  93. #endif
  94. /*!
  95. * @name Initialization and deinitialization
  96. * @{
  97. */
  98. /*!
  99. * @brief Ungates the SNVS clock and configures the peripheral for basic operation.
  100. *
  101. * @note This API should be called at the beginning of the application using the SNVS driver.
  102. *
  103. * @param base SNVS peripheral base address
  104. * @param config Pointer to the user's SNVS configuration structure.
  105. */
  106. void SNVS_HP_RTC_Init(SNVS_Type *base, const snvs_hp_rtc_config_t *config);
  107. /*!
  108. * @brief Stops the RTC and SRTC timers.
  109. *
  110. * @param base SNVS peripheral base address
  111. */
  112. void SNVS_HP_RTC_Deinit(SNVS_Type *base);
  113. /*!
  114. * @brief Fills in the SNVS config struct with the default settings.
  115. *
  116. * The default values are as follows.
  117. * @code
  118. * config->rtccalenable = false;
  119. * config->rtccalvalue = 0U;
  120. * config->PIFreq = 0U;
  121. * @endcode
  122. * @param config Pointer to the user's SNVS configuration structure.
  123. */
  124. void SNVS_HP_RTC_GetDefaultConfig(snvs_hp_rtc_config_t *config);
  125. /*! @}*/
  126. /*!
  127. * @name Non secure RTC current Time & Alarm
  128. * @{
  129. */
  130. /*!
  131. * @brief Sets the SNVS RTC date and time according to the given time structure.
  132. *
  133. * @param base SNVS peripheral base address
  134. * @param datetime Pointer to the structure where the date and time details are stored.
  135. *
  136. * @return kStatus_Success: Success in setting the time and starting the SNVS RTC
  137. * kStatus_InvalidArgument: Error because the datetime format is incorrect
  138. */
  139. status_t SNVS_HP_RTC_SetDatetime(SNVS_Type *base, const snvs_hp_rtc_datetime_t *datetime);
  140. /*!
  141. * @brief Gets the SNVS RTC time and stores it in the given time structure.
  142. *
  143. * @param base SNVS peripheral base address
  144. * @param datetime Pointer to the structure where the date and time details are stored.
  145. */
  146. void SNVS_HP_RTC_GetDatetime(SNVS_Type *base, snvs_hp_rtc_datetime_t *datetime);
  147. /*!
  148. * @brief Sets the SNVS RTC alarm time.
  149. *
  150. * The function sets the RTC alarm. It also checks whether the specified alarm time
  151. * is greater than the present time. If not, the function does not set the alarm
  152. * and returns an error.
  153. *
  154. * @param base SNVS peripheral base address
  155. * @param alarmTime Pointer to the structure where the alarm time is stored.
  156. *
  157. * @return kStatus_Success: success in setting the SNVS RTC alarm
  158. * kStatus_InvalidArgument: Error because the alarm datetime format is incorrect
  159. * kStatus_Fail: Error because the alarm time has already passed
  160. */
  161. status_t SNVS_HP_RTC_SetAlarm(SNVS_Type *base, const snvs_hp_rtc_datetime_t *alarmTime);
  162. /*!
  163. * @brief Returns the SNVS RTC alarm time.
  164. *
  165. * @param base SNVS peripheral base address
  166. * @param datetime Pointer to the structure where the alarm date and time details are stored.
  167. */
  168. void SNVS_HP_RTC_GetAlarm(SNVS_Type *base, snvs_hp_rtc_datetime_t *datetime);
  169. #if (defined(FSL_FEATURE_SNVS_HAS_SRTC) && (FSL_FEATURE_SNVS_HAS_SRTC > 0))
  170. /*!
  171. * @brief The function synchronizes RTC counter value with SRTC.
  172. *
  173. * @param base SNVS peripheral base address
  174. */
  175. void SNVS_HP_RTC_TimeSynchronize(SNVS_Type *base);
  176. #endif /* FSL_FEATURE_SNVS_HAS_SRTC */
  177. /*! @}*/
  178. /*!
  179. * @name Interrupt Interface
  180. * @{
  181. */
  182. /*!
  183. * @brief Enables the selected SNVS interrupts.
  184. *
  185. * @param base SNVS peripheral base address
  186. * @param mask The interrupts to enable. This is a logical OR of members of the
  187. * enumeration ::snvs_interrupt_enable_t
  188. */
  189. static inline void SNVS_HP_RTC_EnableInterrupts(SNVS_Type *base, uint32_t mask)
  190. {
  191. base->HPCR |= mask;
  192. }
  193. /*!
  194. * @brief Disables the selected SNVS interrupts.
  195. *
  196. * @param base SNVS peripheral base address
  197. * @param mask The interrupts to enable. This is a logical OR of members of the
  198. * enumeration ::snvs_interrupt_enable_t
  199. */
  200. static inline void SNVS_HP_RTC_DisableInterrupts(SNVS_Type *base, uint32_t mask)
  201. {
  202. base->HPCR &= ~mask;
  203. }
  204. /*!
  205. * @brief Gets the enabled SNVS interrupts.
  206. *
  207. * @param base SNVS peripheral base address
  208. *
  209. * @return The enabled interrupts. This is the logical OR of members of the
  210. * enumeration ::snvs_interrupt_enable_t
  211. */
  212. uint32_t SNVS_HP_RTC_GetEnabledInterrupts(SNVS_Type *base);
  213. /*! @}*/
  214. /*!
  215. * @name Status Interface
  216. * @{
  217. */
  218. /*!
  219. * @brief Gets the SNVS status flags.
  220. *
  221. * @param base SNVS peripheral base address
  222. *
  223. * @return The status flags. This is the logical OR of members of the
  224. * enumeration ::snvs_status_flags_t
  225. */
  226. uint32_t SNVS_HP_RTC_GetStatusFlags(SNVS_Type *base);
  227. /*!
  228. * @brief Clears the SNVS status flags.
  229. *
  230. * @param base SNVS peripheral base address
  231. * @param mask The status flags to clear. This is a logical OR of members of the
  232. * enumeration ::snvs_status_flags_t
  233. */
  234. static inline void SNVS_HP_RTC_ClearStatusFlags(SNVS_Type *base, uint32_t mask)
  235. {
  236. base->HPSR |= mask;
  237. }
  238. /*! @}*/
  239. /*!
  240. * @name Timer Start and Stop
  241. * @{
  242. */
  243. /*!
  244. * @brief Starts the SNVS RTC time counter.
  245. *
  246. * @param base SNVS peripheral base address
  247. */
  248. static inline void SNVS_HP_RTC_StartTimer(SNVS_Type *base)
  249. {
  250. base->HPCR |= SNVS_HPCR_RTC_EN_MASK;
  251. while (!(base->HPCR & SNVS_HPCR_RTC_EN_MASK))
  252. {
  253. }
  254. }
  255. /*!
  256. * @brief Stops the SNVS RTC time counter.
  257. *
  258. * @param base SNVS peripheral base address
  259. */
  260. static inline void SNVS_HP_RTC_StopTimer(SNVS_Type *base)
  261. {
  262. base->HPCR &= ~SNVS_HPCR_RTC_EN_MASK;
  263. while (base->HPCR & SNVS_HPCR_RTC_EN_MASK)
  264. {
  265. }
  266. }
  267. #if defined(__cplusplus)
  268. }
  269. #endif
  270. /*! @}*/
  271. #endif /* _FSL_SNVS_HP_H_ */