system_MK64F12.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. ** ###################################################################
  3. ** Processors: MK64FN1M0CAJ12
  4. ** MK64FN1M0VDC12
  5. ** MK64FN1M0VLL12
  6. ** MK64FN1M0VLQ12
  7. ** MK64FN1M0VMD12
  8. ** MK64FX512VDC12
  9. ** MK64FX512VLL12
  10. ** MK64FX512VLQ12
  11. ** MK64FX512VMD12
  12. **
  13. ** Compilers: Keil ARM C/C++ Compiler
  14. ** Freescale C/C++ for Embedded ARM
  15. ** GNU C Compiler
  16. ** IAR ANSI C/C++ Compiler for ARM
  17. ** MCUXpresso Compiler
  18. **
  19. ** Reference manual: K64P144M120SF5RM, Rev.2, January 2014
  20. ** Version: rev. 2.9, 2016-03-21
  21. ** Build: b170112
  22. **
  23. ** Abstract:
  24. ** Provides a system configuration function and a global variable that
  25. ** contains the system frequency. It configures the device and initializes
  26. ** the oscillator (PLL) that is part of the microcontroller device.
  27. **
  28. ** Copyright (c) 2016 Freescale Semiconductor, Inc.
  29. ** Copyright 2016 - 2017 NXP
  30. ** Redistribution and use in source and binary forms, with or without modification,
  31. ** are permitted provided that the following conditions are met:
  32. **
  33. ** o Redistributions of source code must retain the above copyright notice, this list
  34. ** of conditions and the following disclaimer.
  35. **
  36. ** o Redistributions in binary form must reproduce the above copyright notice, this
  37. ** list of conditions and the following disclaimer in the documentation and/or
  38. ** other materials provided with the distribution.
  39. **
  40. ** o Neither the name of the copyright holder nor the names of its
  41. ** contributors may be used to endorse or promote products derived from this
  42. ** software without specific prior written permission.
  43. **
  44. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  45. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  46. ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  47. ** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  48. ** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  49. ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  50. ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  51. ** ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  53. ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. **
  55. ** http: www.nxp.com
  56. ** mail: support@nxp.com
  57. **
  58. ** Revisions:
  59. ** - rev. 1.0 (2013-08-12)
  60. ** Initial version.
  61. ** - rev. 2.0 (2013-10-29)
  62. ** Register accessor macros added to the memory map.
  63. ** Symbols for Processor Expert memory map compatibility added to the memory map.
  64. ** Startup file for gcc has been updated according to CMSIS 3.2.
  65. ** System initialization updated.
  66. ** MCG - registers updated.
  67. ** PORTA, PORTB, PORTC, PORTE - registers for digital filter removed.
  68. ** - rev. 2.1 (2013-10-30)
  69. ** Definition of BITBAND macros updated to support peripherals with 32-bit acces disabled.
  70. ** - rev. 2.2 (2013-12-09)
  71. ** DMA - EARS register removed.
  72. ** AIPS0, AIPS1 - MPRA register updated.
  73. ** - rev. 2.3 (2014-01-24)
  74. ** Update according to reference manual rev. 2
  75. ** ENET, MCG, MCM, SIM, USB - registers updated
  76. ** - rev. 2.4 (2014-02-10)
  77. ** The declaration of clock configurations has been moved to separate header file system_MK64F12.h
  78. ** Update of SystemInit() and SystemCoreClockUpdate() functions.
  79. ** - rev. 2.5 (2014-02-10)
  80. ** The declaration of clock configurations has been moved to separate header file system_MK64F12.h
  81. ** Update of SystemInit() and SystemCoreClockUpdate() functions.
  82. ** Module access macro module_BASES replaced by module_BASE_PTRS.
  83. ** - rev. 2.6 (2014-08-28)
  84. ** Update of system files - default clock configuration changed.
  85. ** Update of startup files - possibility to override DefaultISR added.
  86. ** - rev. 2.7 (2014-10-14)
  87. ** Interrupt INT_LPTimer renamed to INT_LPTMR0, interrupt INT_Watchdog renamed to INT_WDOG_EWM.
  88. ** - rev. 2.8 (2015-02-19)
  89. ** Renamed interrupt vector LLW to LLWU.
  90. ** - rev. 2.9 (2016-03-21)
  91. ** Added MK64FN1M0CAJ12 part.
  92. ** GPIO - renamed port instances: PTx -> GPIOx.
  93. **
  94. ** ###################################################################
  95. */
  96. /*!
  97. * @file MK64F12
  98. * @version 2.9
  99. * @date 2016-03-21
  100. * @brief Device specific configuration file for MK64F12 (implementation file)
  101. *
  102. * Provides a system configuration function and a global variable that contains
  103. * the system frequency. It configures the device and initializes the oscillator
  104. * (PLL) that is part of the microcontroller device.
  105. */
  106. #include <stdint.h>
  107. #include "fsl_device_registers.h"
  108. /* ----------------------------------------------------------------------------
  109. -- Core clock
  110. ---------------------------------------------------------------------------- */
  111. uint32_t SystemCoreClock = DEFAULT_SYSTEM_CLOCK;
  112. /* ----------------------------------------------------------------------------
  113. -- SystemInit()
  114. ---------------------------------------------------------------------------- */
  115. void SystemInit (void) {
  116. #if ((__FPU_PRESENT == 1) && (__FPU_USED == 1))
  117. SCB->CPACR |= ((3UL << 10*2) | (3UL << 11*2)); /* set CP10, CP11 Full Access */
  118. #endif /* ((__FPU_PRESENT == 1) && (__FPU_USED == 1)) */
  119. #if (DISABLE_WDOG)
  120. /* WDOG->UNLOCK: WDOGUNLOCK=0xC520 */
  121. WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xC520); /* Key 1 */
  122. /* WDOG->UNLOCK: WDOGUNLOCK=0xD928 */
  123. WDOG->UNLOCK = WDOG_UNLOCK_WDOGUNLOCK(0xD928); /* Key 2 */
  124. /* 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 */
  125. WDOG->STCTRLH = WDOG_STCTRLH_BYTESEL(0x00) |
  126. WDOG_STCTRLH_WAITEN_MASK |
  127. WDOG_STCTRLH_STOPEN_MASK |
  128. WDOG_STCTRLH_ALLOWUPDATE_MASK |
  129. WDOG_STCTRLH_CLKSRC_MASK |
  130. 0x0100U;
  131. #endif /* (DISABLE_WDOG) */
  132. }
  133. /* ----------------------------------------------------------------------------
  134. -- SystemCoreClockUpdate()
  135. ---------------------------------------------------------------------------- */
  136. void SystemCoreClockUpdate (void) {
  137. uint32_t MCGOUTClock; /* Variable to store output clock frequency of the MCG module */
  138. uint16_t Divider;
  139. if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x00U) {
  140. /* Output of FLL or PLL is selected */
  141. if ((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U) {
  142. /* FLL is selected */
  143. if ((MCG->C1 & MCG_C1_IREFS_MASK) == 0x00U) {
  144. /* External reference clock is selected */
  145. switch (MCG->C7 & MCG_C7_OSCSEL_MASK) {
  146. case 0x00U:
  147. MCGOUTClock = CPU_XTAL_CLK_HZ; /* System oscillator drives MCG clock */
  148. break;
  149. case 0x01U:
  150. MCGOUTClock = CPU_XTAL32k_CLK_HZ; /* RTC 32 kHz oscillator drives MCG clock */
  151. break;
  152. case 0x02U:
  153. default:
  154. MCGOUTClock = CPU_INT_IRC_CLK_HZ; /* IRC 48MHz oscillator drives MCG clock */
  155. break;
  156. }
  157. if (((MCG->C2 & MCG_C2_RANGE_MASK) != 0x00U) && ((MCG->C7 & MCG_C7_OSCSEL_MASK) != 0x01U)) {
  158. switch (MCG->C1 & MCG_C1_FRDIV_MASK) {
  159. case 0x38U:
  160. Divider = 1536U;
  161. break;
  162. case 0x30U:
  163. Divider = 1280U;
  164. break;
  165. default:
  166. Divider = (uint16_t)(32LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT));
  167. break;
  168. }
  169. } else {/* ((MCG->C2 & MCG_C2_RANGE_MASK) != 0x00U) */
  170. Divider = (uint16_t)(1LU << ((MCG->C1 & MCG_C1_FRDIV_MASK) >> MCG_C1_FRDIV_SHIFT));
  171. }
  172. MCGOUTClock = (MCGOUTClock / Divider); /* Calculate the divided FLL reference clock */
  173. } else { /* (!((MCG->C1 & MCG_C1_IREFS_MASK) == 0x00U)) */
  174. MCGOUTClock = CPU_INT_SLOW_CLK_HZ; /* The slow internal reference clock is selected */
  175. } /* (!((MCG->C1 & MCG_C1_IREFS_MASK) == 0x00U)) */
  176. /* Select correct multiplier to calculate the MCG output clock */
  177. switch (MCG->C4 & (MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS_MASK)) {
  178. case 0x00U:
  179. MCGOUTClock *= 640U;
  180. break;
  181. case 0x20U:
  182. MCGOUTClock *= 1280U;
  183. break;
  184. case 0x40U:
  185. MCGOUTClock *= 1920U;
  186. break;
  187. case 0x60U:
  188. MCGOUTClock *= 2560U;
  189. break;
  190. case 0x80U:
  191. MCGOUTClock *= 732U;
  192. break;
  193. case 0xA0U:
  194. MCGOUTClock *= 1464U;
  195. break;
  196. case 0xC0U:
  197. MCGOUTClock *= 2197U;
  198. break;
  199. case 0xE0U:
  200. MCGOUTClock *= 2929U;
  201. break;
  202. default:
  203. break;
  204. }
  205. } else { /* (!((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U)) */
  206. /* PLL is selected */
  207. Divider = (((uint16_t)MCG->C5 & MCG_C5_PRDIV0_MASK) + 0x01U);
  208. MCGOUTClock = (uint32_t)(CPU_XTAL_CLK_HZ / Divider); /* Calculate the PLL reference clock */
  209. Divider = (((uint16_t)MCG->C6 & MCG_C6_VDIV0_MASK) + 24U);
  210. MCGOUTClock *= Divider; /* Calculate the MCG output clock */
  211. } /* (!((MCG->C6 & MCG_C6_PLLS_MASK) == 0x00U)) */
  212. } else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x40U) {
  213. /* Internal reference clock is selected */
  214. if ((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U) {
  215. MCGOUTClock = CPU_INT_SLOW_CLK_HZ; /* Slow internal reference clock selected */
  216. } else { /* (!((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U)) */
  217. Divider = (uint16_t)(0x01LU << ((MCG->SC & MCG_SC_FCRDIV_MASK) >> MCG_SC_FCRDIV_SHIFT));
  218. MCGOUTClock = (uint32_t) (CPU_INT_FAST_CLK_HZ / Divider); /* Fast internal reference clock selected */
  219. } /* (!((MCG->C2 & MCG_C2_IRCS_MASK) == 0x00U)) */
  220. } else if ((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U) {
  221. /* External reference clock is selected */
  222. switch (MCG->C7 & MCG_C7_OSCSEL_MASK) {
  223. case 0x00U:
  224. MCGOUTClock = CPU_XTAL_CLK_HZ; /* System oscillator drives MCG clock */
  225. break;
  226. case 0x01U:
  227. MCGOUTClock = CPU_XTAL32k_CLK_HZ; /* RTC 32 kHz oscillator drives MCG clock */
  228. break;
  229. case 0x02U:
  230. default:
  231. MCGOUTClock = CPU_INT_IRC_CLK_HZ; /* IRC 48MHz oscillator drives MCG clock */
  232. break;
  233. }
  234. } else { /* (!((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U)) */
  235. /* Reserved value */
  236. return;
  237. } /* (!((MCG->C1 & MCG_C1_CLKS_MASK) == 0x80U)) */
  238. SystemCoreClock = (MCGOUTClock / (0x01U + ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) >> SIM_CLKDIV1_OUTDIV1_SHIFT)));
  239. }