1
1
/*
2
2
* Copyright (c) 2015, Freescale Semiconductor, Inc.
3
+ * Copyright 2016 - 2019, NXP
3
4
* All rights reserved.
4
5
*
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.
6
+ * SPDX-License-Identifier: BSD-3-Clause
29
7
*/
30
8
31
- #include "fsl_common.h"
32
9
#include "fsl_clock.h"
33
10
34
11
/*******************************************************************************
35
12
* Definitions
36
13
******************************************************************************/
37
14
15
+ /* Component ID definition, used by tools. */
16
+ #ifndef FSL_COMPONENT_ID
17
+ #define FSL_COMPONENT_ID "platform.drivers.clock"
18
+ #endif
19
+
38
20
#if (defined(OSC ) && !(defined(OSC0 )))
39
21
#define OSC0 OSC
40
22
#endif
57
39
******************************************************************************/
58
40
59
41
/* External XTAL0 (OSC0) clock frequency. */
60
- uint32_t g_xtal0Freq ;
61
-
42
+ volatile uint32_t g_xtal0Freq ;
62
43
/* External XTAL32K clock frequency. */
63
- uint32_t g_xtal32Freq ;
44
+ volatile uint32_t g_xtal32Freq ;
64
45
65
46
/*******************************************************************************
66
47
* Prototypes
@@ -126,6 +107,11 @@ static uint8_t CLOCK_GetOscRangeFromFreq(uint32_t freq)
126
107
return range ;
127
108
}
128
109
110
+ /*!
111
+ * brief Get the OSC0 external reference clock frequency (OSC0ERCLK).
112
+ *
113
+ * return Clock frequency in Hz.
114
+ */
129
115
uint32_t CLOCK_GetOsc0ErClkFreq (void )
130
116
{
131
117
if (OSC0 -> CR & OSC_CR_ERCLKEN_MASK )
@@ -140,6 +126,11 @@ uint32_t CLOCK_GetOsc0ErClkFreq(void)
140
126
}
141
127
}
142
128
129
+ /*!
130
+ * brief Get the external reference 32K clock frequency (ERCLK32K).
131
+ *
132
+ * return Clock frequency in Hz.
133
+ */
143
134
uint32_t CLOCK_GetEr32kClkFreq (void )
144
135
{
145
136
uint32_t freq ;
@@ -164,11 +155,21 @@ uint32_t CLOCK_GetEr32kClkFreq(void)
164
155
return freq ;
165
156
}
166
157
158
+ /*!
159
+ * brief Get the platform clock frequency.
160
+ *
161
+ * return Clock frequency in Hz.
162
+ */
167
163
uint32_t CLOCK_GetPlatClkFreq (void )
168
164
{
169
165
return CLOCK_GetOutClkFreq () / (SIM_CLKDIV1_OUTDIV1_VAL + 1 );
170
166
}
171
167
168
+ /*!
169
+ * brief Get the flash clock frequency.
170
+ *
171
+ * return Clock frequency in Hz.
172
+ */
172
173
uint32_t CLOCK_GetFlashClkFreq (void )
173
174
{
174
175
uint32_t freq ;
@@ -179,6 +180,11 @@ uint32_t CLOCK_GetFlashClkFreq(void)
179
180
return freq ;
180
181
}
181
182
183
+ /*!
184
+ * brief Get the bus clock frequency.
185
+ *
186
+ * return Clock frequency in Hz.
187
+ */
182
188
uint32_t CLOCK_GetBusClkFreq (void )
183
189
{
184
190
uint32_t freq ;
@@ -189,11 +195,26 @@ uint32_t CLOCK_GetBusClkFreq(void)
189
195
return freq ;
190
196
}
191
197
198
+ /*!
199
+ * brief Get the core clock or system clock frequency.
200
+ *
201
+ * return Clock frequency in Hz.
202
+ */
192
203
uint32_t CLOCK_GetCoreSysClkFreq (void )
193
204
{
194
205
return CLOCK_GetOutClkFreq () / (SIM_CLKDIV1_OUTDIV1_VAL + 1 );
195
206
}
196
207
208
+ /*!
209
+ * brief Gets the clock frequency for a specific clock name.
210
+ *
211
+ * This function checks the current clock configurations and then calculates
212
+ * the clock frequency for a specific clock name defined in clock_name_t.
213
+ * The MCG must be properly configured before using this function.
214
+ *
215
+ * param clockName Clock names defined in clock_name_t
216
+ * return Clock frequency value in Hertz
217
+ */
197
218
uint32_t CLOCK_GetFreq (clock_name_t clockName )
198
219
{
199
220
uint32_t freq ;
@@ -233,12 +254,26 @@ uint32_t CLOCK_GetFreq(clock_name_t clockName)
233
254
return freq ;
234
255
}
235
256
257
+ /*!
258
+ * brief Set the clock configure in SIM module.
259
+ *
260
+ * This function sets system layer clock settings in SIM module.
261
+ *
262
+ * param config Pointer to the configure structure.
263
+ */
236
264
void CLOCK_SetSimConfig (sim_clock_config_t const * config )
237
265
{
238
266
SIM -> CLKDIV1 = config -> clkdiv1 ;
239
267
CLOCK_SetEr32kClock (config -> er32kSrc );
240
268
}
241
269
270
+ /*! brief Enable USB FS clock.
271
+ *
272
+ * param src USB FS clock source.
273
+ * param freq The frequency specified by src.
274
+ * retval true The clock is set successfully.
275
+ * retval false The clock source is invalid to get proper USB FS clock.
276
+ */
242
277
bool CLOCK_EnableUsbfs0Clock (clock_usb_src_t src , uint32_t freq )
243
278
{
244
279
bool ret = true;
@@ -265,6 +300,14 @@ bool CLOCK_EnableUsbfs0Clock(clock_usb_src_t src, uint32_t freq)
265
300
return ret ;
266
301
}
267
302
303
+ /*!
304
+ * brief Gets the MCG internal reference clock (MCGIRCLK) frequency.
305
+ *
306
+ * This function gets the MCG_Lite internal reference clock frequency in Hz based
307
+ * on the current MCG register value.
308
+ *
309
+ * return The frequency of MCGIRCLK.
310
+ */
268
311
uint32_t CLOCK_GetInternalRefClkFreq (void )
269
312
{
270
313
uint8_t divider1 = MCG_SC_FCRDIV_VAL ;
@@ -273,6 +316,14 @@ uint32_t CLOCK_GetInternalRefClkFreq(void)
273
316
return CLOCK_GetLircClkFreq () >> (divider1 + divider2 );
274
317
}
275
318
319
+ /*
320
+ * brief Gets the current MCGPCLK frequency.
321
+ *
322
+ * This function gets the MCGPCLK frequency in Hz based on the current MCG_Lite
323
+ * register settings.
324
+ *
325
+ * return The frequency of MCGPCLK.
326
+ */
276
327
uint32_t CLOCK_GetPeriphClkFreq (void )
277
328
{
278
329
/* Check whether the HIRC is enabled. */
@@ -286,6 +337,14 @@ uint32_t CLOCK_GetPeriphClkFreq(void)
286
337
}
287
338
}
288
339
340
+ /*!
341
+ * brief Gets the MCG_Lite output clock (MCGOUTCLK) frequency.
342
+ *
343
+ * This function gets the MCG_Lite output clock frequency in Hz based on the current
344
+ * MCG_Lite register value.
345
+ *
346
+ * return The frequency of MCGOUTCLK.
347
+ */
289
348
uint32_t CLOCK_GetOutClkFreq (void )
290
349
{
291
350
uint32_t freq ;
@@ -311,6 +370,13 @@ uint32_t CLOCK_GetOutClkFreq(void)
311
370
return freq ;
312
371
}
313
372
373
+ /*!
374
+ * brief Gets the current MCG_Lite mode.
375
+ *
376
+ * This function checks the MCG_Lite registers and determines the current MCG_Lite mode.
377
+ *
378
+ * return The current MCG_Lite mode or error code.
379
+ */
314
380
mcglite_mode_t CLOCK_GetMode (void )
315
381
{
316
382
mcglite_mode_t mode ;
@@ -341,6 +407,15 @@ mcglite_mode_t CLOCK_GetMode(void)
341
407
return mode ;
342
408
}
343
409
410
+ /*!
411
+ * brief Sets the MCG_Lite configuration.
412
+ *
413
+ * This function configures the MCG_Lite, includes the output clock source, MCGIRCLK
414
+ * settings, HIRC settings, and so on. See ref mcglite_config_t for details.
415
+ *
416
+ * param targetConfig Pointer to the target MCG_Lite mode configuration structure.
417
+ * return Error code.
418
+ */
344
419
status_t CLOCK_SetMcgliteConfig (mcglite_config_t const * targetConfig )
345
420
{
346
421
assert (targetConfig );
@@ -383,6 +458,13 @@ status_t CLOCK_SetMcgliteConfig(mcglite_config_t const *targetConfig)
383
458
return kStatus_Success ;
384
459
}
385
460
461
+ /*!
462
+ * brief Initializes the OSC0.
463
+ *
464
+ * This function initializes the OSC0 according to the board configuration.
465
+ *
466
+ * param config Pointer to the OSC0 configuration structure.
467
+ */
386
468
void CLOCK_InitOsc0 (osc_config_t const * config )
387
469
{
388
470
uint8_t range = CLOCK_GetOscRangeFromFreq (config -> freq );
@@ -401,8 +483,39 @@ void CLOCK_InitOsc0(osc_config_t const *config)
401
483
}
402
484
}
403
485
486
+ /*!
487
+ * brief Deinitializes the OSC0.
488
+ *
489
+ * This function deinitializes the OSC0.
490
+ */
404
491
void CLOCK_DeinitOsc0 (void )
405
492
{
406
493
OSC0 -> CR = 0U ;
407
494
MCG -> C2 &= MCG_C2_IRCS_MASK ;
408
495
}
496
+
497
+ /*!
498
+ * brief Delay at least for several microseconds.
499
+ * Please note that, this API will calculate the microsecond period with the maximum devices
500
+ * supported CPU frequency, so this API will only delay for at least the given microseconds, if precise
501
+ * delay count was needed, please implement a new timer count to achieve this function.
502
+ *
503
+ * param delay_us Delay time in unit of microsecond.
504
+ */
505
+ __attribute__((weak )) void SDK_DelayAtLeastUs (uint32_t delay_us )
506
+ {
507
+ assert (0U != delay_us );
508
+
509
+ uint32_t count = (uint32_t )USEC_TO_COUNT (delay_us , SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY );
510
+
511
+ /*
512
+ * Calculate the real delay count depend on the excute instructions cycles,
513
+ * users can change the divider value to adapt to the real IDE optimise level.
514
+ */
515
+ count = (count / 4U );
516
+
517
+ for (; count > 0UL ; count -- )
518
+ {
519
+ __NOP ();
520
+ }
521
+ }
0 commit comments