fsl_snvs_lp.h 13 KB

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