mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Thu Apr 03 11:45:06 2014 +0100
Revision:
149:1fb5f62b92bd
Parent:
targets/hal/TARGET_Freescale/TARGET_KSDK_MCUS/TARGET_KSDK_CODE/drivers/clock/src/fsl_clock_manager.c@146:f64d43ff0c18
Synchronized with git revision 220c0bb39ceee40016e1e86350c058963d01ed42

Full URL: https://github.com/mbedmicro/mbed/commit/220c0bb39ceee40016e1e86350c058963d01ed42/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 146:f64d43ff0c18 1 /*
mbed_official 146:f64d43ff0c18 2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
mbed_official 146:f64d43ff0c18 3 * All rights reserved.
mbed_official 146:f64d43ff0c18 4 *
mbed_official 146:f64d43ff0c18 5 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 146:f64d43ff0c18 6 * are permitted provided that the following conditions are met:
mbed_official 146:f64d43ff0c18 7 *
mbed_official 146:f64d43ff0c18 8 * o Redistributions of source code must retain the above copyright notice, this list
mbed_official 146:f64d43ff0c18 9 * of conditions and the following disclaimer.
mbed_official 146:f64d43ff0c18 10 *
mbed_official 146:f64d43ff0c18 11 * o Redistributions in binary form must reproduce the above copyright notice, this
mbed_official 146:f64d43ff0c18 12 * list of conditions and the following disclaimer in the documentation and/or
mbed_official 146:f64d43ff0c18 13 * other materials provided with the distribution.
mbed_official 146:f64d43ff0c18 14 *
mbed_official 146:f64d43ff0c18 15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
mbed_official 146:f64d43ff0c18 16 * contributors may be used to endorse or promote products derived from this
mbed_official 146:f64d43ff0c18 17 * software without specific prior written permission.
mbed_official 146:f64d43ff0c18 18 *
mbed_official 146:f64d43ff0c18 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
mbed_official 146:f64d43ff0c18 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
mbed_official 146:f64d43ff0c18 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 146:f64d43ff0c18 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
mbed_official 146:f64d43ff0c18 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
mbed_official 146:f64d43ff0c18 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
mbed_official 146:f64d43ff0c18 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
mbed_official 146:f64d43ff0c18 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
mbed_official 146:f64d43ff0c18 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
mbed_official 146:f64d43ff0c18 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 146:f64d43ff0c18 29 */
mbed_official 146:f64d43ff0c18 30
mbed_official 146:f64d43ff0c18 31 #include "fsl_device_registers.h"
mbed_official 146:f64d43ff0c18 32 #include "fsl_sim_hal.h"
mbed_official 146:f64d43ff0c18 33 #include "fsl_clock_manager.h"
mbed_official 146:f64d43ff0c18 34 #include "fsl_clock_manager_private.h"
mbed_official 146:f64d43ff0c18 35
mbed_official 146:f64d43ff0c18 36 /*******************************************************************************
mbed_official 146:f64d43ff0c18 37 * Definitions
mbed_official 146:f64d43ff0c18 38 ******************************************************************************/
mbed_official 146:f64d43ff0c18 39
mbed_official 146:f64d43ff0c18 40 /* internal function for clock manager. see function header for details */
mbed_official 146:f64d43ff0c18 41 static clock_manager_error_code_t clock_manager_get_frequency_by_name(sim_clock_names_t clockName,
mbed_official 146:f64d43ff0c18 42 uint32_t *frequency);
mbed_official 146:f64d43ff0c18 43
mbed_official 146:f64d43ff0c18 44 /*******************************************************************************
mbed_official 146:f64d43ff0c18 45 * Code
mbed_official 146:f64d43ff0c18 46 ******************************************************************************/
mbed_official 146:f64d43ff0c18 47
mbed_official 146:f64d43ff0c18 48 /*FUNCTION**********************************************************************
mbed_official 146:f64d43ff0c18 49 *
mbed_official 146:f64d43ff0c18 50 * Function Name : clock_manager_get_system_clock
mbed_official 146:f64d43ff0c18 51 * Description : Internal function to get the system clock frequency
mbed_official 146:f64d43ff0c18 52 * This function will check the clock name configuration table for specific
mbed_official 146:f64d43ff0c18 53 * chip family and find out the supported clock name for that chip family
mbed_official 146:f64d43ff0c18 54 * then it will call the mcg hal function to get the basic system clock,
mbed_official 146:f64d43ff0c18 55 * calculate the clock frequency for specified clock name.
mbed_official 146:f64d43ff0c18 56 *
mbed_official 146:f64d43ff0c18 57 *END**************************************************************************/
mbed_official 146:f64d43ff0c18 58 clock_manager_error_code_t clock_manager_get_system_clock(sim_clock_names_t clockName,
mbed_official 146:f64d43ff0c18 59 uint32_t *frequency)
mbed_official 146:f64d43ff0c18 60 {
mbed_official 146:f64d43ff0c18 61 /* system clock out divider*/
mbed_official 146:f64d43ff0c18 62 uint32_t divider;
mbed_official 146:f64d43ff0c18 63 uint32_t i = 0;
mbed_official 146:f64d43ff0c18 64 const sim_clock_name_config_t *table = NULL;
mbed_official 146:f64d43ff0c18 65
mbed_official 146:f64d43ff0c18 66 /* search through whole name table*/
mbed_official 146:f64d43ff0c18 67 while (kSimClockNameConfigTable[i].clockName != kSimClockNameCount)
mbed_official 146:f64d43ff0c18 68 {
mbed_official 146:f64d43ff0c18 69 /* find only the match with name*/
mbed_official 146:f64d43ff0c18 70 if (kSimClockNameConfigTable[i].clockName == clockName)
mbed_official 146:f64d43ff0c18 71 {
mbed_official 146:f64d43ff0c18 72 /* return the table pointer*/
mbed_official 146:f64d43ff0c18 73 table = &kSimClockNameConfigTable[i];
mbed_official 146:f64d43ff0c18 74 break;
mbed_official 146:f64d43ff0c18 75 }
mbed_official 146:f64d43ff0c18 76 i++;
mbed_official 146:f64d43ff0c18 77 }
mbed_official 146:f64d43ff0c18 78
mbed_official 146:f64d43ff0c18 79 /* if the configuration table doesn't exist, return error */
mbed_official 146:f64d43ff0c18 80 if (table == NULL)
mbed_official 146:f64d43ff0c18 81 {
mbed_official 146:f64d43ff0c18 82 return kClockManagerNoSuchClockName;
mbed_official 146:f64d43ff0c18 83 }
mbed_official 146:f64d43ff0c18 84
mbed_official 146:f64d43ff0c18 85 /* check if we need to use a reference clock*/
mbed_official 146:f64d43ff0c18 86 if (table->useOtherRefClock)
mbed_official 146:f64d43ff0c18 87 {
mbed_official 146:f64d43ff0c18 88 /* get other specified ref clock*/
mbed_official 146:f64d43ff0c18 89 if ( kClockManagerSuccess != clock_manager_get_frequency_by_name(table->otherRefClockName,
mbed_official 146:f64d43ff0c18 90 frequency) )
mbed_official 146:f64d43ff0c18 91 {
mbed_official 146:f64d43ff0c18 92 return kClockManagerNoSuchClockName;
mbed_official 146:f64d43ff0c18 93 }
mbed_official 146:f64d43ff0c18 94 }
mbed_official 146:f64d43ff0c18 95 else
mbed_official 146:f64d43ff0c18 96 {
mbed_official 146:f64d43ff0c18 97 /* get default ref clock */
mbed_official 146:f64d43ff0c18 98 *frequency = clock_hal_get_outclk();
mbed_official 146:f64d43ff0c18 99 }
mbed_official 146:f64d43ff0c18 100
mbed_official 146:f64d43ff0c18 101 /* get system clock divider*/
mbed_official 146:f64d43ff0c18 102 if ( clock_hal_get_clock_divider(table->dividerName, &divider) == kSimHalSuccess)
mbed_official 146:f64d43ff0c18 103 {
mbed_official 146:f64d43ff0c18 104 /* get the frequency for the specified clock*/
mbed_official 146:f64d43ff0c18 105 *frequency = (*frequency) / (divider + 1);
mbed_official 146:f64d43ff0c18 106 return kClockManagerSuccess;
mbed_official 146:f64d43ff0c18 107 }
mbed_official 146:f64d43ff0c18 108 else
mbed_official 146:f64d43ff0c18 109 {
mbed_official 146:f64d43ff0c18 110 return kClockManagerNoSuchDivider;
mbed_official 146:f64d43ff0c18 111 }
mbed_official 146:f64d43ff0c18 112 }
mbed_official 146:f64d43ff0c18 113
mbed_official 146:f64d43ff0c18 114 /*FUNCTION**********************************************************************
mbed_official 146:f64d43ff0c18 115 *
mbed_official 146:f64d43ff0c18 116 * Function Name : clock_manager_set_gate
mbed_official 146:f64d43ff0c18 117 * Description : Enable or disable the clock for specified clock module
mbed_official 146:f64d43ff0c18 118 * This function will enable/disable the clock for specified clock module and
mbed_official 146:f64d43ff0c18 119 * instance. Refer to clock_gate_module_names_t for supported clock module name
mbed_official 146:f64d43ff0c18 120 * by this function and refer to reference manual for supported clock moulde
mbed_official 146:f64d43ff0c18 121 * name for a specified chip family. Most module driver will call this function
mbed_official 146:f64d43ff0c18 122 * to gate(disable)/ungate(enable) the clock for that module, but application
mbed_official 146:f64d43ff0c18 123 * can also call this function as needed. Disable the clock will make the module
mbed_official 146:f64d43ff0c18 124 * stop working. Refer to the reference maunal for proper procedure of enalbing
mbed_official 146:f64d43ff0c18 125 * and disabling the clock for the device module.
mbed_official 146:f64d43ff0c18 126 *
mbed_official 146:f64d43ff0c18 127 *END**************************************************************************/
mbed_official 146:f64d43ff0c18 128 clock_manager_error_code_t clock_manager_set_gate(clock_gate_module_names_t moduleName,
mbed_official 146:f64d43ff0c18 129 uint8_t instance, bool enable)
mbed_official 146:f64d43ff0c18 130 {
mbed_official 146:f64d43ff0c18 131 /* calling hal api, to set(Enable/Disable) the clock module gate */
mbed_official 146:f64d43ff0c18 132 if (clock_hal_set_gate(kClockModuleNameSimMap[moduleName], instance, enable) == kSimHalSuccess)
mbed_official 146:f64d43ff0c18 133 {
mbed_official 146:f64d43ff0c18 134 return kClockManagerSuccess;
mbed_official 146:f64d43ff0c18 135 }
mbed_official 146:f64d43ff0c18 136 else
mbed_official 146:f64d43ff0c18 137 {
mbed_official 146:f64d43ff0c18 138 return kClockManagerNoSuchClockModule;
mbed_official 146:f64d43ff0c18 139 }
mbed_official 146:f64d43ff0c18 140 }
mbed_official 146:f64d43ff0c18 141
mbed_official 146:f64d43ff0c18 142 /*FUNCTION**********************************************************************
mbed_official 146:f64d43ff0c18 143 *
mbed_official 146:f64d43ff0c18 144 * Function Name : clock_manager_get_gate
mbed_official 146:f64d43ff0c18 145 * Description : Get the current clock gate status for specified clock module
mbed_official 146:f64d43ff0c18 146 * This function will return the current clock gate status for specified clock
mbed_official 146:f64d43ff0c18 147 * module. Refer to clock_gate_module_names_t for supported clock module name.
mbed_official 146:f64d43ff0c18 148 *
mbed_official 146:f64d43ff0c18 149 *END**************************************************************************/
mbed_official 146:f64d43ff0c18 150 clock_manager_error_code_t clock_manager_get_gate(clock_gate_module_names_t moduleName,
mbed_official 146:f64d43ff0c18 151 uint8_t instance, bool *isEnabled)
mbed_official 146:f64d43ff0c18 152 {
mbed_official 146:f64d43ff0c18 153 /* calling the hal function to get the clock module gate status */
mbed_official 146:f64d43ff0c18 154 if (clock_hal_get_gate(kClockModuleNameSimMap[moduleName], instance, isEnabled)
mbed_official 146:f64d43ff0c18 155 == kSimHalSuccess)
mbed_official 146:f64d43ff0c18 156 {
mbed_official 146:f64d43ff0c18 157 return kClockManagerSuccess;
mbed_official 146:f64d43ff0c18 158 }
mbed_official 146:f64d43ff0c18 159 else
mbed_official 146:f64d43ff0c18 160 {
mbed_official 146:f64d43ff0c18 161 return kClockManagerNoSuchClockModule;
mbed_official 146:f64d43ff0c18 162 }
mbed_official 146:f64d43ff0c18 163 }
mbed_official 146:f64d43ff0c18 164
mbed_official 146:f64d43ff0c18 165 /*FUNCTION**********************************************************************
mbed_official 146:f64d43ff0c18 166 *
mbed_official 146:f64d43ff0c18 167 * Function Name : clock_manager_get_frequency
mbed_official 146:f64d43ff0c18 168 * Description : Get the clock frequency for specified clock name
mbed_official 146:f64d43ff0c18 169 * This function will check the current clock configurations and then calculate
mbed_official 146:f64d43ff0c18 170 * the clock frequency for specified clock name defined in clock_names_t.
mbed_official 146:f64d43ff0c18 171 * The MCG must be properly configured before using this function. Also check
mbed_official 146:f64d43ff0c18 172 * the reference manual for supported clock names on different chip falmily.
mbed_official 146:f64d43ff0c18 173 * The returned value will be in herz. And if it cannot find the clock name
mbed_official 146:f64d43ff0c18 174 * or the name is not supported on specific chip family, it will return an
mbed_official 146:f64d43ff0c18 175 * error.
mbed_official 146:f64d43ff0c18 176 *
mbed_official 146:f64d43ff0c18 177 *END**************************************************************************/
mbed_official 146:f64d43ff0c18 178 clock_manager_error_code_t clock_manager_get_frequency(clock_names_t clockName,
mbed_official 146:f64d43ff0c18 179 uint32_t *frequency)
mbed_official 146:f64d43ff0c18 180 {
mbed_official 146:f64d43ff0c18 181 /* convert clock manager clock name to sim clock name */
mbed_official 146:f64d43ff0c18 182 sim_clock_names_t simClockName = kClockNameSimMap[clockName];
mbed_official 146:f64d43ff0c18 183
mbed_official 146:f64d43ff0c18 184 /* calling internal get frequency by name function */
mbed_official 146:f64d43ff0c18 185 return clock_manager_get_frequency_by_name(simClockName, frequency);
mbed_official 146:f64d43ff0c18 186 }
mbed_official 146:f64d43ff0c18 187
mbed_official 146:f64d43ff0c18 188 /*FUNCTION**********************************************************************
mbed_official 146:f64d43ff0c18 189 *
mbed_official 146:f64d43ff0c18 190 * Function Name : clock_manager_get_frequency_by_name
mbed_official 146:f64d43ff0c18 191 * Description : Internal function to get the frequency by clock name
mbed_official 146:f64d43ff0c18 192 * This function will get/calculate the clock frequency based on clock name
mbed_official 146:f64d43ff0c18 193 * and current configuration of clock generator.
mbed_official 146:f64d43ff0c18 194 *
mbed_official 146:f64d43ff0c18 195 *END**************************************************************************/
mbed_official 146:f64d43ff0c18 196 clock_manager_error_code_t static clock_manager_get_frequency_by_name(sim_clock_names_t clockName,
mbed_official 146:f64d43ff0c18 197 uint32_t *frequency)
mbed_official 146:f64d43ff0c18 198 {
mbed_official 146:f64d43ff0c18 199 /* branch according to clock name */
mbed_official 146:f64d43ff0c18 200 switch(clockName)
mbed_official 146:f64d43ff0c18 201 {
mbed_official 146:f64d43ff0c18 202 /* osc clock*/
mbed_official 146:f64d43ff0c18 203 case kSimOsc32kClock:
mbed_official 146:f64d43ff0c18 204 /* *frequency = CPU_XTAL32k_CLK_HZ; // or should provided by osc module??*/
mbed_official 146:f64d43ff0c18 205 *frequency = 32768;
mbed_official 146:f64d43ff0c18 206 break;
mbed_official 146:f64d43ff0c18 207 case kSimOsc0ErClock:
mbed_official 146:f64d43ff0c18 208 /* *frequency = CPU_XTAL_CLK_HZ; // or should provided by osc module??*/
mbed_official 146:f64d43ff0c18 209 *frequency = 80000; /* for testing purpose only*/
mbed_official 146:f64d43ff0c18 210 break;
mbed_official 146:f64d43ff0c18 211 case kSimOsc1ErClock:
mbed_official 146:f64d43ff0c18 212 /* *frequency = CPU_XTAL_CLK_HZ; // or should provided by osc module??*/
mbed_official 146:f64d43ff0c18 213 *frequency = 80001; /* for testing purpose only*/
mbed_official 146:f64d43ff0c18 214 break;
mbed_official 146:f64d43ff0c18 215
mbed_official 146:f64d43ff0c18 216 /* irc clock*/
mbed_official 146:f64d43ff0c18 217 case kSimIrc48mClock:
mbed_official 146:f64d43ff0c18 218 /* *frequency = CPU_INT_IRC_CLK_HZ; // or should provided by irc module??*/
mbed_official 146:f64d43ff0c18 219 *frequency = 48000;
mbed_official 146:f64d43ff0c18 220 break;
mbed_official 146:f64d43ff0c18 221
mbed_official 146:f64d43ff0c18 222 /* rtc clock*/
mbed_official 146:f64d43ff0c18 223 case kSimRtc32kClock:
mbed_official 146:f64d43ff0c18 224 /* *frequency = CPU_XTAL32k_CLK_HZ; // or should provided by rtc module??*/
mbed_official 146:f64d43ff0c18 225 *frequency = 32768;
mbed_official 146:f64d43ff0c18 226 break;
mbed_official 146:f64d43ff0c18 227
mbed_official 146:f64d43ff0c18 228 case kSimRtc1hzClock:
mbed_official 146:f64d43ff0c18 229 /* *frequency = CPU_XTAL1hz_CLK_HZ; // or should provided by rtc module??*/
mbed_official 146:f64d43ff0c18 230 *frequency = 1;
mbed_official 146:f64d43ff0c18 231 break;
mbed_official 146:f64d43ff0c18 232
mbed_official 146:f64d43ff0c18 233 /* lpo clcok*/
mbed_official 146:f64d43ff0c18 234 case kSimLpoClock:
mbed_official 146:f64d43ff0c18 235 /* *frequency = 1000; // where should we define this constant??*/
mbed_official 146:f64d43ff0c18 236 *frequency = 1000;
mbed_official 146:f64d43ff0c18 237 break;
mbed_official 146:f64d43ff0c18 238
mbed_official 146:f64d43ff0c18 239 /* mcg clocks, calling mcg clock functions */
mbed_official 146:f64d43ff0c18 240 case kSimMcgFllClock:
mbed_official 146:f64d43ff0c18 241 *frequency = clock_hal_get_fllclk();
mbed_official 146:f64d43ff0c18 242 break;
mbed_official 146:f64d43ff0c18 243 case kSimMcgPll0Clock:
mbed_official 146:f64d43ff0c18 244 *frequency = clock_hal_get_pll0clk();
mbed_official 146:f64d43ff0c18 245 break;
mbed_official 146:f64d43ff0c18 246 case kSimMcgOutClock:
mbed_official 146:f64d43ff0c18 247 *frequency = clock_hal_get_outclk();
mbed_official 146:f64d43ff0c18 248 break;
mbed_official 146:f64d43ff0c18 249 case kSimMcgIrClock:
mbed_official 146:f64d43ff0c18 250 *frequency = clock_hal_get_irclk();
mbed_official 146:f64d43ff0c18 251 break;
mbed_official 146:f64d43ff0c18 252
mbed_official 146:f64d43ff0c18 253 /* constant clocks (provided in other header files - TBD)*/
mbed_official 146:f64d43ff0c18 254 case kSimSDHC0_CLKIN:
mbed_official 146:f64d43ff0c18 255 *frequency = 11111; /* for testing use purpose*/
mbed_official 146:f64d43ff0c18 256 break;
mbed_official 146:f64d43ff0c18 257 case kSimENET_1588_CLKIN:
mbed_official 146:f64d43ff0c18 258 *frequency = 22222; /* for testing use purpose*/
mbed_official 146:f64d43ff0c18 259 break;
mbed_official 146:f64d43ff0c18 260 case kSimEXTAL_Clock:
mbed_official 146:f64d43ff0c18 261 /* *frequency = CPU_XTAL_CLK_HZ; // is this correct??*/
mbed_official 146:f64d43ff0c18 262 *frequency = 33333; /* for testing use purpose*/
mbed_official 146:f64d43ff0c18 263 break;
mbed_official 146:f64d43ff0c18 264 case kSimEXTAL1_Clock:
mbed_official 146:f64d43ff0c18 265 /* *frequency = CPU_XTAL_CLK_HZ; // is this correct??*/
mbed_official 146:f64d43ff0c18 266 *frequency = 33334; /* for testing use purpose*/
mbed_official 146:f64d43ff0c18 267 break;
mbed_official 146:f64d43ff0c18 268 case kSimUSB_CLKIN:
mbed_official 146:f64d43ff0c18 269 *frequency = 44444; /* for testing use purpose*/
mbed_official 146:f64d43ff0c18 270 break;
mbed_official 146:f64d43ff0c18 271
mbed_official 146:f64d43ff0c18 272 /* system clocks */
mbed_official 146:f64d43ff0c18 273 case kSimCoreClock:
mbed_official 146:f64d43ff0c18 274 case kSimSystemClock:
mbed_official 146:f64d43ff0c18 275 case kSimPlatformClock:
mbed_official 146:f64d43ff0c18 276 case kSimBusClock:
mbed_official 146:f64d43ff0c18 277 case kSimFlexBusClock:
mbed_official 146:f64d43ff0c18 278 case kSimFlashClock:
mbed_official 146:f64d43ff0c18 279 return clock_manager_get_system_clock(clockName, frequency);
mbed_official 146:f64d43ff0c18 280
mbed_official 146:f64d43ff0c18 281
mbed_official 146:f64d43ff0c18 282 /* reserved value*/
mbed_official 146:f64d43ff0c18 283 case kSimReserved:
mbed_official 146:f64d43ff0c18 284 default:
mbed_official 146:f64d43ff0c18 285 *frequency = 55555; /* for testing use purpose*/
mbed_official 146:f64d43ff0c18 286 return kClockManagerNoSuchClockName;
mbed_official 146:f64d43ff0c18 287 }
mbed_official 146:f64d43ff0c18 288
mbed_official 146:f64d43ff0c18 289 return kClockManagerSuccess;
mbed_official 146:f64d43ff0c18 290 }
mbed_official 146:f64d43ff0c18 291
mbed_official 146:f64d43ff0c18 292 /*FUNCTION**********************************************************************
mbed_official 146:f64d43ff0c18 293 *
mbed_official 146:f64d43ff0c18 294 * Function Name : clock_manager_get_frequency_by_source
mbed_official 146:f64d43ff0c18 295 * Description : Get the clock frequency for specified clock source
mbed_official 146:f64d43ff0c18 296 * This function will get the specified clock source setting and convert it
mbed_official 146:f64d43ff0c18 297 * into a clock name, then calling the internal function to find out the value
mbed_official 146:f64d43ff0c18 298 * for that clock name. Also the returned value is in herz.
mbed_official 146:f64d43ff0c18 299 * If it cannot find the clock source or the source is not supported on the
mbed_official 146:f64d43ff0c18 300 * specific chip family, it will return an error.
mbed_official 146:f64d43ff0c18 301 *
mbed_official 146:f64d43ff0c18 302 *END**************************************************************************/
mbed_official 146:f64d43ff0c18 303 clock_manager_error_code_t clock_manager_get_frequency_by_source(clock_source_names_t clockSource,
mbed_official 146:f64d43ff0c18 304 uint32_t *frequency)
mbed_official 146:f64d43ff0c18 305 {
mbed_official 146:f64d43ff0c18 306 uint8_t setting;
mbed_official 146:f64d43ff0c18 307 uint8_t subsetting1;
mbed_official 146:f64d43ff0c18 308 uint8_t subsetting2;
mbed_official 146:f64d43ff0c18 309 uint32_t frac = 0;
mbed_official 146:f64d43ff0c18 310 uint32_t divider = 0;
mbed_official 146:f64d43ff0c18 311 sim_clock_names_t simClockName;
mbed_official 146:f64d43ff0c18 312 sim_clock_source_names_t simClockSource;
mbed_official 146:f64d43ff0c18 313 sim_clock_divider_names_t dividerName;
mbed_official 146:f64d43ff0c18 314 const sim_clock_source_value_t *valueTable = NULL;
mbed_official 146:f64d43ff0c18 315 const sim_clock_source_value_t *subValueTable1 = NULL;
mbed_official 146:f64d43ff0c18 316 const sim_clock_source_value_t *subValueTable2 = NULL;
mbed_official 146:f64d43ff0c18 317
mbed_official 146:f64d43ff0c18 318 assert (clockSource < kClockSourceMax);
mbed_official 146:f64d43ff0c18 319
mbed_official 146:f64d43ff0c18 320 /* convert clock manager clock source to sim clock source*/
mbed_official 146:f64d43ff0c18 321 simClockSource = kClockSourceNameSimMap[clockSource];
mbed_official 146:f64d43ff0c18 322
mbed_official 146:f64d43ff0c18 323 /* check to see if we need to use clock sel for specified clock source (see if CLKSEL exists)*/
mbed_official 146:f64d43ff0c18 324 switch (simClockSource)
mbed_official 146:f64d43ff0c18 325 {
mbed_official 146:f64d43ff0c18 326 case kSimClockNfcSrc: /* NFCSRC*/
mbed_official 146:f64d43ff0c18 327 simClockSource = kSimClockNfcSel;
mbed_official 146:f64d43ff0c18 328 break;
mbed_official 146:f64d43ff0c18 329 case kSimClockLcdcSrc: /* LCDCSRC*/
mbed_official 146:f64d43ff0c18 330 simClockSource = kSimClockLcdcSel;
mbed_official 146:f64d43ff0c18 331 break;
mbed_official 146:f64d43ff0c18 332 case kSimClockUsbfSrc: /* USBFSRC K70*/
mbed_official 146:f64d43ff0c18 333 simClockSource = kSimClockUsbfSel;
mbed_official 146:f64d43ff0c18 334 break;
mbed_official 146:f64d43ff0c18 335 default:
mbed_official 146:f64d43ff0c18 336 break;
mbed_official 146:f64d43ff0c18 337 }
mbed_official 146:f64d43ff0c18 338
mbed_official 146:f64d43ff0c18 339 /* get the sim clock source setting*/
mbed_official 146:f64d43ff0c18 340 if (clock_hal_get_clock_source(simClockSource, &setting) != kSimHalSuccess)
mbed_official 146:f64d43ff0c18 341 {
mbed_official 146:f64d43ff0c18 342 return kClockManagerNoSuchClockSource;
mbed_official 146:f64d43ff0c18 343 }
mbed_official 146:f64d43ff0c18 344
mbed_official 146:f64d43ff0c18 345 /* get the value index table for the clock source*/
mbed_official 146:f64d43ff0c18 346 valueTable = kSimClockSourceValueTable[simClockSource];
mbed_official 146:f64d43ff0c18 347
mbed_official 146:f64d43ff0c18 348 /* if the source exists*/
mbed_official 146:f64d43ff0c18 349 if (valueTable == NULL)
mbed_official 146:f64d43ff0c18 350 {
mbed_official 146:f64d43ff0c18 351 return kClockManagerNoSuchClockSource;
mbed_official 146:f64d43ff0c18 352 }
mbed_official 146:f64d43ff0c18 353
mbed_official 146:f64d43ff0c18 354 /* check if it is a clock name or selection*/
mbed_official 146:f64d43ff0c18 355 if (valueTable[setting].isSel)
mbed_official 146:f64d43ff0c18 356 {
mbed_official 146:f64d43ff0c18 357
mbed_official 146:f64d43ff0c18 358 /* if it is a selection, then convert the selection into a clock name*/
mbed_official 146:f64d43ff0c18 359 /* get the config table for the clock source*/
mbed_official 146:f64d43ff0c18 360 if (clock_hal_get_clock_source(valueTable[setting].clockSelName, &subsetting1)
mbed_official 146:f64d43ff0c18 361 != kSimHalSuccess)
mbed_official 146:f64d43ff0c18 362 {
mbed_official 146:f64d43ff0c18 363 return kClockManagerNoSuchClockSource;
mbed_official 146:f64d43ff0c18 364 }
mbed_official 146:f64d43ff0c18 365
mbed_official 146:f64d43ff0c18 366 /* get the value index table for the clock source*/
mbed_official 146:f64d43ff0c18 367 subValueTable1 = kSimClockSourceValueTable[valueTable[setting].clockSelName];
mbed_official 146:f64d43ff0c18 368
mbed_official 146:f64d43ff0c18 369 /* check if there's bad data*/
mbed_official 146:f64d43ff0c18 370 if (subValueTable1 == NULL)
mbed_official 146:f64d43ff0c18 371 {
mbed_official 146:f64d43ff0c18 372 return kClockManagerNoSuchClockSource;
mbed_official 146:f64d43ff0c18 373 }
mbed_official 146:f64d43ff0c18 374
mbed_official 146:f64d43ff0c18 375 /* check if there's further clock select*/
mbed_official 146:f64d43ff0c18 376 if (subValueTable1[subsetting1].isSel)
mbed_official 146:f64d43ff0c18 377 {
mbed_official 146:f64d43ff0c18 378 /* if it is a selection, then convert the selection into a clock name*/
mbed_official 146:f64d43ff0c18 379 /* get the config table for the clock source*/
mbed_official 146:f64d43ff0c18 380 if (clock_hal_get_clock_source(subValueTable1[subsetting1].clockSelName, &subsetting2)
mbed_official 146:f64d43ff0c18 381 != kSimHalSuccess)
mbed_official 146:f64d43ff0c18 382 {
mbed_official 146:f64d43ff0c18 383 return kClockManagerNoSuchClockSource;
mbed_official 146:f64d43ff0c18 384 }
mbed_official 146:f64d43ff0c18 385
mbed_official 146:f64d43ff0c18 386 /* get the value index table for the clock source*/
mbed_official 146:f64d43ff0c18 387 subValueTable2 = kSimClockSourceValueTable[subValueTable1[subsetting1].clockSelName];
mbed_official 146:f64d43ff0c18 388
mbed_official 146:f64d43ff0c18 389 /* check if there's bad data*/
mbed_official 146:f64d43ff0c18 390 if (subValueTable2 == NULL)
mbed_official 146:f64d43ff0c18 391 {
mbed_official 146:f64d43ff0c18 392 return kClockManagerNoSuchClockSource;
mbed_official 146:f64d43ff0c18 393 }
mbed_official 146:f64d43ff0c18 394
mbed_official 146:f64d43ff0c18 395 /* further convert the subvalue to clock name */
mbed_official 146:f64d43ff0c18 396 simClockName = subValueTable2[subsetting2].clockName;
mbed_official 146:f64d43ff0c18 397 }
mbed_official 146:f64d43ff0c18 398 else
mbed_official 146:f64d43ff0c18 399 {
mbed_official 146:f64d43ff0c18 400 /* further convert the subvalue to clock name */
mbed_official 146:f64d43ff0c18 401 simClockName = subValueTable1[subsetting1].clockName;
mbed_official 146:f64d43ff0c18 402 }
mbed_official 146:f64d43ff0c18 403 }
mbed_official 146:f64d43ff0c18 404 else
mbed_official 146:f64d43ff0c18 405 {
mbed_official 146:f64d43ff0c18 406 /* convert the value to clock name */
mbed_official 146:f64d43ff0c18 407 simClockName = valueTable[setting].clockName;
mbed_official 146:f64d43ff0c18 408 }
mbed_official 146:f64d43ff0c18 409
mbed_official 146:f64d43ff0c18 410 /* get/calculate the frequency for the specified clock name*/
mbed_official 146:f64d43ff0c18 411 if ( clock_manager_get_frequency_by_name(simClockName, frequency) != kClockManagerSuccess)
mbed_official 146:f64d43ff0c18 412 {
mbed_official 146:f64d43ff0c18 413 return kClockManagerNoSuchClockName;
mbed_official 146:f64d43ff0c18 414 }
mbed_official 146:f64d43ff0c18 415
mbed_official 146:f64d43ff0c18 416 /* find the first available divider*/
mbed_official 146:f64d43ff0c18 417 if (valueTable[setting].hasDivider)
mbed_official 146:f64d43ff0c18 418 {
mbed_official 146:f64d43ff0c18 419 /* get the diviver name */
mbed_official 146:f64d43ff0c18 420 dividerName = valueTable[setting].dividerName;
mbed_official 146:f64d43ff0c18 421 }
mbed_official 146:f64d43ff0c18 422 else if ((subValueTable1 != NULL) && (subValueTable1[subsetting1].hasDivider))
mbed_official 146:f64d43ff0c18 423 {
mbed_official 146:f64d43ff0c18 424 /* get the diviver name */
mbed_official 146:f64d43ff0c18 425 dividerName = subValueTable1[subsetting1].dividerName;
mbed_official 146:f64d43ff0c18 426 }
mbed_official 146:f64d43ff0c18 427 else if ((subValueTable2 != NULL) && (subValueTable2[subsetting2].hasDivider))
mbed_official 146:f64d43ff0c18 428 {
mbed_official 146:f64d43ff0c18 429 /* get the diviver name */
mbed_official 146:f64d43ff0c18 430 dividerName = subValueTable2[subsetting2].dividerName;
mbed_official 146:f64d43ff0c18 431 }
mbed_official 146:f64d43ff0c18 432 else
mbed_official 146:f64d43ff0c18 433 {
mbed_official 146:f64d43ff0c18 434 return kClockManagerSuccess;
mbed_official 146:f64d43ff0c18 435 }
mbed_official 146:f64d43ff0c18 436
mbed_official 146:f64d43ff0c18 437 /* get the divider value*/
mbed_official 146:f64d43ff0c18 438 switch (dividerName)
mbed_official 146:f64d43ff0c18 439 {
mbed_official 146:f64d43ff0c18 440 case kSimClockDividerNfcDiv: /* NFCSRC*/
mbed_official 146:f64d43ff0c18 441 clock_hal_get_clock_divider(kSimClockDividerNfcDiv, &divider);
mbed_official 146:f64d43ff0c18 442 clock_hal_get_clock_divider(kSimClockDividerNfcFrac, &frac);
mbed_official 146:f64d43ff0c18 443 break;
mbed_official 146:f64d43ff0c18 444 case kSimClockDividerLcdcDiv: /* LCDCSRC*/
mbed_official 146:f64d43ff0c18 445 clock_hal_get_clock_divider(kSimClockDividerLcdcDiv, &divider);
mbed_official 146:f64d43ff0c18 446 clock_hal_get_clock_divider(kSimClockDividerLcdcFrac, &frac);
mbed_official 146:f64d43ff0c18 447 break;
mbed_official 146:f64d43ff0c18 448 case kSimClockDividerUsbfsDiv: /* USBFSRC K70*/
mbed_official 146:f64d43ff0c18 449 clock_hal_get_clock_divider(kSimClockDividerUsbfsDiv, &divider);
mbed_official 146:f64d43ff0c18 450 clock_hal_get_clock_divider(kSimClockDividerUsbfsFrac, &frac);
mbed_official 146:f64d43ff0c18 451 break;
mbed_official 146:f64d43ff0c18 452 case kSimClockDividerUsbDiv: /* USBSRC K64, KL25, KV31, and K22*/
mbed_official 146:f64d43ff0c18 453 clock_hal_get_clock_divider(kSimClockDividerUsbDiv, &divider);
mbed_official 146:f64d43ff0c18 454 clock_hal_get_clock_divider(kSimClockDividerUsbFrac, &frac);
mbed_official 146:f64d43ff0c18 455 break;
mbed_official 146:f64d43ff0c18 456 case kSimClockDividerUsbhsDiv: /* USBHSRC K70*/
mbed_official 146:f64d43ff0c18 457 clock_hal_get_clock_divider(kSimClockDividerUsbhsDiv, &divider);
mbed_official 146:f64d43ff0c18 458 clock_hal_get_clock_divider(kSimClockDividerUsbhsFrac, &frac);
mbed_official 146:f64d43ff0c18 459 break;
mbed_official 146:f64d43ff0c18 460 case kSimClockDividerSpecial1: /* Special KL25 divider 2*/
mbed_official 146:f64d43ff0c18 461 clock_hal_get_clock_divider(kSimClockDividerSpecial1, &divider);
mbed_official 146:f64d43ff0c18 462 break;
mbed_official 146:f64d43ff0c18 463 default:
mbed_official 146:f64d43ff0c18 464 break;
mbed_official 146:f64d43ff0c18 465 }
mbed_official 146:f64d43ff0c18 466
mbed_official 146:f64d43ff0c18 467 /* Divider output clock = Divider input clock × [ (USBFRAC+1) / (USBDIV+1) ]*/
mbed_official 146:f64d43ff0c18 468 *frequency = (*frequency) * (frac + 1) / (divider + 1);
mbed_official 146:f64d43ff0c18 469
mbed_official 146:f64d43ff0c18 470 return kClockManagerSuccess;
mbed_official 146:f64d43ff0c18 471 }
mbed_official 146:f64d43ff0c18 472
mbed_official 146:f64d43ff0c18 473 /*******************************************************************************
mbed_official 146:f64d43ff0c18 474 * EOF
mbed_official 146:f64d43ff0c18 475 ******************************************************************************/
mbed_official 146:f64d43ff0c18 476