system_MK82F25615.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ** ###################################################################
  3. ** Processors: MK82FN256CAx15
  4. ** MK82FN256VDC15
  5. ** MK82FN256VLL15
  6. ** MK82FN256VLQ15
  7. **
  8. ** Compilers: Keil ARM C/C++ Compiler
  9. ** Freescale C/C++ for Embedded ARM
  10. ** GNU C Compiler
  11. ** IAR ANSI C/C++ Compiler for ARM
  12. ** MCUXpresso Compiler
  13. **
  14. ** Reference manual: K82P121M150SF5RM, Rev. 0, May 2015
  15. ** Version: rev. 1.2, 2015-07-29
  16. ** Build: b171226
  17. **
  18. ** Abstract:
  19. ** Provides a system configuration function and a global variable that
  20. ** contains the system frequency. It configures the device and initializes
  21. ** the oscillator (PLL) that is part of the microcontroller device.
  22. **
  23. ** The Clear BSD License
  24. ** Copyright 2016 Freescale Semiconductor, Inc.
  25. ** Copyright 2016-2017 NXP
  26. ** All rights reserved.
  27. **
  28. ** Redistribution and use in source and binary forms, with or without
  29. ** modification, are permitted (subject to the limitations in the
  30. ** disclaimer below) provided that the following conditions are met:
  31. **
  32. ** * Redistributions of source code must retain the above copyright
  33. ** notice, this list of conditions and the following disclaimer.
  34. **
  35. ** * Redistributions in binary form must reproduce the above copyright
  36. ** notice, this list of conditions and the following disclaimer in the
  37. ** documentation and/or other materials provided with the distribution.
  38. **
  39. ** * Neither the name of the copyright holder nor the names of its
  40. ** contributors may be used to endorse or promote products derived from
  41. ** this software without specific prior written permission.
  42. **
  43. ** NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  44. ** GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  45. ** HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  46. ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  47. ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  48. ** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  49. ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  50. ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  51. ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  52. ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  53. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  54. ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  55. ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. **
  57. ** http: www.nxp.com
  58. ** mail: support@nxp.com
  59. **
  60. ** Revisions:
  61. ** - rev. 1.0 (2015-04-09)
  62. ** Initial version
  63. ** - rev. 1.1 (2015-05-28)
  64. ** Update according to the reference manual Rev. 0.
  65. ** - rev. 1.2 (2015-07-29)
  66. ** Correction of backward compatibility.
  67. **
  68. ** ###################################################################
  69. */
  70. /*!
  71. * @file MK82F25615
  72. * @version 1.2
  73. * @date 2015-07-29
  74. * @brief Device specific configuration file for MK82F25615 (implementation file)
  75. *
  76. * Provides a system configuration function and a global variable that contains
  77. * the system frequency. It configures the device and initializes the oscillator
  78. * (PLL) that is part of the microcontroller device.
  79. */
  80. #include <stdint.h>
  81. #include "fsl_device_registers.h"
  82. /* ----------------------------------------------------------------------------
  83. -- Core clock
  84. ---------------------------------------------------------------------------- */
  85. uint32_t SystemCoreClock = DEFAULT_SYSTEM_CLOCK;
  86. /* ----------------------------------------------------------------------------
  87. -- SystemInit()
  88. ---------------------------------------------------------------------------- */
  89. void SystemInit (void) {
  90. #if ((__FPU_PRESENT == 1) && (__FPU_USED == 1))
  91. SCB->CPACR |= ((3UL << 10*2) | (3UL << 11*2)); /* set CP10, CP11 Full Access */
  92. #endif /* ((__FPU_PRESENT == 1) && (__FPU_USED == 1)) */
  93. #if (DISABLE_WDOG)
  94. /* WDOG->UNLOCK: WDOGUNLOCK=0xC520 */
  95. WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xC520); /* Key 1 */
  96. /* WDOG->UNLOCK: WDOGUNLOCK=0xD928 */
  97. WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xD928); /* Key 2 */
  98. /* WDOG->STCTRLH: ?=0,DISTESTWDOG=0,BYTESEL=0,TESTSEL=0,TESTWDOG=0,?=0,?=1,WAITEN=1,STOPEN=1,DBGEN=0,ALLOWUPDATE=1,WINEN=0,IRQRSTEN=0,CLKSRC=1,WDOGEN=0 */
  99. WDOG->STCTRLH = WDOG_STCTRLH_BYTESEL(0x00) |
  100. WDOG_STCTRLH_WAITEN_MASK |
  101. WDOG_STCTRLH_STOPEN_MASK |
  102. WDOG_STCTRLH_ALLOWUPDATE_MASK |
  103. WDOG_STCTRLH_CLKSRC_MASK |
  104. 0x0100U;
  105. #endif /* (DISABLE_WDOG) */
  106. SystemInitHook();
  107. }
  108. /* ----------------------------------------------------------------------------
  109. -- SystemCoreClockUpdate()
  110. ---------------------------------------------------------------------------- */
  111. void SystemCoreClockUpdate (void) {
  112. uint32_t MCGOUTClock; /* Variable to store output clock frequency of the MCG module */
  113. uint16_t Divider;
  114. if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x00U) {
  115. /* Output of FLL or PLL is selected */
  116. if ((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U) {
  117. /* FLL is selected */
  118. if ((MCG->C1 & MCG_C1_IREFS_MASK) == 0x00U) {
  119. /* External reference clock is selected */
  120. switch (MCG->C7 & MCG_C7_OSCSEL_MASK) {
  121. case 0x00U:
  122. MCGOUTClock = CPU_XTAL_CLK_HZ; /* System oscillator drives MCG clock */
  123. break;
  124. case 0x01U:
  125. MCGOUTClock = CPU_XTAL32k_CLK_HZ; /* RTC 32 kHz oscillator drives MCG clock */
  126. break;
  127. case 0x02U:
  128. default:
  129. MCGOUTClock = CPU_INT_IRC_CLK_HZ; /* IRC 48MHz oscillator drives MCG clock */
  130. break;
  131. }
  132. if (((MCG->C2 & MCG_C2_RANGE_MASK) != 0x00U) && ((MCG->C7 & MCG_C7_OSCSEL_MASK) != 0x01U)) {
  133. switch (MCG->C1 & MCG_C1_FRDIV_MASK) {
  134. case 0x38U:
  135. Divider = 1536U;
  136. break;
  137. case 0x30U:
  138. Divider = 1280U;
  139. break;
  140. default:
  141. Divider = (uint16_t)(32LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT));
  142. break;
  143. }
  144. } else {/* ((MCG->C2 & MCG_C2_RANGE_MASK) != 0x00U) */
  145. Divider = (uint16_t)(1LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT));
  146. }
  147. MCGOUTClock = (MCGOUTClock / Divider); /* Calculate the divided FLL reference clock */
  148. } else { /* (!((MCG->C1 & MCG_C1_IREFS_MASK) == 0x00U)) */
  149. MCGOUTClock = CPU_INT_SLOW_CLK_HZ; /* The slow internal reference clock is selected */
  150. } /* (!((MCG->C1 & MCG_C1_IREFS_MASK) == 0x00U)) */
  151. /* Select correct multiplier to calculate the MCG output clock */
  152. switch (MCG->C4 & (MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) {
  153. case 0x00U:
  154. MCGOUTClock *= 640U;
  155. break;
  156. case 0x20U:
  157. MCGOUTClock *= 1280U;
  158. break;
  159. case 0x40U:
  160. MCGOUTClock *= 1920U;
  161. break;
  162. case 0x60U:
  163. MCGOUTClock *= 2560U;
  164. break;
  165. case 0x80U:
  166. MCGOUTClock *= 732U;
  167. break;
  168. case 0xA0U:
  169. MCGOUTClock *= 1464U;
  170. break;
  171. case 0xC0U:
  172. MCGOUTClock *= 2197U;
  173. break;
  174. case 0xE0U:
  175. MCGOUTClock *= 2929U;
  176. break;
  177. default:
  178. break;
  179. }
  180. } else { /* (!((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U)) */
  181. /* PLL is selected */
  182. Divider = (((uint16_t)MCG->C5 & MCG_C5_PRDIV_MASK) + 0x01U);
  183. MCGOUTClock = (uint32_t)(CPU_XTAL_CLK_HZ / Divider); /* Calculate the PLL reference clock */
  184. Divider = (((uint16_t)MCG->C6 & MCG_C6_VDIV_MASK) + 16U);
  185. MCGOUTClock *= Divider; /* Calculate the VCO output clock */
  186. MCGOUTClock /= 2; /* Calculate the MCG output clock */
  187. } /* (!((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U)) */
  188. } else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x40U) {
  189. /* Internal reference clock is selected */
  190. if ((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U) {
  191. MCGOUTClock = CPU_INT_SLOW_CLK_HZ; /* Slow internal reference clock selected */
  192. } else { /* (!((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U)) */
  193. Divider = (uint16_t)(0x01LU << ((MCG->SC & MCG_SC_FCRDIV_MASK) >> MCG_SC_FCRDIV_SHIFT));
  194. MCGOUTClock = (uint32_t) (CPU_INT_FAST_CLK_HZ / Divider); /* Fast internal reference clock selected */
  195. } /* (!((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U)) */
  196. } else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U) {
  197. /* External reference clock is selected */
  198. switch (MCG->C7 & MCG_C7_OSCSEL_MASK) {
  199. case 0x00U:
  200. MCGOUTClock = CPU_XTAL_CLK_HZ; /* System oscillator drives MCG clock */
  201. break;
  202. case 0x01U:
  203. MCGOUTClock = CPU_XTAL32k_CLK_HZ; /* RTC 32 kHz oscillator drives MCG clock */
  204. break;
  205. case 0x02U:
  206. default:
  207. MCGOUTClock = CPU_INT_IRC_CLK_HZ; /* IRC 48MHz oscillator drives MCG clock */
  208. break;
  209. }
  210. } else { /* (!((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U)) */
  211. /* Reserved value */
  212. return;
  213. } /* (!((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U)) */
  214. SystemCoreClock = (MCGOUTClock / (0x01U + ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) >> SIM_CLKDIV1_OUTDIV1_SHIFT)));
  215. }
  216. /* ----------------------------------------------------------------------------
  217. -- SystemInitHook()
  218. ---------------------------------------------------------------------------- */
  219. __attribute__ ((weak)) void SystemInitHook (void) {
  220. /* Void implementation of the weak function. */
  221. }