fsl_snvs_lp.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2017-2021, NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_snvs_lp.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.snvs_lp"
  15. #endif
  16. #define SECONDS_IN_A_DAY (86400U)
  17. #define SECONDS_IN_A_HOUR (3600U)
  18. #define SECONDS_IN_A_MINUTE (60U)
  19. #define DAYS_IN_A_YEAR (365U)
  20. #define YEAR_RANGE_START (1970U)
  21. #define YEAR_RANGE_END (2099U)
  22. #define SNVS_DEFAULT_PGD_VALUE (0x41736166U)
  23. /*******************************************************************************
  24. * Prototypes
  25. ******************************************************************************/
  26. /*!
  27. * @brief Checks whether the date and time passed in is valid
  28. *
  29. * @param datetime Pointer to structure where the date and time details are stored
  30. *
  31. * @return Returns false if the date & time details are out of range; true if in range
  32. */
  33. static bool SNVS_LP_CheckDatetimeFormat(const snvs_lp_srtc_datetime_t *datetime);
  34. /*!
  35. * @brief Converts time data from datetime to seconds
  36. *
  37. * @param datetime Pointer to datetime structure where the date and time details are stored
  38. *
  39. * @return The result of the conversion in seconds
  40. */
  41. static uint32_t SNVS_LP_ConvertDatetimeToSeconds(const snvs_lp_srtc_datetime_t *datetime);
  42. /*!
  43. * @brief Converts time data from seconds to a datetime structure
  44. *
  45. * @param seconds Seconds value that needs to be converted to datetime format
  46. * @param datetime Pointer to the datetime structure where the result of the conversion is stored
  47. */
  48. static void SNVS_LP_ConvertSecondsToDatetime(uint32_t seconds, snvs_lp_srtc_datetime_t *datetime);
  49. /*!
  50. * @brief Returns SRTC time in seconds.
  51. *
  52. * This function is used internally to get actual SRTC time in seconds.
  53. *
  54. * @param base SNVS peripheral base address
  55. *
  56. * @return SRTC time in seconds
  57. */
  58. static uint32_t SNVS_LP_SRTC_GetSeconds(SNVS_Type *base);
  59. #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
  60. defined(SNVS_LP_CLOCKS))
  61. /*!
  62. * @brief Get the SNVS instance from peripheral base address.
  63. *
  64. * @param base SNVS peripheral base address.
  65. *
  66. * @return SNVS instance.
  67. */
  68. static uint32_t SNVS_LP_GetInstance(SNVS_Type *base);
  69. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  70. /*******************************************************************************
  71. * Variables
  72. ******************************************************************************/
  73. #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
  74. defined(SNVS_LP_CLOCKS))
  75. /*! @brief Pointer to snvs_lp clock. */
  76. const clock_ip_name_t s_snvsLpClock[] = SNVS_LP_CLOCKS;
  77. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  78. /*******************************************************************************
  79. * Code
  80. ******************************************************************************/
  81. static bool SNVS_LP_CheckDatetimeFormat(const snvs_lp_srtc_datetime_t *datetime)
  82. {
  83. assert(datetime != NULL);
  84. /* Table of days in a month for a non leap year. First entry in the table is not used,
  85. * valid months start from 1
  86. */
  87. uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
  88. /* Check year, month, hour, minute, seconds */
  89. if ((datetime->year < YEAR_RANGE_START) || (datetime->year > YEAR_RANGE_END) || (datetime->month > 12U) ||
  90. (datetime->month < 1U) || (datetime->hour >= 24U) || (datetime->minute >= 60U) || (datetime->second >= 60U))
  91. {
  92. /* If not correct then error*/
  93. return false;
  94. }
  95. /* Adjust the days in February for a leap year */
  96. if ((((datetime->year & 3U) == 0U) && (datetime->year % 100U != 0U)) || (datetime->year % 400U == 0U))
  97. {
  98. daysPerMonth[2] = 29U;
  99. }
  100. /* Check the validity of the day */
  101. if ((datetime->day > daysPerMonth[datetime->month]) || (datetime->day < 1U))
  102. {
  103. return false;
  104. }
  105. return true;
  106. }
  107. static uint32_t SNVS_LP_ConvertDatetimeToSeconds(const snvs_lp_srtc_datetime_t *datetime)
  108. {
  109. assert(datetime != NULL);
  110. /* Number of days from begin of the non Leap-year*/
  111. /* Number of days from begin of the non Leap-year*/
  112. uint16_t monthDays[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U, 212U, 243U, 273U, 304U, 334U};
  113. uint32_t seconds;
  114. /* Compute number of days from 1970 till given year*/
  115. seconds = ((uint32_t)datetime->year - 1970U) * DAYS_IN_A_YEAR;
  116. /* Add leap year days */
  117. seconds += (((uint32_t)datetime->year / 4U) - (1970U / 4U));
  118. /* Add number of days till given month*/
  119. seconds += monthDays[datetime->month];
  120. /* Add days in given month. We subtract the current day as it is
  121. * represented in the hours, minutes and seconds field*/
  122. seconds += ((uint32_t)datetime->day - 1U);
  123. /* For leap year if month less than or equal to Febraury, decrement day counter*/
  124. if ((0U == (datetime->year & 3U)) && (datetime->month <= 2U))
  125. {
  126. seconds--;
  127. }
  128. seconds = (seconds * SECONDS_IN_A_DAY) + (datetime->hour * SECONDS_IN_A_HOUR) +
  129. (datetime->minute * SECONDS_IN_A_MINUTE) + datetime->second;
  130. return seconds;
  131. }
  132. static void SNVS_LP_ConvertSecondsToDatetime(uint32_t seconds, snvs_lp_srtc_datetime_t *datetime)
  133. {
  134. assert(datetime != NULL);
  135. uint32_t x;
  136. uint32_t secondsRemaining, days;
  137. uint16_t daysInYear;
  138. /* Table of days in a month for a non leap year. First entry in the table is not used,
  139. * valid months start from 1
  140. */
  141. uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
  142. /* Start with the seconds value that is passed in to be converted to date time format */
  143. secondsRemaining = seconds;
  144. /* Calcuate the number of days, we add 1 for the current day which is represented in the
  145. * hours and seconds field
  146. */
  147. days = secondsRemaining / SECONDS_IN_A_DAY + 1U;
  148. /* Update seconds left*/
  149. secondsRemaining = secondsRemaining % SECONDS_IN_A_DAY;
  150. /* Calculate the datetime hour, minute and second fields */
  151. datetime->hour = (uint8_t)(secondsRemaining / SECONDS_IN_A_HOUR);
  152. secondsRemaining = secondsRemaining % SECONDS_IN_A_HOUR;
  153. datetime->minute = (uint8_t)(secondsRemaining / 60U);
  154. datetime->second = (uint8_t)(secondsRemaining % SECONDS_IN_A_MINUTE);
  155. /* Calculate year */
  156. daysInYear = DAYS_IN_A_YEAR;
  157. datetime->year = YEAR_RANGE_START;
  158. while (days > daysInYear)
  159. {
  160. /* Decrease day count by a year and increment year by 1 */
  161. days -= daysInYear;
  162. datetime->year++;
  163. /* Adjust the number of days for a leap year */
  164. if ((datetime->year & 3U) != 0U)
  165. {
  166. daysInYear = DAYS_IN_A_YEAR;
  167. }
  168. else
  169. {
  170. daysInYear = DAYS_IN_A_YEAR + 1U;
  171. }
  172. }
  173. /* Adjust the days in February for a leap year */
  174. if (0U == (datetime->year & 3U))
  175. {
  176. daysPerMonth[2] = 29U;
  177. }
  178. for (x = 1U; x <= 12U; x++)
  179. {
  180. if (days <= daysPerMonth[x])
  181. {
  182. datetime->month = (uint8_t)x;
  183. break;
  184. }
  185. else
  186. {
  187. days -= daysPerMonth[x];
  188. }
  189. }
  190. datetime->day = (uint8_t)days;
  191. }
  192. #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
  193. defined(SNVS_LP_CLOCKS))
  194. static uint32_t SNVS_LP_GetInstance(SNVS_Type *base)
  195. {
  196. return 0U;
  197. }
  198. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  199. /*!
  200. * brief Ungates the SNVS clock and configures the peripheral for basic operation.
  201. *
  202. * note This API should be called at the beginning of the application using the SNVS driver.
  203. *
  204. * param base SNVS peripheral base address
  205. * param config Pointer to the user's SNVS configuration structure.
  206. */
  207. void SNVS_LP_Init(SNVS_Type *base)
  208. {
  209. #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
  210. defined(SNVS_LP_CLOCKS))
  211. uint32_t instance = SNVS_LP_GetInstance(base);
  212. CLOCK_EnableClock(s_snvsLpClock[instance]);
  213. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  214. /* Power glitch detector: set the PGD/LVD value and clear the previous status. */
  215. #if defined(SNVS_LPPGDR_PGD)
  216. base->LPPGDR = SNVS_DEFAULT_PGD_VALUE;
  217. base->LPSR = SNVS_LPSR_PGD_MASK;
  218. #elif defined(SNVS_LPLVDR_LVD)
  219. base->LPLVDR = SNVS_DEFAULT_PGD_VALUE;
  220. base->LPSR = SNVS_LPSR_LVD_MASK;
  221. #else
  222. #error "No power/voltage detector register defined"
  223. #endif
  224. }
  225. /*!
  226. * brief Deinit the SNVS LP section.
  227. *
  228. * param base SNVS peripheral base address
  229. */
  230. void SNVS_LP_Deinit(SNVS_Type *base)
  231. {
  232. #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
  233. defined(SNVS_LP_CLOCKS))
  234. uint32_t instance = SNVS_LP_GetInstance(base);
  235. CLOCK_DisableClock(s_snvsLpClock[instance]);
  236. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  237. }
  238. /*!
  239. * brief Ungates the SNVS clock and configures the peripheral for basic operation.
  240. *
  241. * note This API should be called at the beginning of the application using the SNVS driver.
  242. *
  243. * param base SNVS peripheral base address
  244. * param config Pointer to the user's SNVS configuration structure.
  245. */
  246. void SNVS_LP_SRTC_Init(SNVS_Type *base, const snvs_lp_srtc_config_t *config)
  247. {
  248. assert(config != NULL);
  249. #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
  250. defined(SNVS_LP_CLOCKS))
  251. uint32_t instance = SNVS_LP_GetInstance(base);
  252. CLOCK_EnableClock(s_snvsLpClock[instance]);
  253. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  254. if (config->srtcCalEnable)
  255. {
  256. base->LPCR = (base->LPCR & ~SNVS_LPCR_LPCALB_VAL_MASK) | SNVS_LPCR_LPCALB_VAL(config->srtcCalValue);
  257. base->LPCR |= SNVS_LPCR_LPCALB_EN_MASK;
  258. }
  259. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 0)
  260. int pin;
  261. for (pin = (int32_t)kSNVS_ExternalTamper1; pin <= (int32_t)SNVS_LP_MAX_TAMPER; pin++)
  262. {
  263. SNVS_LP_DisableExternalTamper(SNVS, (snvs_lp_external_tamper_t)pin);
  264. SNVS_LP_ClearExternalTamperStatus(SNVS, (snvs_lp_external_tamper_t)pin);
  265. }
  266. #endif /* defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 0) */
  267. }
  268. /*!
  269. * brief Stops the SRTC timer.
  270. *
  271. * param base SNVS peripheral base address
  272. */
  273. void SNVS_LP_SRTC_Deinit(SNVS_Type *base)
  274. {
  275. base->LPCR &= ~SNVS_LPCR_SRTC_ENV_MASK;
  276. #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
  277. defined(SNVS_LP_CLOCKS))
  278. uint32_t instance = SNVS_LP_GetInstance(base);
  279. CLOCK_DisableClock(s_snvsLpClock[instance]);
  280. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  281. }
  282. /*!
  283. * brief Fills in the SNVS_LP config struct with the default settings.
  284. *
  285. * The default values are as follows.
  286. * code
  287. * config->srtccalenable = false;
  288. * config->srtccalvalue = 0U;
  289. * endcode
  290. * param config Pointer to the user's SNVS configuration structure.
  291. */
  292. void SNVS_LP_SRTC_GetDefaultConfig(snvs_lp_srtc_config_t *config)
  293. {
  294. assert(config != NULL);
  295. /* Initializes the configure structure to zero. */
  296. (void)memset(config, 0, sizeof(*config));
  297. config->srtcCalEnable = false;
  298. config->srtcCalValue = 0U;
  299. }
  300. static uint32_t SNVS_LP_SRTC_GetSeconds(SNVS_Type *base)
  301. {
  302. uint32_t seconds = 0;
  303. uint32_t tmp = 0;
  304. /* Do consecutive reads until value is correct */
  305. do
  306. {
  307. seconds = tmp;
  308. tmp = (base->LPSRTCMR << 17U);
  309. tmp |= (base->LPSRTCLR >> 15U);
  310. } while (tmp != seconds);
  311. return seconds;
  312. }
  313. /*!
  314. * brief Sets the SNVS SRTC date and time according to the given time structure.
  315. *
  316. * param base SNVS peripheral base address
  317. * param datetime Pointer to the structure where the date and time details are stored.
  318. *
  319. * return kStatus_Success: Success in setting the time and starting the SNVS SRTC
  320. * kStatus_InvalidArgument: Error because the datetime format is incorrect
  321. */
  322. status_t SNVS_LP_SRTC_SetDatetime(SNVS_Type *base, const snvs_lp_srtc_datetime_t *datetime)
  323. {
  324. assert(datetime != NULL);
  325. uint32_t seconds = 0U;
  326. uint32_t tmp = base->LPCR;
  327. /* disable RTC */
  328. SNVS_LP_SRTC_StopTimer(base);
  329. /* Return error if the time provided is not valid */
  330. if (!(SNVS_LP_CheckDatetimeFormat(datetime)))
  331. {
  332. return kStatus_InvalidArgument;
  333. }
  334. /* Set time in seconds */
  335. seconds = SNVS_LP_ConvertDatetimeToSeconds(datetime);
  336. base->LPSRTCMR = (uint32_t)(seconds >> 17U);
  337. base->LPSRTCLR = (uint32_t)(seconds << 15U);
  338. /* reenable SRTC in case that it was enabled before */
  339. if ((tmp & SNVS_LPCR_SRTC_ENV_MASK) != 0U)
  340. {
  341. SNVS_LP_SRTC_StartTimer(base);
  342. }
  343. return kStatus_Success;
  344. }
  345. /*!
  346. * brief Gets the SNVS SRTC time and stores it in the given time structure.
  347. *
  348. * param base SNVS peripheral base address
  349. * param datetime Pointer to the structure where the date and time details are stored.
  350. */
  351. void SNVS_LP_SRTC_GetDatetime(SNVS_Type *base, snvs_lp_srtc_datetime_t *datetime)
  352. {
  353. assert(datetime != NULL);
  354. SNVS_LP_ConvertSecondsToDatetime(SNVS_LP_SRTC_GetSeconds(base), datetime);
  355. }
  356. /*!
  357. * brief Sets the SNVS SRTC alarm time.
  358. *
  359. * The function sets the SRTC alarm. It also checks whether the specified alarm
  360. * time is greater than the present time. If not, the function does not set the alarm
  361. * and returns an error.
  362. * Please note, that SRTC alarm has limited resolution because only 32 most
  363. * significant bits of SRTC counter are compared to SRTC Alarm register.
  364. * If the alarm time is beyond SRTC resolution, the function does not set the alarm
  365. * and returns an error.
  366. *
  367. * param base SNVS peripheral base address
  368. * param alarmTime Pointer to the structure where the alarm time is stored.
  369. *
  370. * return kStatus_Success: success in setting the SNVS SRTC alarm
  371. * kStatus_InvalidArgument: Error because the alarm datetime format is incorrect
  372. * kStatus_Fail: Error because the alarm time has already passed or is beyond resolution
  373. */
  374. status_t SNVS_LP_SRTC_SetAlarm(SNVS_Type *base, const snvs_lp_srtc_datetime_t *alarmTime)
  375. {
  376. assert(alarmTime != NULL);
  377. uint32_t alarmSeconds = 0U;
  378. uint32_t currSeconds = 0U;
  379. uint32_t tmp = base->LPCR;
  380. /* Return error if the alarm time provided is not valid */
  381. if (!(SNVS_LP_CheckDatetimeFormat(alarmTime)))
  382. {
  383. return kStatus_InvalidArgument;
  384. }
  385. alarmSeconds = SNVS_LP_ConvertDatetimeToSeconds(alarmTime);
  386. currSeconds = SNVS_LP_SRTC_GetSeconds(base);
  387. /* Return error if the alarm time has passed */
  388. if (alarmSeconds <= currSeconds)
  389. {
  390. return kStatus_Fail;
  391. }
  392. /* disable SRTC alarm interrupt */
  393. base->LPCR &= ~SNVS_LPCR_LPTA_EN_MASK;
  394. while ((base->LPCR & SNVS_LPCR_LPTA_EN_MASK) != 0U)
  395. {
  396. }
  397. /* Set alarm in seconds*/
  398. base->LPTAR = alarmSeconds;
  399. /* reenable SRTC alarm interrupt in case that it was enabled before */
  400. base->LPCR = tmp;
  401. return kStatus_Success;
  402. }
  403. /*!
  404. * brief Returns the SNVS SRTC alarm time.
  405. *
  406. * param base SNVS peripheral base address
  407. * param datetime Pointer to the structure where the alarm date and time details are stored.
  408. */
  409. void SNVS_LP_SRTC_GetAlarm(SNVS_Type *base, snvs_lp_srtc_datetime_t *datetime)
  410. {
  411. assert(datetime != NULL);
  412. uint32_t alarmSeconds = 0U;
  413. /* Get alarm in seconds */
  414. alarmSeconds = base->LPTAR;
  415. SNVS_LP_ConvertSecondsToDatetime(alarmSeconds, datetime);
  416. }
  417. /*!
  418. * brief Gets the SNVS status flags.
  419. *
  420. * param base SNVS peripheral base address
  421. *
  422. * return The status flags. This is the logical OR of members of the
  423. * enumeration ::snvs_status_flags_t
  424. */
  425. uint32_t SNVS_LP_SRTC_GetStatusFlags(SNVS_Type *base)
  426. {
  427. uint32_t flags = 0U;
  428. if ((base->LPSR & SNVS_LPSR_LPTA_MASK) != 0U)
  429. {
  430. flags |= (uint32_t)kSNVS_SRTC_AlarmInterruptFlag;
  431. }
  432. return flags;
  433. }
  434. /*!
  435. * brief Gets the enabled SNVS interrupts.
  436. *
  437. * param base SNVS peripheral base address
  438. *
  439. * return The enabled interrupts. This is the logical OR of members of the
  440. * enumeration ::snvs_interrupt_enable_t
  441. */
  442. uint32_t SNVS_LP_SRTC_GetEnabledInterrupts(SNVS_Type *base)
  443. {
  444. uint32_t val = 0U;
  445. if ((base->LPCR & SNVS_LPCR_LPTA_EN_MASK) != 0U)
  446. {
  447. val |= (uint32_t)kSNVS_SRTC_AlarmInterrupt;
  448. }
  449. return val;
  450. }
  451. /*!
  452. * brief Fills in the SNVS tamper pin config struct with the default settings.
  453. *
  454. * The default values are as follows.
  455. * code
  456. * config->polarity = 0U;
  457. * config->filterenable = 0U;
  458. * config->filter = 0U;
  459. * endcode
  460. * param config Pointer to the user's SNVS configuration structure.
  461. */
  462. void SNVS_LP_PassiveTamperPin_GetDefaultConfig(snvs_lp_passive_tamper_t *config)
  463. {
  464. assert(config != NULL);
  465. /* Initializes the configure structure to zero. */
  466. (void)memset(config, 0, sizeof(*config));
  467. config->polarity = 0U;
  468. #if defined(FSL_FEATURE_SNVS_PASSIVE_TAMPER_FILTER) && (FSL_FEATURE_SNVS_PASSIVE_TAMPER_FILTER > 0)
  469. config->filterenable = 0U;
  470. config->filter = 0U;
  471. #endif /* FSL_FEATURE_SNVS_PASSIVE_TAMPER_FILTER */
  472. }
  473. #if defined(FSL_FEATURE_SNVS_HAS_ACTIVE_TAMPERS) && (FSL_FEATURE_SNVS_HAS_ACTIVE_TAMPERS > 0)
  474. /*!
  475. * brief Fills in the SNVS tamper pin config struct with the default settings.
  476. *
  477. * The default values are as follows.
  478. * code
  479. * config->clock = kSNVS_ActiveTamper16HZ;
  480. * config->seed = 0U;
  481. * config->polynomial = 0U;
  482. * endcode
  483. * param config Pointer to the user's SNVS configuration structure.
  484. */
  485. void SNVS_LP_TamperPinTx_GetDefaultConfig(tamper_active_tx_config_t *config)
  486. {
  487. assert(config != NULL);
  488. /* Initializes the configure structure to zero. */
  489. (void)memset(config, 0, sizeof(*config));
  490. config->clock = kSNVS_ActiveTamper16HZ;
  491. config->seed = 0U;
  492. config->polynomial = 0U;
  493. }
  494. /*!
  495. * brief Fills in the SNVS tamper pin config struct with the default settings.
  496. *
  497. * The default values are as follows.
  498. * code
  499. * config->filterenable = 0U;
  500. * config->filter = 0U;
  501. * config->tx = kSNVS_ActiveTamper1;
  502. * endcode
  503. * param config Pointer to the user's SNVS configuration structure.
  504. */
  505. void SNVS_LP_TamperPinRx_GetDefaultConfig(tamper_active_rx_config_t *config)
  506. {
  507. assert(config != NULL);
  508. /* Initializes the configure structure to zero. */
  509. (void)memset(config, 0, sizeof(*config));
  510. config->filterenable = 0U;
  511. config->filter = 0U;
  512. config->activeTamper = kSNVS_ActiveTamper1;
  513. }
  514. #endif /* FSL_FEATURE_SNVS_HAS_ACTIVE_TAMPERS */
  515. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 0)
  516. /*!
  517. * brief Enables the specified SNVS external tamper.
  518. *
  519. * param base SNVS peripheral base address
  520. * param pin SNVS external tamper pin
  521. * param config Configuration structure of external passive tamper
  522. */
  523. void SNVS_LP_EnablePassiveTamper(SNVS_Type *base, snvs_lp_external_tamper_t pin, snvs_lp_passive_tamper_t config)
  524. {
  525. switch (pin)
  526. {
  527. case (kSNVS_ExternalTamper1):
  528. /* Set polarity */
  529. if (config.polarity != 0U)
  530. {
  531. SNVS->LPTDCR |= SNVS_LPTDCR_ET1P_MASK;
  532. }
  533. else
  534. {
  535. SNVS->LPTDCR &= ~SNVS_LPTDCR_ET1P_MASK;
  536. }
  537. #if defined(FSL_FEATURE_SNVS_PASSIVE_TAMPER_FILTER) && (FSL_FEATURE_SNVS_PASSIVE_TAMPER_FILTER > 0)
  538. /* Enable filter and set it's value, dissable otherwise */
  539. if (config.filterenable != 0U)
  540. {
  541. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF1_EN_MASK;
  542. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF1(config.filter);
  543. }
  544. else
  545. {
  546. SNVS->LPTGFCR &= ~SNVS_LPTGFCR_ETGF1_EN_MASK;
  547. }
  548. #endif /* FSL_FEATURE_SNVS_PASSIVE_TAMPER_FILTER */
  549. /* enable tamper pin */
  550. base->LPTDCR |= SNVS_LPTDCR_ET1_EN_MASK;
  551. break;
  552. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 1)
  553. case (kSNVS_ExternalTamper2):
  554. /* Set polarity */
  555. base->LPTDCR =
  556. (base->LPTDCR & ~(SNVS_LPTDCR_ET2P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDCR_ET2P_SHIFT);
  557. /* Enable filter and set it's value, dissable otherwise */
  558. if (config.filterenable != 0U)
  559. {
  560. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF2_EN_MASK;
  561. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF2(config.filter);
  562. }
  563. else
  564. {
  565. SNVS->LPTGFCR &= ~SNVS_LPTGFCR_ETGF2_EN_MASK;
  566. }
  567. /* enable tamper pin */
  568. SNVS->LPTDCR |= SNVS_LPTDCR_ET2_EN_MASK;
  569. break;
  570. case (kSNVS_ExternalTamper3):
  571. /* Set polarity */
  572. base->LPTDC2R =
  573. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET3P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET3P_SHIFT);
  574. /* Enable filter and set it's value if set */
  575. if (config.filterenable != 0U)
  576. {
  577. /* Set filter value */
  578. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF3(config.filter);
  579. /* Enable tamper 3 glitch filter */
  580. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF3_EN(1U);
  581. }
  582. /* enable tamper pin */
  583. base->LPTDC2R |= SNVS_LPTDC2R_ET3_EN_MASK;
  584. break;
  585. case (kSNVS_ExternalTamper4):
  586. /* Set polarity */
  587. base->LPTDC2R =
  588. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET4P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET4P_SHIFT);
  589. /* Enable filter and set it's value if set */
  590. if (config.filterenable != 0U)
  591. {
  592. /* Set filter value */
  593. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF4(config.filter);
  594. /* Enable tamper 4 glitch filter */
  595. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF4_EN(1U);
  596. }
  597. /* enable tamper pin */
  598. base->LPTDC2R |= SNVS_LPTDC2R_ET4_EN_MASK;
  599. break;
  600. case (kSNVS_ExternalTamper5):
  601. /* Set polarity */
  602. base->LPTDC2R =
  603. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET5P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET5P_SHIFT);
  604. /* Enable filter and set it's value if set */
  605. if (config.filterenable != 0U)
  606. {
  607. /* Set filter value */
  608. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF5(config.filter);
  609. /* Enable tamper 5 glitch filter */
  610. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF5_EN(1U);
  611. }
  612. /* enable tamper pin */
  613. base->LPTDC2R |= SNVS_LPTDC2R_ET5_EN_MASK;
  614. break;
  615. case (kSNVS_ExternalTamper6):
  616. /* Set polarity */
  617. base->LPTDC2R =
  618. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET6P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET6P_SHIFT);
  619. /* Enable filter and set it's value if set */
  620. if (config.filterenable != 0U)
  621. {
  622. /* Set filter value */
  623. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF6(config.filter);
  624. /* Enable tamper 6 glitch filter */
  625. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF6_EN(1U);
  626. }
  627. /* enable tamper pin */
  628. base->LPTDC2R |= SNVS_LPTDC2R_ET6_EN_MASK;
  629. break;
  630. case (kSNVS_ExternalTamper7):
  631. /* Set polarity */
  632. base->LPTDC2R =
  633. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET7P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET7P_SHIFT);
  634. /* Enable filter and set it's value if set */
  635. if (config.filterenable != 0U)
  636. {
  637. /* Set filter value */
  638. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF7(config.filter);
  639. /* Enable tamper 6 glitch filter */
  640. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF7_EN(1U);
  641. }
  642. /* enable tamper pin */
  643. base->LPTDC2R |= SNVS_LPTDC2R_ET7_EN_MASK;
  644. break;
  645. case (kSNVS_ExternalTamper8):
  646. /* Set polarity */
  647. base->LPTDC2R =
  648. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET8P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET8P_SHIFT);
  649. /* Enable filter and set it's value if set */
  650. if (config.filterenable != 0U)
  651. {
  652. /* Set filter value */
  653. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF8(config.filter);
  654. /* Enable tamper 8 glitch filter */
  655. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF8_EN(1U);
  656. }
  657. /* enable tamper pin */
  658. base->LPTDC2R |= SNVS_LPTDC2R_ET8_EN_MASK;
  659. break;
  660. case (kSNVS_ExternalTamper9):
  661. /* Set polarity */
  662. base->LPTDC2R =
  663. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET9P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET9P_SHIFT);
  664. /* Enable filter and set it's value if set */
  665. if (config.filterenable != 0U)
  666. {
  667. /* Set filter value */
  668. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF9(config.filter);
  669. /* Enable tamper 9 glitch filter */
  670. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF9_EN(1U);
  671. }
  672. /* enable tamper pin */
  673. base->LPTDC2R |= SNVS_LPTDC2R_ET9_EN_MASK;
  674. break;
  675. case (kSNVS_ExternalTamper10):
  676. /* Set polarity */
  677. base->LPTDC2R =
  678. (base->LPTDC2R & ~(SNVS_LPTDC2R_ET10P_MASK)) | ((uint32_t)config.polarity << SNVS_LPTDC2R_ET10P_SHIFT);
  679. /* Enable filter and set it's value if set */
  680. if (config.filterenable != 0U)
  681. {
  682. /* Set filter value */
  683. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF10(config.filter);
  684. /* Enable tamper 10 glitch filter */
  685. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF10_EN(1U);
  686. }
  687. /* enable tamper pin */
  688. base->LPTDC2R |= SNVS_LPTDC2R_ET10_EN_MASK;
  689. break;
  690. #endif
  691. default:
  692. /* All the cases have been listed above, the default clause should not be reached. */
  693. break;
  694. }
  695. }
  696. /*!
  697. * brief Disables the specified SNVS external tamper.
  698. *
  699. * param base SNVS peripheral base address
  700. * param pin SNVS external tamper pin
  701. */
  702. void SNVS_LP_DisableExternalTamper(SNVS_Type *base, snvs_lp_external_tamper_t pin)
  703. {
  704. switch (pin)
  705. {
  706. case (kSNVS_ExternalTamper1):
  707. base->LPTDCR &= ~SNVS_LPTDCR_ET1_EN_MASK;
  708. break;
  709. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 1)
  710. case (kSNVS_ExternalTamper2):
  711. base->LPTDCR &= ~SNVS_LPTDCR_ET2_EN_MASK;
  712. break;
  713. case (kSNVS_ExternalTamper3):
  714. base->LPTDC2R &= ~SNVS_LPTDC2R_ET3_EN_MASK;
  715. break;
  716. case (kSNVS_ExternalTamper4):
  717. base->LPTDC2R &= ~SNVS_LPTDC2R_ET4_EN_MASK;
  718. break;
  719. case (kSNVS_ExternalTamper5):
  720. base->LPTDC2R &= ~SNVS_LPTDC2R_ET5_EN_MASK;
  721. break;
  722. case (kSNVS_ExternalTamper6):
  723. base->LPTDC2R &= ~SNVS_LPTDC2R_ET6_EN_MASK;
  724. break;
  725. case (kSNVS_ExternalTamper7):
  726. base->LPTDC2R &= ~SNVS_LPTDC2R_ET7_EN_MASK;
  727. break;
  728. case (kSNVS_ExternalTamper8):
  729. base->LPTDC2R &= ~SNVS_LPTDC2R_ET8_EN_MASK;
  730. break;
  731. case (kSNVS_ExternalTamper9):
  732. base->LPTDC2R &= ~SNVS_LPTDC2R_ET9_EN_MASK;
  733. break;
  734. case (kSNVS_ExternalTamper10):
  735. base->LPTDC2R &= ~SNVS_LPTDC2R_ET10_EN_MASK;
  736. break;
  737. #endif
  738. default:
  739. /* All the cases have been listed above, the default clause should not be reached. */
  740. break;
  741. }
  742. }
  743. /*!
  744. * brief Disable all external tamper.
  745. *
  746. * param base SNVS peripheral base address
  747. */
  748. void SNVS_LP_DisableAllExternalTamper(SNVS_Type *base)
  749. {
  750. for (int pin = (int8_t)kSNVS_ExternalTamper1; pin <= (int8_t)SNVS_LP_MAX_TAMPER; pin++)
  751. {
  752. SNVS_LP_DisableExternalTamper(SNVS, (snvs_lp_external_tamper_t)pin);
  753. }
  754. }
  755. /*!
  756. * brief Returns status of the specified external tamper.
  757. *
  758. * param base SNVS peripheral base address
  759. * param pin SNVS external tamper pin
  760. *
  761. * return The status flag. This is the enumeration ::snvs_external_tamper_status_t
  762. */
  763. snvs_lp_external_tamper_status_t SNVS_LP_GetExternalTamperStatus(SNVS_Type *base, snvs_lp_external_tamper_t pin)
  764. {
  765. snvs_lp_external_tamper_status_t status = kSNVS_TamperNotDetected;
  766. switch (pin)
  767. {
  768. case (kSNVS_ExternalTamper1):
  769. status = (bool)(base->LPSR & SNVS_LPSR_ET1D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  770. break;
  771. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 1)
  772. case (kSNVS_ExternalTamper2):
  773. status = (bool)(base->LPSR & SNVS_LPSR_ET2D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  774. break;
  775. case (kSNVS_ExternalTamper3):
  776. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET3D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  777. break;
  778. case (kSNVS_ExternalTamper4):
  779. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET4D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  780. break;
  781. case (kSNVS_ExternalTamper5):
  782. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET5D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  783. break;
  784. case (kSNVS_ExternalTamper6):
  785. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET6D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  786. break;
  787. case (kSNVS_ExternalTamper7):
  788. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET7D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  789. break;
  790. case (kSNVS_ExternalTamper8):
  791. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET8D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  792. break;
  793. case (kSNVS_ExternalTamper9):
  794. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET9D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  795. break;
  796. case (kSNVS_ExternalTamper10):
  797. status = (bool)(base->LPTDSR & SNVS_LPTDSR_ET10D_MASK) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  798. break;
  799. #endif
  800. default:
  801. /* All the cases have been listed above, the default clause should not be reached. */
  802. break;
  803. }
  804. return status;
  805. }
  806. /*!
  807. * brief Clears status of the specified external tamper.
  808. *
  809. * param base SNVS peripheral base address
  810. * param pin SNVS external tamper pin
  811. */
  812. void SNVS_LP_ClearExternalTamperStatus(SNVS_Type *base, snvs_lp_external_tamper_t pin)
  813. {
  814. base->LPSR |= SNVS_LPSR_ET1D_MASK;
  815. switch (pin)
  816. {
  817. case (kSNVS_ExternalTamper1):
  818. base->LPSR |= SNVS_LPSR_ET1D_MASK;
  819. break;
  820. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 1)
  821. case (kSNVS_ExternalTamper2):
  822. base->LPSR |= SNVS_LPSR_ET2D_MASK;
  823. break;
  824. case (kSNVS_ExternalTamper3):
  825. base->LPTDSR |= SNVS_LPTDSR_ET3D_MASK;
  826. break;
  827. case (kSNVS_ExternalTamper4):
  828. base->LPTDSR |= SNVS_LPTDSR_ET4D_MASK;
  829. break;
  830. case (kSNVS_ExternalTamper5):
  831. base->LPTDSR |= SNVS_LPTDSR_ET5D_MASK;
  832. break;
  833. case (kSNVS_ExternalTamper6):
  834. base->LPTDSR |= SNVS_LPTDSR_ET6D_MASK;
  835. break;
  836. case (kSNVS_ExternalTamper7):
  837. base->LPTDSR |= SNVS_LPTDSR_ET7D_MASK;
  838. break;
  839. case (kSNVS_ExternalTamper8):
  840. base->LPTDSR |= SNVS_LPTDSR_ET8D_MASK;
  841. break;
  842. case (kSNVS_ExternalTamper9):
  843. base->LPTDSR |= SNVS_LPTDSR_ET9D_MASK;
  844. break;
  845. case (kSNVS_ExternalTamper10):
  846. base->LPTDSR |= SNVS_LPTDSR_ET10D_MASK;
  847. break;
  848. #endif
  849. default:
  850. /* All the cases have been listed above, the default clause should not be reached. */
  851. break;
  852. }
  853. }
  854. /*!
  855. * brief Clears status of the all external tamper.
  856. *
  857. * param base SNVS peripheral base address
  858. */
  859. void SNVS_LP_ClearAllExternalTamperStatus(SNVS_Type *base)
  860. {
  861. for (int pin = (int8_t)kSNVS_ExternalTamper1; pin <= (int8_t)SNVS_LP_MAX_TAMPER; pin++)
  862. {
  863. SNVS_LP_ClearExternalTamperStatus(SNVS, (snvs_lp_external_tamper_t)pin);
  864. }
  865. }
  866. #endif /* (!(defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 0)) */
  867. #if defined(FSL_FEATURE_SNVS_HAS_ACTIVE_TAMPERS) && (FSL_FEATURE_SNVS_HAS_ACTIVE_TAMPERS > 0)
  868. /*!
  869. * brief Enable active tamper tx external pad
  870. *
  871. * param base SNVS peripheral base address
  872. * param pin SNVS external tamper pin
  873. */
  874. status_t SNVS_LP_EnableTxActiveTamper(SNVS_Type *base, snvs_lp_active_tx_tamper_t pin, tamper_active_tx_config_t config)
  875. {
  876. status_t status = kStatus_Success;
  877. switch (pin)
  878. {
  879. case (kSNVS_ActiveTamper1):
  880. {
  881. /* Enable active tamper tx external pad */
  882. base->LPATCTLR |= SNVS_LPATCTLR_AT1_PAD_EN_MASK;
  883. /* Set seed and polynomial */
  884. base->LPATCR[0] |= SNVS_LPATCR_Seed(config.seed) | SNVS_LPATCR_Polynomial(config.polynomial);
  885. /* Set clock */
  886. base->LPATCLKR |= SNVS_LPATCLKR_AT1_CLK_CTL(config.clock);
  887. /* Enable active tamper pin */
  888. base->LPATCTLR |= SNVS_LPATCTLR_AT1_EN_MASK;
  889. break;
  890. }
  891. case (kSNVS_ActiveTamper2):
  892. {
  893. base->LPATCTLR |= SNVS_LPATCTLR_AT2_PAD_EN_MASK;
  894. base->LPATCR[1] |= SNVS_LPATCR_Seed(config.seed) | SNVS_LPATCR_Polynomial(config.polynomial);
  895. base->LPATCLKR |= SNVS_LPATCLKR_AT2_CLK_CTL(config.clock);
  896. base->LPATCTLR |= SNVS_LPATCTLR_AT2_EN_MASK;
  897. break;
  898. }
  899. case (kSNVS_ActiveTamper3):
  900. {
  901. base->LPATCTLR |= SNVS_LPATCTLR_AT3_PAD_EN_MASK;
  902. base->LPATCR[2] |= SNVS_LPATCR_Seed(config.seed) | SNVS_LPATCR_Polynomial(config.polynomial);
  903. base->LPATCLKR |= SNVS_LPATCLKR_AT3_CLK_CTL(config.clock);
  904. base->LPATCTLR |= SNVS_LPATCTLR_AT3_EN_MASK;
  905. break;
  906. }
  907. case (kSNVS_ActiveTamper4):
  908. {
  909. base->LPATCTLR |= SNVS_LPATCTLR_AT4_PAD_EN_MASK;
  910. base->LPATCR[3] |= SNVS_LPATCR_Seed(config.seed) | SNVS_LPATCR_Polynomial(config.polynomial);
  911. base->LPATCLKR |= SNVS_LPATCLKR_AT4_CLK_CTL(config.clock);
  912. base->LPATCTLR |= SNVS_LPATCTLR_AT4_EN_MASK;
  913. break;
  914. }
  915. case (kSNVS_ActiveTamper5):
  916. {
  917. base->LPATCTLR |= SNVS_LPATCTLR_AT5_PAD_EN_MASK;
  918. base->LPATCR[4] |= SNVS_LPATCR_Seed(config.seed) | SNVS_LPATCR_Polynomial(config.polynomial);
  919. base->LPATCLKR |= SNVS_LPATCLKR_AT5_CLK_CTL(config.clock);
  920. base->LPATCTLR |= SNVS_LPATCTLR_AT5_EN_MASK;
  921. break;
  922. }
  923. default:
  924. status = kStatus_InvalidArgument;
  925. /* All the cases have been listed above, the default clause should not be reached. */
  926. break;
  927. }
  928. return status;
  929. }
  930. /*!
  931. * brief Enable active tamper rx external pad
  932. *
  933. * param base SNVS peripheral base address
  934. * param rx SNVS external RX tamper pin
  935. * param config SNVS RX tamper config structure
  936. */
  937. status_t SNVS_LP_EnableRxActiveTamper(SNVS_Type *base, snvs_lp_external_tamper_t rx, tamper_active_rx_config_t config)
  938. {
  939. status_t status = kStatus_Success;
  940. switch (rx)
  941. {
  942. case (kSNVS_ExternalTamper1):
  943. /* Enable filter and set it's value, dissable otherwise */
  944. if (config.filterenable != 0U)
  945. {
  946. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF1_EN_MASK;
  947. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF1(config.filter);
  948. }
  949. else
  950. {
  951. SNVS->LPTGFCR &= ~SNVS_LPTGFCR_ETGF1_EN_MASK;
  952. }
  953. /* Route TX to external tamper 1 */
  954. base->LPATRC1R = SNVS_LPATRC1R_ET1RCTL(config.activeTamper);
  955. /* enable tamper pin */
  956. base->LPTDCR |= SNVS_LPTDCR_ET1_EN_MASK;
  957. break;
  958. #if defined(FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER) && (FSL_FEATURE_SNVS_HAS_MULTIPLE_TAMPER > 1)
  959. case (kSNVS_ExternalTamper2):
  960. /* Enable filter and set it's value, dissable otherwise */
  961. if (config.filterenable != 0U)
  962. {
  963. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF2_EN_MASK;
  964. SNVS->LPTGFCR |= SNVS_LPTGFCR_ETGF2(config.filter);
  965. }
  966. else
  967. {
  968. SNVS->LPTGFCR &= ~SNVS_LPTGFCR_ETGF2_EN_MASK;
  969. }
  970. /* Route TX to external tamper 2 */
  971. base->LPATRC1R = SNVS_LPATRC1R_ET2RCTL(config.activeTamper);
  972. /* enable tamper pin */
  973. SNVS->LPTDCR |= SNVS_LPTDCR_ET2_EN_MASK;
  974. break;
  975. case (kSNVS_ExternalTamper3):
  976. /* Enable filter and set it's value if set */
  977. if (config.filterenable != 0U)
  978. {
  979. /* Set filter value */
  980. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF3(config.filter);
  981. /* Enable tamper 3 glitch filter */
  982. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF3_EN(1U);
  983. }
  984. /* Route TX to external tamper 3 */
  985. base->LPATRC1R = SNVS_LPATRC1R_ET3RCTL(config.activeTamper);
  986. /* enable tamper pin */
  987. base->LPTDC2R |= SNVS_LPTDC2R_ET3_EN_MASK;
  988. break;
  989. case (kSNVS_ExternalTamper4):
  990. /* Enable filter and set it's value if set */
  991. if (config.filterenable != 0U)
  992. {
  993. /* Set filter value */
  994. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF4(config.filter);
  995. /* Enable tamper 4 glitch filter */
  996. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF4_EN(1U);
  997. }
  998. /* Route TX to external tamper 4 */
  999. base->LPATRC1R = SNVS_LPATRC1R_ET4RCTL(config.activeTamper);
  1000. /* enable tamper pin */
  1001. base->LPTDC2R |= SNVS_LPTDC2R_ET4_EN_MASK;
  1002. break;
  1003. case (kSNVS_ExternalTamper5):
  1004. /* Enable filter and set it's value if set */
  1005. if (config.filterenable != 0U)
  1006. {
  1007. /* Set filter value */
  1008. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF5(config.filter);
  1009. /* Enable tamper 5 glitch filter */
  1010. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF5_EN(1U);
  1011. }
  1012. /* Route TX to external tamper 5 */
  1013. base->LPATRC1R = SNVS_LPATRC1R_ET5RCTL(config.activeTamper);
  1014. /* enable tamper pin */
  1015. base->LPTDC2R |= SNVS_LPTDC2R_ET5_EN_MASK;
  1016. break;
  1017. case (kSNVS_ExternalTamper6):
  1018. /* Enable filter and set it's value if set */
  1019. if (config.filterenable != 0U)
  1020. {
  1021. /* Set filter value */
  1022. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF6(config.filter);
  1023. /* Enable tamper 6 glitch filter */
  1024. SNVS->LPTGF1CR |= SNVS_LPTGF1CR_ETGF6_EN(1U);
  1025. }
  1026. /* Route TX to external tamper 6 */
  1027. base->LPATRC1R = SNVS_LPATRC1R_ET6RCTL(config.activeTamper);
  1028. /* enable tamper pin */
  1029. base->LPTDC2R |= SNVS_LPTDC2R_ET6_EN_MASK;
  1030. break;
  1031. case (kSNVS_ExternalTamper7):
  1032. /* Enable filter and set it's value if set */
  1033. if (config.filterenable != 0U)
  1034. {
  1035. /* Set filter value */
  1036. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF7(config.filter);
  1037. /* Enable tamper 6 glitch filter */
  1038. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF7_EN(1U);
  1039. }
  1040. /* Route TX to external tamper 7 */
  1041. base->LPATRC1R = SNVS_LPATRC1R_ET7RCTL(config.activeTamper);
  1042. /* enable tamper pin */
  1043. base->LPTDC2R |= SNVS_LPTDC2R_ET7_EN_MASK;
  1044. break;
  1045. case (kSNVS_ExternalTamper8):
  1046. /* Enable filter and set it's value if set */
  1047. if (config.filterenable != 0U)
  1048. {
  1049. /* Set filter value */
  1050. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF8(config.filter);
  1051. /* Enable tamper 8 glitch filter */
  1052. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF8_EN(1U);
  1053. }
  1054. /* Route TX to external tamper 8 */
  1055. base->LPATRC1R = SNVS_LPATRC1R_ET8RCTL(config.activeTamper);
  1056. /* enable tamper pin */
  1057. base->LPTDC2R |= SNVS_LPTDC2R_ET8_EN_MASK;
  1058. break;
  1059. case (kSNVS_ExternalTamper9):
  1060. /* Enable filter and set it's value if set */
  1061. if (config.filterenable != 0U)
  1062. {
  1063. /* Set filter value */
  1064. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF9(config.filter);
  1065. /* Enable tamper 9 glitch filter */
  1066. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF9_EN(1U);
  1067. }
  1068. /* Route TX to external tamper 9 */
  1069. base->LPATRC1R = SNVS_LPATRC2R_ET9RCTL(config.activeTamper);
  1070. /* enable tamper pin */
  1071. base->LPTDC2R |= SNVS_LPTDC2R_ET9_EN_MASK;
  1072. break;
  1073. case (kSNVS_ExternalTamper10):
  1074. /* Enable filter and set it's value if set */
  1075. if (config.filterenable != 0U)
  1076. {
  1077. /* Set filter value */
  1078. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF10(config.filter);
  1079. /* Enable tamper 10 glitch filter */
  1080. SNVS->LPTGF2CR |= SNVS_LPTGF2CR_ETGF10_EN(1U);
  1081. }
  1082. /* Route TX to external tamper 10 */
  1083. base->LPATRC1R = SNVS_LPATRC2R_ET10RCTL(config.activeTamper);
  1084. /* enable tamper pin */
  1085. base->LPTDC2R |= SNVS_LPTDC2R_ET10_EN_MASK;
  1086. break;
  1087. #endif
  1088. default:
  1089. status = kStatus_InvalidArgument;
  1090. /* All the cases have been listed above, the default clause should not be reached. */
  1091. break;
  1092. }
  1093. return status;
  1094. }
  1095. /*!
  1096. * brief Sets voltage tamper detect
  1097. *
  1098. * param base SNVS peripheral base address
  1099. * param enable True if enable false if disable
  1100. */
  1101. status_t SNVS_LP_SetVoltageTamper(SNVS_Type *base, bool enable)
  1102. {
  1103. base->LPTDCR |= SNVS_LPTDCR_VT_EN(enable);
  1104. return kStatus_Success;
  1105. }
  1106. /*!
  1107. * brief Sets temperature tamper detect
  1108. *
  1109. * param base SNVS peripheral base address
  1110. * param enable True if enable false if disable
  1111. */
  1112. status_t SNVS_LP_SetTemperatureTamper(SNVS_Type *base, bool enable)
  1113. {
  1114. SNVS->LPTDCR |= SNVS_LPTDCR_TT_EN(enable);
  1115. return kStatus_Success;
  1116. }
  1117. /*!
  1118. * brief Sets clock tamper detect
  1119. *
  1120. * param base SNVS peripheral base address
  1121. * param enable True if enable false if disable
  1122. */
  1123. status_t SNVS_LP_SetClockTamper(SNVS_Type *base, bool enable)
  1124. {
  1125. SNVS->LPTDCR |= SNVS_LPTDCR_CT_EN(enable);
  1126. return kStatus_Success;
  1127. }
  1128. /*!
  1129. * brief Check voltage tamper
  1130. *
  1131. * param base SNVS peripheral base address
  1132. */
  1133. snvs_lp_external_tamper_status_t SNVS_LP_CheckVoltageTamper(SNVS_Type *base)
  1134. {
  1135. return ((SNVS->LPSR & SNVS_LPSR_VTD_MASK) != 0U) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  1136. }
  1137. /*!
  1138. * brief Check temperature tamper
  1139. *
  1140. * param base SNVS peripheral base address
  1141. */
  1142. snvs_lp_external_tamper_status_t SNVS_LP_CheckTemperatureTamper(SNVS_Type *base)
  1143. {
  1144. return ((SNVS->LPSR & SNVS_LPSR_TTD_MASK) != 0U) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  1145. }
  1146. /*!
  1147. * brief Check clock tamper
  1148. *
  1149. * param base SNVS peripheral base address
  1150. */
  1151. snvs_lp_external_tamper_status_t SNVS_LP_CheckClockTamper(SNVS_Type *base)
  1152. {
  1153. return ((SNVS->LPSR & SNVS_LPSR_CTD_MASK) != 0U) ? kSNVS_TamperDetected : kSNVS_TamperNotDetected;
  1154. }
  1155. #endif /* defined(FSL_FEATURE_SNVS_HAS_ACTIVE_TAMPERS) && (FSL_FEATURE_SNVS_HAS_ACTIVE_TAMPERS > 0) */
  1156. /*!
  1157. * brief Get the current Monotonic Counter.
  1158. *
  1159. * param base SNVS peripheral base address
  1160. * return Current Monotonic Counter value.
  1161. */
  1162. uint64_t SNVS_LP_GetMonotonicCounter(SNVS_Type *base)
  1163. {
  1164. uint32_t mc_lsb, mc_msb;
  1165. mc_msb = base->LPSMCMR;
  1166. mc_lsb = base->LPSMCLR;
  1167. return ((uint64_t)mc_msb << 32UL) | (uint64_t)mc_lsb;
  1168. }
  1169. /*!
  1170. * brief Write Zeroizable Master Key (ZMK) to the SNVS registers.
  1171. *
  1172. * param base SNVS peripheral base address
  1173. * param ZMKey The ZMK write to the SNVS register.
  1174. */
  1175. void SNVS_LP_WriteZeroizableMasterKey(SNVS_Type *base, uint32_t ZMKey[SNVS_ZMK_REG_COUNT])
  1176. {
  1177. uint8_t i = 0;
  1178. for (i = 0; i < SNVS_ZMK_REG_COUNT; i++)
  1179. {
  1180. base->LPZMKR[i] = ZMKey[i];
  1181. }
  1182. }
  1183. #if defined(FSL_FEATURE_SNVS_HAS_STATE_TRANSITION) && (FSL_FEATURE_SNVS_HAS_STATE_TRANSITION > 0)
  1184. /*!
  1185. * brief Transition SNVS SSM state to Trusted/Non-secure from Check state
  1186. *
  1187. * param base SNVS peripheral base address
  1188. *
  1189. * return kStatus_Success: Success in transitioning SSM State
  1190. * kStatus_Fail: SSM State transition failed
  1191. */
  1192. status_t SNVS_LP_SSM_State_Transition(SNVS_Type *base)
  1193. {
  1194. uint32_t curr_ssm_state = ((base->HPSR & SNVS_HPSR_SSM_STATE_MASK) >> SNVS_HPSR_SSM_STATE_SHIFT);
  1195. uint32_t sec_config = ((OCOTP_CTRL->HW_OCOTP_OTFAD_CFG3 & OCOTP_CTRL_HW_OCOTP_SEC_CONFIG1_MASK) >>
  1196. OCOTP_CTRL_HW_OCOTP_SEC_CONFIG1_SHIFT);
  1197. /* Check if SSM State is Check state */
  1198. if (curr_ssm_state == SNVS_SSM_STATE_CHECK)
  1199. {
  1200. if (sec_config == SEC_CONFIG_OPEN)
  1201. {
  1202. /* Transition to Non-secure state */
  1203. base->HPCOMR |= SNVS_HPCOMR_SW_SV(1);
  1204. }
  1205. else
  1206. {
  1207. /* Transition to Trusted state */
  1208. base->HPCOMR |= SNVS_HPCOMR_SSM_ST(1);
  1209. }
  1210. }
  1211. uint32_t new_ssm_state = ((base->HPSR & SNVS_HPSR_SSM_STATE_MASK) >> SNVS_HPSR_SSM_STATE_SHIFT);
  1212. if (new_ssm_state != SNVS_SSM_STATE_CHECK)
  1213. {
  1214. return kStatus_Success;
  1215. }
  1216. else
  1217. {
  1218. return kStatus_Fail;
  1219. }
  1220. }
  1221. #endif /* FSL_FEATURE_SNVS_HAS_STATE_TRANSITION */