Clock_Ip_Monitor.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral :
  5. * Dependencies : none
  6. *
  7. * Autosar Version : 4.4.0
  8. * Autosar Revision : ASR_REL_4_4_REV_0000
  9. * Autosar Conf.Variant :
  10. * SW Version : 1.0.0
  11. * Build Version : S32K1_RTD_1_0_0_HF01_D2109_ASR_REL_4_4_REV_0000_20210907
  12. *
  13. * (c) Copyright 2020-2021 NXP Semiconductors
  14. * All Rights Reserved.
  15. *
  16. * NXP Confidential. This software is owned or controlled by NXP and may only be
  17. * used strictly in accordance with the applicable license terms. By expressly
  18. * accepting such terms or by downloading, installing, activating and/or otherwise
  19. * using the software, you are agreeing that you have read, and that you agree to
  20. * comply with and are bound by, such license terms. If you do not agree to be
  21. * bound by the applicable license terms, then you may not retain, install,
  22. * activate or otherwise use the software.
  23. ==================================================================================================*/
  24. /**
  25. * @file Clock_Ip_Monitor.c
  26. * @version 1.0.0
  27. *
  28. * @brief CLOCK driver implementations.
  29. * @details CLOCK driver implementations.
  30. *
  31. * @addtogroup CLOCK_DRIVER Clock Ip Driver
  32. * @{
  33. */
  34. #include "Clock_Ip_Private.h"
  35. #include "SchM_Mcu.h"
  36. /*==================================================================================================
  37. SOURCE FILE VERSION INFORMATION
  38. ==================================================================================================*/
  39. #define CLOCK_IP_MONITOR_VENDOR_ID_C 43
  40. #define CLOCK_IP_MONITOR_AR_RELEASE_MAJOR_VERSION_C 4
  41. #define CLOCK_IP_MONITOR_AR_RELEASE_MINOR_VERSION_C 4
  42. #define CLOCK_IP_MONITOR_AR_RELEASE_REVISION_VERSION_C 0
  43. #define CLOCK_IP_MONITOR_SW_MAJOR_VERSION_C 1
  44. #define CLOCK_IP_MONITOR_SW_MINOR_VERSION_C 0
  45. #define CLOCK_IP_MONITOR_SW_PATCH_VERSION_C 0
  46. /*==================================================================================================
  47. * FILE VERSION CHECKS
  48. ==================================================================================================*/
  49. /* Check if Clock_Ip_Monitor.c file and Clock_Ip_Private.h file are of the same vendor */
  50. #if (CLOCK_IP_MONITOR_VENDOR_ID_C != CLOCK_IP_PRIVATE_VENDOR_ID)
  51. #error "Clock_Ip_IntOsc.c and Clock_Ip_Private.h have different vendor ids"
  52. #endif
  53. /* Check if Clock_Ip_Monitor.c file and Clock_Ip_Private.h file are of the same Autosar version */
  54. #if ((CLOCK_IP_MONITOR_AR_RELEASE_MAJOR_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_MAJOR_VERSION) || \
  55. (CLOCK_IP_MONITOR_AR_RELEASE_MINOR_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_MINOR_VERSION) || \
  56. (CLOCK_IP_MONITOR_AR_RELEASE_REVISION_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_REVISION_VERSION) \
  57. )
  58. #error "AutoSar Version Numbers of Clock_Ip_Monitor.c and Clock_Ip_Private.h are different"
  59. #endif
  60. /* Check if Clock_Ip_Monitor.c file and Clock_Ip_Private.h file are of the same Software version */
  61. #if ((CLOCK_IP_MONITOR_SW_MAJOR_VERSION_C != CLOCK_IP_PRIVATE_SW_MAJOR_VERSION) || \
  62. (CLOCK_IP_MONITOR_SW_MINOR_VERSION_C != CLOCK_IP_PRIVATE_SW_MINOR_VERSION) || \
  63. (CLOCK_IP_MONITOR_SW_PATCH_VERSION_C != CLOCK_IP_PRIVATE_SW_PATCH_VERSION) \
  64. )
  65. #error "Software Version Numbers of Clock_Ip_Monitor.c and Clock_Ip_Private.h are different"
  66. #endif
  67. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  68. /* Check if Clock_Ip_Monitor.c file and SchM_Mcu.h file are of the same Autosar version */
  69. #if ((CLOCK_IP_MONITOR_AR_RELEASE_MAJOR_VERSION_C != SCHM_MCU_AR_RELEASE_MAJOR_VERSION) || \
  70. (CLOCK_IP_MONITOR_AR_RELEASE_MINOR_VERSION_C != SCHM_MCU_AR_RELEASE_MINOR_VERSION))
  71. #error "AutoSar Version Numbers of Clock_Ip_Monitor.c and SchM_Mcu.h are different"
  72. #endif
  73. #endif
  74. /*==================================================================================================
  75. LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  76. ==================================================================================================*/
  77. /*==================================================================================================
  78. * LOCAL MACROS
  79. ==================================================================================================*/
  80. #ifdef CMU_FC_FCE_REF_CNT_LFREF_HFREF
  81. #define CMU_REFERENCE_COUNTER_MINIMUM_VALUE_MULTIPLIER 10U
  82. #define CMU_FC_VAR 3U
  83. #define CMU_REFERENCE_CLOCK_VARIATION 33U
  84. #define CMU_MONITORED_CLOCK_VARIATION 11U
  85. #define DIVIDE_BY_1000000 1000000U
  86. #define DIVIDE_BY_100000 100000U
  87. #define DIVIDE_BY_1000 1000U
  88. #if defined(FEATURE_OFFSET_REFERENCE_COUNT_FORMULA1)
  89. #define OFFSET_REFERENCE_COUNT_FORMULA1 FEATURE_OFFSET_REFERENCE_COUNT_FORMULA1
  90. #else
  91. #define OFFSET_REFERENCE_COUNT_FORMULA1 1U
  92. #endif
  93. #if defined(FEATURE_MULTIPLIER_REFERENCE_COUNT_FORMULA1)
  94. #define MULTIPLIER_REFERENCE_COUNT_FORMULA1 FEATURE_MULTIPLIER_REFERENCE_COUNT_FORMULA1
  95. #else
  96. #define MULTIPLIER_REFERENCE_COUNT_FORMULA1 3U
  97. #endif
  98. #if defined(FEATURE_OFFSET_REFERENCE_COUNT_FORMULA2)
  99. #define OFFSET_REFERENCE_COUNT_FORMULA2 FEATURE_OFFSET_REFERENCE_COUNT_FORMULA2
  100. #else
  101. #define OFFSET_REFERENCE_COUNT_FORMULA2 9U
  102. #endif
  103. #if defined(FEATURE_MULTIPLIER_REFERENCE_COUNT_FORMULA2)
  104. #define MULTIPLIER_REFERENCE_COUNT_FORMULA2 FEATURE_MULTIPLIER_REFERENCE_COUNT_FORMULA2
  105. #else
  106. #define MULTIPLIER_REFERENCE_COUNT_FORMULA2 5U
  107. #endif
  108. #endif
  109. /*==================================================================================================
  110. LOCAL CONSTANTS
  111. ==================================================================================================*/
  112. /*==================================================================================================
  113. LOCAL VARIABLES
  114. ==================================================================================================*/
  115. /*==================================================================================================
  116. GLOBAL CONSTANTS
  117. ==================================================================================================*/
  118. /*==================================================================================================
  119. GLOBAL VARIABLES
  120. ==================================================================================================*/
  121. /* Clock start section code */
  122. #define MCU_START_SEC_CODE
  123. #include "Mcu_MemMap.h"
  124. /* TODO ARTD-738 Implement CMU in Clock_Ip driver */
  125. static void ClockMonitorEmpty(Clock_Ip_CmuConfigType const* config);
  126. static void ClockMonitorEmpty_Disable(Clock_Ip_NameType name);
  127. static void ClockMonitorEmpty_ClearStatus(Clock_Ip_NameType name);
  128. static Clock_Ip_CmuStatusType ClockMonitorEmpty_GetStatus(Clock_Ip_NameType name);
  129. #ifdef CMU_FC_FCE_REF_CNT_LFREF_HFREF
  130. static void ResetCmuFcFceRefCntLfrefHfref(Clock_Ip_CmuConfigType const* config);
  131. static void SetCmuFcFceRefCntLfrefHfref(Clock_Ip_CmuConfigType const* config);
  132. static void DisableCmuFcFceRefCntLfrefHfref(Clock_Ip_NameType name);
  133. static void ClearStatusCmuFcFceRefCntLfrefHfref(Clock_Ip_NameType name);
  134. static Clock_Ip_CmuStatusType GetStatusCmuFcFceRefCntLfrefHfref(Clock_Ip_NameType name);
  135. static void EnableCmuFcFceRefCntLfrefHfref(Clock_Ip_CmuConfigType const* config);
  136. #endif
  137. /* Clock stop section code */
  138. #define MCU_STOP_SEC_CODE
  139. #include "Mcu_MemMap.h"
  140. /* Clock start constant section data */
  141. #define MCU_START_SEC_CONST_UNSPECIFIED
  142. #include "Mcu_MemMap.h"
  143. const clockMonitorCallback cmuCallbacks[CMU_CALLBACKS_COUNT] =
  144. {
  145. {
  146. ClockMonitorEmpty, /* Reset */
  147. ClockMonitorEmpty, /* Set */
  148. ClockMonitorEmpty_Disable, /* Disable */
  149. ClockMonitorEmpty_ClearStatus, /* Clear */
  150. ClockMonitorEmpty_GetStatus, /* Get status */
  151. ClockMonitorEmpty, /* Enable */
  152. },
  153. #ifdef CMU_FC_FCE_REF_CNT_LFREF_HFREF
  154. {
  155. ResetCmuFcFceRefCntLfrefHfref, /* Reset */
  156. SetCmuFcFceRefCntLfrefHfref, /* Set */
  157. DisableCmuFcFceRefCntLfrefHfref, /* Disable */
  158. ClearStatusCmuFcFceRefCntLfrefHfref, /* Clear */
  159. GetStatusCmuFcFceRefCntLfrefHfref, /* Get status */
  160. EnableCmuFcFceRefCntLfrefHfref, /* Enable */
  161. },
  162. #endif
  163. };
  164. /* Clock stop constant section data */
  165. #define MCU_STOP_SEC_CONST_UNSPECIFIED
  166. #include "Mcu_MemMap.h"
  167. /* Clock start section code */
  168. #define MCU_START_SEC_CODE
  169. #include "Mcu_MemMap.h"
  170. static void ClockMonitorEmpty(Clock_Ip_CmuConfigType const* config)
  171. {
  172. (void)config;
  173. /* No implementation */
  174. }
  175. static void ClockMonitorEmpty_Disable(Clock_Ip_NameType name)
  176. {
  177. (void)name;
  178. /* No implementation */
  179. }
  180. static void ClockMonitorEmpty_ClearStatus(Clock_Ip_NameType name)
  181. {
  182. (void)name;
  183. /* No implementation */
  184. }
  185. static Clock_Ip_CmuStatusType ClockMonitorEmpty_GetStatus(Clock_Ip_NameType name)
  186. {
  187. (void)name;
  188. /* No implementation */
  189. return CLOCK_IP_CMU_STATUS_UNDEFINED;
  190. }
  191. #ifdef CMU_FC_FCE_REF_CNT_LFREF_HFREF
  192. static void DisableCmuFcFceRefCntLfrefHfref(Clock_Ip_NameType name)
  193. {
  194. const cmuInfoType * cmuInformation = &cmuInfo[clockFeatures[name][CMU_INDEX]];
  195. volatile ClockMonitor_Type* const cmu_fc = cmuInformation->cmuInstance;
  196. boolean TimeoutOccurred = FALSE;
  197. uint32 StartTime;
  198. uint32 ElapsedTime;
  199. uint32 TimeoutTicks;
  200. uint32 FrequencyCheckStatus;
  201. #if defined(S32K3XX)
  202. /* Check clock status for CMU */
  203. if (((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U))
  204. {
  205. /* Enable clock for CMU device */
  206. IP_MC_ME->PRTN1_COFB1_CLKEN |= MC_ME_PRTN1_COFB1_CLKEN_REQ47(1U); /* REQ47: Clock monitor unit */
  207. IP_MC_ME->PRTN1_PCONF |= MC_ME_PRTN1_PCONF_PCE_MASK; /* PCE=1: Enable the clock to Partition #1 */
  208. IP_MC_ME->PRTN1_PUPD |= MC_ME_PRTN1_PUPD_PCUD_MASK; /* PCUD=1: Trigger the hardware process */
  209. McMeEnterKey();
  210. /* Wait until CMU clock is running */
  211. ClockStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, CLOCK_TIMEOUT_VALUE_US);
  212. do
  213. {
  214. TimeoutOccurred = ClockTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  215. }
  216. while(((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U) && (FALSE == TimeoutOccurred));
  217. /* timeout notification */
  218. if (TRUE == TimeoutOccurred)
  219. {
  220. /* Report timeout error */
  221. ReportClockErrors(CLOCK_IP_REPORT_TIMEOUT_ERROR, RESERVED_CLK);
  222. }
  223. }
  224. #endif
  225. #if (defined (CLOCK_IP_S32K118) || defined(CLOCK_IP_S32K116))
  226. if(((IP_PCC->PCCn[62] & PCC_PCCn_CGC_MASK) >> PCC_PCCn_CGC_SHIFT) == 0U)
  227. {
  228. /* Enable clock gate for CMU0 device */
  229. IP_PCC->PCCn[62] |= PCC_PCCn_CGC_MASK;
  230. }
  231. if(((IP_PCC->PCCn[63] & PCC_PCCn_CGC_MASK) >> PCC_PCCn_CGC_SHIFT) == 0U)
  232. {
  233. /* Enable clock gate for CMU1 device */
  234. IP_PCC->PCCn[63] |= PCC_PCCn_CGC_MASK;
  235. }
  236. #endif
  237. /* Enter critical region*/
  238. SchM_Enter_Mcu_MCU_EXCLUSIVE_AREA_01();
  239. /* Only disable frequency check if it is enabled */
  240. if ((cmu_fc->GCR & CMU_FC_GCR_FCE_MASK) == CMU_FREQUENCY_CHECK_ENABLED)
  241. {
  242. ClockStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, CLOCK_TIMEOUT_VALUE_US);
  243. /* Wait for frequency check to be running. */
  244. do
  245. {
  246. FrequencyCheckStatus = (cmu_fc->SR & CMU_FC_SR_RS_MASK);
  247. TimeoutOccurred = ClockTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  248. }
  249. while((FrequencyCheckStatus == CMU_FREQUENCY_CHECK_STOPPED) && (!TimeoutOccurred));
  250. /* timeout notification */
  251. if (TimeoutOccurred)
  252. {
  253. /* Report timeout error */
  254. ReportClockErrors(CLOCK_IP_REPORT_TIMEOUT_ERROR, name);
  255. }
  256. else
  257. {
  258. /* Disable frequency check */
  259. cmu_fc->GCR &= ~CMU_FC_GCR_FCE_MASK;
  260. }
  261. }
  262. /* Disable interupts */
  263. cmu_fc->IER &= ~(CMU_FC_IER_FLLIE_MASK | CMU_FC_IER_FHHIE_MASK | CMU_FC_IER_FLLAIE_MASK | CMU_FC_IER_FHHAIE_MASK);
  264. /* Clear flags */
  265. cmu_fc->SR |= (CMU_FC_SR_FLL_MASK | CMU_FC_SR_FHH_MASK | CMU_FC_SR_RS_MASK);
  266. /* Exit critical region. */
  267. SchM_Exit_Mcu_MCU_EXCLUSIVE_AREA_01();
  268. }
  269. static void ResetCmuFcFceRefCntLfrefHfref(Clock_Ip_CmuConfigType const* config)
  270. {
  271. DisableCmuFcFceRefCntLfrefHfref(config->name);
  272. }
  273. static void SetCmuFcFceRefCntLfrefHfref(Clock_Ip_CmuConfigType const* config)
  274. {
  275. const cmuInfoType * cmuInformation = &cmuInfo[clockFeatures[config->name][CMU_INDEX]];
  276. volatile ClockMonitor_Type* const cmu_fc = cmuInformation->cmuInstance;
  277. static uint32 hash[CMU_INFO_SIZE];
  278. uint32 fReferenceClk = 0U, fMonitoredClk = 0U, fBusClk = 0U;
  279. uint32 refCount = 0U, hfRef = 0U, lfRef = 0U;
  280. uint32 cmp1, cmp2;
  281. uint32 divideBy, dividerResult, moduloValue;
  282. #if defined(S32K3XX)
  283. boolean TimeoutOccurred = FALSE;
  284. uint32 StartTime;
  285. uint32 ElapsedTime;
  286. uint32 TimeoutTicks;
  287. /* Check clock status for CMU */
  288. if (((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U))
  289. {
  290. /* Enable clock for CMU device */
  291. IP_MC_ME->PRTN1_COFB1_CLKEN |= MC_ME_PRTN1_COFB1_CLKEN_REQ47(1U); /* REQ47: Clock monitor unit */
  292. IP_MC_ME->PRTN1_PCONF |= MC_ME_PRTN1_PCONF_PCE_MASK; /* PCE=1: Enable the clock to Partition #1 */
  293. IP_MC_ME->PRTN1_PUPD |= MC_ME_PRTN1_PUPD_PCUD_MASK; /* PCUD=1: Trigger the hardware process */
  294. McMeEnterKey();
  295. /* Wait until CMU clock is running */
  296. ClockStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, CLOCK_TIMEOUT_VALUE_US);
  297. do
  298. {
  299. TimeoutOccurred = ClockTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  300. }
  301. while(((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U) && (FALSE == TimeoutOccurred));
  302. /* timeout notification */
  303. if (TRUE == TimeoutOccurred)
  304. {
  305. /* Report timeout error */
  306. ReportClockErrors(CLOCK_IP_REPORT_TIMEOUT_ERROR, RESERVED_CLK);
  307. }
  308. }
  309. #endif
  310. /* Do not calculate cmu values if these values are already calculated and written in hw registers */
  311. if (hash[clockFeatures[config->name][CMU_INDEX]] != ((((uint32)config->enable) ^ ((uint32)config->interrupt) ^ ((uint32)config->monitoredClockFrequency) ^ ((uint32)config->name))))
  312. {
  313. hash[clockFeatures[config->name][CMU_INDEX]] = ((((uint32)config->enable) ^ ((uint32)config->interrupt) ^ ((uint32)config->monitoredClockFrequency) ^ ((uint32)config->name)));
  314. switch(cmuInformation->reference)
  315. {
  316. #if defined(FEATURE_CLOCK_IP_HAS_FIRC_CLK)
  317. case FIRC_CLK:
  318. {
  319. fReferenceClk = GetConfiguredFrequencyValue(FIRC_CLK) / DIVIDE_BY_1000;
  320. break;
  321. }
  322. #endif
  323. #if defined(FEATURE_CLOCK_IP_HAS_FXOSC_CLK)
  324. case FXOSC_CLK:
  325. {
  326. fReferenceClk = GetConfiguredFrequencyValue(FXOSC_CLK) / DIVIDE_BY_1000;
  327. break;
  328. }
  329. #endif
  330. #if defined(FEATURE_CLOCK_IP_HAS_SIRC_CLK)
  331. case SIRC_CLK:
  332. {
  333. fReferenceClk = GetConfiguredFrequencyValue(SIRC_CLK) / DIVIDE_BY_1000;
  334. break;
  335. }
  336. #endif
  337. default:
  338. fReferenceClk = 0U;
  339. break;
  340. }
  341. fMonitoredClk = config->monitoredClockFrequency / DIVIDE_BY_1000;
  342. switch(cmuInformation->bus)
  343. {
  344. #if defined(FEATURE_CLOCK_IP_HAS_XBAR_DIV3_CLK)
  345. case XBAR_DIV3_CLK:
  346. {
  347. fBusClk = GetConfiguredFrequencyValue(XBAR_DIV3_CLK) / DIVIDE_BY_1000;
  348. break;
  349. }
  350. #endif
  351. #if defined(FEATURE_CLOCK_IP_HAS_AIPS_SLOW_CLK)
  352. case AIPS_SLOW_CLK:
  353. {
  354. fBusClk = GetConfiguredFrequencyValue(AIPS_SLOW_CLK) / DIVIDE_BY_1000;
  355. break;
  356. }
  357. #endif
  358. #if defined(FEATURE_CLOCK_IP_HAS_BUS_CLK)
  359. case BUS_CLK:
  360. {
  361. if((IP_SMC->PMSTAT & SMC_PMSTAT_PMSTAT_MASK) == 1U)
  362. {
  363. fBusClk = GetConfiguredFrequencyValue(BUS_RUN_CLK) / DIVIDE_BY_1000;
  364. }
  365. else
  366. {
  367. fBusClk = GetConfiguredFrequencyValue(BUS_VLPR_CLK) / DIVIDE_BY_1000;
  368. }
  369. break;
  370. }
  371. #endif
  372. default:
  373. fBusClk = 0U;
  374. break;
  375. }
  376. #if (defined(CLOCK_IP_DEV_ERROR_DETECT) && (CLOCK_IP_DEV_ERROR_DETECT == STD_ON))
  377. CLOCK_DEV_ASSERT(fReferenceClk != 0U);
  378. CLOCK_DEV_ASSERT(fBusClk != 0U);
  379. CLOCK_DEV_ASSERT(fMonitoredClk != 0U);
  380. #endif
  381. /* Avoid divide by zero */
  382. if ((fReferenceClk != 0U) && (fBusClk != 0U) && (fMonitoredClk != 0U))
  383. {
  384. /* cmp1 = ceiling of (3 * fRef/ fBus) */
  385. cmp1 = OFFSET_REFERENCE_COUNT_FORMULA1 + (uint32)((MULTIPLIER_REFERENCE_COUNT_FORMULA1 * fReferenceClk) / fBusClk);
  386. /* cmp2 = ceiling of (8 + (5 * fRef / fMonitor)) */
  387. cmp2 = OFFSET_REFERENCE_COUNT_FORMULA2 + (uint32)((MULTIPLIER_REFERENCE_COUNT_FORMULA2 * fReferenceClk) / fMonitoredClk);
  388. /* REF count = Max(cmp1,cmp2) */
  389. refCount = (cmp1 > cmp2) ? cmp1 : cmp2;
  390. /* (fMonitoredClk mul_by (1000U plus CMU_MONITORED_CLOCK_VARIATION)) divide_by (fReferenceClk mul_by (1000U minus CMU_REFERENCE_CLOCK_VARIATION)) plus CMU_FC_VAR plus 1U */
  391. hfRef = fMonitoredClk * (1000U + CMU_MONITORED_CLOCK_VARIATION) * refCount;
  392. divideBy = fReferenceClk * (1000U - CMU_REFERENCE_CLOCK_VARIATION);
  393. dividerResult = (uint32) hfRef / divideBy;
  394. moduloValue = hfRef - (divideBy * dividerResult);
  395. hfRef = (dividerResult * CMU_REFERENCE_COUNTER_MINIMUM_VALUE_MULTIPLIER) + ((uint32)((moduloValue * CMU_REFERENCE_COUNTER_MINIMUM_VALUE_MULTIPLIER) / divideBy)) + (CMU_FC_VAR + 1U);
  396. /* (fMonitoredClk mul_by (1000U minus CMU_MONITORED_CLOCK_VARIATION)) divide_by (fReferenceClk mul_by (1000U plus CMU_REFERENCE_CLOCK_VARIATION)) minus CMU_FC_VAR */
  397. lfRef = fMonitoredClk * (1000U - CMU_MONITORED_CLOCK_VARIATION) * refCount;
  398. divideBy = fReferenceClk * (1000U + CMU_REFERENCE_CLOCK_VARIATION);
  399. dividerResult = (uint32) lfRef / divideBy;
  400. moduloValue = lfRef - (divideBy * dividerResult);
  401. lfRef = (dividerResult * CMU_REFERENCE_COUNTER_MINIMUM_VALUE_MULTIPLIER) + ((uint32)((moduloValue * CMU_REFERENCE_COUNTER_MINIMUM_VALUE_MULTIPLIER) / divideBy)) - CMU_FC_VAR;
  402. /* Set reference counter */
  403. cmu_fc->RCCR = refCount * CMU_REFERENCE_COUNTER_MINIMUM_VALUE_MULTIPLIER;
  404. /* Set high limit */
  405. cmu_fc->HTCR = hfRef;
  406. /* Set low limit */
  407. cmu_fc->LTCR = lfRef;
  408. }
  409. }
  410. /* Enable/disable interrupts */
  411. cmu_fc->IER = config->interrupt;
  412. }
  413. static void ClearStatusCmuFcFceRefCntLfrefHfref(Clock_Ip_NameType name)
  414. {
  415. const cmuInfoType * cmuInformation = &cmuInfo[clockFeatures[name][CMU_INDEX]];
  416. volatile ClockMonitor_Type* const cmu_fc = cmuInformation->cmuInstance;
  417. uint32 cmuIsrValue;
  418. #if defined(S32K3XX)
  419. boolean TimeoutOccurred = FALSE;
  420. uint32 StartTime;
  421. uint32 ElapsedTime;
  422. uint32 TimeoutTicks;
  423. /* Check clock status for CMU */
  424. if (((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U))
  425. {
  426. /* Enable clock for CMU device */
  427. IP_MC_ME->PRTN1_COFB1_CLKEN |= MC_ME_PRTN1_COFB1_CLKEN_REQ47(1U); /* REQ47: Clock monitor unit */
  428. IP_MC_ME->PRTN1_PCONF |= MC_ME_PRTN1_PCONF_PCE_MASK; /* PCE=1: Enable the clock to Partition #1 */
  429. IP_MC_ME->PRTN1_PUPD |= MC_ME_PRTN1_PUPD_PCUD_MASK; /* PCUD=1: Trigger the hardware process */
  430. McMeEnterKey();
  431. /* Wait until CMU clock is running */
  432. ClockStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, CLOCK_TIMEOUT_VALUE_US);
  433. do
  434. {
  435. TimeoutOccurred = ClockTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  436. }
  437. while(((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U) && (FALSE == TimeoutOccurred));
  438. /* timeout notification */
  439. if (TRUE == TimeoutOccurred)
  440. {
  441. /* Report timeout error */
  442. ReportClockErrors(CLOCK_IP_REPORT_TIMEOUT_ERROR, RESERVED_CLK);
  443. }
  444. }
  445. #endif
  446. /* Read flags */
  447. cmuIsrValue = cmu_fc->SR & CMU_ISR_MASK;
  448. /* Clear flags */
  449. cmu_fc->SR = cmuIsrValue;
  450. }
  451. static Clock_Ip_CmuStatusType GetStatusCmuFcFceRefCntLfrefHfref(Clock_Ip_NameType name)
  452. {
  453. const cmuInfoType * cmuInformation = &cmuInfo[clockFeatures[name][CMU_INDEX]];
  454. volatile const ClockMonitor_Type* cmu_fc = cmuInformation->cmuInstance;
  455. Clock_Ip_CmuStatusType status = CLOCK_IP_CMU_STATUS_UNDEFINED;
  456. uint32 cmuIerValue, cmuIsrValue;
  457. #if defined(S32K3XX)
  458. boolean TimeoutOccurred = FALSE;
  459. uint32 StartTime;
  460. uint32 ElapsedTime;
  461. uint32 TimeoutTicks;
  462. /* Check clock status for CMU */
  463. if (((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U))
  464. {
  465. /* Enable clock for CMU device */
  466. IP_MC_ME->PRTN1_COFB1_CLKEN |= MC_ME_PRTN1_COFB1_CLKEN_REQ47(1U); /* REQ47: Clock monitor unit */
  467. IP_MC_ME->PRTN1_PCONF |= MC_ME_PRTN1_PCONF_PCE_MASK; /* PCE=1: Enable the clock to Partition #1 */
  468. IP_MC_ME->PRTN1_PUPD |= MC_ME_PRTN1_PUPD_PCUD_MASK; /* PCUD=1: Trigger the hardware process */
  469. McMeEnterKey();
  470. /* Wait until CMU clock is running */
  471. ClockStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, CLOCK_TIMEOUT_VALUE_US);
  472. do
  473. {
  474. TimeoutOccurred = ClockTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  475. }
  476. while(((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U) && (FALSE == TimeoutOccurred));
  477. /* timeout notification */
  478. if (TRUE == TimeoutOccurred)
  479. {
  480. /* Report timeout error */
  481. ReportClockErrors(CLOCK_IP_REPORT_TIMEOUT_ERROR, RESERVED_CLK);
  482. }
  483. }
  484. #endif
  485. /* Read flags */
  486. cmuIsrValue = cmu_fc->SR & CMU_ISR_MASK;
  487. /* Read interrupt enable */
  488. cmuIerValue = cmu_fc->IER & CMU_ISR_MASK;
  489. cmuIsrValue = cmuIsrValue & cmuIerValue;
  490. /* Gheck flash if frequency check is enabled */
  491. if ((cmu_fc->GCR & CMU_FC_GCR_FCE_MASK) == CMU_FREQUENCY_CHECK_ENABLED)
  492. {
  493. if ( ( CMU_FC_SR_FHH_MASK == (cmuIsrValue & CMU_FC_SR_FHH_MASK) ) )
  494. {
  495. status = CLOCK_IP_CMU_HIGH_FREQ;
  496. }
  497. else if ( CMU_FC_SR_FLL_MASK == (cmuIsrValue & CMU_FC_SR_FLL_MASK) )
  498. {
  499. status = CLOCK_IP_CMU_LOW_FREQ;
  500. }
  501. else
  502. {
  503. status = CLOCK_IP_CMU_IN_RANGE;
  504. }
  505. }
  506. return status;
  507. }
  508. static void EnableCmuFcFceRefCntLfrefHfref(Clock_Ip_CmuConfigType const* config)
  509. {
  510. const cmuInfoType *cmuInformation = &cmuInfo[clockFeatures[config->name][CMU_INDEX]];
  511. volatile ClockMonitor_Type* const cmu_fc = cmuInformation->cmuInstance;
  512. #if defined(S32K3XX)
  513. boolean TimeoutOccurred = FALSE;
  514. uint32 StartTime;
  515. uint32 ElapsedTime;
  516. uint32 TimeoutTicks;
  517. /* Check clock status for CMU */
  518. if (((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U))
  519. {
  520. /* Enable clock for CMU device */
  521. IP_MC_ME->PRTN1_COFB1_CLKEN |= MC_ME_PRTN1_COFB1_CLKEN_REQ47(1U); /* REQ47: Clock monitor unit */
  522. IP_MC_ME->PRTN1_PCONF |= MC_ME_PRTN1_PCONF_PCE_MASK; /* PCE=1: Enable the clock to Partition #1 */
  523. IP_MC_ME->PRTN1_PUPD |= MC_ME_PRTN1_PUPD_PCUD_MASK; /* PCUD=1: Trigger the hardware process */
  524. McMeEnterKey();
  525. /* Wait until CMU clock is running */
  526. ClockStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, CLOCK_TIMEOUT_VALUE_US);
  527. do
  528. {
  529. TimeoutOccurred = ClockTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  530. }
  531. while(((IP_MC_ME->PRTN1_COFB1_STAT & MC_ME_PRTN1_COFB1_STAT_BLOCK47_MASK) == 0U) && (FALSE == TimeoutOccurred));
  532. /* timeout notification */
  533. if (TRUE == TimeoutOccurred)
  534. {
  535. /* Report timeout error */
  536. ReportClockErrors(CLOCK_IP_REPORT_TIMEOUT_ERROR, RESERVED_CLK);
  537. }
  538. }
  539. #endif
  540. /* Enable cmu */
  541. if (config->enable != 0U)
  542. {
  543. cmu_fc->GCR |= CMU_FC_GCR_FCE_MASK;
  544. }
  545. else
  546. {
  547. cmu_fc->GCR &= ~CMU_FC_GCR_FCE_MASK;
  548. }
  549. }
  550. #endif
  551. #if CMU_INSTANCES_ARRAY_SIZE > 0U
  552. uint32 Mcu_CMU_GetInterruptStatus(uint8 u8IndexCmu)
  553. {
  554. uint32 cmuIsrValue;
  555. /* Read flags */
  556. cmuIsrValue = cmu[u8IndexCmu]->SR & CMU_ISR_MASK;
  557. return cmuIsrValue;
  558. }
  559. /**
  560. * @brief This function clear the CMU interrupt flag from CMU module.
  561. * @details Called by RGM ISR routine when a user notification for CMU FCCU events is configured
  562. *
  563. * @return void
  564. *
  565. * @implements Mcu_CMU_ClockFailInt_Activity
  566. *
  567. */
  568. void Mcu_CMU_ClockFailInt(void)
  569. {
  570. uint32 cmuIerValue, cmuIsrValue, indexCmu;
  571. for (indexCmu = 0U; indexCmu < CMU_INSTANCES_ARRAY_SIZE; indexCmu++)
  572. {
  573. /* Read flags */
  574. cmuIsrValue = cmu[indexCmu]->SR & CMU_ISR_MASK;
  575. /* Clear status flag */
  576. cmu[indexCmu]->SR = cmuIsrValue;
  577. /* Check whether driver is initialized */
  578. if(NULL_PTR != clockConfig)
  579. {
  580. /* Read interrupt enable */
  581. cmuIerValue = cmu[indexCmu]->IER & CMU_ISR_MASK;
  582. /* Filter all interrupts that are not enabled from cmuIsrValue */
  583. cmuIsrValue = cmuIsrValue & cmuIerValue;
  584. /* If at least one interrupt has been triggered */
  585. if (cmuIsrValue != 0U)
  586. {
  587. ReportClockErrors(CLOCK_IP_CMU_NOTIFICATION, cmuNames[indexCmu]);
  588. }
  589. }
  590. }
  591. }
  592. #endif
  593. /* Clock stop section code */
  594. #define MCU_STOP_SEC_CODE
  595. #include "Mcu_MemMap.h"
  596. /*! @}*/
  597. /*******************************************************************************
  598. * EOF
  599. ******************************************************************************/