system_MK24F12.c 10 KB

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