Clock_Ip_ProgFreqSwitch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_ProgFreqSwitch.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. /*==================================================================================================
  36. SOURCE FILE VERSION INFORMATION
  37. ==================================================================================================*/
  38. #define CLOCK_IP_PROGFREQSWITCH_VENDOR_ID_C 43
  39. #define CLOCK_IP_PROGFREQSWITCH_AR_RELEASE_MAJOR_VERSION_C 4
  40. #define CLOCK_IP_PROGFREQSWITCH_AR_RELEASE_MINOR_VERSION_C 4
  41. #define CLOCK_IP_PROGFREQSWITCH_AR_RELEASE_REVISION_VERSION_C 0
  42. #define CLOCK_IP_PROGFREQSWITCH_SW_MAJOR_VERSION_C 1
  43. #define CLOCK_IP_PROGFREQSWITCH_SW_MINOR_VERSION_C 0
  44. #define CLOCK_IP_PROGFREQSWITCH_SW_PATCH_VERSION_C 0
  45. /*==================================================================================================
  46. * FILE VERSION CHECKS
  47. ==================================================================================================*/
  48. /* Check if Clock_Ip_ProgFreqSwitch.c file and Clock_Ip_Private.h file are of the same vendor */
  49. #if (CLOCK_IP_PROGFREQSWITCH_VENDOR_ID_C != CLOCK_IP_PRIVATE_VENDOR_ID)
  50. #error "Clock_Ip_ProgFreqSwitch.c and Clock_Ip_Private.h have different vendor ids"
  51. #endif
  52. /* Check if Clock_Ip_ProgFreqSwitch.c file and Clock_Ip_Private.h file are of the same Autosar version */
  53. #if ((CLOCK_IP_PROGFREQSWITCH_AR_RELEASE_MAJOR_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_MAJOR_VERSION) || \
  54. (CLOCK_IP_PROGFREQSWITCH_AR_RELEASE_MINOR_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_MINOR_VERSION) || \
  55. (CLOCK_IP_PROGFREQSWITCH_AR_RELEASE_REVISION_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_REVISION_VERSION) \
  56. )
  57. #error "AutoSar Version Numbers of Clock_Ip_ProgFreqSwitch.c and Clock_Ip_Private.h are different"
  58. #endif
  59. /* Check if Clock_Ip_ProgFreqSwitch.c file and Clock_Ip_Private.h file are of the same Software version */
  60. #if ((CLOCK_IP_PROGFREQSWITCH_SW_MAJOR_VERSION_C != CLOCK_IP_PRIVATE_SW_MAJOR_VERSION) || \
  61. (CLOCK_IP_PROGFREQSWITCH_SW_MINOR_VERSION_C != CLOCK_IP_PRIVATE_SW_MINOR_VERSION) || \
  62. (CLOCK_IP_PROGFREQSWITCH_SW_PATCH_VERSION_C != CLOCK_IP_PRIVATE_SW_PATCH_VERSION) \
  63. )
  64. #error "Software Version Numbers of Clock_Ip_ProgFreqSwitch.c and Clock_Ip_Private.h are different"
  65. #endif
  66. /*==================================================================================================
  67. LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  68. ==================================================================================================*/
  69. /*==================================================================================================
  70. * LOCAL MACROS
  71. ==================================================================================================*/
  72. #ifdef CGM_X_PCFS_SDUR_DIVC_DIVE_DIVS
  73. /* Pcfs settings that are dependent on device */
  74. #define A_MAX_SIZE 6U
  75. /* microA per MHz */
  76. #define DYNAMIC_IDD_CHANGE 2360U
  77. #define DIVIDE_BY_1000000 1000000U
  78. #define DIVIDE_BY_100000 100000U
  79. #define CONSTANT_2048000 (1024U * 2000U)
  80. #endif
  81. /*==================================================================================================
  82. LOCAL CONSTANTS
  83. ==================================================================================================*/
  84. /* Clock start constant section data */
  85. #define MCU_START_SEC_CONST_UNSPECIFIED
  86. #include "Mcu_MemMap.h"
  87. #ifdef CGM_X_PCFS_SDUR_DIVC_DIVE_DIVS
  88. static const uint32 aMax[A_MAX_SIZE] = {0U,5U,10U,15U,20U,100U};
  89. static const uint32 pcfsRate[A_MAX_SIZE] = {0U,12U,48U,112U,184U,1000U};
  90. #endif
  91. /* Clock stop constant section data */
  92. #define MCU_STOP_SEC_CONST_UNSPECIFIED
  93. #include "Mcu_MemMap.h"
  94. /*==================================================================================================
  95. LOCAL VARIABLES
  96. ==================================================================================================*/
  97. /*==================================================================================================
  98. GLOBAL CONSTANTS
  99. ==================================================================================================*/
  100. /*==================================================================================================
  101. GLOBAL VARIABLES
  102. ==================================================================================================*/
  103. /* Clock start section code */
  104. #define MCU_START_SEC_CODE
  105. #include "Mcu_MemMap.h"
  106. static void ProgressiveFrequencyClockSwitchEmpty(Clock_Ip_PcfsConfigType const* config);
  107. #ifdef CGM_X_PCFS_SDUR_SDUR
  108. static void CgmXPcfsSdurSdur(Clock_Ip_PcfsConfigType const *config);
  109. #endif
  110. #ifdef CGM_X_PCFS_SDUR_DIVC_DIVE_DIVS
  111. static void CgmXPcfsSdurDivcDiveDivs(Clock_Ip_PcfsConfigType const *config);
  112. #endif
  113. /* Clock stop section code */
  114. #define MCU_STOP_SEC_CODE
  115. #include "Mcu_MemMap.h"
  116. /* Clock start constant section data */
  117. #define MCU_START_SEC_CONST_UNSPECIFIED
  118. #include "Mcu_MemMap.h"
  119. const pcfsCallback pcfsCallbacks[PCFS_CALLBACKS_COUNT] =
  120. {
  121. {
  122. ProgressiveFrequencyClockSwitchEmpty, /* Set */
  123. },
  124. #ifdef CGM_X_PCFS_SDUR_SDUR
  125. {
  126. CgmXPcfsSdurSdur, /* Set */
  127. },
  128. #endif
  129. #ifdef CGM_X_PCFS_SDUR_DIVC_DIVE_DIVS
  130. {
  131. CgmXPcfsSdurDivcDiveDivs, /* Set */
  132. },
  133. #endif
  134. };
  135. /* Clock stop constant section data */
  136. #define MCU_STOP_SEC_CONST_UNSPECIFIED
  137. #include "Mcu_MemMap.h"
  138. /* Clock start section code */
  139. #define MCU_START_SEC_CODE
  140. #include "Mcu_MemMap.h"
  141. static void ProgressiveFrequencyClockSwitchEmpty(Clock_Ip_PcfsConfigType const* config)
  142. {
  143. (void)config;
  144. /* No implementation */
  145. }
  146. #ifdef CGM_X_PCFS_SDUR_SDUR
  147. static void CgmXPcfsSdurSdur(Clock_Ip_PcfsConfigType const *config)
  148. {
  149. uint32 instance = clockFeatures[config->name][CLOCK_MODULE_INSTANCE];
  150. uint32 hwIndex = clockFeatures[config->name][PCFS_HW_INDEX];
  151. uint32 swIndex = clockFeatures[config->name][PCFS_SW_INDEX];
  152. pcfsEntry *pcfs = GetPcfs(swIndex);
  153. cgmPcfs[instance]->PCFS_SDUR = MC_CGM_PCFS_SDUR_SDUR(pcfs->sdur);
  154. (void)hwIndex; /* hw index is not used because divc_init, divc_rate, divc_rate, div_startValue, div_endValue registers are not implemented */
  155. /* No implementation */
  156. }
  157. #endif
  158. #ifdef CGM_X_PCFS_SDUR_DIVC_DIVE_DIVS
  159. static void CgmXPcfsSdurDivcDiveDivs(Clock_Ip_PcfsConfigType const *config)
  160. {
  161. volatile cgmPcfs_Type* cgmPcfs_base = cgmPcfs[clockFeatures[config->selectorName][CLOCK_MODULE_INSTANCE]];
  162. uint32 hwIndex = clockFeatures[config->name][PCFS_INDEX];
  163. static uint32 hash[CMU_INFO_SIZE];
  164. uint32 finput = 0U, fsafe = 0U;
  165. uint32 amaxBrut;
  166. uint32 RATE = 0U;
  167. uint32 i, var1, var2, var3, K;
  168. uint32 sdur, divc_init, divc_rate, div_startValue, div_endValue;
  169. #if (defined(CLOCK_IP_DEV_ERROR_DETECT) && (CLOCK_IP_DEV_ERROR_DETECT == STD_ON))
  170. CLOCK_DEV_ASSERT(config->selectorName != RESERVED_CLK);
  171. #endif
  172. if (hash[clockFeatures[config->name][PCFS_INDEX]] != ((((uint32)config->clockSourceFrequency) ^ ((uint32)config->maxAllowableIDDchange) ^ ((uint32)config->name) ^ ((uint32)config->selectorName) ^ ((uint32)config->stepDuration))))
  173. {
  174. hash[clockFeatures[config->name][PCFS_INDEX]] = ((((uint32)config->clockSourceFrequency) ^ ((uint32)config->maxAllowableIDDchange) ^ ((uint32)config->name) ^ ((uint32)config->selectorName) ^ ((uint32)config->stepDuration)));
  175. finput = config->clockSourceFrequency / DIVIDE_BY_1000000;
  176. #if defined(FEATURE_CLOCK_IP_HAS_FIRC_CLK)
  177. fsafe = GetConfiguredFrequencyValue(FIRC_CLK) / DIVIDE_BY_1000000;
  178. #endif
  179. #if (defined(CLOCK_IP_DEV_ERROR_DETECT) && (CLOCK_IP_DEV_ERROR_DETECT == STD_ON))
  180. CLOCK_DEV_ASSERT(finput != 0U);
  181. CLOCK_DEV_ASSERT(fsafe != 0U);
  182. CLOCK_DEV_ASSERT(config->maxAllowableIDDchange != 0U);
  183. CLOCK_DEV_ASSERT(config->stepDuration != 0U);
  184. #endif
  185. /* Calculate amax=fchg/Finput */
  186. amaxBrut = (config->maxAllowableIDDchange * config->stepDuration * DIVIDE_BY_100000 / (finput * DYNAMIC_IDD_CHANGE));
  187. RATE = amaxBrut;
  188. /* Round pcfs rate by rounding amax */
  189. if (amaxBrut < aMax[0U])
  190. {
  191. RATE = pcfsRate[0U];
  192. }
  193. else if (amaxBrut > aMax[A_MAX_SIZE-1U])
  194. {
  195. RATE = pcfsRate[A_MAX_SIZE-1U];
  196. }
  197. else
  198. {
  199. for (i = 1U; i < (uint8)A_MAX_SIZE; i++)
  200. {
  201. if (aMax[i-1U] < amaxBrut)
  202. {
  203. RATE = pcfsRate[i];
  204. }
  205. }
  206. }
  207. /* Calculate K by using formula k = ceil(0.5 + sqrt(0.25 - (2000 * (1 -(Finput/fsafe)) / RATE))) */
  208. var1 = 256U + ((CONSTANT_2048000 * finput) / (fsafe * RATE)) - (CONSTANT_2048000 / RATE);
  209. var2 = 1UL << 30U; /* The second-to-top bit is set: use 1u << 14 for uint16 type; use 1uL<<30 for uint32 type */
  210. var3 = 0U;
  211. /* Implement sqrt from K formula by using a square-root computing in embedded C */
  212. /* "one" starts at the highest power of four <= than the argument */
  213. while (var2 > var1)
  214. {
  215. var2 = var2 >> 2;
  216. }
  217. /* Implement sqrt from K formula by using a square-root computing in embedded C */
  218. while (var2 != 0U)
  219. {
  220. if (var1 >= (var3 + var2))
  221. {
  222. var1 = var1 - (var3 + var2);
  223. var3 = var3 + (var2 << 1U);
  224. }
  225. var3 = var3 >> 1U;
  226. var2 = var2 >> 2U;
  227. }
  228. K = (64U + 127U + (var3 << 2U)) >> 7U; /* Calculated K from k = ceil(0.5 + sqrt(0.25 - (2000 * (1 -(Fi/fsafe)) / RATE))) */
  229. sdur = config->stepDuration * fsafe;
  230. divc_init = RATE * K;
  231. divc_rate = RATE;
  232. div_startValue = 999U + ((RATE * K * (K+1U)) >> 1U);
  233. div_endValue = (finput * 1000U / fsafe) - 1U;
  234. /* Configure pcfs registers */
  235. cgmPcfs_base->PCFS_SDUR = MC_CGM_PCFS_SDUR_SDUR(sdur);
  236. cgmPcfs_base->PCFS[hwIndex].DIVC = MC_CGM_PCFS_DIVC_RATE(divc_rate) | MC_CGM_PCFS_DIVC_INIT(divc_init);
  237. cgmPcfs_base->PCFS[hwIndex].DIVE = MC_CGM_PCFS_DIVE_DIVE(div_endValue);
  238. cgmPcfs_base->PCFS[hwIndex].DIVS = MC_CGM_PCFS_DIVS_DIVS(div_startValue);
  239. }
  240. }
  241. #endif
  242. /* Clock stop section code */
  243. #define MCU_STOP_SEC_CODE
  244. #include "Mcu_MemMap.h"
  245. /*! @}*/
  246. /*******************************************************************************
  247. * EOF
  248. ******************************************************************************/