Skip to content

Commit 6c674ec

Browse files
committed
Kinetis MCUXpresso: Update the clock drivers
Updated driver includes 1. Bug fixes 2. Formatting updates. 3. Fix for warnings 4. Better code commenting Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 201a264 commit 6c674ec

File tree

20 files changed

+7844
-3048
lines changed

20 files changed

+7844
-3048
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/drivers/fsl_clock.c

Lines changed: 840 additions & 225 deletions
Large diffs are not rendered by default.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/drivers/fsl_clock.h

Lines changed: 153 additions & 145 deletions
Large diffs are not rendered by default.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/drivers/fsl_clock.c

Lines changed: 796 additions & 231 deletions
Large diffs are not rendered by default.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/drivers/fsl_clock.h

Lines changed: 160 additions & 129 deletions
Large diffs are not rendered by default.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/drivers/fsl_clock.c

Lines changed: 141 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
/*
22
* Copyright (c) 2015, Freescale Semiconductor, Inc.
3+
* Copyright 2016 - 2019, NXP
34
* All rights reserved.
45
*
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
297
*/
308

31-
#include "fsl_common.h"
329
#include "fsl_clock.h"
3310

3411
/*******************************************************************************
3512
* Definitions
3613
******************************************************************************/
3714

15+
/* Component ID definition, used by tools. */
16+
#ifndef FSL_COMPONENT_ID
17+
#define FSL_COMPONENT_ID "platform.drivers.clock"
18+
#endif
19+
3820
#if (defined(OSC) && !(defined(OSC0)))
3921
#define OSC0 OSC
4022
#endif
@@ -57,10 +39,9 @@
5739
******************************************************************************/
5840

5941
/* External XTAL0 (OSC0) clock frequency. */
60-
uint32_t g_xtal0Freq;
61-
42+
volatile uint32_t g_xtal0Freq;
6243
/* External XTAL32K clock frequency. */
63-
uint32_t g_xtal32Freq;
44+
volatile uint32_t g_xtal32Freq;
6445

6546
/*******************************************************************************
6647
* Prototypes
@@ -126,6 +107,11 @@ static uint8_t CLOCK_GetOscRangeFromFreq(uint32_t freq)
126107
return range;
127108
}
128109

110+
/*!
111+
* brief Get the OSC0 external reference clock frequency (OSC0ERCLK).
112+
*
113+
* return Clock frequency in Hz.
114+
*/
129115
uint32_t CLOCK_GetOsc0ErClkFreq(void)
130116
{
131117
if (OSC0->CR & OSC_CR_ERCLKEN_MASK)
@@ -140,6 +126,11 @@ uint32_t CLOCK_GetOsc0ErClkFreq(void)
140126
}
141127
}
142128

129+
/*!
130+
* brief Get the external reference 32K clock frequency (ERCLK32K).
131+
*
132+
* return Clock frequency in Hz.
133+
*/
143134
uint32_t CLOCK_GetEr32kClkFreq(void)
144135
{
145136
uint32_t freq;
@@ -164,11 +155,21 @@ uint32_t CLOCK_GetEr32kClkFreq(void)
164155
return freq;
165156
}
166157

158+
/*!
159+
* brief Get the platform clock frequency.
160+
*
161+
* return Clock frequency in Hz.
162+
*/
167163
uint32_t CLOCK_GetPlatClkFreq(void)
168164
{
169165
return CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV1_VAL + 1);
170166
}
171167

168+
/*!
169+
* brief Get the flash clock frequency.
170+
*
171+
* return Clock frequency in Hz.
172+
*/
172173
uint32_t CLOCK_GetFlashClkFreq(void)
173174
{
174175
uint32_t freq;
@@ -179,6 +180,11 @@ uint32_t CLOCK_GetFlashClkFreq(void)
179180
return freq;
180181
}
181182

