libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
pmc.c
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2013 Gareth McMullin <gareth@blacksphere.co.nz>
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#include <libopencm3/sam/pmc.h>
21#include <libopencm3/sam/eefc.h>
22
23/** Default peripheral clock frequency after reset. */
24uint32_t pmc_mck_frequency = 4000000;
25
26void pmc_xtal_enable(bool en, uint8_t startup_time)
27{
28 if (en) {
29 CKGR_MOR = (CKGR_MOR & ~CKGR_MOR_MOSCXTST_MASK) |
30 CKGR_MOR_KEY | CKGR_MOR_MOSCXTEN |
31 (startup_time << 8);
32 while (!(PMC_SR & PMC_SR_MOSCXTS));
33 } else {
34 CKGR_MOR = CKGR_MOR_KEY | (CKGR_MOR & ~CKGR_MOR_MOSCXTEN);
35 }
36}
37
38void pmc_plla_config(uint8_t mul, uint8_t div)
39{
40 CKGR_PLLAR = CKGR_PLLAR_ONE | ((mul - 1) << 16) |
41 CKGR_PLLAR_PLLACOUNT_MASK | div;
42 while (!(PMC_SR & PMC_SR_LOCKA));
43}
44
46{
47#if defined(PMC_PCER1)
48 if (pid < 32) {
49 PMC_PCER0 = 1 << pid;
50 } else {
51 PMC_PCER1 = 1 << (pid & 31);
52 }
53#else
54 /* SAM3N and SAM3U only have one Peripheral Clock Enable Register */
55 PMC_PCER = 1 << pid;
56#endif
57}
58
60{
61#if defined(PMC_PCER1)
62 if (pid < 32) {
63 PMC_PCDR0 = 1 << pid;
64 } else {
65 PMC_PCDR1 = 1 << (pid & 31);
66 }
67#else
68 PMC_PCDR = 1 << pid;
69#endif
70}
71
72void pmc_mck_set_source(enum mck_src src)
73{
74 PMC_MCKR = (PMC_MCKR & ~PMC_MCKR_CSS_MASK) | src;
75 while (!(PMC_SR & PMC_SR_MCKRDY));
76}
77
79{
81
82 /* 12MHz external xtal, maximum possible startup time */
83 pmc_xtal_enable(true, 0xff);
84 /* Select as main oscillator */
85 CKGR_MOR |= CKGR_MOR_KEY | CKGR_MOR_MOSCSEL;
86 /* Multiply by 7 for 84MHz */
87 pmc_plla_config(7, 1);
88 pmc_mck_set_source(MCK_SRC_PLLA);
89
90 pmc_mck_frequency = 84000000;
91}
92
94{
96
97 /* Select as main oscillator */
98 CKGR_MOR = CKGR_MOR_KEY |
99 (CKGR_MOR & ~(CKGR_MOR_MOSCSEL | CKGR_MOR_MOSCRCF_MASK));
100 /* Multiply by 21 for 84MHz */
101 pmc_plla_config(21, 1);
102 pmc_mck_set_source(MCK_SRC_PLLA);
103
104 pmc_mck_frequency = 84000000;
105}
106
static void eefc_set_latency(uint8_t wait)
Definition: eefc.h:72
void pmc_clock_setup_in_rc_4mhz_out_84mhz(void)
Definition: pmc.c:93
void pmc_xtal_enable(bool en, uint8_t startup_time)
Definition: pmc.c:26
void pmc_peripheral_clock_enable(uint8_t pid)
Definition: pmc.c:45
uint32_t pmc_mck_frequency
Default peripheral clock frequency after reset.
Definition: pmc.c:24
void pmc_clock_setup_in_xtal_12mhz_out_84mhz(void)
Definition: pmc.c:78
void pmc_peripheral_clock_disable(uint8_t pid)
Definition: pmc.c:59
void pmc_plla_config(uint8_t mul, uint8_t div)
Definition: pmc.c:38
void pmc_mck_set_source(enum mck_src src)
Definition: pmc.c:72