]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K22F/MK22F51212/fsl_clock_K22F51212.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_K22F / MK22F51212 / fsl_clock_K22F51212.c
1 /*
2  * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <stdbool.h>
34 #include "fsl_device_registers.h"
35 #include "fsl_sim_hal.h"
36 #include "fsl_clock_manager.h"
37 #include "fsl_osc_hal.h"
38
39 /*******************************************************************************
40  * Definitions
41  ******************************************************************************/
42
43 /* Table of base addresses for instances. */
44 extern const uint32_t g_simBaseAddr[];
45 extern const uint32_t g_mcgBaseAddr[];
46 const uint32_t g_oscBaseAddr[] = OSC_BASE_ADDRS;
47
48 /*******************************************************************************
49  * Code
50  ******************************************************************************/
51
52 /*FUNCTION**********************************************************************
53  *
54  * Function Name : CLOCK_SYS_GetDmaFreq
55  * Description   : Gets the clock frequency for DMA module
56  * This function gets the clock frequency for DMA moudle.
57  *
58  *END**************************************************************************/
59 uint32_t    CLOCK_SYS_GetDmaFreq(uint32_t instance)
60 {
61     uint32_t freq = 0;
62     CLOCK_SYS_GetFreq(kSystemClock, &freq);
63     return freq;
64 }
65
66 /*FUNCTION**********************************************************************
67  *
68  * Function Name : CLOCK_SYS_GetDmamuxFreq
69  * Description   : Gets the clock frequency for DMAMUX module
70  * This function gets the clock frequency for DMAMUX moudle.
71  *
72  *END**************************************************************************/
73 uint32_t    CLOCK_SYS_GetDmamuxFreq(uint32_t instance)
74 {
75     uint32_t freq = 0;
76     CLOCK_SYS_GetFreq(kBusClock, &freq);
77     return freq;
78 }
79
80 /*FUNCTION**********************************************************************
81  *
82  * Function Name : CLOCK_SYS_GetPortFreq
83  * Description   : Gets the clock frequency for PORT module
84  * This function gets the clock frequency for PORT moudle.
85  *
86  *END**************************************************************************/
87 uint32_t    CLOCK_SYS_GetPortFreq(uint32_t instance)
88 {
89     uint32_t freq = 0;
90     CLOCK_SYS_GetFreq(kLpoClock, &freq);
91     return freq;
92 }
93
94 /*FUNCTION**********************************************************************
95  *
96  * Function Name : CLOCK_SYS_GetEwmFreq
97  * Description   : Gets the clock frequency for Ewm module
98  * This function gets the clock frequency for Ewm moudle.
99  *
100  *END**************************************************************************/
101 uint32_t    CLOCK_SYS_GetEwmFreq(uint32_t instance)
102 {
103     uint32_t freq = 0;
104     CLOCK_SYS_GetFreq(kLpoClock, &freq);
105     return freq;
106 }
107
108 /*FUNCTION**********************************************************************
109  *
110  * Function Name : CLOCK_SYS_GetFlexbusFreq
111  * Description   : Gets the clock frequency for FLEXBUS module
112  * This function gets the clock frequency for FLEXBUS moudle.
113  *
114  *END**************************************************************************/
115 uint32_t    CLOCK_SYS_GetFlexbusFreq(uint32_t instance)
116 {
117     uint32_t freq = 0;
118     CLOCK_SYS_GetFreq(kSystemClock, &freq);
119     return freq;
120 }
121
122 /*FUNCTION**********************************************************************
123  *
124  * Function Name : CLOCK_SYS_GetFtfFreq
125  * Description   : Gets the clock frequency for FTF module. (Flash Memory)
126  * This function gets the clock frequency for FTF moudle.
127  *
128  *END**************************************************************************/
129 uint32_t    CLOCK_SYS_GetFtfFreq(uint32_t instance)
130 {
131     uint32_t freq = 0;
132     CLOCK_SYS_GetFreq(kFlashClock, &freq);
133     return freq;
134 }
135
136 /*FUNCTION**********************************************************************
137  *
138  * Function Name : CLOCK_SYS_GetCrcFreq
139  * Description   : Gets the clock frequency for CRC module
140  * This function gets the clock frequency for CRC moudle.
141  *
142  *END**************************************************************************/
143 uint32_t    CLOCK_SYS_GetCrcFreq(uint32_t instance)
144 {
145     uint32_t freq = 0;
146     CLOCK_SYS_GetFreq(kBusClock, &freq);
147     return freq;
148 }
149
150 /*FUNCTION**********************************************************************
151  *
152  * Function Name : CLOCK_SYS_GetRngaFreq
153  * Description   : Gets the clock frequency for RNGA module
154  * This function gets the clock frequency for RNGA moudle.
155  *
156  *END**************************************************************************/
157 uint32_t    CLOCK_SYS_GetRngaFreq(uint32_t instance)
158 {
159     uint32_t freq = 0;
160     CLOCK_SYS_GetFreq(kBusClock, &freq);
161     return freq;
162 }
163
164 /*FUNCTION**********************************************************************
165  *
166  * Function Name : CLOCK_SYS_GetAdcFreq
167  * Description   : Gets the clock frequency for ADC module
168  * This function gets the clock frequency for ADC moudle.
169  *
170  *END**************************************************************************/
171 uint32_t    CLOCK_SYS_GetAdcFreq(uint32_t instance)
172 {
173     uint32_t freq = 0;
174     uint32_t divider;
175
176     CLOCK_SYS_GetFreq(kOsc0ErClock, &freq);
177
178     divider = OSC_HAL_GetExternalRefClkDivCmd(g_oscBaseAddr[0]);
179     freq = freq >> divider;   /* 2 bits divider, divide by 1/2/4/8 */
180
181     return freq;
182 }
183
184 /*FUNCTION**********************************************************************
185  *
186  * Function Name : CLOCK_SYS_GetCmpFreq
187  * Description   : Gets the clock frequency for CMP module
188  * This function gets the clock frequency for CMP moudle.
189  *
190  *END**************************************************************************/
191 uint32_t    CLOCK_SYS_GetCmpFreq(uint32_t instance)
192 {
193     uint32_t freq = 0;
194     CLOCK_SYS_GetFreq(kBusClock, &freq);
195     return freq;
196 }
197
198 /*FUNCTION**********************************************************************
199  *
200  * Function Name : CLOCK_SYS_GetVrefFreq
201  * Description   : Gets the clock frequency for VREF module
202  * This function gets the clock frequency for VREF moudle.
203  *
204  *END**************************************************************************/
205 uint32_t    CLOCK_SYS_GetVrefFreq(uint32_t instance)
206 {
207     uint32_t freq = 0;
208     CLOCK_SYS_GetFreq(kBusClock, &freq);
209     return freq;
210 }
211
212 /*FUNCTION**********************************************************************
213  *
214  * Function Name : CLOCK_SYS_GetPdbFreq
215  * Description   : Gets the clock frequency for PDB module
216  * This function gets the clock frequency for PDB moudle.
217  *
218  *END**************************************************************************/
219 uint32_t    CLOCK_SYS_GetPdbFreq(uint32_t instance)
220 {
221     uint32_t freq = 0;
222     CLOCK_SYS_GetFreq(kBusClock, &freq);
223     return freq;
224 }
225
226 /*FUNCTION**********************************************************************
227  *
228  * Function Name : CLOCK_SYS_GetFtmFreq
229  * Description   : Gets the clock frequency for FTM module. (FlexTimers)
230  * This function gets the clock frequency for FTM moudle.
231  *
232  *END**************************************************************************/
233 uint32_t    CLOCK_SYS_GetFtmFreq(uint32_t instance)
234 {
235     uint32_t freq = 0;
236     CLOCK_SYS_GetFreq(kMcgFfClock, &freq);
237     return freq;
238 }
239
240 /*FUNCTION**********************************************************************
241  *
242  * Function Name : CLOCK_SYS_GetPitFreq
243  * Description   : Gets the clock frequency for Pit module. 
244  * This function gets the clock frequency for Pit moudle.
245  *
246  *END**************************************************************************/
247 uint32_t    CLOCK_SYS_GetPitFreq(uint32_t instance)
248 {
249     uint32_t freq = 0;
250     CLOCK_SYS_GetFreq(kBusClock, &freq);
251     return freq;
252 }
253
254 /*FUNCTION**********************************************************************
255  *
256  * Function Name : CLOCK_SYS_GetUsbFreq
257  * Description   : Gets the clock frequency for USB FS OTG module. 
258  * This function gets the clock frequency for USB FS OTG moudle.
259  *
260  *END**************************************************************************/
261 uint32_t    CLOCK_SYS_GetUsbFreq(uint32_t instance)
262 {
263     uint32_t freq = 0;
264     uint8_t setting;
265     clock_names_t clockName;
266     uint32_t frac = 0;
267     uint32_t divider = 0;
268
269     /* get the sim clock source setting*/
270     if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockUsbSrc, &setting) != kSimHalSuccess)
271     {
272         return freq;
273     }
274
275     switch ((sim_usb_clock_source_t)setting)
276     {
277     case kSimUsbSrcClkIn:  /* Core/system clock */
278         clockName = kUSB_CLKIN;
279         break;
280     case kSimUsbSrcPllFllSel:   /* clock as selected by SOPT2[PLLFLLSEL]. */
281         /* get the sim clock source setting*/
282         if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockPllfllSel, &setting) != kSimHalSuccess)
283         {
284             return freq;
285         }
286
287         switch ((sim_pllfll_clock_sel_t)setting) 
288         {
289         case kSimPllFllSelFll:       /* Fll clock */
290             clockName = kMcgFllClock;
291             break;
292         case kSimPllFllSelPll:        /* Pll0 clock */
293             clockName = kMcgPll0Clock;
294             break;
295         case kSimPllFllSelIrc:        /* Irc 48Mhz clock */
296             clockName = kIrc48mClock;
297             break;
298         default:
299             clockName = kReserved;
300             break;
301         }
302         break;
303     default:
304         clockName = kReserved;
305         break;
306     }
307
308     /* Get ref clock freq */
309     CLOCK_SYS_GetFreq(clockName, &freq);
310
311     /* Get divider and frac */
312     CLOCK_HAL_GetDivider(g_simBaseAddr[0], kClockDividerUsbDiv, &divider);
313     CLOCK_HAL_GetDivider(g_simBaseAddr[0], kClockDividerUsbFrac, &frac);
314
315     /* Divider output clock = Divider input clock × [ (FRAC+1) / (DIV+1) ]*/
316     freq = (freq) * (frac + 1) / (divider + 1);
317
318     return freq;
319 }
320
321 /*FUNCTION**********************************************************************
322  *
323  * Function Name : CLOCK_SYS_GetSpiFreq
324  * Description   : Gets the clock frequency for SPI module. 
325  * This function gets the clock frequency for SPI moudle.
326  *
327  *END**************************************************************************/
328 uint32_t    CLOCK_SYS_GetSpiFreq(uint32_t instance)
329 {
330     uint32_t freq = 0;
331     CLOCK_SYS_GetFreq(kBusClock, &freq);
332     return freq;
333 }
334
335 /*FUNCTION**********************************************************************
336  *
337  * Function Name : CLOCK_SYS_GetI2cFreq
338  * Description   : Gets the clock frequency for I2C module. 
339  * This function gets the clock frequency for I2C moudle.
340  *
341  *END**************************************************************************/
342 uint32_t    CLOCK_SYS_GetI2cFreq(uint32_t instance)
343 {
344     uint32_t freq = 0;
345     CLOCK_SYS_GetFreq(kBusClock, &freq);
346     return freq;
347 }
348
349 /*FUNCTION**********************************************************************
350  *
351  * Function Name : CLOCK_SYS_GetUartFreq
352  * Description   : Gets the clock frequency for UART module. 
353  * This function gets the clock frequency for UART moudle.
354  *
355  *END**************************************************************************/
356 uint32_t    CLOCK_SYS_GetUartFreq(uint32_t instance)
357 {
358     uint32_t freq = 0;
359
360     switch (instance)
361     {
362     case 0:
363     case 1:
364         CLOCK_SYS_GetFreq(kSystemClock, &freq);
365         break;
366     case 2:
367         CLOCK_SYS_GetFreq(kBusClock, &freq);
368         break;
369     default:
370         break;
371     }
372
373     return freq;
374 }
375
376 /*FUNCTION**********************************************************************
377  *
378  * Function Name : CLOCK_SYS_GetLpuartFreq
379  * Description   : Gets the clock frequency for LPUART module. 
380  * This function gets the clock frequency for LPUART moudle.
381  *
382  *END**************************************************************************/
383 uint32_t    CLOCK_SYS_GetLpuartFreq(uint32_t instance)
384 {
385     uint32_t freq = 0;
386     uint8_t setting;
387     uint8_t setting1;
388     clock_names_t clockName;
389     uint32_t divider = 0;
390
391     /* get the sim clock source setting*/
392     if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockLpuartSrc, &setting) != kSimHalSuccess)
393     {
394         return freq;
395     }
396
397     switch ((sim_lpuart_clock_source_t)setting)
398     {
399     case kSimLpuartSrcPllFllSel:   /* clock as selected by SOPT2[PLLFLLSEL]. */
400         /* get the sim clock source setting*/
401         if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockPllfllSel, &setting1) != kSimHalSuccess)
402         {
403             return freq;
404         }
405
406         switch ((sim_pllfll_clock_sel_t)setting1) 
407         {
408         case kSimPllFllSelFll:       /* Fll clock */
409             clockName = kMcgFllClock;
410             break;
411         case kSimPllFllSelPll:        /* Pll0 clock */
412             clockName = kMcgPll0Clock;
413             break;
414         case kSimPllFllSelIrc:        /* Irc 48Mhz clock */
415             clockName = kIrc48mClock;
416             break;
417         default:
418             clockName = kReserved;
419             break;
420         }
421         break;
422     case kSimLpuartSrcOscErclk:     /* OscErClk with divider */
423         clockName = kOsc0ErClock;
424         break;
425     case kSimLpuartSrcMcgIrclk:     /* MCGIRCLK */
426         clockName = kMcgIrClock;
427         break;
428     default:
429         clockName = kReserved;
430         break;
431     }
432
433     /* Get ref clock freq */
434     CLOCK_SYS_GetFreq(clockName, &freq);
435
436     if ((sim_lpuart_clock_source_t)setting == kSimLpuartSrcOscErclk)
437     {
438         divider = OSC_HAL_GetExternalRefClkDivCmd(g_oscBaseAddr[0]);
439         freq = freq >> divider;   /* 2 bits divider, divide by 1/2/4/8 */
440     }
441
442     return freq;
443 }
444
445 /*FUNCTION**********************************************************************
446  *
447  * Function Name : CLOCK_SYS_GetSaiFreq
448  * Description   : Gets the clock frequency for I2S module
449  * This function gets the clock frequency for I2S moudle.
450  *
451  *END**************************************************************************/
452 uint32_t    CLOCK_SYS_GetSaiFreq(uint32_t instance)
453 {
454     uint32_t freq = 0;
455     CLOCK_SYS_GetFreq(kBusClock, &freq);
456     return freq;
457 }
458
459 /*FUNCTION**********************************************************************
460  *
461  * Function Name : CLOCK_SYS_GetGpioFreq
462  * Description   : Gets the clock frequency for GPIO module. 
463  * This function gets the clock frequency for GPIO moudle.
464  *
465  *END**************************************************************************/
466 uint32_t    CLOCK_SYS_GetGpioFreq(uint32_t instance)
467 {
468     uint32_t freq = 0;
469
470     CLOCK_SYS_GetFreq(kPlatformClock, &freq);
471
472     return freq;
473 }
474
475 /*******************************************************************************
476  * EOF
477  ******************************************************************************/