libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
cmu.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 * Copyright (C) 2018 Seb Holzapfel <schnommus@gmail.com>
9 *
10 * This library is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library. If not, see <http://www.gnu.org/licenses/>.
22 */
23
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 USHFRCO:
103 break;
104 case HFXO:
106 break;
107 case LFXO:
109 break;
110 case AUXHFRCO:
112 break;
113 default:
114 /* not applicable */
115 break;
116 }
117}
118
119/**
120 * Turn off Oscillator
121 * @param[in] osc enum cmu_osc Oscillator name
122 */
123void cmu_osc_off(enum cmu_osc osc)
124{
125 switch (osc) {
126 case HFRCO:
128 break;
129 case LFRCO:
131 break;
132 case USHFRCO:
134 break;
135 case HFXO:
137 break;
138 case LFXO:
140 break;
141 case AUXHFRCO:
143 break;
144 default:
145 /* not applicable */
146 break;
147 }
148}
149
150/**
151 * Get Oscillator read flag
152 * @param[in] osc enum cmu_osc Oscillator name
153 * @retval true if flag is set
154 * @retval false if flag is not set
155 */
157{
158 switch (osc) {
159 case HFRCO:
160 return (CMU_STATUS & CMU_STATUS_HFRCORDY) != 0;
161 break;
162 case LFRCO:
163 return (CMU_STATUS & CMU_STATUS_LFRCORDY) != 0;
164 break;
165 case USHFRCO:
166 return (CMU_STATUS & CMU_STATUS_USHFRCORDY) != 0;
167 break;
168 case HFXO:
169 return (CMU_STATUS & CMU_STATUS_HFXORDY) != 0;
170 break;
171 case LFXO:
172 return (CMU_STATUS & CMU_STATUS_LFXORDY) != 0;
173 break;
174 case AUXHFRCO:
175 return (CMU_STATUS & CMU_STATUS_AUXHFRCORDY) != 0;
176 break;
177 default:
178 /* not applicable */
179 break;
180 }
181
182 return false;
183}
184
185/**
186 * Wait while oscillator is not ready
187 * @param[in] osc enum cmu_osc Oscillator name
188 */
190{
191 switch (osc) {
192 case HFRCO:
193 while ((CMU_STATUS & CMU_STATUS_HFRCORDY) == 0);
194 break;
195 case LFRCO:
196 while ((CMU_STATUS & CMU_STATUS_LFRCORDY) == 0);
197 break;
198 case USHFRCO:
199 while ((CMU_STATUS & CMU_STATUS_USHFRCORDY) == 0);
200 break;
201 case HFXO:
202 while ((CMU_STATUS & CMU_STATUS_HFXORDY) == 0);
203 break;
204 case LFXO:
205 while ((CMU_STATUS & CMU_STATUS_LFXORDY) == 0);
206 break;
207 case AUXHFRCO:
208 while ((CMU_STATUS & CMU_STATUS_AUXHFRCORDY) == 0);
209 break;
210 default:
211 /* not applicable */
212 break;
213 }
214}
215
216/**
217 * Set HFCLK clock source
218 * @param[in] osc enum cmu_osc Oscillator name
219 * @note calling cmu_set_hfclk_source() do not set source immediately, use
220 * @a cmu_get_hfclk_source() to verify that the source has been set.
221 * @see cmu_get_hfclk_source()
222 */
224{
225 switch (osc) {
226 case HFXO:
228 break;
229 case HFRCO:
231 break;
232 case LFXO:
234 break;
235 case LFRCO:
237 break;
238 case USHFRCODIV2:
240 break;
241 default:
242 /* not applicable */
243 return;
244 }
245}
246
247/**
248 * Get HFCLK clock source
249 * @retval enum cmu_osc Oscillator name
250 */
252{
253 uint32_t status = CMU_STATUS;
254 if (status & CMU_STATUS_LFXOSEL) {
255 return LFXO;
256 } else if (status & CMU_STATUS_LFRCOSEL) {
257 return LFRCO;
258 } else if (status & CMU_STATUS_HFXOSEL) {
259 return HFXO;
260 } else if (status & CMU_STATUS_HFRCOSEL) {
261 return HFRCO;
262 } else if (status & CMU_STATUS_USHFRCODIV2SEL) {
263 return USHFRCODIV2;
264 }
265
266 /* never reached */
267 return (enum cmu_osc) -1;
268}
269
270/**
271 * Set USBCLK clock source
272 * @param osc Oscillator name
273 */
275{
276 switch (osc) {
277 case LFXO:
279 break;
280 case LFRCO:
282 break;
283 case USHFRCO:
285 break;
286 default:
287 /* not applicable */
288 return;
289 }
290}
291
292/**
293 * Wait while USBCLK is not selected
294 * @param[in] osc enum cmu_osc Oscillator name
295 */
297{
298 switch (osc) {
299 case LFXO:
300 while ((CMU_STATUS & CMU_STATUS_USBCLFXOSEL) == 0);
301 break;
302 case LFRCO:
303 while ((CMU_STATUS & CMU_STATUS_USBCLFRCOSEL) == 0);
304 break;
305 case USHFRCO:
306 while ((CMU_STATUS & CMU_STATUS_USBCUSHFRCOSEL) == 0);
307 break;
308 default:
309 /* not applicable */
310 return;
311 }
312}
313
314/**@}*/
#define CMU_OSCENCMD_LFXOEN
Definition: hg/cmu.h:334
#define CMU_OSCENCMD_LFRCOEN
Definition: hg/cmu.h:336
#define CMU_OSCENCMD_HFXOEN
Definition: hg/cmu.h:340
cmu_periph_clken
Definition: hg/cmu.h:605
#define CMU_STATUS_HFRCOSEL
Definition: hg/cmu.h:408
#define CMU_STATUS_HFXORDY
Definition: hg/cmu.h:415
#define CMU_CMD_HFCLKSEL_HFXO
Definition: hg/cmu.h:361
#define CMU_LOCK
Definition: hg/cmu.h:63
#define CMU_OSCENCMD_USHFRCOEN
Definition: hg/cmu.h:332
#define CMU_CMD_USBCCLKSEL_USHFRCO
Definition: hg/cmu.h:351
#define CMU_STATUS_LFRCORDY
Definition: hg/cmu.h:411
#define CMU_OSCENCMD_LFRCODIS
Definition: hg/cmu.h:335
#define CMU_STATUS_AUXHFRCORDY
Definition: hg/cmu.h:413
#define CMU_LOCK_LOCKKEY_UNLOCK
Definition: hg/cmu.h:576
#define CMU_OSCENCMD
Definition: hg/cmu.h:44
#define CMU_OSCENCMD_AUXHFRCOEN
Definition: hg/cmu.h:338
#define CMU_OSCENCMD_HFXODIS
Definition: hg/cmu.h:339
#define CMU_STATUS_LFRCOSEL
Definition: hg/cmu.h:406
#define CMU_STATUS_HFXOSEL
Definition: hg/cmu.h:407
#define CMU_OSCENCMD_HFRCOEN
Definition: hg/cmu.h:342
#define CMU_STATUS_USHFRCORDY
Definition: hg/cmu.h:396
#define CMU_STATUS_LFXORDY
Definition: hg/cmu.h:409
#define CMU_STATUS_USBCLFXOSEL
Definition: hg/cmu.h:402
#define CMU_CMD_HFCLKSEL_LFXO
Definition: hg/cmu.h:363
#define CMU_LOCK_LOCKKEY_MASK
Definition: hg/cmu.h:572
#define CMU_STATUS_USBCLFRCOSEL
Definition: hg/cmu.h:401
#define CMU_LOCK_LOCKKEY_LOCK
Definition: hg/cmu.h:575
#define CMU_STATUS_LFXOSEL
Definition: hg/cmu.h:405
#define CMU_STATUS_USHFRCODIV2SEL
Definition: hg/cmu.h:393
#define CMU_CMD_USBCCLKSEL_LFRCO
Definition: hg/cmu.h:350
#define CMU_CMD_USBCCLKSEL_LFXO
Definition: hg/cmu.h:349
#define CMU_CMD_HFCLKSEL_HFRCO
Definition: hg/cmu.h:360
#define CMU_CMD_HFCLKSEL_USHFRCODIV2
Definition: hg/cmu.h:364
#define CMU_OSCENCMD_LFXODIS
Definition: hg/cmu.h:333
cmu_osc
Definition: hg/cmu.h:639
#define CMU_STATUS_USBCUSHFRCOSEL
Definition: hg/cmu.h:400
#define CMU_STATUS_HFRCORDY
Definition: hg/cmu.h:417
#define CMU_CMD_HFCLKSEL_LFRCO
Definition: hg/cmu.h:362
#define CMU_CMD
Definition: hg/cmu.h:45
#define CMU_OSCENCMD_HFRCODIS
Definition: hg/cmu.h:341
#define CMU_STATUS
Definition: hg/cmu.h:47
#define CMU_OSCENCMD_USHFRCODIS
Definition: hg/cmu.h:331
#define CMU_OSCENCMD_AUXHFRCODIS
Definition: hg/cmu.h:337
#define CMU_LOCK_LOCKKEY_LOCKED
Definition: hg/cmu.h:574
@ AUXHFRCO
Internal, 1-28Mhz.
Definition: hg/cmu.h:644
@ USHFRCO
Internal, 48MHz.
Definition: hg/cmu.h:645
@ HFRCO
Internal, 1 - 28Mhz.
Definition: hg/cmu.h:640
@ LFRCO
Internal, 32.768kHz.
Definition: hg/cmu.h:641
@ HFXO
External, 4-48Mhz.
Definition: hg/cmu.h:642
@ USHFRCODIV2
Internal, 24MHz.
Definition: hg/cmu.h:646
@ LFXO
External, 32.768kHz.
Definition: hg/cmu.h:643
void cmu_wait_for_usbclk_selected(enum cmu_osc osc)
Wait while USBCLK is not selected.
Definition: cmu.c:296
void cmu_osc_off(enum cmu_osc osc)
Turn off Oscillator.
Definition: cmu.c:123
enum cmu_osc cmu_get_hfclk_source(void)
Get HFCLK clock source.
Definition: cmu.c:251
void cmu_periph_clock_enable(enum cmu_periph_clken clken)
Enable Peripheral Clock in running mode.
Definition: cmu.c:68
void cmu_set_usbclk_source(enum cmu_osc osc)
Set USBCLK clock source.
Definition: cmu.c:274
void cmu_periph_clock_disable(enum cmu_periph_clken clken)
Disable Peripheral Clock in running mode.
Definition: cmu.c:83
bool cmu_get_lock_flag(void)
Get CMU register lock flag.
Definition: cmu.c:49
#define _CMU_REG(i)
Definition: cmu.c:54
bool cmu_osc_ready_flag(enum cmu_osc osc)
Get Oscillator read flag.
Definition: cmu.c:156
#define _CMU_BIT(i)
Definition: cmu.c:55
void cmu_enable_lock(void)
Enable CMU registers lock.
Definition: cmu.c:31
void cmu_disable_lock(void)
Disable CMU registers lock.
Definition: cmu.c:39
void cmu_osc_on(enum cmu_osc osc)
Turn on Oscillator.
Definition: cmu.c:92
void cmu_set_hfclk_source(enum cmu_osc osc)
Set HFCLK clock source.
Definition: cmu.c:223
void cmu_wait_for_osc_ready(enum cmu_osc osc)
Wait while oscillator is not ready.
Definition: cmu.c:189