183+
/*!
184+
* brief Get the bus clock frequency.
185+
*
186+
* return Clock frequency in Hz.
187+
*/
182188
uint32_t CLOCK_GetBusClkFreq(void)
183189
{
184190
uint32_t freq;
@@ -189,11 +195,26 @@ uint32_t CLOCK_GetBusClkFreq(void)
189195
return freq;
190196
}
191197

198+
/*!
199+
* brief Get the core clock or system clock frequency.
200+
*
201+
* return Clock frequency in Hz.
202+
*/
192203
uint32_t CLOCK_GetCoreSysClkFreq(void)
193204
{
194205
return CLOCK_GetOutClkFreq() / (SIM_CLKDIV1_OUTDIV1_VAL + 1);
195206
}
196207

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+
*/
197218
uint32_t CLOCK_GetFreq(clock_name_t clockName)
198219
{
199220
uint32_t freq;
@@ -233,12 +254,26 @@ uint32_t CLOCK_GetFreq(clock_name_t clockName)
233254
return freq;
234255
}
235256

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+
*/
236264
void CLOCK_SetSimConfig(sim_clock_config_t const *config)
237265
{
238266
SIM->CLKDIV1 = config->clkdiv1;
239267
CLOCK_SetEr32kClock(config->er32kSrc);
240268
}
241269

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+
*/
242277
bool CLOCK_EnableUsbfs0Clock(clock_usb_src_t src, uint32_t freq)
243278
{
244279
bool ret = true;
@@ -265,6 +300,14 @@ bool CLOCK_EnableUsbfs0Clock(clock_usb_src_t src, uint32_t freq)
265300
return ret;
266301
}
267302

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+
*/
268311
uint32_t CLOCK_GetInternalRefClkFreq(void)
269312
{
270313
uint8_t divider1 = MCG_SC_FCRDIV_VAL;
@@ -273,6 +316,14 @@ uint32_t CLOCK_GetInternalRefClkFreq(void)
273316
return CLOCK_GetLircClkFreq() >> (divider1 + divider2);
274317
}
275318

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+
*/
276327
uint32_t CLOCK_GetPeriphClkFreq(void)
277328
{
278329
/* Check whether the HIRC is enabled. */
@@ -286,6 +337,14 @@ uint32_t CLOCK_GetPeriphClkFreq(void)
286337
}
287338
}
288339

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+
*/
289348
uint32_t CLOCK_GetOutClkFreq(void)
290349
{
291350
uint32_t freq;
@@ -311,6 +370,13 @@ uint32_t CLOCK_GetOutClkFreq(void)
311370
return freq;
312371
}
313372

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+
*/
314380
mcglite_mode_t CLOCK_GetMode(void)
315381
{
316382
mcglite_mode_t mode;
@@ -341,6 +407,15 @@ mcglite_mode_t CLOCK_GetMode(void)
341407
return mode;
342408
}
343409

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+
*/
344419
status_t CLOCK_SetMcgliteConfig(mcglite_config_t const *targetConfig)
345420
{
346421
assert(targetConfig);
@@ -383,6 +458,13 @@ status_t CLOCK_SetMcgliteConfig(mcglite_config_t const *targetConfig)
383458
return kStatus_Success;
384459
}
385460

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+
*/
386468
void CLOCK_InitOsc0(osc_config_t const *config)
387469
{
388470
uint8_t range = CLOCK_GetOscRangeFromFreq(config->freq);
@@ -401,8 +483,39 @@ void CLOCK_InitOsc0(osc_config_t const *config)
401483
}
402484
}
403485

486+
/*!
487+
* brief Deinitializes the OSC0.
488+
*
489+
* This function deinitializes the OSC0.
490+
*/
404491
void CLOCK_DeinitOsc0(void)
405492
{
406493
OSC0->CR = 0U;
407494
MCG->C2 &= MCG_C2_IRCS_MASK;
408495
}
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

Comments
 (0)