libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
cmu_common.c
Go to the documentation of this file.
1/** @addtogroup cmu_file CMU peripheral API
2 * @ingroup peripheral_apis
3 */
4/*
5 * This file is part of the libopencm3 project.
6 *
7 * Copyright (C) 2015 Kuldeep Singh Dhaka <kuldeepdhaka9@gmail.com>
8 *
9 * This library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22
25
26/**@{*/
27
28/**
29 * Enable CMU registers lock.
30 */
32{
34}
35
36/**
37 * Disable CMU registers lock
38 */
40{
42}
43
44/**
45 * Get CMU register lock flag
46 * @retval true if flag is set
47 * @retval false if flag is not set
48 */
50{
52}
53
54#define _CMU_REG(i) MMIO32(CMU_BASE + ((i) >> 5))
55#define _CMU_BIT(i) (1 << ((i) & 0x1f))
56
57/**
58 * @brief Enable Peripheral Clock in running mode.
59 *
60 * Enable the clock on particular peripheral.
61 *
62 * @param[in] clken Peripheral Name
63 *
64 * For available constants, see @a enum::cmu_periph_clken (CMU_LEUART1 for
65 * example)
66 */
67
69{
70 _CMU_REG(clken) |= _CMU_BIT(clken);
71}
72
73/**
74 * @brief Disable Peripheral Clock in running mode.
75 * Disable the clock on particular peripheral.
76 *
77 * @param[in] clken Peripheral Name
78 *
79 * For available constants, see @a enum::cmu_periph_clken (CMU_LEUART1 for
80 * example)
81 */
82
84{
85 _CMU_REG(clken) &= ~_CMU_BIT(clken);
86}
87
88/**
89 * Turn on Oscillator
90 * @param[in] osc enum cmu_osc Oscillator name
91 */
92void cmu_osc_on(enum cmu_osc osc)
93{
94 switch (osc) {
95 case HFRCO:
97 break;
98 case LFRCO:
100 break;
101 case ULFRCO:
102 /* TODO: but how? */
103 break;
104 case HFXO:
106 break;
107 case LFXO:
109 break;
110 case AUXHFRCO:
112 break;
113 }
114}
115
116/**
117 * Turn off Oscillator
118 * @param[in] osc enum cmu_osc Oscillator name
119 */
120void cmu_osc_off(enum cmu_osc osc)
121{
122 switch (osc) {
123 case HFRCO:
125 break;
126 case LFRCO:
128 break;
129 case ULFRCO:
130 /* TODO: but how? */
131 break;
132 case HFXO:
134 break;
135 case LFXO:
137 break;
138 case AUXHFRCO:
140 break;
141 }
142}
143
144/**
145 * Get Oscillator read flag
146 * @param[in] osc enum cmu_osc Oscillator name
147 * @retval true if flag is set
148 * @retval false if flag is not set
149 */
151{
152 switch (osc) {
153 case HFRCO:
154 return (CMU_STATUS & CMU_STATUS_HFRCORDY) != 0;
155 break;
156 case LFRCO:
157 return (CMU_STATUS & CMU_STATUS_LFRCORDY) != 0;
158 break;
159 case ULFRCO:
160 /* TODO: but how? */
161 break;
162 case HFXO:
163 return (CMU_STATUS & CMU_STATUS_HFXORDY) != 0;
164 break;
165 case LFXO:
166 return (CMU_STATUS & CMU_STATUS_LFXORDY) != 0;
167 break;
168 case AUXHFRCO:
169 return (CMU_STATUS & CMU_STATUS_AUXHFRCORDY) != 0;
170 break;
171 }
172
173 return false;
174}
175
176/**
177 * Wait till oscillator is not ready
178 * @param[in] osc enum cmu_osc Oscillator name
179 */
181{
182 switch (osc) {
183 case HFRCO:
184 while ((CMU_STATUS & CMU_STATUS_HFRCORDY) == 0);
185 break;
186 case LFRCO:
187 while ((CMU_STATUS & CMU_STATUS_LFRCORDY) == 0);
188 break;
189 case ULFRCO:
190 /* TODO: but how? */
191 break;
192 case HFXO:
193 while ((CMU_STATUS & CMU_STATUS_HFXORDY) == 0);
194 break;
195 case LFXO:
196 while ((CMU_STATUS & CMU_STATUS_LFXORDY) == 0);
197 break;
198 case AUXHFRCO:
199 while ((CMU_STATUS & CMU_STATUS_AUXHFRCORDY) == 0);
200 break;
201 }
202}
203
204/**
205 * Set HFCLK clock source
206 * @param[in] osc enum cmu_osc Oscillator name
207 * @note calling cmu_set_hfclk_source() do not set source immediately, use
208 * @a cmu_get_hfclk_source() to verify that the source has been set.
209 * @see cmu_get_hfclk_source()
210 */
212{
213 switch (osc) {
214 case HFXO:
216 break;
217 case HFRCO:
219 break;
220 case LFXO:
222 break;
223 case LFRCO:
225 break;
226 default:
227 /* not applicable */
228 return;
229 }
230}
231
233{
234 uint32_t status = CMU_STATUS;
235 if (status & CMU_STATUS_LFXOSEL) {
236 return LFXO;
237 } else if (status & CMU_STATUS_LFRCOSEL) {
238 return LFRCO;
239 } else if (status & CMU_STATUS_HFXOSEL) {
240 return HFXO;
241 } else if (status & CMU_STATUS_HFRCOSEL) {
242 return HFRCO;
243 }
244
245 /* never reached */
246 return (enum cmu_osc) -1;
247}
248
249/**
250 * HFXO output 48Mhz and core running at 48Mhz
251 */
253{
254 /* configure HFXO and prescaler */
261
262 /* enable HFXO */
264
265 /* wait for HFXO */
267
268 /* set flash wait state */
269 MSC_READCTRL = (MSC_READCTRL & ~MSC_READCTRL_MODE_MASK)
271
272 /* switch to HFXO */
274
275 /* wait till HFXO not selected */
276 while (cmu_get_hfclk_source() != HFXO);
277}
278
279/**@}*/
#define CMU_OSCENCMD_LFXOEN
Definition: cmu_common.h:328
#define CMU_OSCENCMD_LFRCOEN
Definition: cmu_common.h:330
#define CMU_OSCENCMD_HFXOEN
Definition: cmu_common.h:334
#define CMU_HFCORECLKDIV
Definition: cmu_common.h:30
cmu_periph_clken
Definition: cmu_common.h:617
#define CMU_STATUS_HFRCOSEL
Definition: cmu_common.h:381
#define CMU_STATUS_HFXORDY
Definition: cmu_common.h:388
#define CMU_CMD_HFCLKSEL_HFXO
Definition: cmu_common.h:355
#define CMU_CTRL
Definition: cmu_common.h:29
#define CMU_LOCK
Definition: cmu_common.h:56
#define CMU_STATUS_LFRCORDY
Definition: cmu_common.h:384
#define CMU_OSCENCMD_LFRCODIS
Definition: cmu_common.h:329
#define CMU_STATUS_AUXHFRCORDY
Definition: cmu_common.h:386
#define CMU_LOCK_LOCKKEY_UNLOCK
Definition: cmu_common.h:613
#define CMU_HFCORECLKDIV_HFCORECLKDIV_NODIV
Definition: cmu_common.h:188
#define CMU_CTRL_HFXOBUFCUR_MASK
Definition: cmu_common.h:135
#define CMU_OSCENCMD
Definition: cmu_common.h:37
#define CMU_CTRL_HFCLKDIV_NODIV
Definition: cmu_common.h:104
#define CMU_CTRL_HFCLKDIV_MASK
Definition: cmu_common.h:101
#define CMU_OSCENCMD_AUXHFRCOEN
Definition: cmu_common.h:332
#define CMU_OSCENCMD_HFXODIS
Definition: cmu_common.h:333
#define CMU_HFCORECLKDIV_HFCORECLKLEDIV
Definition: cmu_common.h:159
#define CMU_STATUS_LFRCOSEL
Definition: cmu_common.h:379
#define CMU_STATUS_HFXOSEL
Definition: cmu_common.h:380
#define CMU_OSCENCMD_HFRCOEN
Definition: cmu_common.h:336
#define CMU_STATUS_LFXORDY
Definition: cmu_common.h:382
#define CMU_CMD_HFCLKSEL_LFXO
Definition: cmu_common.h:357
#define CMU_LOCK_LOCKKEY_MASK
Definition: cmu_common.h:609
#define CMU_LOCK_LOCKKEY_LOCK
Definition: cmu_common.h:612
#define CMU_STATUS_LFXOSEL
Definition: cmu_common.h:378
#define CMU_CMD_HFCLKSEL_HFRCO
Definition: cmu_common.h:354
#define CMU_OSCENCMD_LFXODIS
Definition: cmu_common.h:327
cmu_osc
Definition: cmu_common.h:662
#define CMU_STATUS_HFRCORDY
Definition: cmu_common.h:390
#define CMU_CMD_HFCLKSEL_LFRCO
Definition: cmu_common.h:356
#define CMU_CMD
Definition: cmu_common.h:38
#define CMU_OSCENCMD_HFRCODIS
Definition: cmu_common.h:335
#define CMU_STATUS
Definition: cmu_common.h:40
#define CMU_OSCENCMD_AUXHFRCODIS
Definition: cmu_common.h:331
#define CMU_LOCK_LOCKKEY_LOCKED
Definition: cmu_common.h:611
#define CMU_CTRL_HFXOBUFCUR_BOOSTABOVE32MHZ
Definition: cmu_common.h:139
@ AUXHFRCO
Internal, 1-28Mhz.
Definition: cmu_common.h:668
@ HFRCO
Internal, 1 - 28Mhz.
Definition: cmu_common.h:663
@ LFRCO
Internal, 32.768kHz.
Definition: cmu_common.h:664
@ ULFRCO
Internal, 1Khz.
Definition: cmu_common.h:665
@ HFXO
External, 4-48Mhz.
Definition: cmu_common.h:666
@ LFXO
External, 32.768kHz.
Definition: cmu_common.h:667
void cmu_osc_off(enum cmu_osc osc)
Turn off Oscillator.
Definition: cmu_common.c:120
enum cmu_osc cmu_get_hfclk_source(void)
Definition: cmu_common.c:232
void cmu_periph_clock_enable(enum cmu_periph_clken clken)
Enable Peripheral Clock in running mode.
Definition: cmu_common.c:68
void cmu_periph_clock_disable(enum cmu_periph_clken clken)
Disable Peripheral Clock in running mode.
Definition: cmu_common.c:83
bool cmu_get_lock_flag(void)
Get CMU register lock flag.
Definition: cmu_common.c:49
#define _CMU_REG(i)
Definition: cmu_common.c:54
void cmu_clock_setup_in_hfxo_out_48mhz(void)
HFXO output 48Mhz and core running at 48Mhz.
Definition: cmu_common.c:252
bool cmu_osc_ready_flag(enum cmu_osc osc)
Get Oscillator read flag.
Definition: cmu_common.c:150
#define _CMU_BIT(i)
Definition: cmu_common.c:55
void cmu_enable_lock(void)
Enable CMU registers lock.
Definition: cmu_common.c:31
void cmu_disable_lock(void)
Disable CMU registers lock.
Definition: cmu_common.c:39
void cmu_osc_on(enum cmu_osc osc)
Turn on Oscillator.
Definition: cmu_common.c:92
void cmu_set_hfclk_source(enum cmu_osc osc)
Set HFCLK clock source.
Definition: cmu_common.c:211
void cmu_wait_for_osc_ready(enum cmu_osc osc)
Wait till oscillator is not ready.
Definition: cmu_common.c:180
#define MSC_READCTRL
Definition: msc_common.h:30
#define MSC_READCTRL_MODE_WS2
Definition: msc_common.h:77