fsl_snvs_lp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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_LP_H_
  35. #define _FSL_SNVS_LP_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup snvs_lp
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. #define FSL_SNVS_LP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
  47. /*@}*/
  48. /*! @brief List of SNVS_LP interrupts */
  49. typedef enum _snvs_lp_srtc_interrupts
  50. {
  51. kSNVS_SRTC_AlarmInterrupt = SNVS_LPCR_LPTA_EN_MASK, /*!< SRTC time alarm.*/
  52. } snvs_lp_srtc_interrupts_t;
  53. /*! @brief List of SNVS_LP flags */
  54. typedef enum _snvs_lp_srtc_status_flags
  55. {
  56. kSNVS_SRTC_AlarmInterruptFlag = SNVS_LPSR_LPTA_MASK, /*!< SRTC time alarm flag */
  57. } snvs_lp_srtc_status_flags_t;
  58. /*! @brief List of SNVS_LP external tampers */
  59. typedef enum _snvs_lp_external_tamper
  60. {
  61. kSNVS_ExternalTamper1 = 1U,
  62. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 1)
  63. kSNVS_ExternalTamper2 = 2U,
  64. kSNVS_ExternalTamper3 = 3U,
  65. kSNVS_ExternalTamper4 = 4U,
  66. kSNVS_ExternalTamper5 = 5U,
  67. kSNVS_ExternalTamper6 = 6U,
  68. kSNVS_ExternalTamper7 = 7U,
  69. kSNVS_ExternalTamper8 = 8U,
  70. kSNVS_ExternalTamper9 = 9U,
  71. kSNVS_ExternalTamper10 = 10U
  72. #endif
  73. } snvs_lp_external_tamper_t;
  74. /* define max possible tamper present */
  75. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 1)
  76. #define SNVS_LP_MAX_TAMPER kSNVS_ExternalTamper10
  77. #else
  78. #define SNVS_LP_MAX_TAMPER kSNVS_ExternalTamper1
  79. #endif
  80. /*! @brief List of SNVS_LP external tampers status */
  81. typedef enum _snvs_lp_external_tamper_status
  82. {
  83. kSNVS_TamperNotDetected = 0U,
  84. kSNVS_TamperDetected = 1U
  85. } snvs_lp_external_tamper_status_t;
  86. /*! @brief SNVS_LP external tamper polarity */
  87. typedef enum _snvs_lp_external_tamper_polarity
  88. {
  89. kSNVS_ExternalTamperActiveLow = 0U,
  90. kSNVS_ExternalTamperActiveHigh = 1U
  91. } snvs_lp_external_tamper_polarity_t;
  92. /*! @brief Structure is used to hold the date and time */
  93. typedef struct _snvs_lp_srtc_datetime
  94. {
  95. uint16_t year; /*!< Range from 1970 to 2099.*/
  96. uint8_t month; /*!< Range from 1 to 12.*/
  97. uint8_t day; /*!< Range from 1 to 31 (depending on month).*/
  98. uint8_t hour; /*!< Range from 0 to 23.*/
  99. uint8_t minute; /*!< Range from 0 to 59.*/
  100. uint8_t second; /*!< Range from 0 to 59.*/
  101. } snvs_lp_srtc_datetime_t;
  102. /*!
  103. * @brief SNVS_LP config structure
  104. *
  105. * This structure holds the configuration settings for the SNVS_LP peripheral. To initialize this
  106. * structure to reasonable defaults, call the SNVS_LP_GetDefaultConfig() function and pass a
  107. * pointer to your config structure instance.
  108. *
  109. * The config struct can be made const so it resides in flash
  110. */
  111. typedef struct _snvs_lp_srtc_config
  112. {
  113. bool srtcCalEnable; /*!< true: SRTC calibration mechanism is enabled;
  114. false: No calibration is used */
  115. uint32_t srtcCalValue; /*!< Defines signed calibration value for SRTC;
  116. This is a 5-bit 2's complement value, range from -16 to +15 */
  117. } snvs_lp_srtc_config_t;
  118. /*******************************************************************************
  119. * API
  120. ******************************************************************************/
  121. #if defined(__cplusplus)
  122. extern "C" {
  123. #endif
  124. /*!
  125. * @name Initialization and deinitialization
  126. * @{
  127. */
  128. /*!
  129. * @brief Ungates the SNVS clock and configures the peripheral for basic operation.
  130. *
  131. * @note This API should be called at the beginning of the application using the SNVS driver.
  132. *
  133. * @param base SNVS peripheral base address
  134. * @param config Pointer to the user's SNVS configuration structure.
  135. */
  136. void SNVS_LP_SRTC_Init(SNVS_Type *base, const snvs_lp_srtc_config_t *config);
  137. /*!
  138. * @brief Stops the SRTC timer.
  139. *
  140. * @param base SNVS peripheral base address
  141. */
  142. void SNVS_LP_SRTC_Deinit(SNVS_Type *base);
  143. /*!
  144. * @brief Fills in the SNVS_LP config struct with the default settings.
  145. *
  146. * The default values are as follows.
  147. * @code
  148. * config->srtccalenable = false;
  149. * config->srtccalvalue = 0U;
  150. * @endcode
  151. * @param config Pointer to the user's SNVS configuration structure.
  152. */
  153. void SNVS_LP_SRTC_GetDefaultConfig(snvs_lp_srtc_config_t *config);
  154. /*! @}*/
  155. /*!
  156. * @name Secure RTC (SRTC) current Time & Alarm
  157. * @{
  158. */
  159. /*!
  160. * @brief Sets the SNVS SRTC date and time according to the given time structure.
  161. *
  162. * @param base SNVS peripheral base address
  163. * @param datetime Pointer to the structure where the date and time details are stored.
  164. *
  165. * @return kStatus_Success: Success in setting the time and starting the SNVS SRTC
  166. * kStatus_InvalidArgument: Error because the datetime format is incorrect
  167. */
  168. status_t SNVS_LP_SRTC_SetDatetime(SNVS_Type *base, const snvs_lp_srtc_datetime_t *datetime);
  169. /*!
  170. * @brief Gets the SNVS SRTC time and stores it in the given time structure.
  171. *
  172. * @param base SNVS peripheral base address
  173. * @param datetime Pointer to the structure where the date and time details are stored.
  174. */
  175. void SNVS_LP_SRTC_GetDatetime(SNVS_Type *base, snvs_lp_srtc_datetime_t *datetime);
  176. /*!
  177. * @brief Sets the SNVS SRTC alarm time.
  178. *
  179. * The function sets the SRTC alarm. It also checks whether the specified alarm
  180. * time is greater than the present time. If not, the function does not set the alarm
  181. * and returns an error.
  182. * Please note, that SRTC alarm has limited resolution because only 32 most
  183. * significant bits of SRTC counter are compared to SRTC Alarm register.
  184. * If the alarm time is beyond SRTC resolution, the function does not set the alarm
  185. * and returns an error.
  186. *
  187. * @param base SNVS peripheral base address
  188. * @param alarmTime Pointer to the structure where the alarm time is stored.
  189. *
  190. * @return kStatus_Success: success in setting the SNVS SRTC alarm
  191. * kStatus_InvalidArgument: Error because the alarm datetime format is incorrect
  192. * kStatus_Fail: Error because the alarm time has already passed or is beyond resolution
  193. */
  194. status_t SNVS_LP_SRTC_SetAlarm(SNVS_Type *base, const snvs_lp_srtc_datetime_t *alarmTime);
  195. /*!
  196. * @brief Returns the SNVS SRTC alarm time.
  197. *
  198. * @param base SNVS peripheral base address
  199. * @param datetime Pointer to the structure where the alarm date and time details are stored.
  200. */
  201. void SNVS_LP_SRTC_GetAlarm(SNVS_Type *base, snvs_lp_srtc_datetime_t *datetime);
  202. /*! @}*/
  203. /*!
  204. * @name Interrupt Interface
  205. * @{
  206. */
  207. /*!
  208. * @brief Enables the selected SNVS interrupts.
  209. *
  210. * @param base SNVS peripheral base address
  211. * @param mask The interrupts to enable. This is a logical OR of members of the
  212. * enumeration ::snvs_interrupt_enable_t
  213. */
  214. static inline void SNVS_LP_SRTC_EnableInterrupts(SNVS_Type *base, uint32_t mask)
  215. {
  216. base->LPCR |= mask;
  217. }
  218. /*!
  219. * @brief Disables the selected SNVS interrupts.
  220. *
  221. * @param base SNVS peripheral base address
  222. * @param mask The interrupts to enable. This is a logical OR of members of the
  223. * enumeration ::snvs_interrupt_enable_t
  224. */
  225. static inline void SNVS_LP_SRTC_DisableInterrupts(SNVS_Type *base, uint32_t mask)
  226. {
  227. base->LPCR &= ~mask;
  228. }
  229. /*!
  230. * @brief Gets the enabled SNVS interrupts.
  231. *
  232. * @param base SNVS peripheral base address
  233. *
  234. * @return The enabled interrupts. This is the logical OR of members of the
  235. * enumeration ::snvs_interrupt_enable_t
  236. */
  237. uint32_t SNVS_LP_SRTC_GetEnabledInterrupts(SNVS_Type *base);
  238. /*! @}*/
  239. /*!
  240. * @name Status Interface
  241. * @{
  242. */
  243. /*!
  244. * @brief Gets the SNVS status flags.
  245. *
  246. * @param base SNVS peripheral base address
  247. *
  248. * @return The status flags. This is the logical OR of members of the
  249. * enumeration ::snvs_status_flags_t
  250. */
  251. uint32_t SNVS_LP_SRTC_GetStatusFlags(SNVS_Type *base);
  252. /*!
  253. * @brief Clears the SNVS status flags.
  254. *
  255. * @param base SNVS peripheral base address
  256. * @param mask The status flags to clear. This is a logical OR of members of the
  257. * enumeration ::snvs_status_flags_t
  258. */
  259. static inline void SNVS_LP_SRTC_ClearStatusFlags(SNVS_Type *base, uint32_t mask)
  260. {
  261. base->LPSR |= mask;
  262. }
  263. /*! @}*/
  264. /*!
  265. * @name Timer Start and Stop
  266. * @{
  267. */
  268. /*!
  269. * @brief Starts the SNVS SRTC time counter.
  270. *
  271. * @param base SNVS peripheral base address
  272. */
  273. static inline void SNVS_LP_SRTC_StartTimer(SNVS_Type *base)
  274. {
  275. base->LPCR |= SNVS_LPCR_SRTC_ENV_MASK;
  276. while (!(base->LPCR & SNVS_LPCR_SRTC_ENV_MASK))
  277. {
  278. }
  279. }
  280. /*!
  281. * @brief Stops the SNVS SRTC time counter.
  282. *
  283. * @param base SNVS peripheral base address
  284. */
  285. static inline void SNVS_LP_SRTC_StopTimer(SNVS_Type *base)
  286. {
  287. base->LPCR &= ~SNVS_LPCR_SRTC_ENV_MASK;
  288. while (base->LPCR & SNVS_LPCR_SRTC_ENV_MASK)
  289. {
  290. }
  291. }
  292. /*! @}*/
  293. /*!
  294. * @name External tampering
  295. * @{
  296. */
  297. /*!
  298. * @brief Enables the specified SNVS external tamper.
  299. *
  300. * @param base SNVS peripheral base address
  301. * @param pin SNVS external tamper pin
  302. * @param polarity Polarity of external tamper
  303. */
  304. void SNVS_LP_EnableExternalTamper(SNVS_Type *base,
  305. snvs_lp_external_tamper_t pin,
  306. snvs_lp_external_tamper_polarity_t polarity);
  307. /*!
  308. * @brief Disables the specified SNVS external tamper.
  309. *
  310. * @param base SNVS peripheral base address
  311. * @param pin SNVS external tamper pin
  312. */
  313. void SNVS_LP_DisableExternalTamper(SNVS_Type *base, snvs_lp_external_tamper_t pin);
  314. /*!
  315. * @brief Returns status of the specified external tamper.
  316. *
  317. * @param base SNVS peripheral base address
  318. * @param pin SNVS external tamper pin
  319. *
  320. * @return The status flag. This is the enumeration ::snvs_external_tamper_status_t
  321. */
  322. snvs_lp_external_tamper_status_t SNVS_LP_GetExternalTamperStatus(SNVS_Type *base, snvs_lp_external_tamper_t pin);
  323. /*!
  324. * @brief Clears status of the specified external tamper.
  325. *
  326. * @param base SNVS peripheral base address
  327. * @param pin SNVS external tamper pin
  328. */
  329. void SNVS_LP_ClearExternalTamperStatus(SNVS_Type *base, snvs_lp_external_tamper_t pin);
  330. #if defined(__cplusplus)
  331. }
  332. #endif
  333. /*! @}*/
  334. #endif /* _FSL_SNVS_LP_H_ */