libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
pm.c
Go to the documentation of this file.
1/** @addtogroup scif
2 *
3 * @brief <b>Access functions for the SAM4 Power Manager (PM)</b>
4 * @ingroup SAM4
5 * LGPL License Terms @ref lgpl_license
6 * @author @htmlonly &copy; @endhtmlonly 2016
7 * Maxim Sloyko <maxims@google.com>
8 *
9 */
10
11/*
12 * This file is part of the libopencm3 project.
13 *
14 * This library is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#include <libopencm3/sam/pm.h>
29
30void pm_select_main_clock(enum mck_src source_clock)
31{
34 while (!(PM_SR & PM_SR_CKRDY));
35}
36
37void pm_enable_clock_div(enum pm_cksel sel_target, uint8_t div)
38{
39 while (!(PM_SR & PM_SR_CKRDY));
40 uint32_t reg = (PM_CKSEL_DIV | (div & PM_CKSEL_MASK));
41
42 PM_UNLOCK = PM_CKSEL_KEY(sel_target);
43 PM_CKSEL(sel_target) = reg;
44 while (!(PM_SR & PM_SR_CKRDY));
45}
46
47void pm_set_divmask_clock(uint8_t mask)
48{
50 PM_PBADIVMASK = mask;
51}
52
53static void set_peripheral_clock_status(enum pm_peripheral periph, bool on)
54{
55 uint8_t reg_id = periph/32;
56 uint8_t bit_offset = periph % 32;
57 uint32_t reg_mask = PM_MASK(reg_id);
58 if (on) {
59 reg_mask |= (1 << bit_offset);
60 } else {
61 reg_mask &= ~(1 << bit_offset);
62 }
63 PM_UNLOCK = PM_MASK_KEY(reg_id);
64 PM_MASK(reg_id) = reg_mask;
65}
66
68{
69 set_peripheral_clock_status(periph, true);
70}
71
73{
74 set_peripheral_clock_status(periph, false);
75}
#define PM_CKSEL_DIV
Definition: 4l/pm.h:114
#define PM_CKSEL(I)
Definition: 4l/pm.h:30
#define PM_MCCTRL
Definition: 4l/pm.h:26
mck_src
Definition: 4l/pm.h:189
#define PM_PBADIVMASK_KEY
Definition: 4l/pm.h:57
#define PM_CKSEL_KEY(I)
Definition: 4l/pm.h:31
#define PM_MCCTRL_MCSEL_SHIFT
Definition: 4l/pm.h:110
#define PM_SR_CKRDY
Definition: 4l/pm.h:186
#define PM_MCCTRL_KEY
Definition: 4l/pm.h:27
#define PM_MASK_KEY(I)
Definition: 4l/pm.h:34
#define PM_SR
Definition: 4l/pm.h:82
#define PM_UNLOCK
Definition: 4l/pm.h:63
#define PM_CKSEL_MASK
Definition: 4l/pm.h:115
pm_peripheral
Definition: 4l/pm.h:212
#define PM_MASK(I)
Definition: 4l/pm.h:33
pm_cksel
Definition: 4l/pm.h:199
#define PM_MCCTRL_MCSEL_MASK
Definition: 4l/pm.h:111
#define PM_PBADIVMASK
Definition: 4l/pm.h:56
void pm_set_divmask_clock(uint8_t mask)
Definition: pm.c:47
void pm_enable_peripheral_clock(enum pm_peripheral periph)
Definition: pm.c:67
void pm_disable_peripheral_clock(enum pm_peripheral periph)
Definition: pm.c:72
void pm_select_main_clock(enum mck_src source_clock)
Definition: pm.c:30
static void set_peripheral_clock_status(enum pm_peripheral periph, bool on)
Definition: pm.c:53
void pm_enable_clock_div(enum pm_cksel sel_target, uint8_t div)
Definition: pm.c:37