libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
rcc.c
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 *
6 * This library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21/**
22 * @defgroup rcc_file RCC
23 *
24 * @ingroup LM4Fxx
25 *
26@author @htmlonly &copy; @endhtmlonly 2012
27Alexandru Gagniuc <mr.nuke.me@gmail.com>
28
29 * \brief <b>libopencm3 LM4F Clock control API</b>
30 *
31 * The LM4F clock API provides functionality for manipulating the system clock,
32 * oscillator, and PLL. Functions are provided for fine-grained control of clock
33 * control registers, while also providing higher level functionality to easily
34 * configure the main system clock source.
35 *
36 * The following code snippet uses fine-grained mechanisms to configures the
37 * chip to run off an external 16MHz crystal, and use the PLL to derive a clock
38 * frequency of 80MHz.
39 * @code{.c}
40 * // A divisor of 5 gives us a clock of 400/5 = 80MHz
41 * #define PLLDIV_80MHZ 5
42 *
43 * // Enable the main oscillator
44 * rcc_enable_main_osc();
45 *
46 * // Make RCC2 override RCC
47 * rcc_enable_rcc2();
48 *
49 * // Set XTAL value to 16MHz
50 * rcc_configure_xtal(XTAL_16M);
51 * // Set the oscillator source as the main oscillator
52 * rcc_set_osc_source(OSCSRC_MOSC);
53 * // Enable the PLL
54 * rcc_pll_on();
55 *
56 * // Change the clock divisor
57 * rcc_set_pll_divisor(PLLDIV_80MHZ);
58 *
59 * // We cannot use the PLL as a clock source until it locks
60 * rcc_wait_for_pll_ready();
61 * // Disable PLL bypass to derive the system clock from the PLL clock
62 * rcc_pll_bypass_disable();
63 *
64 * // Keep track of frequency
65 * lm4f_rcc_sysclk_freq = 80E6;
66 * @endcode
67 *
68 * The same can be achieved by a simple call to high-level routines:
69 * @code
70 * // A divisor of 5 gives us a clock of 400/5 = 80MHz
71 * #define PLLDIV_80MHZ 5
72 *
73 * rcc_sysclk_config(OSCSRC_MOSC, XTAL_16M, PLLDIV_80MHZ);
74 * @endcode
75 *
76 * @{
77 */
78
79#include <libopencm3/lm4f/rcc.h>
80
81/**
82 * @defgroup rcc_low_level Low-level clock control API
83@ingroup rcc_file
84 * @{
85 */
86/**
87 * \brief System clock frequency
88 *
89 * This variable is provided to keep track of the system clock frequency. It
90 * should be updated every time the system clock is changed via the fine-grained
91 * mechanisms. The initial value is 16MHz, which corresponds to the clock of the
92 * internal 16MHz oscillator.
93 *
94 * High-level routines update the system clock automatically.
95 * For read access, it is recommended to access this variable via
96 * @code
97 * rcc_get_system_clock_frequency();
98 * @endcode
99 *
100 * If write access is desired (i.e. when changing the system clock via the
101 * fine-grained mechanisms), then include the following line in your code:
102 * @code
103 * extern uint32_t lm4f_rcc_sysclk_freq;
104 * @endcode
105 */
106uint32_t lm4f_rcc_sysclk_freq = 16000000;
107
108
109/**
110 * \brief Configure the crystal type connected to the device.
111 *
112 * Configure the crystal type connected between the OSCO and OSCI pins by
113 * writing the appropriate value to the XTAL field in SYSCTL_RCC. The PLL
114 * parameters are automatically adjusted in hardware to provide a PLL clock of
115 * 400MHz.
116 *
117 * @param[in] xtal predefined crystal type @see xtal_t
118 */
120{
121 uint32_t reg32;
122
123 reg32 = SYSCTL_RCC;
124 reg32 &= ~SYSCTL_RCC_XTAL_MASK;
125 reg32 |= (xtal & SYSCTL_RCC_XTAL_MASK);
126 SYSCTL_RCC = reg32;
127}
128
129/**
130 * \brief Disable the main oscillator
131 *
132 * Sets the IOSCDIS bit in SYSCTL_RCC, disabling the main oscillator.
133 */
135{
137}
138
139/**
140 * \brief Disable the internal oscillator
141 *
142 * Sets the IOSCDIS bit in SYSCTL_RCC, disabling the internal oscillator.
143 */
145{
147}
148
149/**
150 * \brief Enable the main oscillator
151 *
152 * Clears the MOSCDIS bit in SYSCTL_RCC, enabling the main oscillator.
153 */
155{
156 SYSCTL_RCC &= ~SYSCTL_RCC_MOSCDIS;
157}
158
159/**
160 * \brief Enable the internal oscillator
161 *
162 * Clears the IOSCDIS bit in SYSCTL_RCC, enabling the internal oscillator.
163 */
165{
166 SYSCTL_RCC &= ~SYSCTL_RCC_IOSCDIS;
167}
168
169/**
170 * \brief Enable the use of SYSCTL_RCC2 register for clock control
171 *
172 * Enables the USERCC2 bit in SYSCTTL_RCC2. Settings in SYSCTL_RCC2 will
173 * override settings in SYSCTL_RCC.
174 * This function must be called before other calls to manipulate the clock, as
175 * libopencm3 uses the SYSCTL_RCC2 register.
176 */
178{
180}
181
182/**
183 * \brief Power down the main PLL
184 *
185 * Sets the SYSCTL_RCC2_PWRDN2 in SYSCTL_RCC2 to power down the PLL.
186 *
187 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
188 * function.
189 */
190void rcc_pll_off(void)
191{
193}
194
195/**
196 * \brief Power up the main PLL
197 *
198 * Clears the PWRDN2 in SYSCTL_RCC2 to power on the PLL.
199 *
200 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
201 * function.
202 */
203void rcc_pll_on(void)
204{
205 SYSCTL_RCC2 &= ~SYSCTL_RCC2_PWRDN2;
206}
207
208/**
209 * \brief Set the oscillator source to be used by the system clock
210 *
211 * Set the clock source for the system clock.
212 *
213 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
214 * function.
215 */
217{
218 uint32_t reg32;
219
220 reg32 = SYSCTL_RCC2;
221 reg32 &= ~SYSCTL_RCC2_OSCSRC2_MASK;
222 reg32 |= (src & SYSCTL_RCC2_OSCSRC2_MASK);
223 SYSCTL_RCC2 = reg32;
224}
225
226/**
227 * \brief Disable the PLL bypass and use the PLL clock
228 *
229 * Clear BYPASS2 in SYSCTL_RCC2. The system clock is derived from the PLL
230 * clock divided by the divisor specified in SYSDIV2.
231 *
232 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
233 * function.
234 */
236{
237 SYSCTL_RCC2 &= ~SYSCTL_RCC2_BYPASS2;
238}
239
240/**
241 * \brief Enable the PLL bypass and use the oscillator clock
242 *
243 * Set BYPASS2 in SYSCTL_RCC2. The system clock is derived from the oscillator
244 * clock divided by the divisor specified in SYSDIV2.
245 *
246 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
247 * function.
248 */
250{
252}
253
254/**
255 * \brief Set the PLL clock divisor (from 400MHz)
256 *
257 * Set the binary divisor used to predivide the system clock down for use as the
258 * timing reference for the PWM module. The divisor is expected to be a divisor
259 * from 400MHz, not 200MHz. The DIV400 is also set.
260 *
261 * Specifies the divisor that used to generate the system clock from either the
262 * PLL output or the oscillator source (depending on the BYPASS2 bit in
263 * SYSCTL_RCC2). SYSDIV2 is used for the divisor when both the USESYSDIV bit in
264 * SYSCTL_RCC is set.
265 *
266 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
267 * function.
268 *
269 * @param[in] div clock divisor to apply to the 400MHz PLL clock. It is the
270 * caller's responsibility to ensure that the divisor will not create
271 * a system clock that is out of spec.
272 */
273void rcc_set_pll_divisor(uint8_t div400)
274{
275 uint32_t reg32;
276
278
279 reg32 = SYSCTL_RCC2;
280 reg32 &= ~SYSCTL_RCC2_SYSDIV400_MASK;
281 reg32 |= ((div400 - 1) << 22) & SYSCTL_RCC2_SYSDIV400_MASK;
282 /* We are expecting a divider from 400MHz */
283 reg32 |= SYSCTL_RCC2_DIV400;
284 SYSCTL_RCC2 = reg32;
285}
286/**
287 * \brief Set the PWM unit clock divisor
288 *
289 * Set the binary divisor used to predivide the system clock down for use as the
290 * timing reference for the PWM module.
291 *
292 * @param[in] div clock divisor to use @see pwm_clkdiv_t
293 */
295{
296 uint32_t reg32;
297
298 reg32 = SYSCTL_RCC;
299 reg32 &= ~SYSCTL_RCC_PWMDIV_MASK;
300 reg32 |= (div & SYSCTL_RCC_PWMDIV_MASK);
301 SYSCTL_RCC = reg32;
302}
303
304/**
305 * \brief Power down the USB PLL
306 *
307 * Sets the USBPWRDN in SYSCTL_RCC2 to power down the USB PLL.
308 *
309 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
310 * function.
311 */
313{
315}
316
317/**
318 * \brief Power up the USB PLL
319 *
320 * Clears the USBPWRDN in SYSCTL_RCC2 to power on the USB PLL.
321 *
322 * USERCC2 must have been set by a call to rcc_enable_rcc2() before calling this
323 * function.
324 */
326{
327 SYSCTL_RCC2 &= ~SYSCTL_RCC2_USBPWRDN;
328}
329
330/**
331 * \brief Wait for main PLL to lock
332 *
333 * Waits until the LOCK bit in SYSCTL_PLLSTAT is set. This guarantees that the
334 * PLL is locked, and ready to use.
335 */
337{
339}
340
341/**
342 * @}
343 */
344
345/**
346 * @defgroup rcc_high_level High-level clock control API
347@ingroup rcc_file
348 * @{
349 */
350
351/**
352 * \brief Change the PLL divisor
353 *
354 * Changes the divisor applied to the 400MHz PLL clock. The PLL must have
355 * previously been configured by selecting an appropriate XTAL value, and
356 * turning on the PLL. This function does not reconfigure the XTAL value or
357 * oscillator source. It only changes the PLL divisor.
358 *
359 * The PLL is bypassed before modifying the divisor, and the function blocks
360 * until the PLL is locked, then the bypass is disabled, before returning.
361 *
362 * @param [in] pll_div400 The clock divisor to apply to the 400MHz PLL clock.
363 */
364void rcc_change_pll_divisor(uint8_t pll_div400)
365{
366 /* Bypass the PLL while its settings are modified */
368 /* Change the clock divisor */
369 rcc_set_pll_divisor(pll_div400);
370 /* We cannot use the PLL as a clock source until it locks */
372 /* Disable PLL bypass to derive the system clock from the PLL clock */
374 /* Update the system clock frequency for housekeeping */
375 lm4f_rcc_sysclk_freq = (uint32_t)400E6 / pll_div400;
376}
377
378/**
379 * \brief Get the system clock frequency
380 *
381 * @return System clock frequency in Hz
382 */
384{
386}
387
388/* Get the clock frequency corresponding to a given XTAL value */
389static uint32_t xtal_to_freq(enum xtal_t xtal)
390{
391 const uint32_t freqs[] = {
392 4000000, /* XTAL_4M */
393 4096000, /* XTAL_4M_096 */
394 4915200, /* XTAL_4M_9152 */
395 5000000, /* ,XTAL_5M */
396 5120000, /* XTAL_5M_12 */
397 6000000, /* XTAL_6M */
398 6144000, /* XTAL_6M_144 */
399 7372800, /* XTAL_7M_3728 */
400 8000000, /* XTAL_8M */
401 8192000, /* XTAL_8M_192 */
402 10000000, /* XTAL_10M */
403 12000000, /* XTAL_12M */
404 12288000, /* XTAL_12M_288 */
405 13560000, /* XTAL_13M_56 */
406 14318180, /* XTAL_14M_31818 */
407 16000000, /* XTAL_16M */
408 16384000, /* XTAL_16M_384 */
409 18000000, /* XTAL_18M */
410 20000000, /* XTAL_20M */
411 24000000, /* XTAL_24M */
412 25000000, /* XTAL_25M */
413 };
414
415 return freqs[xtal - XTAL_4M];
416}
417
418/**
419 * \brief Configure the system clock source
420 *
421 * Sets up the system clock, including configuring the oscillator source, and
422 * PLL to achieve the desired system clock frequency. Where applicable, The LM4F
423 * clock API uses the new RCC2 register to configure clock parameters.
424 *
425 * Enables the main oscillator if the clock source is OSCSRC_MOSC. If the main
426 * oscillator was previously enabled, it will not be disabled. If desired, it
427 * can be separately disabled by a call to rcc_disable_main_osc().
428 *
429 * Configures the system clock to run from the 400MHz PLL with a divisor of
430 * pll_div400 applied. If pll_div400 is 0, then the PLL is disabled, and the
431 * system clock is configured to run off a "raw" clock. If the PLL was
432 * previously powered on, it will not be disabled. If desired, it can de powered
433 * off by a call to rcc_pll_off().
434 *
435 * @param [in] osc_src Oscillator from where to derive the system clock.
436 * @param [in] xtal Type of crystal connected to the OSCO/OSCI pins
437 * @param [in] pll_div400 The clock divisor to apply to the 400MHz PLL clock.
438 * If 0, then the PLL is disabled, and the system runs
439 * off a "raw" clock.
440 *
441 * @return System clock frequency in Hz
442 */
443void rcc_sysclk_config(enum osc_src src, enum xtal_t xtal, uint8_t pll_div400)
444{
445 /*
446 * We could be using the PLL at this point, or we could be running of a
447 * raw clock. Either way, it is safer to bypass the PLL now.
448 */
450
451 /* Enable the main oscillator, if needed */
452 if (src == OSCSRC_MOSC) {
454 }
455
456 /* Make RCC2 override RCC */
458
459 /* Set XTAL value to 16MHz */
460 rcc_configure_xtal(xtal);
461 /* Set the oscillator source */
463 if (pll_div400) {
464 /* Enable the PLL */
465 rcc_pll_on();
466 /* Configure the PLL to the divisor we want */
467 rcc_change_pll_divisor(pll_div400);
468 } else {
469 /* We are running off a raw clock */
470 switch (src) {
471 case OSCSRC_PIOSC:
472 lm4f_rcc_sysclk_freq = 16000000;
473 break;
474 case OSCSRC_PIOSC_D4:
475 lm4f_rcc_sysclk_freq = 4000000;
476 break;
477 case OSCSRC_MOSC:
479 break;
480 case OSCSRC_32K_EXT:
481 lm4f_rcc_sysclk_freq = 32768;
482 break;
483 case OSCSRC_30K_INT: /* Fall through. */
484 default:
485 /*
486 * We either are running off the internal 30KHz
487 * oscillator, which is +- 50% imprecise, or we got a
488 * bad osc_src parameter.
489 */
491 }
492 }
493
494}
495
496/**
497 * @}
498 * @}
499 */
osc_src
Oscillator source values.
Definition: rcc.h:48
xtal_t
Predefined crystal values.
Definition: rcc.h:79
pwm_clkdiv
PWM clock divisor values.
Definition: rcc.h:62
@ OSCSRC_PIOSC_D4
Definition: rcc.h:51
@ OSCSRC_PIOSC
Definition: rcc.h:50
@ OSCSRC_32K_EXT
Definition: rcc.h:53
@ OSCSRC_MOSC
Definition: rcc.h:49
@ OSCSRC_30K_INT
Definition: rcc.h:52
@ XTAL_4M
Definition: rcc.h:80
uint32_t rcc_get_system_clock_frequency(void)
Get the system clock frequency.
Definition: rcc.c:383
void rcc_change_pll_divisor(uint8_t pll_div400)
Change the PLL divisor.
Definition: rcc.c:364
static uint32_t xtal_to_freq(enum xtal_t xtal)
Definition: rcc.c:389
void rcc_sysclk_config(enum osc_src src, enum xtal_t xtal, uint8_t pll_div400)
Configure the system clock source.
Definition: rcc.c:443
void rcc_wait_for_pll_ready(void)
Wait for main PLL to lock.
Definition: rcc.c:336
void rcc_set_pll_divisor(uint8_t div400)
Set the PLL clock divisor (from 400MHz)
Definition: rcc.c:273
void rcc_enable_interal_osc(void)
Enable the internal oscillator.
Definition: rcc.c:164
void rcc_disable_interal_osc(void)
Disable the internal oscillator.
Definition: rcc.c:144
void rcc_pll_bypass_enable(void)
Enable the PLL bypass and use the oscillator clock.
Definition: rcc.c:249
void rcc_configure_xtal(enum xtal_t xtal)
Configure the crystal type connected to the device.
Definition: rcc.c:119
void rcc_pll_off(void)
Power down the main PLL.
Definition: rcc.c:190
uint32_t lm4f_rcc_sysclk_freq
System clock frequency.
Definition: rcc.c:106
void rcc_disable_main_osc(void)
Disable the main oscillator.
Definition: rcc.c:134
void rcc_usb_pll_off(void)
Power down the USB PLL.
Definition: rcc.c:312
void rcc_set_osc_source(enum osc_src src)
Set the oscillator source to be used by the system clock.
Definition: rcc.c:216
void rcc_pll_bypass_disable(void)
Disable the PLL bypass and use the PLL clock.
Definition: rcc.c:235
void rcc_set_pwm_divisor(enum pwm_clkdiv div)
Set the PWM unit clock divisor.
Definition: rcc.c:294
void rcc_enable_rcc2(void)
Enable the use of SYSCTL_RCC2 register for clock control.
Definition: rcc.c:177
void rcc_usb_pll_on(void)
Power up the USB PLL.
Definition: rcc.c:325
void rcc_pll_on(void)
Power up the main PLL.
Definition: rcc.c:203
void rcc_enable_main_osc(void)
Enable the main oscillator.
Definition: rcc.c:154
#define SYSCTL_RCC2_BYPASS2
PLL Bypass 2.
#define SYSCTL_RCC_XTAL_MASK
Crystal Value.
#define SYSCTL_PLLSTAT
Definition: systemcontrol.h:82
#define SYSCTL_RCC
Definition: systemcontrol.h:60
#define SYSCTL_RCC2_USBPWRDN
Power-Down USB PLL.
#define SYSCTL_PLLSTAT_LOCK
PLL lock.
#define SYSCTL_RCC_MOSCDIS
Main Oscillator Disable.
#define SYSCTL_RCC2_PWRDN2
PLL Power Down 2.
#define SYSCTL_RCC_PWMDIV_MASK
PWM Unit Clock Divisor.
#define SYSCTL_RCC2
Definition: systemcontrol.h:66
#define SYSCTL_RCC2_SYSDIV400_MASK
System clock divisor mask when RCC2_DIV400 is set.
#define SYSCTL_RCC2_DIV400
Divide PLL as 400 MHz vs.
#define SYSCTL_RCC_IOSCDIS
Precision Internal Oscillator Disable.
#define SYSCTL_RCC2_USERCC2
RCC2 overides RCC.
#define SYSCTL_RCC_USESYSDIV
Enable System Clock Divider.
#define SYSCTL_RCC2_OSCSRC2_MASK
Oscillator Source 2.