fsl_snvs_hp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2017, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  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 Freescale Semiconductor, Inc. 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. * Copyright (c) 2017, NXP Semiconductors, Inc.
  31. * All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without modification,
  34. * are permitted provided that the following conditions are met:
  35. *
  36. * o Redistributions of source code must retain the above copyright notice, this list
  37. * of conditions and the following disclaimer.
  38. *
  39. * o Redistributions in binary form must reproduce the above copyright notice, this
  40. * list of conditions and the following disclaimer in the documentation and/or
  41. * other materials provided with the distribution.
  42. *
  43. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  44. * contributors may be used to endorse or promote products derived from this
  45. * software without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  48. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  50. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  51. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  52. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  53. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  54. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  55. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  56. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57. */
  58. #ifndef _FSL_SNVS_HP_H_
  59. #define _FSL_SNVS_HP_H_
  60. #include "fsl_common.h"
  61. /*!
  62. * @addtogroup snvs_hp
  63. * @{
  64. */
  65. /*******************************************************************************
  66. * Definitions
  67. ******************************************************************************/
  68. /*! @name Driver version */
  69. /*@{*/
  70. #define FSL_SNVS_HP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  71. /*@}*/
  72. /*! @brief List of SNVS interrupts */
  73. typedef enum _snvs_hp_interrupt_enable
  74. {
  75. kSNVS_RTC_PeriodicInterruptEnable = 1U, /*!< RTC periodic interrupt.*/
  76. kSNVS_RTC_AlarmInterruptEnable = 2U, /*!< RTC time alarm.*/
  77. } snvs_hp_interrupt_enable_t;
  78. /*! @brief List of SNVS flags */
  79. typedef enum _snvs_hp_status_flags
  80. {
  81. kSNVS_RTC_PeriodicInterruptFlag = 1U, /*!< RTC periodic interrupt flag */
  82. kSNVS_RTC_AlarmInterruptFlag = 2U, /*!< RTC time alarm flag */
  83. } snvs_hp_status_flags_t;
  84. /*! @brief Structure is used to hold the date and time */
  85. typedef struct _snvs_hp_rtc_datetime
  86. {
  87. uint16_t year; /*!< Range from 1970 to 2099.*/
  88. uint8_t month; /*!< Range from 1 to 12.*/
  89. uint8_t day; /*!< Range from 1 to 31 (depending on month).*/
  90. uint8_t hour; /*!< Range from 0 to 23.*/
  91. uint8_t minute; /*!< Range from 0 to 59.*/
  92. uint8_t second; /*!< Range from 0 to 59.*/
  93. } snvs_hp_rtc_datetime_t;
  94. /*!
  95. * @brief SNVS config structure
  96. *
  97. * This structure holds the configuration settings for the SNVS peripheral. To initialize this
  98. * structure to reasonable defaults, call the SNVS_GetDefaultConfig() function and pass a
  99. * pointer to your config structure instance.
  100. *
  101. * The config struct can be made const so it resides in flash
  102. */
  103. typedef struct _snvs_hp_rtc_config
  104. {
  105. bool rtcCalEnable; /*!< true: RTC calibration mechanism is enabled;
  106. false:No calibration is used */
  107. uint32_t rtcCalValue; /*!< Defines signed calibration value for nonsecure RTC;
  108. This is a 5-bit 2's complement value, range from -16 to +15 */
  109. uint32_t periodicInterruptFreq; /*!< Defines frequency of the periodic interrupt;
  110. Range from 0 to 15 */
  111. } snvs_hp_rtc_config_t;
  112. /*******************************************************************************
  113. * API
  114. ******************************************************************************/
  115. #if defined(__cplusplus)
  116. extern "C" {
  117. #endif
  118. /*!
  119. * @name Initialization and deinitialization
  120. * @{
  121. */
  122. /*!
  123. * @brief Ungates the SNVS clock and configures the peripheral for basic operation.
  124. *
  125. * @note This API should be called at the beginning of the application using the SNVS driver.
  126. *
  127. * @param base SNVS peripheral base address
  128. * @param config Pointer to the user's SNVS configuration structure.
  129. */
  130. void SNVS_HP_RTC_Init(SNVS_Type *base, const snvs_hp_rtc_config_t *config);
  131. /*!
  132. * @brief Stops the RTC and SRTC timers.
  133. *
  134. * @param base SNVS peripheral base address
  135. */
  136. void SNVS_HP_RTC_Deinit(SNVS_Type *base);
  137. /*!
  138. * @brief Fills in the SNVS config struct with the default settings.
  139. *
  140. * The default values are as follows.
  141. * @code
  142. * config->rtccalenable = false;
  143. * config->rtccalvalue = 0U;
  144. * config->PIFreq = 0U;
  145. * @endcode
  146. * @param config Pointer to the user's SNVS configuration structure.
  147. */
  148. void SNVS_HP_RTC_GetDefaultConfig(snvs_hp_rtc_config_t *config);
  149. /*! @}*/
  150. /*!
  151. * @name Non secure RTC current Time & Alarm
  152. * @{
  153. */
  154. /*!
  155. * @brief Sets the SNVS RTC date and time according to the given time structure.
  156. *
  157. * @param base SNVS peripheral base address
  158. * @param datetime Pointer to the structure where the date and time details are stored.
  159. *
  160. * @return kStatus_Success: Success in setting the time and starting the SNVS RTC
  161. * kStatus_InvalidArgument: Error because the datetime format is incorrect
  162. */
  163. status_t SNVS_HP_RTC_SetDatetime(SNVS_Type *base, const snvs_hp_rtc_datetime_t *datetime);
  164. /*!
  165. * @brief Gets the SNVS RTC time and stores it in the given time structure.
  166. *
  167. * @param base SNVS peripheral base address
  168. * @param datetime Pointer to the structure where the date and time details are stored.
  169. */
  170. void SNVS_HP_RTC_GetDatetime(SNVS_Type *base, snvs_hp_rtc_datetime_t *datetime);
  171. /*!
  172. * @brief Sets the SNVS RTC alarm time.
  173. *
  174. * The function sets the RTC alarm. It also checks whether the specified alarm time
  175. * is greater than the present time. If not, the function does not set the alarm
  176. * and returns an error.
  177. *
  178. * @param base SNVS peripheral base address
  179. * @param alarmTime Pointer to the structure where the alarm time is stored.
  180. *
  181. * @return kStatus_Success: success in setting the SNVS RTC alarm
  182. * kStatus_InvalidArgument: Error because the alarm datetime format is incorrect
  183. * kStatus_Fail: Error because the alarm time has already passed
  184. */
  185. status_t SNVS_HP_RTC_SetAlarm(SNVS_Type *base, const snvs_hp_rtc_datetime_t *alarmTime);
  186. /*!
  187. * @brief Returns the SNVS RTC alarm time.
  188. *
  189. * @param base SNVS peripheral base address
  190. * @param datetime Pointer to the structure where the alarm date and time details are stored.
  191. */
  192. void SNVS_HP_RTC_GetAlarm(SNVS_Type *base, snvs_hp_rtc_datetime_t *datetime);
  193. #if (defined(FSL_FEATURE_SNVS_HAS_SRTC) && (FSL_FEATURE_SNVS_HAS_SRTC > 0))
  194. /*!
  195. * @brief The function synchronizes RTC counter value with SRTC.
  196. *
  197. * @param base SNVS peripheral base address
  198. */
  199. void SNVS_HP_RTC_TimeSynchronize(SNVS_Type *base);
  200. #endif /* FSL_FEATURE_SNVS_HAS_SRTC */
  201. /*! @}*/
  202. /*!
  203. * @name Interrupt Interface
  204. * @{
  205. */
  206. /*!
  207. * @brief Enables the selected SNVS interrupts.
  208. *
  209. * @param base SNVS peripheral base address
  210. * @param mask The interrupts to enable. This is a logical OR of members of the
  211. * enumeration ::snvs_interrupt_enable_t
  212. */
  213. void SNVS_HP_RTC_EnableInterrupts(SNVS_Type *base, uint32_t mask);
  214. /*!
  215. * @brief Disables the selected SNVS interrupts.
  216. *
  217. * @param base SNVS peripheral base address
  218. * @param mask The interrupts to enable. This is a logical OR of members of the
  219. * enumeration ::snvs_interrupt_enable_t
  220. */
  221. void SNVS_HP_RTC_DisableInterrupts(SNVS_Type *base, uint32_t mask);
  222. /*!
  223. * @brief Gets the enabled SNVS interrupts.
  224. *
  225. * @param base SNVS peripheral base address
  226. *
  227. * @return The enabled interrupts. This is the logical OR of members of the
  228. * enumeration ::snvs_interrupt_enable_t
  229. */
  230. uint32_t SNVS_HP_RTC_GetEnabledInterrupts(SNVS_Type *base);
  231. /*! @}*/
  232. /*!
  233. * @name Status Interface
  234. * @{
  235. */
  236. /*!
  237. * @brief Gets the SNVS status flags.
  238. *
  239. * @param base SNVS peripheral base address
  240. *
  241. * @return The status flags. This is the logical OR of members of the
  242. * enumeration ::snvs_status_flags_t
  243. */
  244. uint32_t SNVS_HP_RTC_GetStatusFlags(SNVS_Type *base);
  245. /*!
  246. * @brief Clears the SNVS status flags.
  247. *
  248. * @param base SNVS peripheral base address
  249. * @param mask The status flags to clear. This is a logical OR of members of the
  250. * enumeration ::snvs_status_flags_t
  251. */
  252. void SNVS_HP_RTC_ClearStatusFlags(SNVS_Type *base, uint32_t mask);
  253. /*! @}*/
  254. /*!
  255. * @name Timer Start and Stop
  256. * @{
  257. */
  258. /*!
  259. * @brief Starts the SNVS RTC time counter.
  260. *
  261. * @param base SNVS peripheral base address
  262. */
  263. static inline void SNVS_HP_RTC_StartTimer(SNVS_Type *base)
  264. {
  265. base->HPCR |= SNVS_HPCR_RTC_EN_MASK;
  266. while (!(base->HPCR & SNVS_HPCR_RTC_EN_MASK))
  267. {
  268. }
  269. }
  270. /*!
  271. * @brief Stops the SNVS RTC time counter.
  272. *
  273. * @param base SNVS peripheral base address
  274. */
  275. static inline void SNVS_HP_RTC_StopTimer(SNVS_Type *base)
  276. {
  277. base->HPCR &= ~SNVS_HPCR_RTC_EN_MASK;
  278. while (base->HPCR & SNVS_HPCR_RTC_EN_MASK)
  279. {
  280. }
  281. }
  282. #if defined(__cplusplus)
  283. }
  284. #endif
  285. /*! @}*/
  286. #endif /* _FSL_SNVS_HP_H_ */