1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #ifndef MBED_CLK_FREQS_H
17 #define MBED_CLK_FREQS_H
23 #include "PeripheralPins.h"
25 //Get the peripheral bus clock frequency
26 static inline uint32_t bus_frequency(void) {
27 return (SystemCoreClock
/ (((SIM
->CLKDIV1
& SIM_CLKDIV1_OUTDIV4_MASK
) >> SIM_CLKDIV1_OUTDIV4_SHIFT
) + 1));
30 #if defined(TARGET_KL43Z)
32 static inline uint32_t extosc_frequency(void) {
33 return CPU_XTAL_CLK_HZ
;
36 static inline uint32_t fastirc_frequency(void) {
37 return CPU_INT_FAST_CLK_HZ
;
40 static inline uint32_t mcgirc_frequency(void) {
41 uint32_t mcgirc_clock
= 0;
43 if (MCG
->C1
& MCG_C1_IREFSTEN_MASK
) {
44 mcgirc_clock
= (MCG
->C2
& MCG_C2_IRCS_MASK
) ? 8000000u : 2000000u;
45 mcgirc_clock
/= 1u + ((MCG
->SC
& MCG_SC_FCRDIV_MASK
) >> MCG_SC_FCRDIV_SHIFT
);
46 mcgirc_clock
/= 1u + (MCG
->MC
& MCG_MC_LIRC_DIV2_MASK
);
54 //Get external oscillator (crystal) frequency
55 static uint32_t extosc_frequency(void) {
56 uint32_t MCGClock
= SystemCoreClock
* (1u + ((SIM
->CLKDIV1
& SIM_CLKDIV1_OUTDIV1_MASK
) >> SIM_CLKDIV1_OUTDIV1_SHIFT
));
58 if ((MCG
->C1
& MCG_C1_CLKS_MASK
) == MCG_C1_CLKS(2)) //MCG clock = external reference clock
61 uint32_t divider
, multiplier
;
62 #ifdef MCG_C5_PLLCLKEN0_MASK //PLL available
63 if ((MCG
->C1
& MCG_C1_CLKS_MASK
) == MCG_C1_CLKS(0)) { //PLL/FLL is selected
64 if ((MCG
->C6
& MCG_C6_PLLS_MASK
) == 0x0u
) { //FLL is selected
66 if ((MCG
->S
& MCG_S_IREFST_MASK
) == 0x0u
) { //FLL uses external reference
67 divider
= (uint8_t)(1u << ((MCG
->C1
& MCG_C1_FRDIV_MASK
) >> MCG_C1_FRDIV_SHIFT
));
68 if ((MCG
->C2
& MCG_C2_RANGE0_MASK
) != 0x0u
)
70 /* Select correct multiplier to calculate the MCG output clock */
71 switch (MCG
->C4
& (MCG_C4_DMX32_MASK
| MCG_C4_DRST_DRS_MASK
)) {
99 return MCGClock
* divider
/ multiplier
;
101 #ifdef MCG_C5_PLLCLKEN0_MASK
102 } else { //PLL is selected
103 divider
= (1u + (MCG
->C5
& MCG_C5_PRDIV0_MASK
));
104 multiplier
= ((MCG
->C6
& MCG_C6_VDIV0_MASK
) + 24u);
105 return MCGClock
* divider
/ multiplier
;
110 //In all other cases either there is no crystal or we cannot determine it
111 //For example when the FLL is running on the internal reference, and there is also an
112 //external crystal. However these are unlikely situations
116 //Get MCG PLL/2 or FLL frequency, depending on which one is active, sets PLLFLLSEL bit
117 static uint32_t mcgpllfll_frequency(void) {
118 if ((MCG
->C1
& MCG_C1_CLKS_MASK
) != MCG_C1_CLKS(0)) //PLL/FLL is not selected
121 uint32_t MCGClock
= SystemCoreClock
* (1u + ((SIM
->CLKDIV1
& SIM_CLKDIV1_OUTDIV1_MASK
) >> SIM_CLKDIV1_OUTDIV1_SHIFT
));
122 #ifdef MCG_C5_PLLCLKEN0_MASK
123 if ((MCG
->C6
& MCG_C6_PLLS_MASK
) == 0x0u
) { //FLL is selected
124 SIM
->SOPT2
&= ~SIM_SOPT2_PLLFLLSEL_MASK
; //MCG peripheral clock is FLL output
127 #ifdef MCG_C5_PLLCLKEN0_MASK
128 } else { //PLL is selected
129 SIM
->SOPT2
|= SIM_SOPT2_PLLFLLSEL_MASK
; //MCG peripheral clock is PLL output
130 return (MCGClock
>> 1);
134 //It is possible the SystemCoreClock isn't running on the PLL, and the PLL is still active
135 //for the peripherals, this is however an unlikely setup