4 * \brief PLL management
\r
6 * Copyright (c) 2010-2011 Atmel Corporation. All rights reserved.
\r
12 * Redistribution and use in source and binary forms, with or without
\r
13 * modification, are permitted provided that the following conditions are met:
\r
15 * 1. Redistributions of source code must retain the above copyright notice,
\r
16 * this list of conditions and the following disclaimer.
\r
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
19 * this list of conditions and the following disclaimer in the documentation
\r
20 * and/or other materials provided with the distribution.
\r
22 * 3. The name of Atmel may not be used to endorse or promote products derived
\r
23 * from this software without specific prior written permission.
\r
25 * 4. This software may only be redistributed and used in connection with an
\r
26 * Atmel microcontroller product.
\r
28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
\r
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
\r
32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
\r
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
38 * POSSIBILITY OF SUCH DAMAGE.
\r
43 #ifndef CLK_PLL_H_INCLUDED
\r
44 #define CLK_PLL_H_INCLUDED
\r
47 #include "conf_clock.h"
\r
50 # include "sam3s/pll.h"
\r
52 # include "sam3x/pll.h"
\r
54 # include "sam3u/pll.h"
\r
56 # include "sam3n/pll.h"
\r
58 # include "sam4s/pll.h"
\r
59 #elif (UC3A0 || UC3A1)
\r
60 # include "uc3a0_a1/pll.h"
\r
62 # include "uc3a3_a4/pll.h"
\r
64 # include "uc3b0_b1/pll.h"
\r
66 # include "uc3c/pll.h"
\r
68 # include "uc3d/pll.h"
\r
69 #elif (UC3L0128 || UC3L0256 || UC3L3_L4)
\r
70 # include "uc3l/pll.h"
\r
72 # include "xmega/pll.h"
\r
74 # error Unsupported chip type
\r
78 * \ingroup clk_group
\r
79 * \defgroup pll_group PLL Management
\r
81 * This group contains functions and definitions related to configuring
\r
82 * and enabling/disabling on-chip PLLs. A PLL will take an input signal
\r
83 * (the \em source), optionally divide the frequency by a configurable
\r
84 * \em divider, and then multiply the frequency by a configurable \em
\r
87 * Some devices don't support input dividers; specifying any other
\r
88 * divisor than 1 on these devices will result in an assertion failure.
\r
89 * Other devices may have various restrictions to the frequency range of
\r
90 * the input and output signals.
\r
92 * \par Example: Setting up PLL0 with default parameters
\r
94 * The following example shows how to configure and enable PLL0 using
\r
95 * the default parameters specified using the configuration symbols
\r
98 pll_enable_config_defaults(0); \endcode
\r
100 * To configure, enable PLL0 using the default parameters and to disable
\r
101 * a specific feature like Wide Bandwidth Mode (a UC3A3-specific
\r
102 * PLL option.), you can use this initialization process.
\r
104 struct pll_config pllcfg;
\r
105 if (pll_is_locked(pll_id)) {
\r
106 return; // Pll already running
\r
108 pll_enable_source(CONFIG_PLL0_SOURCE);
\r
109 pll_config_defaults(&pllcfg, 0);
\r
110 pll_config_set_option(&pllcfg, PLL_OPT_WBM_DISABLE);
\r
111 pll_enable(&pllcfg, 0);
\r
112 pll_wait_for_lock(0); \endcode
\r
114 * When the last function call returns, PLL0 is ready to be used as the
\r
115 * main system clock source.
\r
117 * \section pll_group_config Configuration Symbols
\r
119 * Each PLL has a set of default parameters determined by the following
\r
120 * configuration symbols in the application's configuration file:
\r
121 * - \b CONFIG_PLLn_SOURCE: The default clock source connected to the
\r
122 * input of PLL \a n. Must be one of the values defined by the
\r
123 * #pll_source enum.
\r
124 * - \b CONFIG_PLLn_MUL: The default multiplier (loop divider) of PLL
\r
126 * - \b CONFIG_PLLn_DIV: The default input divider of PLL \a n.
\r
128 * These configuration symbols determine the result of calling
\r
129 * pll_config_defaults() and pll_get_default_rate().
\r
134 //! \name Chip-specific PLL characteristics
\r
137 * \def PLL_MAX_STARTUP_CYCLES
\r
138 * \brief Maximum PLL startup time in number of slow clock cycles
\r
142 * \brief Number of on-chip PLLs
\r
147 * \brief Minimum frequency that the PLL can generate
\r
151 * \brief Maximum frequency that the PLL can generate
\r
154 * \def PLL_NR_OPTIONS
\r
155 * \brief Number of PLL option bits
\r
161 * \brief PLL clock source
\r
164 //! \name PLL configuration
\r
168 * \struct pll_config
\r
169 * \brief Hardware-specific representation of PLL configuration.
\r
171 * This structure contains one or more device-specific values
\r
172 * representing the current PLL configuration. The contents of this
\r
173 * structure is typically different from platform to platform, and the
\r
174 * user should not access any fields except through the PLL
\r
175 * configuration API.
\r
179 * \fn void pll_config_init(struct pll_config *cfg,
\r
180 * enum pll_source src, unsigned int div, unsigned int mul)
\r
181 * \brief Initialize PLL configuration from standard parameters.
\r
183 * \note This function may be defined inline because it is assumed to be
\r
184 * called very few times, and usually with constant parameters. Inlining
\r
185 * it will in such cases reduce the code size significantly.
\r
187 * \param cfg The PLL configuration to be initialized.
\r
188 * \param src The oscillator to be used as input to the PLL.
\r
189 * \param div PLL input divider.
\r
190 * \param mul PLL loop divider (i.e. multiplier).
\r
192 * \return A configuration which will make the PLL run at
\r
193 * (\a mul / \a div) times the frequency of \a src
\r
196 * \def pll_config_defaults(cfg, pll_id)
\r
197 * \brief Initialize PLL configuration using default parameters.
\r
199 * After this function returns, \a cfg will contain a configuration
\r
200 * which will make the PLL run at (CONFIG_PLLx_MUL / CONFIG_PLLx_DIV)
\r
201 * times the frequency of CONFIG_PLLx_SOURCE.
\r
203 * \param cfg The PLL configuration to be initialized.
\r
204 * \param pll_id Use defaults for this PLL.
\r
207 * \def pll_get_default_rate(pll_id)
\r
208 * \brief Get the default rate in Hz of \a pll_id
\r
211 * \fn void pll_config_set_option(struct pll_config *cfg,
\r
212 * unsigned int option)
\r
213 * \brief Set the PLL option bit \a option in the configuration \a cfg.
\r
215 * \param cfg The PLL configuration to be changed.
\r
216 * \param option The PLL option bit to be set.
\r
219 * \fn void pll_config_clear_option(struct pll_config *cfg,
\r
220 * unsigned int option)
\r
221 * \brief Clear the PLL option bit \a option in the configuration \a cfg.
\r
223 * \param cfg The PLL configuration to be changed.
\r
224 * \param option The PLL option bit to be cleared.
\r
227 * \fn void pll_config_read(struct pll_config *cfg, unsigned int pll_id)
\r
228 * \brief Read the currently active configuration of \a pll_id.
\r
230 * \param cfg The configuration object into which to store the currently
\r
231 * active configuration.
\r
232 * \param pll_id The ID of the PLL to be accessed.
\r
235 * \fn void pll_config_write(const struct pll_config *cfg,
\r
236 * unsigned int pll_id)
\r
237 * \brief Activate the configuration \a cfg on \a pll_id
\r
239 * \param cfg The configuration object representing the PLL
\r
240 * configuration to be activated.
\r
241 * \param pll_id The ID of the PLL to be updated.
\r
246 //! \name Interaction with the PLL hardware
\r
249 * \fn void pll_enable(const struct pll_config *cfg,
\r
250 * unsigned int pll_id)
\r
251 * \brief Activate the configuration \a cfg and enable PLL \a pll_id.
\r
253 * \param cfg The PLL configuration to be activated.
\r
254 * \param pll_id The ID of the PLL to be enabled.
\r
257 * \fn void pll_disable(unsigned int pll_id)
\r
258 * \brief Disable the PLL identified by \a pll_id.
\r
260 * After this function is called, the PLL identified by \a pll_id will
\r
261 * be disabled. The PLL configuration stored in hardware may be affected
\r
262 * by this, so if the caller needs to restore the same configuration
\r
263 * later, it should either do a pll_config_read() before disabling the
\r
264 * PLL, or remember the last configuration written to the PLL.
\r
266 * \param pll_id The ID of the PLL to be disabled.
\r
269 * \fn bool pll_is_locked(unsigned int pll_id)
\r
270 * \brief Determine whether the PLL is locked or not.
\r
272 * \param pll_id The ID of the PLL to check.
\r
274 * \retval true The PLL is locked and ready to use as a clock source
\r
275 * \retval false The PLL is not yet locked, or has not been enabled.
\r
278 * \fn void pll_enable_source(enum pll_source src)
\r
279 * \brief Enable the source of the pll.
\r
280 * The source is enabled, if the source is not already running.
\r
282 * \param src The ID of the PLL source to enable.
\r
285 * \fn void pll_enable_config_defaults(unsigned int pll_id)
\r
286 * \brief Enable the pll with the default configuration.
\r
287 * PLL is enabled, if the PLL is not already locked.
\r
289 * \param pll_id The ID of the PLL to enable.
\r
293 * \brief Wait for PLL \a pll_id to become locked
\r
295 * \todo Use a timeout to avoid waiting forever and hanging the system
\r
297 * \param pll_id The ID of the PLL to wait for.
\r
299 * \retval STATUS_OK The PLL is now locked.
\r
300 * \retval ERR_TIMEOUT Timed out waiting for PLL to become locked.
\r
302 static inline int pll_wait_for_lock(unsigned int pll_id)
\r
304 Assert(pll_id < NR_PLLS);
\r
306 while (!pll_is_locked(pll_id)) {
\r
316 #endif /* CLK_PLL_H_INCLUDED */
\r