1*1b2596b5SMatthias Ringwald /**
2*1b2596b5SMatthias Ringwald * \file
3*1b2596b5SMatthias Ringwald *
4*1b2596b5SMatthias Ringwald * \brief Power Management Controller (PMC) driver for SAM.
5*1b2596b5SMatthias Ringwald *
6*1b2596b5SMatthias Ringwald * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
7*1b2596b5SMatthias Ringwald *
8*1b2596b5SMatthias Ringwald * \asf_license_start
9*1b2596b5SMatthias Ringwald *
10*1b2596b5SMatthias Ringwald * \page License
11*1b2596b5SMatthias Ringwald *
12*1b2596b5SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
13*1b2596b5SMatthias Ringwald * modification, are permitted provided that the following conditions are met:
14*1b2596b5SMatthias Ringwald *
15*1b2596b5SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright notice,
16*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer.
17*1b2596b5SMatthias Ringwald *
18*1b2596b5SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright notice,
19*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer in the documentation
20*1b2596b5SMatthias Ringwald * and/or other materials provided with the distribution.
21*1b2596b5SMatthias Ringwald *
22*1b2596b5SMatthias Ringwald * 3. The name of Atmel may not be used to endorse or promote products derived
23*1b2596b5SMatthias Ringwald * from this software without specific prior written permission.
24*1b2596b5SMatthias Ringwald *
25*1b2596b5SMatthias Ringwald * 4. This software may only be redistributed and used in connection with an
26*1b2596b5SMatthias Ringwald * Atmel microcontroller product.
27*1b2596b5SMatthias Ringwald *
28*1b2596b5SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29*1b2596b5SMatthias Ringwald * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30*1b2596b5SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31*1b2596b5SMatthias Ringwald * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32*1b2596b5SMatthias Ringwald * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33*1b2596b5SMatthias Ringwald * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34*1b2596b5SMatthias Ringwald * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35*1b2596b5SMatthias Ringwald * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36*1b2596b5SMatthias Ringwald * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37*1b2596b5SMatthias Ringwald * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38*1b2596b5SMatthias Ringwald * POSSIBILITY OF SUCH DAMAGE.
39*1b2596b5SMatthias Ringwald *
40*1b2596b5SMatthias Ringwald * \asf_license_stop
41*1b2596b5SMatthias Ringwald *
42*1b2596b5SMatthias Ringwald */
43*1b2596b5SMatthias Ringwald /*
44*1b2596b5SMatthias Ringwald * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45*1b2596b5SMatthias Ringwald */
46*1b2596b5SMatthias Ringwald
47*1b2596b5SMatthias Ringwald #include "pmc.h"
48*1b2596b5SMatthias Ringwald
49*1b2596b5SMatthias Ringwald #if (SAM3N)
50*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 31
51*1b2596b5SMatthias Ringwald #elif (SAM3XA)
52*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 44
53*1b2596b5SMatthias Ringwald #elif (SAM3U)
54*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 29
55*1b2596b5SMatthias Ringwald #elif (SAM3S || SAM4S)
56*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 34
57*1b2596b5SMatthias Ringwald #elif (SAM4E)
58*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 47
59*1b2596b5SMatthias Ringwald #elif (SAMV71)
60*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 63
61*1b2596b5SMatthias Ringwald #elif (SAMV70)
62*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 63
63*1b2596b5SMatthias Ringwald #elif (SAME70)
64*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 63
65*1b2596b5SMatthias Ringwald #elif (SAMS70)
66*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 63
67*1b2596b5SMatthias Ringwald #elif (SAM4N)
68*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 31
69*1b2596b5SMatthias Ringwald #elif (SAM4C || SAM4CM || SAM4CP)
70*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 43
71*1b2596b5SMatthias Ringwald #elif (SAMG51)
72*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 47
73*1b2596b5SMatthias Ringwald #elif (SAMG53)
74*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 47
75*1b2596b5SMatthias Ringwald #elif (SAMG54)
76*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 47
77*1b2596b5SMatthias Ringwald #elif (SAMG55)
78*1b2596b5SMatthias Ringwald # define MAX_PERIPH_ID 50
79*1b2596b5SMatthias Ringwald #endif
80*1b2596b5SMatthias Ringwald
81*1b2596b5SMatthias Ringwald /// @cond 0
82*1b2596b5SMatthias Ringwald /**INDENT-OFF**/
83*1b2596b5SMatthias Ringwald #ifdef __cplusplus
84*1b2596b5SMatthias Ringwald extern "C" {
85*1b2596b5SMatthias Ringwald #endif
86*1b2596b5SMatthias Ringwald /**INDENT-ON**/
87*1b2596b5SMatthias Ringwald /// @endcond
88*1b2596b5SMatthias Ringwald
89*1b2596b5SMatthias Ringwald /**
90*1b2596b5SMatthias Ringwald * \defgroup sam_drivers_pmc_group Power Management Controller (PMC)
91*1b2596b5SMatthias Ringwald *
92*1b2596b5SMatthias Ringwald * \par Purpose
93*1b2596b5SMatthias Ringwald *
94*1b2596b5SMatthias Ringwald * The Power Management Controller (PMC) optimizes power consumption by
95*1b2596b5SMatthias Ringwald * controlling all system and user peripheral clocks. The PMC enables/disables
96*1b2596b5SMatthias Ringwald * the clock inputs to many of the peripherals and the Cortex-M Processor.
97*1b2596b5SMatthias Ringwald *
98*1b2596b5SMatthias Ringwald * @{
99*1b2596b5SMatthias Ringwald */
100*1b2596b5SMatthias Ringwald
101*1b2596b5SMatthias Ringwald /**
102*1b2596b5SMatthias Ringwald * \brief Set the prescaler of the MCK.
103*1b2596b5SMatthias Ringwald *
104*1b2596b5SMatthias Ringwald * \param ul_pres Prescaler value.
105*1b2596b5SMatthias Ringwald */
pmc_mck_set_prescaler(uint32_t ul_pres)106*1b2596b5SMatthias Ringwald void pmc_mck_set_prescaler(uint32_t ul_pres)
107*1b2596b5SMatthias Ringwald {
108*1b2596b5SMatthias Ringwald PMC->PMC_MCKR =
109*1b2596b5SMatthias Ringwald (PMC->PMC_MCKR & (~PMC_MCKR_PRES_Msk)) | ul_pres;
110*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MCKRDY));
111*1b2596b5SMatthias Ringwald }
112*1b2596b5SMatthias Ringwald
113*1b2596b5SMatthias Ringwald #if SAMV71 || SAMV70 || SAME70 || SAMS70
114*1b2596b5SMatthias Ringwald /**
115*1b2596b5SMatthias Ringwald * \brief Set the division of the MCK.
116*1b2596b5SMatthias Ringwald *
117*1b2596b5SMatthias Ringwald * \param ul_div Division value.
118*1b2596b5SMatthias Ringwald */
pmc_mck_set_division(uint32_t ul_div)119*1b2596b5SMatthias Ringwald void pmc_mck_set_division(uint32_t ul_div)
120*1b2596b5SMatthias Ringwald {
121*1b2596b5SMatthias Ringwald switch (ul_div) {
122*1b2596b5SMatthias Ringwald case 1:
123*1b2596b5SMatthias Ringwald ul_div = PMC_MCKR_MDIV_EQ_PCK;
124*1b2596b5SMatthias Ringwald break;
125*1b2596b5SMatthias Ringwald case 2:
126*1b2596b5SMatthias Ringwald ul_div = PMC_MCKR_MDIV_PCK_DIV2;
127*1b2596b5SMatthias Ringwald break;
128*1b2596b5SMatthias Ringwald case 3:
129*1b2596b5SMatthias Ringwald ul_div = PMC_MCKR_MDIV_PCK_DIV3;
130*1b2596b5SMatthias Ringwald break;
131*1b2596b5SMatthias Ringwald case 4:
132*1b2596b5SMatthias Ringwald ul_div = PMC_MCKR_MDIV_PCK_DIV4;
133*1b2596b5SMatthias Ringwald break;
134*1b2596b5SMatthias Ringwald default:
135*1b2596b5SMatthias Ringwald ul_div = PMC_MCKR_MDIV_EQ_PCK;
136*1b2596b5SMatthias Ringwald break;
137*1b2596b5SMatthias Ringwald }
138*1b2596b5SMatthias Ringwald PMC->PMC_MCKR =
139*1b2596b5SMatthias Ringwald (PMC->PMC_MCKR & (~PMC_MCKR_MDIV_Msk)) | ul_div;
140*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MCKRDY));
141*1b2596b5SMatthias Ringwald }
142*1b2596b5SMatthias Ringwald #endif
143*1b2596b5SMatthias Ringwald
144*1b2596b5SMatthias Ringwald /**
145*1b2596b5SMatthias Ringwald * \brief Set the source of the MCK.
146*1b2596b5SMatthias Ringwald *
147*1b2596b5SMatthias Ringwald * \param ul_source Source selection value.
148*1b2596b5SMatthias Ringwald */
pmc_mck_set_source(uint32_t ul_source)149*1b2596b5SMatthias Ringwald void pmc_mck_set_source(uint32_t ul_source)
150*1b2596b5SMatthias Ringwald {
151*1b2596b5SMatthias Ringwald PMC->PMC_MCKR =
152*1b2596b5SMatthias Ringwald (PMC->PMC_MCKR & (~PMC_MCKR_CSS_Msk)) | ul_source;
153*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MCKRDY));
154*1b2596b5SMatthias Ringwald }
155*1b2596b5SMatthias Ringwald
156*1b2596b5SMatthias Ringwald /**
157*1b2596b5SMatthias Ringwald * \brief Switch master clock source selection to slow clock.
158*1b2596b5SMatthias Ringwald *
159*1b2596b5SMatthias Ringwald * \param ul_pres Processor clock prescaler.
160*1b2596b5SMatthias Ringwald *
161*1b2596b5SMatthias Ringwald * \retval 0 Success.
162*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
163*1b2596b5SMatthias Ringwald */
pmc_switch_mck_to_sclk(uint32_t ul_pres)164*1b2596b5SMatthias Ringwald uint32_t pmc_switch_mck_to_sclk(uint32_t ul_pres)
165*1b2596b5SMatthias Ringwald {
166*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
167*1b2596b5SMatthias Ringwald
168*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_CSS_Msk)) |
169*1b2596b5SMatthias Ringwald PMC_MCKR_CSS_SLOW_CLK;
170*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
171*1b2596b5SMatthias Ringwald --ul_timeout) {
172*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
173*1b2596b5SMatthias Ringwald return 1;
174*1b2596b5SMatthias Ringwald }
175*1b2596b5SMatthias Ringwald }
176*1b2596b5SMatthias Ringwald
177*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_PRES_Msk)) | ul_pres;
178*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
179*1b2596b5SMatthias Ringwald --ul_timeout) {
180*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
181*1b2596b5SMatthias Ringwald return 1;
182*1b2596b5SMatthias Ringwald }
183*1b2596b5SMatthias Ringwald }
184*1b2596b5SMatthias Ringwald
185*1b2596b5SMatthias Ringwald return 0;
186*1b2596b5SMatthias Ringwald }
187*1b2596b5SMatthias Ringwald
188*1b2596b5SMatthias Ringwald /**
189*1b2596b5SMatthias Ringwald * \brief Switch master clock source selection to main clock.
190*1b2596b5SMatthias Ringwald *
191*1b2596b5SMatthias Ringwald * \param ul_pres Processor clock prescaler.
192*1b2596b5SMatthias Ringwald *
193*1b2596b5SMatthias Ringwald * \retval 0 Success.
194*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
195*1b2596b5SMatthias Ringwald */
pmc_switch_mck_to_mainck(uint32_t ul_pres)196*1b2596b5SMatthias Ringwald uint32_t pmc_switch_mck_to_mainck(uint32_t ul_pres)
197*1b2596b5SMatthias Ringwald {
198*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
199*1b2596b5SMatthias Ringwald
200*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_CSS_Msk)) |
201*1b2596b5SMatthias Ringwald PMC_MCKR_CSS_MAIN_CLK;
202*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
203*1b2596b5SMatthias Ringwald --ul_timeout) {
204*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
205*1b2596b5SMatthias Ringwald return 1;
206*1b2596b5SMatthias Ringwald }
207*1b2596b5SMatthias Ringwald }
208*1b2596b5SMatthias Ringwald
209*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_PRES_Msk)) | ul_pres;
210*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
211*1b2596b5SMatthias Ringwald --ul_timeout) {
212*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
213*1b2596b5SMatthias Ringwald return 1;
214*1b2596b5SMatthias Ringwald }
215*1b2596b5SMatthias Ringwald }
216*1b2596b5SMatthias Ringwald
217*1b2596b5SMatthias Ringwald return 0;
218*1b2596b5SMatthias Ringwald }
219*1b2596b5SMatthias Ringwald
220*1b2596b5SMatthias Ringwald /**
221*1b2596b5SMatthias Ringwald * \brief Switch master clock source selection to PLLA clock.
222*1b2596b5SMatthias Ringwald *
223*1b2596b5SMatthias Ringwald * \param ul_pres Processor clock prescaler.
224*1b2596b5SMatthias Ringwald *
225*1b2596b5SMatthias Ringwald * \retval 0 Success.
226*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
227*1b2596b5SMatthias Ringwald */
pmc_switch_mck_to_pllack(uint32_t ul_pres)228*1b2596b5SMatthias Ringwald uint32_t pmc_switch_mck_to_pllack(uint32_t ul_pres)
229*1b2596b5SMatthias Ringwald {
230*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
231*1b2596b5SMatthias Ringwald
232*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_PRES_Msk)) | ul_pres;
233*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
234*1b2596b5SMatthias Ringwald --ul_timeout) {
235*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
236*1b2596b5SMatthias Ringwald return 1;
237*1b2596b5SMatthias Ringwald }
238*1b2596b5SMatthias Ringwald }
239*1b2596b5SMatthias Ringwald
240*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_CSS_Msk)) |
241*1b2596b5SMatthias Ringwald PMC_MCKR_CSS_PLLA_CLK;
242*1b2596b5SMatthias Ringwald
243*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
244*1b2596b5SMatthias Ringwald --ul_timeout) {
245*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
246*1b2596b5SMatthias Ringwald return 1;
247*1b2596b5SMatthias Ringwald }
248*1b2596b5SMatthias Ringwald }
249*1b2596b5SMatthias Ringwald
250*1b2596b5SMatthias Ringwald return 0;
251*1b2596b5SMatthias Ringwald }
252*1b2596b5SMatthias Ringwald
253*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM4C || SAM4CM || SAM4CP || SAMG55)
254*1b2596b5SMatthias Ringwald /**
255*1b2596b5SMatthias Ringwald * \brief Switch master clock source selection to PLLB clock.
256*1b2596b5SMatthias Ringwald *
257*1b2596b5SMatthias Ringwald * \param ul_pres Processor clock prescaler.
258*1b2596b5SMatthias Ringwald *
259*1b2596b5SMatthias Ringwald * \retval 0 Success.
260*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
261*1b2596b5SMatthias Ringwald */
pmc_switch_mck_to_pllbck(uint32_t ul_pres)262*1b2596b5SMatthias Ringwald uint32_t pmc_switch_mck_to_pllbck(uint32_t ul_pres)
263*1b2596b5SMatthias Ringwald {
264*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
265*1b2596b5SMatthias Ringwald
266*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_PRES_Msk)) | ul_pres;
267*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
268*1b2596b5SMatthias Ringwald --ul_timeout) {
269*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
270*1b2596b5SMatthias Ringwald return 1;
271*1b2596b5SMatthias Ringwald }
272*1b2596b5SMatthias Ringwald }
273*1b2596b5SMatthias Ringwald
274*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_CSS_Msk)) |
275*1b2596b5SMatthias Ringwald PMC_MCKR_CSS_PLLB_CLK;
276*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
277*1b2596b5SMatthias Ringwald --ul_timeout) {
278*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
279*1b2596b5SMatthias Ringwald return 1;
280*1b2596b5SMatthias Ringwald }
281*1b2596b5SMatthias Ringwald }
282*1b2596b5SMatthias Ringwald
283*1b2596b5SMatthias Ringwald return 0;
284*1b2596b5SMatthias Ringwald }
285*1b2596b5SMatthias Ringwald #endif
286*1b2596b5SMatthias Ringwald
287*1b2596b5SMatthias Ringwald #if (SAM3XA || SAM3U || SAMV71 || SAMV70 || SAME70 || SAMS70)
288*1b2596b5SMatthias Ringwald /**
289*1b2596b5SMatthias Ringwald * \brief Switch master clock source selection to UPLL clock.
290*1b2596b5SMatthias Ringwald *
291*1b2596b5SMatthias Ringwald * \param ul_pres Processor clock prescaler.
292*1b2596b5SMatthias Ringwald *
293*1b2596b5SMatthias Ringwald * \retval 0 Success.
294*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
295*1b2596b5SMatthias Ringwald */
pmc_switch_mck_to_upllck(uint32_t ul_pres)296*1b2596b5SMatthias Ringwald uint32_t pmc_switch_mck_to_upllck(uint32_t ul_pres)
297*1b2596b5SMatthias Ringwald {
298*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
299*1b2596b5SMatthias Ringwald
300*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_PRES_Msk)) | ul_pres;
301*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
302*1b2596b5SMatthias Ringwald --ul_timeout) {
303*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
304*1b2596b5SMatthias Ringwald return 1;
305*1b2596b5SMatthias Ringwald }
306*1b2596b5SMatthias Ringwald }
307*1b2596b5SMatthias Ringwald
308*1b2596b5SMatthias Ringwald PMC->PMC_MCKR = (PMC->PMC_MCKR & (~PMC_MCKR_CSS_Msk)) |
309*1b2596b5SMatthias Ringwald PMC_MCKR_CSS_UPLL_CLK;
310*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT; !(PMC->PMC_SR & PMC_SR_MCKRDY);
311*1b2596b5SMatthias Ringwald --ul_timeout) {
312*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
313*1b2596b5SMatthias Ringwald return 1;
314*1b2596b5SMatthias Ringwald }
315*1b2596b5SMatthias Ringwald }
316*1b2596b5SMatthias Ringwald
317*1b2596b5SMatthias Ringwald return 0;
318*1b2596b5SMatthias Ringwald }
319*1b2596b5SMatthias Ringwald #endif
320*1b2596b5SMatthias Ringwald
321*1b2596b5SMatthias Ringwald /**
322*1b2596b5SMatthias Ringwald * \brief Switch slow clock source selection to external 32k (Xtal or Bypass).
323*1b2596b5SMatthias Ringwald *
324*1b2596b5SMatthias Ringwald * \note Switching SCLK back to 32krc is only possible by shutting down the
325*1b2596b5SMatthias Ringwald * VDDIO power supply.
326*1b2596b5SMatthias Ringwald *
327*1b2596b5SMatthias Ringwald * \param ul_bypass 0 for Xtal, 1 for bypass.
328*1b2596b5SMatthias Ringwald */
pmc_switch_sclk_to_32kxtal(uint32_t ul_bypass)329*1b2596b5SMatthias Ringwald void pmc_switch_sclk_to_32kxtal(uint32_t ul_bypass)
330*1b2596b5SMatthias Ringwald {
331*1b2596b5SMatthias Ringwald /* Set Bypass mode if required */
332*1b2596b5SMatthias Ringwald if (ul_bypass == 1) {
333*1b2596b5SMatthias Ringwald SUPC->SUPC_MR |= SUPC_MR_KEY_PASSWD |
334*1b2596b5SMatthias Ringwald SUPC_MR_OSCBYPASS;
335*1b2596b5SMatthias Ringwald }
336*1b2596b5SMatthias Ringwald
337*1b2596b5SMatthias Ringwald SUPC->SUPC_CR = SUPC_CR_KEY_PASSWD | SUPC_CR_XTALSEL;
338*1b2596b5SMatthias Ringwald }
339*1b2596b5SMatthias Ringwald
340*1b2596b5SMatthias Ringwald /**
341*1b2596b5SMatthias Ringwald * \brief Check if the external 32k Xtal is ready.
342*1b2596b5SMatthias Ringwald *
343*1b2596b5SMatthias Ringwald * \retval 1 External 32k Xtal is ready.
344*1b2596b5SMatthias Ringwald * \retval 0 External 32k Xtal is not ready.
345*1b2596b5SMatthias Ringwald */
pmc_osc_is_ready_32kxtal(void)346*1b2596b5SMatthias Ringwald uint32_t pmc_osc_is_ready_32kxtal(void)
347*1b2596b5SMatthias Ringwald {
348*1b2596b5SMatthias Ringwald return ((SUPC->SUPC_SR & SUPC_SR_OSCSEL)
349*1b2596b5SMatthias Ringwald && (PMC->PMC_SR & PMC_SR_OSCSELS));
350*1b2596b5SMatthias Ringwald }
351*1b2596b5SMatthias Ringwald
352*1b2596b5SMatthias Ringwald /**
353*1b2596b5SMatthias Ringwald * \brief Switch main clock source selection to internal fast RC.
354*1b2596b5SMatthias Ringwald *
355*1b2596b5SMatthias Ringwald * \param ul_moscrcf Fast RC oscillator(4/8/12Mhz).
356*1b2596b5SMatthias Ringwald *
357*1b2596b5SMatthias Ringwald * \retval 0 Success.
358*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
359*1b2596b5SMatthias Ringwald * \retval 2 Invalid frequency.
360*1b2596b5SMatthias Ringwald */
pmc_switch_mainck_to_fastrc(uint32_t ul_moscrcf)361*1b2596b5SMatthias Ringwald void pmc_switch_mainck_to_fastrc(uint32_t ul_moscrcf)
362*1b2596b5SMatthias Ringwald {
363*1b2596b5SMatthias Ringwald /* Enable Fast RC oscillator but DO NOT switch to RC now */
364*1b2596b5SMatthias Ringwald PMC->CKGR_MOR |= (CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCRCEN);
365*1b2596b5SMatthias Ringwald
366*1b2596b5SMatthias Ringwald /* Wait the Fast RC to stabilize */
367*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MOSCRCS));
368*1b2596b5SMatthias Ringwald
369*1b2596b5SMatthias Ringwald /* Change Fast RC oscillator frequency */
370*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCRCF_Msk) |
371*1b2596b5SMatthias Ringwald CKGR_MOR_KEY_PASSWD | ul_moscrcf;
372*1b2596b5SMatthias Ringwald
373*1b2596b5SMatthias Ringwald /* Wait the Fast RC to stabilize */
374*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MOSCRCS));
375*1b2596b5SMatthias Ringwald
376*1b2596b5SMatthias Ringwald /* Switch to Fast RC */
377*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCSEL) |
378*1b2596b5SMatthias Ringwald CKGR_MOR_KEY_PASSWD;
379*1b2596b5SMatthias Ringwald }
380*1b2596b5SMatthias Ringwald
381*1b2596b5SMatthias Ringwald /**
382*1b2596b5SMatthias Ringwald * \brief Enable fast RC oscillator.
383*1b2596b5SMatthias Ringwald *
384*1b2596b5SMatthias Ringwald * \param ul_rc Fast RC oscillator(4/8/12Mhz).
385*1b2596b5SMatthias Ringwald */
pmc_osc_enable_fastrc(uint32_t ul_rc)386*1b2596b5SMatthias Ringwald void pmc_osc_enable_fastrc(uint32_t ul_rc)
387*1b2596b5SMatthias Ringwald {
388*1b2596b5SMatthias Ringwald /* Enable Fast RC oscillator but DO NOT switch to RC */
389*1b2596b5SMatthias Ringwald PMC->CKGR_MOR |= (CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCRCEN);
390*1b2596b5SMatthias Ringwald /* Wait the Fast RC to stabilize */
391*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MOSCRCS));
392*1b2596b5SMatthias Ringwald
393*1b2596b5SMatthias Ringwald /* Change Fast RC oscillator frequency */
394*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCRCF_Msk) |
395*1b2596b5SMatthias Ringwald CKGR_MOR_KEY_PASSWD | ul_rc;
396*1b2596b5SMatthias Ringwald /* Wait the Fast RC to stabilize */
397*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MOSCRCS));
398*1b2596b5SMatthias Ringwald }
399*1b2596b5SMatthias Ringwald
400*1b2596b5SMatthias Ringwald /**
401*1b2596b5SMatthias Ringwald * \brief Disable the internal fast RC.
402*1b2596b5SMatthias Ringwald */
pmc_osc_disable_fastrc(void)403*1b2596b5SMatthias Ringwald void pmc_osc_disable_fastrc(void)
404*1b2596b5SMatthias Ringwald {
405*1b2596b5SMatthias Ringwald /* Disable Fast RC oscillator */
406*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCRCEN &
407*1b2596b5SMatthias Ringwald ~CKGR_MOR_MOSCRCF_Msk)
408*1b2596b5SMatthias Ringwald | CKGR_MOR_KEY_PASSWD;
409*1b2596b5SMatthias Ringwald }
410*1b2596b5SMatthias Ringwald
411*1b2596b5SMatthias Ringwald /**
412*1b2596b5SMatthias Ringwald * \brief Check if the main fastrc is ready.
413*1b2596b5SMatthias Ringwald *
414*1b2596b5SMatthias Ringwald * \retval 0 Xtal is not ready, otherwise ready.
415*1b2596b5SMatthias Ringwald */
pmc_osc_is_ready_fastrc(void)416*1b2596b5SMatthias Ringwald uint32_t pmc_osc_is_ready_fastrc(void)
417*1b2596b5SMatthias Ringwald {
418*1b2596b5SMatthias Ringwald return (PMC->PMC_SR & PMC_SR_MOSCRCS);
419*1b2596b5SMatthias Ringwald }
420*1b2596b5SMatthias Ringwald
421*1b2596b5SMatthias Ringwald /**
422*1b2596b5SMatthias Ringwald * \brief Enable main XTAL oscillator.
423*1b2596b5SMatthias Ringwald *
424*1b2596b5SMatthias Ringwald * \param ul_xtal_startup_time Xtal start-up time, in number of slow clocks.
425*1b2596b5SMatthias Ringwald */
pmc_osc_enable_main_xtal(uint32_t ul_xtal_startup_time)426*1b2596b5SMatthias Ringwald void pmc_osc_enable_main_xtal(uint32_t ul_xtal_startup_time)
427*1b2596b5SMatthias Ringwald {
428*1b2596b5SMatthias Ringwald uint32_t mor = PMC->CKGR_MOR;
429*1b2596b5SMatthias Ringwald mor &= ~(CKGR_MOR_MOSCXTBY|CKGR_MOR_MOSCXTEN);
430*1b2596b5SMatthias Ringwald mor |= CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCXTEN |
431*1b2596b5SMatthias Ringwald CKGR_MOR_MOSCXTST(ul_xtal_startup_time);
432*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = mor;
433*1b2596b5SMatthias Ringwald /* Wait the main Xtal to stabilize */
434*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MOSCXTS));
435*1b2596b5SMatthias Ringwald }
436*1b2596b5SMatthias Ringwald
437*1b2596b5SMatthias Ringwald /**
438*1b2596b5SMatthias Ringwald * \brief Bypass main XTAL.
439*1b2596b5SMatthias Ringwald */
pmc_osc_bypass_main_xtal(void)440*1b2596b5SMatthias Ringwald void pmc_osc_bypass_main_xtal(void)
441*1b2596b5SMatthias Ringwald {
442*1b2596b5SMatthias Ringwald uint32_t mor = PMC->CKGR_MOR;
443*1b2596b5SMatthias Ringwald mor &= ~(CKGR_MOR_MOSCXTBY|CKGR_MOR_MOSCXTEN);
444*1b2596b5SMatthias Ringwald mor |= CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCXTBY;
445*1b2596b5SMatthias Ringwald /* Enable Crystal oscillator but DO NOT switch now. Keep MOSCSEL to 0 */
446*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = mor;
447*1b2596b5SMatthias Ringwald /* The MOSCXTS in PMC_SR is automatically set */
448*1b2596b5SMatthias Ringwald }
449*1b2596b5SMatthias Ringwald
450*1b2596b5SMatthias Ringwald /**
451*1b2596b5SMatthias Ringwald * \brief Disable the main Xtal.
452*1b2596b5SMatthias Ringwald */
pmc_osc_disable_main_xtal(void)453*1b2596b5SMatthias Ringwald void pmc_osc_disable_main_xtal(void)
454*1b2596b5SMatthias Ringwald {
455*1b2596b5SMatthias Ringwald uint32_t mor = PMC->CKGR_MOR;
456*1b2596b5SMatthias Ringwald mor &= ~(CKGR_MOR_MOSCXTBY|CKGR_MOR_MOSCXTEN);
457*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = CKGR_MOR_KEY_PASSWD | mor;
458*1b2596b5SMatthias Ringwald }
459*1b2596b5SMatthias Ringwald
460*1b2596b5SMatthias Ringwald /**
461*1b2596b5SMatthias Ringwald * \brief Check if the main crystal is bypassed.
462*1b2596b5SMatthias Ringwald *
463*1b2596b5SMatthias Ringwald * \retval 0 Xtal is bypassed, otherwise not.
464*1b2596b5SMatthias Ringwald */
pmc_osc_is_bypassed_main_xtal(void)465*1b2596b5SMatthias Ringwald uint32_t pmc_osc_is_bypassed_main_xtal(void)
466*1b2596b5SMatthias Ringwald {
467*1b2596b5SMatthias Ringwald return (PMC->CKGR_MOR & CKGR_MOR_MOSCXTBY);
468*1b2596b5SMatthias Ringwald }
469*1b2596b5SMatthias Ringwald
470*1b2596b5SMatthias Ringwald /**
471*1b2596b5SMatthias Ringwald * \brief Check if the main crystal is ready.
472*1b2596b5SMatthias Ringwald *
473*1b2596b5SMatthias Ringwald * \note If main crystal is bypassed, it's always ready.
474*1b2596b5SMatthias Ringwald *
475*1b2596b5SMatthias Ringwald * \retval 0 main crystal is not ready, otherwise ready.
476*1b2596b5SMatthias Ringwald */
pmc_osc_is_ready_main_xtal(void)477*1b2596b5SMatthias Ringwald uint32_t pmc_osc_is_ready_main_xtal(void)
478*1b2596b5SMatthias Ringwald {
479*1b2596b5SMatthias Ringwald return (PMC->PMC_SR & PMC_SR_MOSCXTS);
480*1b2596b5SMatthias Ringwald }
481*1b2596b5SMatthias Ringwald
482*1b2596b5SMatthias Ringwald /**
483*1b2596b5SMatthias Ringwald * \brief Switch main clock source selection to external Xtal/Bypass.
484*1b2596b5SMatthias Ringwald *
485*1b2596b5SMatthias Ringwald * \note The function may switch MCK to SCLK if MCK source is MAINCK to avoid
486*1b2596b5SMatthias Ringwald * any system crash.
487*1b2596b5SMatthias Ringwald *
488*1b2596b5SMatthias Ringwald * \note If used in Xtal mode, the Xtal is automatically enabled.
489*1b2596b5SMatthias Ringwald *
490*1b2596b5SMatthias Ringwald * \param ul_bypass 0 for Xtal, 1 for bypass.
491*1b2596b5SMatthias Ringwald *
492*1b2596b5SMatthias Ringwald * \retval 0 Success.
493*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
494*1b2596b5SMatthias Ringwald */
pmc_switch_mainck_to_xtal(uint32_t ul_bypass,uint32_t ul_xtal_startup_time)495*1b2596b5SMatthias Ringwald void pmc_switch_mainck_to_xtal(uint32_t ul_bypass,
496*1b2596b5SMatthias Ringwald uint32_t ul_xtal_startup_time)
497*1b2596b5SMatthias Ringwald {
498*1b2596b5SMatthias Ringwald /* Enable Main Xtal oscillator */
499*1b2596b5SMatthias Ringwald if (ul_bypass) {
500*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCXTEN) |
501*1b2596b5SMatthias Ringwald CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCXTBY |
502*1b2596b5SMatthias Ringwald CKGR_MOR_MOSCSEL;
503*1b2596b5SMatthias Ringwald } else {
504*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCXTBY) |
505*1b2596b5SMatthias Ringwald CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCXTEN |
506*1b2596b5SMatthias Ringwald CKGR_MOR_MOSCXTST(ul_xtal_startup_time);
507*1b2596b5SMatthias Ringwald /* Wait the Xtal to stabilize */
508*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MOSCXTS));
509*1b2596b5SMatthias Ringwald
510*1b2596b5SMatthias Ringwald PMC->CKGR_MOR |= CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCSEL;
511*1b2596b5SMatthias Ringwald }
512*1b2596b5SMatthias Ringwald }
513*1b2596b5SMatthias Ringwald
514*1b2596b5SMatthias Ringwald /**
515*1b2596b5SMatthias Ringwald * \brief Disable the external Xtal.
516*1b2596b5SMatthias Ringwald *
517*1b2596b5SMatthias Ringwald * \param ul_bypass 0 for Xtal, 1 for bypass.
518*1b2596b5SMatthias Ringwald */
pmc_osc_disable_xtal(uint32_t ul_bypass)519*1b2596b5SMatthias Ringwald void pmc_osc_disable_xtal(uint32_t ul_bypass)
520*1b2596b5SMatthias Ringwald {
521*1b2596b5SMatthias Ringwald /* Disable xtal oscillator */
522*1b2596b5SMatthias Ringwald if (ul_bypass) {
523*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCXTBY) |
524*1b2596b5SMatthias Ringwald CKGR_MOR_KEY_PASSWD;
525*1b2596b5SMatthias Ringwald } else {
526*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = (PMC->CKGR_MOR & ~CKGR_MOR_MOSCXTEN) |
527*1b2596b5SMatthias Ringwald CKGR_MOR_KEY_PASSWD;
528*1b2596b5SMatthias Ringwald }
529*1b2596b5SMatthias Ringwald }
530*1b2596b5SMatthias Ringwald
531*1b2596b5SMatthias Ringwald /**
532*1b2596b5SMatthias Ringwald * \brief Check if the MAINCK is ready. Depending on MOSCEL, MAINCK can be one
533*1b2596b5SMatthias Ringwald * of Xtal, bypass or internal RC.
534*1b2596b5SMatthias Ringwald *
535*1b2596b5SMatthias Ringwald * \retval 1 Xtal is ready.
536*1b2596b5SMatthias Ringwald * \retval 0 Xtal is not ready.
537*1b2596b5SMatthias Ringwald */
pmc_osc_is_ready_mainck(void)538*1b2596b5SMatthias Ringwald uint32_t pmc_osc_is_ready_mainck(void)
539*1b2596b5SMatthias Ringwald {
540*1b2596b5SMatthias Ringwald return PMC->PMC_SR & PMC_SR_MOSCSELS;
541*1b2596b5SMatthias Ringwald }
542*1b2596b5SMatthias Ringwald
543*1b2596b5SMatthias Ringwald /**
544*1b2596b5SMatthias Ringwald * \brief Select Main Crystal or internal RC as main clock source.
545*1b2596b5SMatthias Ringwald *
546*1b2596b5SMatthias Ringwald * \note This function will not enable/disable RC or Main Crystal.
547*1b2596b5SMatthias Ringwald *
548*1b2596b5SMatthias Ringwald * \param ul_xtal_rc 0 internal RC is selected, otherwise Main Crystal.
549*1b2596b5SMatthias Ringwald */
pmc_mainck_osc_select(uint32_t ul_xtal_rc)550*1b2596b5SMatthias Ringwald void pmc_mainck_osc_select(uint32_t ul_xtal_rc)
551*1b2596b5SMatthias Ringwald {
552*1b2596b5SMatthias Ringwald uint32_t mor = PMC->CKGR_MOR;
553*1b2596b5SMatthias Ringwald if (ul_xtal_rc) {
554*1b2596b5SMatthias Ringwald mor |= CKGR_MOR_MOSCSEL;
555*1b2596b5SMatthias Ringwald } else {
556*1b2596b5SMatthias Ringwald mor &= ~CKGR_MOR_MOSCSEL;
557*1b2596b5SMatthias Ringwald }
558*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = CKGR_MOR_KEY_PASSWD | mor;
559*1b2596b5SMatthias Ringwald }
560*1b2596b5SMatthias Ringwald
561*1b2596b5SMatthias Ringwald /**
562*1b2596b5SMatthias Ringwald * \brief Enable PLLA clock.
563*1b2596b5SMatthias Ringwald *
564*1b2596b5SMatthias Ringwald * \param mula PLLA multiplier.
565*1b2596b5SMatthias Ringwald * \param pllacount PLLA counter.
566*1b2596b5SMatthias Ringwald * \param diva Divider.
567*1b2596b5SMatthias Ringwald */
pmc_enable_pllack(uint32_t mula,uint32_t pllacount,uint32_t diva)568*1b2596b5SMatthias Ringwald void pmc_enable_pllack(uint32_t mula, uint32_t pllacount, uint32_t diva)
569*1b2596b5SMatthias Ringwald {
570*1b2596b5SMatthias Ringwald /* first disable the PLL to unlock the lock */
571*1b2596b5SMatthias Ringwald pmc_disable_pllack();
572*1b2596b5SMatthias Ringwald
573*1b2596b5SMatthias Ringwald #if (SAM4C || SAM4CM || SAM4CP || SAMG)
574*1b2596b5SMatthias Ringwald PMC->CKGR_PLLAR = CKGR_PLLAR_PLLAEN(diva) |
575*1b2596b5SMatthias Ringwald CKGR_PLLAR_PLLACOUNT(pllacount) | CKGR_PLLAR_MULA(mula);
576*1b2596b5SMatthias Ringwald #else
577*1b2596b5SMatthias Ringwald PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | CKGR_PLLAR_DIVA(diva) |
578*1b2596b5SMatthias Ringwald CKGR_PLLAR_PLLACOUNT(pllacount) | CKGR_PLLAR_MULA(mula);
579*1b2596b5SMatthias Ringwald #endif
580*1b2596b5SMatthias Ringwald while ((PMC->PMC_SR & PMC_SR_LOCKA) == 0);
581*1b2596b5SMatthias Ringwald }
582*1b2596b5SMatthias Ringwald
583*1b2596b5SMatthias Ringwald /**
584*1b2596b5SMatthias Ringwald * \brief Disable PLLA clock.
585*1b2596b5SMatthias Ringwald */
pmc_disable_pllack(void)586*1b2596b5SMatthias Ringwald void pmc_disable_pllack(void)
587*1b2596b5SMatthias Ringwald {
588*1b2596b5SMatthias Ringwald #if (SAM4C || SAM4CM || SAM4CP || SAMG)
589*1b2596b5SMatthias Ringwald PMC->CKGR_PLLAR = CKGR_PLLAR_MULA(0);
590*1b2596b5SMatthias Ringwald #else
591*1b2596b5SMatthias Ringwald PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | CKGR_PLLAR_MULA(0);
592*1b2596b5SMatthias Ringwald #endif
593*1b2596b5SMatthias Ringwald }
594*1b2596b5SMatthias Ringwald
595*1b2596b5SMatthias Ringwald /**
596*1b2596b5SMatthias Ringwald * \brief Is PLLA locked?
597*1b2596b5SMatthias Ringwald *
598*1b2596b5SMatthias Ringwald * \retval 0 Not locked.
599*1b2596b5SMatthias Ringwald * \retval 1 Locked.
600*1b2596b5SMatthias Ringwald */
pmc_is_locked_pllack(void)601*1b2596b5SMatthias Ringwald uint32_t pmc_is_locked_pllack(void)
602*1b2596b5SMatthias Ringwald {
603*1b2596b5SMatthias Ringwald return (PMC->PMC_SR & PMC_SR_LOCKA);
604*1b2596b5SMatthias Ringwald }
605*1b2596b5SMatthias Ringwald
606*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM4C || SAM4CM || SAM4CP || SAMG55)
607*1b2596b5SMatthias Ringwald /**
608*1b2596b5SMatthias Ringwald * \brief Enable PLLB clock.
609*1b2596b5SMatthias Ringwald *
610*1b2596b5SMatthias Ringwald * \param mulb PLLB multiplier.
611*1b2596b5SMatthias Ringwald * \param pllbcount PLLB counter.
612*1b2596b5SMatthias Ringwald * \param divb Divider.
613*1b2596b5SMatthias Ringwald */
pmc_enable_pllbck(uint32_t mulb,uint32_t pllbcount,uint32_t divb)614*1b2596b5SMatthias Ringwald void pmc_enable_pllbck(uint32_t mulb, uint32_t pllbcount, uint32_t divb)
615*1b2596b5SMatthias Ringwald {
616*1b2596b5SMatthias Ringwald /* first disable the PLL to unlock the lock */
617*1b2596b5SMatthias Ringwald pmc_disable_pllbck();
618*1b2596b5SMatthias Ringwald
619*1b2596b5SMatthias Ringwald #if SAMG55
620*1b2596b5SMatthias Ringwald PMC->CKGR_PLLAR = CKGR_PLLAR_PLLAEN(divb) |
621*1b2596b5SMatthias Ringwald CKGR_PLLAR_PLLACOUNT(pllbcount) | CKGR_PLLAR_MULA(mulb);
622*1b2596b5SMatthias Ringwald #else
623*1b2596b5SMatthias Ringwald PMC->CKGR_PLLBR =
624*1b2596b5SMatthias Ringwald CKGR_PLLBR_DIVB(divb) | CKGR_PLLBR_PLLBCOUNT(pllbcount)
625*1b2596b5SMatthias Ringwald | CKGR_PLLBR_MULB(mulb);
626*1b2596b5SMatthias Ringwald #endif
627*1b2596b5SMatthias Ringwald while ((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
628*1b2596b5SMatthias Ringwald }
629*1b2596b5SMatthias Ringwald
630*1b2596b5SMatthias Ringwald /**
631*1b2596b5SMatthias Ringwald * \brief Disable PLLB clock.
632*1b2596b5SMatthias Ringwald */
pmc_disable_pllbck(void)633*1b2596b5SMatthias Ringwald void pmc_disable_pllbck(void)
634*1b2596b5SMatthias Ringwald {
635*1b2596b5SMatthias Ringwald PMC->CKGR_PLLBR = CKGR_PLLBR_MULB(0);
636*1b2596b5SMatthias Ringwald }
637*1b2596b5SMatthias Ringwald
638*1b2596b5SMatthias Ringwald /**
639*1b2596b5SMatthias Ringwald * \brief Is PLLB locked?
640*1b2596b5SMatthias Ringwald *
641*1b2596b5SMatthias Ringwald * \retval 0 Not locked.
642*1b2596b5SMatthias Ringwald * \retval 1 Locked.
643*1b2596b5SMatthias Ringwald */
pmc_is_locked_pllbck(void)644*1b2596b5SMatthias Ringwald uint32_t pmc_is_locked_pllbck(void)
645*1b2596b5SMatthias Ringwald {
646*1b2596b5SMatthias Ringwald return (PMC->PMC_SR & PMC_SR_LOCKB);
647*1b2596b5SMatthias Ringwald }
648*1b2596b5SMatthias Ringwald #endif
649*1b2596b5SMatthias Ringwald
650*1b2596b5SMatthias Ringwald #if (SAM3XA || SAM3U || SAMV71 || SAMV70 || SAME70 || SAMS70)
651*1b2596b5SMatthias Ringwald /**
652*1b2596b5SMatthias Ringwald * \brief Enable UPLL clock.
653*1b2596b5SMatthias Ringwald */
pmc_enable_upll_clock(void)654*1b2596b5SMatthias Ringwald void pmc_enable_upll_clock(void)
655*1b2596b5SMatthias Ringwald {
656*1b2596b5SMatthias Ringwald PMC->CKGR_UCKR = CKGR_UCKR_UPLLCOUNT(3) | CKGR_UCKR_UPLLEN;
657*1b2596b5SMatthias Ringwald
658*1b2596b5SMatthias Ringwald /* Wait UTMI PLL Lock Status */
659*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_LOCKU));
660*1b2596b5SMatthias Ringwald }
661*1b2596b5SMatthias Ringwald
662*1b2596b5SMatthias Ringwald /**
663*1b2596b5SMatthias Ringwald * \brief Disable UPLL clock.
664*1b2596b5SMatthias Ringwald */
pmc_disable_upll_clock(void)665*1b2596b5SMatthias Ringwald void pmc_disable_upll_clock(void)
666*1b2596b5SMatthias Ringwald {
667*1b2596b5SMatthias Ringwald PMC->CKGR_UCKR &= ~CKGR_UCKR_UPLLEN;
668*1b2596b5SMatthias Ringwald }
669*1b2596b5SMatthias Ringwald
670*1b2596b5SMatthias Ringwald /**
671*1b2596b5SMatthias Ringwald * \brief Is UPLL locked?
672*1b2596b5SMatthias Ringwald *
673*1b2596b5SMatthias Ringwald * \retval 0 Not locked.
674*1b2596b5SMatthias Ringwald * \retval 1 Locked.
675*1b2596b5SMatthias Ringwald */
pmc_is_locked_upll(void)676*1b2596b5SMatthias Ringwald uint32_t pmc_is_locked_upll(void)
677*1b2596b5SMatthias Ringwald {
678*1b2596b5SMatthias Ringwald return (PMC->PMC_SR & PMC_SR_LOCKU);
679*1b2596b5SMatthias Ringwald }
680*1b2596b5SMatthias Ringwald #endif
681*1b2596b5SMatthias Ringwald
682*1b2596b5SMatthias Ringwald /**
683*1b2596b5SMatthias Ringwald * \brief Enable the specified peripheral clock.
684*1b2596b5SMatthias Ringwald *
685*1b2596b5SMatthias Ringwald * \note The ID must NOT be shifted (i.e., 1 << ID_xxx).
686*1b2596b5SMatthias Ringwald *
687*1b2596b5SMatthias Ringwald * \param ul_id Peripheral ID (ID_xxx).
688*1b2596b5SMatthias Ringwald *
689*1b2596b5SMatthias Ringwald * \retval 0 Success.
690*1b2596b5SMatthias Ringwald * \retval 1 Invalid parameter.
691*1b2596b5SMatthias Ringwald */
pmc_enable_periph_clk(uint32_t ul_id)692*1b2596b5SMatthias Ringwald uint32_t pmc_enable_periph_clk(uint32_t ul_id)
693*1b2596b5SMatthias Ringwald {
694*1b2596b5SMatthias Ringwald if (ul_id > MAX_PERIPH_ID) {
695*1b2596b5SMatthias Ringwald return 1;
696*1b2596b5SMatthias Ringwald }
697*1b2596b5SMatthias Ringwald
698*1b2596b5SMatthias Ringwald if (ul_id < 32) {
699*1b2596b5SMatthias Ringwald if ((PMC->PMC_PCSR0 & (1u << ul_id)) != (1u << ul_id)) {
700*1b2596b5SMatthias Ringwald PMC->PMC_PCER0 = 1 << ul_id;
701*1b2596b5SMatthias Ringwald }
702*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAM4C || SAM4CM || SAM4CP || SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
703*1b2596b5SMatthias Ringwald } else {
704*1b2596b5SMatthias Ringwald ul_id -= 32;
705*1b2596b5SMatthias Ringwald if ((PMC->PMC_PCSR1 & (1u << ul_id)) != (1u << ul_id)) {
706*1b2596b5SMatthias Ringwald PMC->PMC_PCER1 = 1 << ul_id;
707*1b2596b5SMatthias Ringwald }
708*1b2596b5SMatthias Ringwald #endif
709*1b2596b5SMatthias Ringwald }
710*1b2596b5SMatthias Ringwald
711*1b2596b5SMatthias Ringwald return 0;
712*1b2596b5SMatthias Ringwald }
713*1b2596b5SMatthias Ringwald
714*1b2596b5SMatthias Ringwald /**
715*1b2596b5SMatthias Ringwald * \brief Disable the specified peripheral clock.
716*1b2596b5SMatthias Ringwald *
717*1b2596b5SMatthias Ringwald * \note The ID must NOT be shifted (i.e., 1 << ID_xxx).
718*1b2596b5SMatthias Ringwald *
719*1b2596b5SMatthias Ringwald * \param ul_id Peripheral ID (ID_xxx).
720*1b2596b5SMatthias Ringwald *
721*1b2596b5SMatthias Ringwald * \retval 0 Success.
722*1b2596b5SMatthias Ringwald * \retval 1 Invalid parameter.
723*1b2596b5SMatthias Ringwald */
pmc_disable_periph_clk(uint32_t ul_id)724*1b2596b5SMatthias Ringwald uint32_t pmc_disable_periph_clk(uint32_t ul_id)
725*1b2596b5SMatthias Ringwald {
726*1b2596b5SMatthias Ringwald if (ul_id > MAX_PERIPH_ID) {
727*1b2596b5SMatthias Ringwald return 1;
728*1b2596b5SMatthias Ringwald }
729*1b2596b5SMatthias Ringwald
730*1b2596b5SMatthias Ringwald if (ul_id < 32) {
731*1b2596b5SMatthias Ringwald if ((PMC->PMC_PCSR0 & (1u << ul_id)) == (1u << ul_id)) {
732*1b2596b5SMatthias Ringwald PMC->PMC_PCDR0 = 1 << ul_id;
733*1b2596b5SMatthias Ringwald }
734*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAM4C || SAM4CM || SAM4CP || SAMG55 || SAMV71 \
735*1b2596b5SMatthias Ringwald || SAMV70 || SAME70 || SAMS70)
736*1b2596b5SMatthias Ringwald } else {
737*1b2596b5SMatthias Ringwald ul_id -= 32;
738*1b2596b5SMatthias Ringwald if ((PMC->PMC_PCSR1 & (1u << ul_id)) == (1u << ul_id)) {
739*1b2596b5SMatthias Ringwald PMC->PMC_PCDR1 = 1 << ul_id;
740*1b2596b5SMatthias Ringwald }
741*1b2596b5SMatthias Ringwald #endif
742*1b2596b5SMatthias Ringwald }
743*1b2596b5SMatthias Ringwald return 0;
744*1b2596b5SMatthias Ringwald }
745*1b2596b5SMatthias Ringwald
746*1b2596b5SMatthias Ringwald /**
747*1b2596b5SMatthias Ringwald * \brief Enable all peripheral clocks.
748*1b2596b5SMatthias Ringwald */
pmc_enable_all_periph_clk(void)749*1b2596b5SMatthias Ringwald void pmc_enable_all_periph_clk(void)
750*1b2596b5SMatthias Ringwald {
751*1b2596b5SMatthias Ringwald PMC->PMC_PCER0 = PMC_MASK_STATUS0;
752*1b2596b5SMatthias Ringwald while ((PMC->PMC_PCSR0 & PMC_MASK_STATUS0) != PMC_MASK_STATUS0);
753*1b2596b5SMatthias Ringwald
754*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAM4C || SAM4CM || SAM4CP || SAMV71 \
755*1b2596b5SMatthias Ringwald || SAMV70 || SAME70 || SAMS70)
756*1b2596b5SMatthias Ringwald PMC->PMC_PCER1 = PMC_MASK_STATUS1;
757*1b2596b5SMatthias Ringwald while ((PMC->PMC_PCSR1 & PMC_MASK_STATUS1) != PMC_MASK_STATUS1);
758*1b2596b5SMatthias Ringwald #endif
759*1b2596b5SMatthias Ringwald }
760*1b2596b5SMatthias Ringwald
761*1b2596b5SMatthias Ringwald /**
762*1b2596b5SMatthias Ringwald * \brief Disable all peripheral clocks.
763*1b2596b5SMatthias Ringwald */
pmc_disable_all_periph_clk(void)764*1b2596b5SMatthias Ringwald void pmc_disable_all_periph_clk(void)
765*1b2596b5SMatthias Ringwald {
766*1b2596b5SMatthias Ringwald PMC->PMC_PCDR0 = PMC_MASK_STATUS0;
767*1b2596b5SMatthias Ringwald while ((PMC->PMC_PCSR0 & PMC_MASK_STATUS0) != 0);
768*1b2596b5SMatthias Ringwald
769*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAM4C || SAM4CM || SAM4CP || SAMV71 \
770*1b2596b5SMatthias Ringwald || SAMV70 || SAME70 || SAMS70)
771*1b2596b5SMatthias Ringwald PMC->PMC_PCDR1 = PMC_MASK_STATUS1;
772*1b2596b5SMatthias Ringwald while ((PMC->PMC_PCSR1 & PMC_MASK_STATUS1) != 0);
773*1b2596b5SMatthias Ringwald #endif
774*1b2596b5SMatthias Ringwald }
775*1b2596b5SMatthias Ringwald
776*1b2596b5SMatthias Ringwald /**
777*1b2596b5SMatthias Ringwald * \brief Check if the specified peripheral clock is enabled.
778*1b2596b5SMatthias Ringwald *
779*1b2596b5SMatthias Ringwald * \note The ID must NOT be shifted (i.e., 1 << ID_xxx).
780*1b2596b5SMatthias Ringwald *
781*1b2596b5SMatthias Ringwald * \param ul_id Peripheral ID (ID_xxx).
782*1b2596b5SMatthias Ringwald *
783*1b2596b5SMatthias Ringwald * \retval 0 Peripheral clock is disabled or unknown.
784*1b2596b5SMatthias Ringwald * \retval 1 Peripheral clock is enabled.
785*1b2596b5SMatthias Ringwald */
pmc_is_periph_clk_enabled(uint32_t ul_id)786*1b2596b5SMatthias Ringwald uint32_t pmc_is_periph_clk_enabled(uint32_t ul_id)
787*1b2596b5SMatthias Ringwald {
788*1b2596b5SMatthias Ringwald if (ul_id > MAX_PERIPH_ID) {
789*1b2596b5SMatthias Ringwald return 0;
790*1b2596b5SMatthias Ringwald }
791*1b2596b5SMatthias Ringwald
792*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAM4C || SAM4CM || SAM4CP || SAMV71 \
793*1b2596b5SMatthias Ringwald || SAMV70 || SAME70 || SAMS70)
794*1b2596b5SMatthias Ringwald if (ul_id < 32) {
795*1b2596b5SMatthias Ringwald #endif
796*1b2596b5SMatthias Ringwald if ((PMC->PMC_PCSR0 & (1u << ul_id))) {
797*1b2596b5SMatthias Ringwald return 1;
798*1b2596b5SMatthias Ringwald } else {
799*1b2596b5SMatthias Ringwald return 0;
800*1b2596b5SMatthias Ringwald }
801*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAM4C || SAM4CM || SAM4CP || SAMV71 \
802*1b2596b5SMatthias Ringwald || SAMV70 || SAME70 || SAMS70)
803*1b2596b5SMatthias Ringwald } else {
804*1b2596b5SMatthias Ringwald ul_id -= 32;
805*1b2596b5SMatthias Ringwald if ((PMC->PMC_PCSR1 & (1u << ul_id))) {
806*1b2596b5SMatthias Ringwald return 1;
807*1b2596b5SMatthias Ringwald } else {
808*1b2596b5SMatthias Ringwald return 0;
809*1b2596b5SMatthias Ringwald }
810*1b2596b5SMatthias Ringwald }
811*1b2596b5SMatthias Ringwald #endif
812*1b2596b5SMatthias Ringwald }
813*1b2596b5SMatthias Ringwald
814*1b2596b5SMatthias Ringwald /**
815*1b2596b5SMatthias Ringwald * \brief Set the prescaler for the specified programmable clock.
816*1b2596b5SMatthias Ringwald *
817*1b2596b5SMatthias Ringwald * \param ul_id Peripheral ID.
818*1b2596b5SMatthias Ringwald * \param ul_pres Prescaler value.
819*1b2596b5SMatthias Ringwald */
pmc_pck_set_prescaler(uint32_t ul_id,uint32_t ul_pres)820*1b2596b5SMatthias Ringwald void pmc_pck_set_prescaler(uint32_t ul_id, uint32_t ul_pres)
821*1b2596b5SMatthias Ringwald {
822*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] =
823*1b2596b5SMatthias Ringwald (PMC->PMC_PCK[ul_id] & ~PMC_PCK_PRES_Msk) | ul_pres;
824*1b2596b5SMatthias Ringwald while ((PMC->PMC_SCER & (PMC_SCER_PCK0 << ul_id))
825*1b2596b5SMatthias Ringwald && !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id)));
826*1b2596b5SMatthias Ringwald }
827*1b2596b5SMatthias Ringwald
828*1b2596b5SMatthias Ringwald /**
829*1b2596b5SMatthias Ringwald * \brief Set the source oscillator for the specified programmable clock.
830*1b2596b5SMatthias Ringwald *
831*1b2596b5SMatthias Ringwald * \param ul_id Peripheral ID.
832*1b2596b5SMatthias Ringwald * \param ul_source Source selection value.
833*1b2596b5SMatthias Ringwald */
pmc_pck_set_source(uint32_t ul_id,uint32_t ul_source)834*1b2596b5SMatthias Ringwald void pmc_pck_set_source(uint32_t ul_id, uint32_t ul_source)
835*1b2596b5SMatthias Ringwald {
836*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] =
837*1b2596b5SMatthias Ringwald (PMC->PMC_PCK[ul_id] & ~PMC_PCK_CSS_Msk) | ul_source;
838*1b2596b5SMatthias Ringwald while ((PMC->PMC_SCER & (PMC_SCER_PCK0 << ul_id))
839*1b2596b5SMatthias Ringwald && !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id)));
840*1b2596b5SMatthias Ringwald }
841*1b2596b5SMatthias Ringwald
842*1b2596b5SMatthias Ringwald /**
843*1b2596b5SMatthias Ringwald * \brief Switch programmable clock source selection to slow clock.
844*1b2596b5SMatthias Ringwald *
845*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
846*1b2596b5SMatthias Ringwald * \param ul_pres Programmable clock prescaler.
847*1b2596b5SMatthias Ringwald *
848*1b2596b5SMatthias Ringwald * \retval 0 Success.
849*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
850*1b2596b5SMatthias Ringwald */
pmc_switch_pck_to_sclk(uint32_t ul_id,uint32_t ul_pres)851*1b2596b5SMatthias Ringwald uint32_t pmc_switch_pck_to_sclk(uint32_t ul_id, uint32_t ul_pres)
852*1b2596b5SMatthias Ringwald {
853*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
854*1b2596b5SMatthias Ringwald
855*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] = PMC_PCK_CSS_SLOW_CLK | ul_pres;
856*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT;
857*1b2596b5SMatthias Ringwald !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id)); --ul_timeout) {
858*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
859*1b2596b5SMatthias Ringwald return 1;
860*1b2596b5SMatthias Ringwald }
861*1b2596b5SMatthias Ringwald }
862*1b2596b5SMatthias Ringwald
863*1b2596b5SMatthias Ringwald return 0;
864*1b2596b5SMatthias Ringwald }
865*1b2596b5SMatthias Ringwald
866*1b2596b5SMatthias Ringwald /**
867*1b2596b5SMatthias Ringwald * \brief Switch programmable clock source selection to main clock.
868*1b2596b5SMatthias Ringwald *
869*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
870*1b2596b5SMatthias Ringwald * \param ul_pres Programmable clock prescaler.
871*1b2596b5SMatthias Ringwald *
872*1b2596b5SMatthias Ringwald * \retval 0 Success.
873*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
874*1b2596b5SMatthias Ringwald */
pmc_switch_pck_to_mainck(uint32_t ul_id,uint32_t ul_pres)875*1b2596b5SMatthias Ringwald uint32_t pmc_switch_pck_to_mainck(uint32_t ul_id, uint32_t ul_pres)
876*1b2596b5SMatthias Ringwald {
877*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
878*1b2596b5SMatthias Ringwald
879*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] = PMC_PCK_CSS_MAIN_CLK | ul_pres;
880*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT;
881*1b2596b5SMatthias Ringwald !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id)); --ul_timeout) {
882*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
883*1b2596b5SMatthias Ringwald return 1;
884*1b2596b5SMatthias Ringwald }
885*1b2596b5SMatthias Ringwald }
886*1b2596b5SMatthias Ringwald
887*1b2596b5SMatthias Ringwald return 0;
888*1b2596b5SMatthias Ringwald }
889*1b2596b5SMatthias Ringwald
890*1b2596b5SMatthias Ringwald /**
891*1b2596b5SMatthias Ringwald * \brief Switch programmable clock source selection to PLLA clock.
892*1b2596b5SMatthias Ringwald *
893*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
894*1b2596b5SMatthias Ringwald * \param ul_pres Programmable clock prescaler.
895*1b2596b5SMatthias Ringwald *
896*1b2596b5SMatthias Ringwald * \retval 0 Success.
897*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
898*1b2596b5SMatthias Ringwald */
pmc_switch_pck_to_pllack(uint32_t ul_id,uint32_t ul_pres)899*1b2596b5SMatthias Ringwald uint32_t pmc_switch_pck_to_pllack(uint32_t ul_id, uint32_t ul_pres)
900*1b2596b5SMatthias Ringwald {
901*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
902*1b2596b5SMatthias Ringwald
903*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] = PMC_PCK_CSS_PLLA_CLK | ul_pres;
904*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT;
905*1b2596b5SMatthias Ringwald !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id)); --ul_timeout) {
906*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
907*1b2596b5SMatthias Ringwald return 1;
908*1b2596b5SMatthias Ringwald }
909*1b2596b5SMatthias Ringwald }
910*1b2596b5SMatthias Ringwald
911*1b2596b5SMatthias Ringwald return 0;
912*1b2596b5SMatthias Ringwald }
913*1b2596b5SMatthias Ringwald
914*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM4C || SAM4CM || SAM4CP || SAMG55)
915*1b2596b5SMatthias Ringwald /**
916*1b2596b5SMatthias Ringwald * \brief Switch programmable clock source selection to PLLB clock.
917*1b2596b5SMatthias Ringwald *
918*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
919*1b2596b5SMatthias Ringwald * \param ul_pres Programmable clock prescaler.
920*1b2596b5SMatthias Ringwald *
921*1b2596b5SMatthias Ringwald * \retval 0 Success.
922*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
923*1b2596b5SMatthias Ringwald */
pmc_switch_pck_to_pllbck(uint32_t ul_id,uint32_t ul_pres)924*1b2596b5SMatthias Ringwald uint32_t pmc_switch_pck_to_pllbck(uint32_t ul_id, uint32_t ul_pres)
925*1b2596b5SMatthias Ringwald {
926*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
927*1b2596b5SMatthias Ringwald
928*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] = PMC_PCK_CSS_PLLB_CLK | ul_pres;
929*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT;
930*1b2596b5SMatthias Ringwald !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id));
931*1b2596b5SMatthias Ringwald --ul_timeout) {
932*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
933*1b2596b5SMatthias Ringwald return 1;
934*1b2596b5SMatthias Ringwald }
935*1b2596b5SMatthias Ringwald }
936*1b2596b5SMatthias Ringwald
937*1b2596b5SMatthias Ringwald return 0;
938*1b2596b5SMatthias Ringwald }
939*1b2596b5SMatthias Ringwald #endif
940*1b2596b5SMatthias Ringwald
941*1b2596b5SMatthias Ringwald #if (SAM3XA || SAM3U || SAMV71 || SAMV70 || SAME70 || SAMS70)
942*1b2596b5SMatthias Ringwald /**
943*1b2596b5SMatthias Ringwald * \brief Switch programmable clock source selection to UPLL clock.
944*1b2596b5SMatthias Ringwald *
945*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
946*1b2596b5SMatthias Ringwald * \param ul_pres Programmable clock prescaler.
947*1b2596b5SMatthias Ringwald *
948*1b2596b5SMatthias Ringwald * \retval 0 Success.
949*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
950*1b2596b5SMatthias Ringwald */
pmc_switch_pck_to_upllck(uint32_t ul_id,uint32_t ul_pres)951*1b2596b5SMatthias Ringwald uint32_t pmc_switch_pck_to_upllck(uint32_t ul_id, uint32_t ul_pres)
952*1b2596b5SMatthias Ringwald {
953*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
954*1b2596b5SMatthias Ringwald
955*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] = PMC_PCK_CSS_UPLL_CLK | ul_pres;
956*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT;
957*1b2596b5SMatthias Ringwald !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id));
958*1b2596b5SMatthias Ringwald --ul_timeout) {
959*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
960*1b2596b5SMatthias Ringwald return 1;
961*1b2596b5SMatthias Ringwald }
962*1b2596b5SMatthias Ringwald }
963*1b2596b5SMatthias Ringwald
964*1b2596b5SMatthias Ringwald return 0;
965*1b2596b5SMatthias Ringwald }
966*1b2596b5SMatthias Ringwald #endif
967*1b2596b5SMatthias Ringwald
968*1b2596b5SMatthias Ringwald /**
969*1b2596b5SMatthias Ringwald * \brief Switch programmable clock source selection to mck.
970*1b2596b5SMatthias Ringwald *
971*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
972*1b2596b5SMatthias Ringwald * \param ul_pres Programmable clock prescaler.
973*1b2596b5SMatthias Ringwald *
974*1b2596b5SMatthias Ringwald * \retval 0 Success.
975*1b2596b5SMatthias Ringwald * \retval 1 Timeout error.
976*1b2596b5SMatthias Ringwald */
pmc_switch_pck_to_mck(uint32_t ul_id,uint32_t ul_pres)977*1b2596b5SMatthias Ringwald uint32_t pmc_switch_pck_to_mck(uint32_t ul_id, uint32_t ul_pres)
978*1b2596b5SMatthias Ringwald {
979*1b2596b5SMatthias Ringwald uint32_t ul_timeout;
980*1b2596b5SMatthias Ringwald
981*1b2596b5SMatthias Ringwald PMC->PMC_PCK[ul_id] = PMC_PCK_CSS_MCK | ul_pres;
982*1b2596b5SMatthias Ringwald for (ul_timeout = PMC_TIMEOUT;
983*1b2596b5SMatthias Ringwald !(PMC->PMC_SR & (PMC_SR_PCKRDY0 << ul_id)); --ul_timeout) {
984*1b2596b5SMatthias Ringwald if (ul_timeout == 0) {
985*1b2596b5SMatthias Ringwald return 1;
986*1b2596b5SMatthias Ringwald }
987*1b2596b5SMatthias Ringwald }
988*1b2596b5SMatthias Ringwald
989*1b2596b5SMatthias Ringwald return 0;
990*1b2596b5SMatthias Ringwald }
991*1b2596b5SMatthias Ringwald
992*1b2596b5SMatthias Ringwald /**
993*1b2596b5SMatthias Ringwald * \brief Enable the specified programmable clock.
994*1b2596b5SMatthias Ringwald *
995*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
996*1b2596b5SMatthias Ringwald */
pmc_enable_pck(uint32_t ul_id)997*1b2596b5SMatthias Ringwald void pmc_enable_pck(uint32_t ul_id)
998*1b2596b5SMatthias Ringwald {
999*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_PCK0 << ul_id;
1000*1b2596b5SMatthias Ringwald }
1001*1b2596b5SMatthias Ringwald
1002*1b2596b5SMatthias Ringwald /**
1003*1b2596b5SMatthias Ringwald * \brief Disable the specified programmable clock.
1004*1b2596b5SMatthias Ringwald *
1005*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
1006*1b2596b5SMatthias Ringwald */
pmc_disable_pck(uint32_t ul_id)1007*1b2596b5SMatthias Ringwald void pmc_disable_pck(uint32_t ul_id)
1008*1b2596b5SMatthias Ringwald {
1009*1b2596b5SMatthias Ringwald PMC->PMC_SCDR = PMC_SCER_PCK0 << ul_id;
1010*1b2596b5SMatthias Ringwald }
1011*1b2596b5SMatthias Ringwald
1012*1b2596b5SMatthias Ringwald /**
1013*1b2596b5SMatthias Ringwald * \brief Enable all programmable clocks.
1014*1b2596b5SMatthias Ringwald */
pmc_enable_all_pck(void)1015*1b2596b5SMatthias Ringwald void pmc_enable_all_pck(void)
1016*1b2596b5SMatthias Ringwald {
1017*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_PCK0 | PMC_SCER_PCK1 | PMC_SCER_PCK2;
1018*1b2596b5SMatthias Ringwald }
1019*1b2596b5SMatthias Ringwald
1020*1b2596b5SMatthias Ringwald /**
1021*1b2596b5SMatthias Ringwald * \brief Disable all programmable clocks.
1022*1b2596b5SMatthias Ringwald */
pmc_disable_all_pck(void)1023*1b2596b5SMatthias Ringwald void pmc_disable_all_pck(void)
1024*1b2596b5SMatthias Ringwald {
1025*1b2596b5SMatthias Ringwald PMC->PMC_SCDR = PMC_SCDR_PCK0 | PMC_SCDR_PCK1 | PMC_SCDR_PCK2;
1026*1b2596b5SMatthias Ringwald }
1027*1b2596b5SMatthias Ringwald
1028*1b2596b5SMatthias Ringwald /**
1029*1b2596b5SMatthias Ringwald * \brief Check if the specified programmable clock is enabled.
1030*1b2596b5SMatthias Ringwald *
1031*1b2596b5SMatthias Ringwald * \param ul_id Id of the programmable clock.
1032*1b2596b5SMatthias Ringwald *
1033*1b2596b5SMatthias Ringwald * \retval 0 Programmable clock is disabled or unknown.
1034*1b2596b5SMatthias Ringwald * \retval 1 Programmable clock is enabled.
1035*1b2596b5SMatthias Ringwald */
pmc_is_pck_enabled(uint32_t ul_id)1036*1b2596b5SMatthias Ringwald uint32_t pmc_is_pck_enabled(uint32_t ul_id)
1037*1b2596b5SMatthias Ringwald {
1038*1b2596b5SMatthias Ringwald if (ul_id > 2) {
1039*1b2596b5SMatthias Ringwald return 0;
1040*1b2596b5SMatthias Ringwald }
1041*1b2596b5SMatthias Ringwald
1042*1b2596b5SMatthias Ringwald return (PMC->PMC_SCSR & (PMC_SCSR_PCK0 << ul_id));
1043*1b2596b5SMatthias Ringwald }
1044*1b2596b5SMatthias Ringwald
1045*1b2596b5SMatthias Ringwald #if (SAM4C || SAM4CM || SAM4CP)
1046*1b2596b5SMatthias Ringwald /**
1047*1b2596b5SMatthias Ringwald * \brief Enable Coprocessor Clocks.
1048*1b2596b5SMatthias Ringwald */
pmc_enable_cpck(void)1049*1b2596b5SMatthias Ringwald void pmc_enable_cpck(void)
1050*1b2596b5SMatthias Ringwald {
1051*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_CPCK | PMC_SCER_CPKEY_PASSWD;
1052*1b2596b5SMatthias Ringwald }
1053*1b2596b5SMatthias Ringwald
1054*1b2596b5SMatthias Ringwald /**
1055*1b2596b5SMatthias Ringwald * \brief Disable Coprocessor Clocks.
1056*1b2596b5SMatthias Ringwald */
pmc_disable_cpck(void)1057*1b2596b5SMatthias Ringwald void pmc_disable_cpck(void)
1058*1b2596b5SMatthias Ringwald {
1059*1b2596b5SMatthias Ringwald PMC->PMC_SCDR = PMC_SCDR_CPCK | PMC_SCDR_CPKEY_PASSWD;
1060*1b2596b5SMatthias Ringwald }
1061*1b2596b5SMatthias Ringwald
1062*1b2596b5SMatthias Ringwald /**
1063*1b2596b5SMatthias Ringwald * \brief Check if the Coprocessor Clocks is enabled.
1064*1b2596b5SMatthias Ringwald *
1065*1b2596b5SMatthias Ringwald * \retval 0 Coprocessor Clocks is disabled.
1066*1b2596b5SMatthias Ringwald * \retval 1 Coprocessor Clocks is enabled.
1067*1b2596b5SMatthias Ringwald */
pmc_is_cpck_enabled(void)1068*1b2596b5SMatthias Ringwald bool pmc_is_cpck_enabled(void)
1069*1b2596b5SMatthias Ringwald {
1070*1b2596b5SMatthias Ringwald if(PMC->PMC_SCSR & PMC_SCSR_CPCK) {
1071*1b2596b5SMatthias Ringwald return 1;
1072*1b2596b5SMatthias Ringwald } else {
1073*1b2596b5SMatthias Ringwald return 0;
1074*1b2596b5SMatthias Ringwald }
1075*1b2596b5SMatthias Ringwald }
1076*1b2596b5SMatthias Ringwald
1077*1b2596b5SMatthias Ringwald /**
1078*1b2596b5SMatthias Ringwald * \brief Enable Coprocessor Bus Master Clocks.
1079*1b2596b5SMatthias Ringwald */
pmc_enable_cpbmck(void)1080*1b2596b5SMatthias Ringwald void pmc_enable_cpbmck(void)
1081*1b2596b5SMatthias Ringwald {
1082*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_CPCK | PMC_SCER_CPKEY_PASSWD;
1083*1b2596b5SMatthias Ringwald }
1084*1b2596b5SMatthias Ringwald
1085*1b2596b5SMatthias Ringwald /**
1086*1b2596b5SMatthias Ringwald * \brief Disable Coprocessor Bus Master Clocks.
1087*1b2596b5SMatthias Ringwald */
pmc_disable_cpbmck(void)1088*1b2596b5SMatthias Ringwald void pmc_disable_cpbmck(void)
1089*1b2596b5SMatthias Ringwald {
1090*1b2596b5SMatthias Ringwald PMC->PMC_SCDR = PMC_SCDR_CPCK | PMC_SCDR_CPKEY_PASSWD;
1091*1b2596b5SMatthias Ringwald }
1092*1b2596b5SMatthias Ringwald
1093*1b2596b5SMatthias Ringwald /**
1094*1b2596b5SMatthias Ringwald * \brief Check if the Coprocessor Bus Master Clocks is enabled.
1095*1b2596b5SMatthias Ringwald *
1096*1b2596b5SMatthias Ringwald * \retval 0 Coprocessor Bus Master Clocks is disabled.
1097*1b2596b5SMatthias Ringwald * \retval 1 Coprocessor Bus Master Clocks is enabled.
1098*1b2596b5SMatthias Ringwald */
pmc_is_cpbmck_enabled(void)1099*1b2596b5SMatthias Ringwald bool pmc_is_cpbmck_enabled(void)
1100*1b2596b5SMatthias Ringwald {
1101*1b2596b5SMatthias Ringwald if(PMC->PMC_SCSR & PMC_SCSR_CPBMCK) {
1102*1b2596b5SMatthias Ringwald return 1;
1103*1b2596b5SMatthias Ringwald } else {
1104*1b2596b5SMatthias Ringwald return 0;
1105*1b2596b5SMatthias Ringwald }
1106*1b2596b5SMatthias Ringwald }
1107*1b2596b5SMatthias Ringwald
1108*1b2596b5SMatthias Ringwald /**
1109*1b2596b5SMatthias Ringwald * \brief Set the prescaler for the Coprocessor Master Clock.
1110*1b2596b5SMatthias Ringwald *
1111*1b2596b5SMatthias Ringwald * \param ul_pres Prescaler value.
1112*1b2596b5SMatthias Ringwald */
pmc_cpck_set_prescaler(uint32_t ul_pres)1113*1b2596b5SMatthias Ringwald void pmc_cpck_set_prescaler(uint32_t ul_pres)
1114*1b2596b5SMatthias Ringwald {
1115*1b2596b5SMatthias Ringwald PMC->PMC_MCKR =
1116*1b2596b5SMatthias Ringwald (PMC->PMC_MCKR & (~PMC_MCKR_CPPRES_Msk)) | PMC_MCKR_CPPRES(ul_pres);
1117*1b2596b5SMatthias Ringwald }
1118*1b2596b5SMatthias Ringwald
1119*1b2596b5SMatthias Ringwald /**
1120*1b2596b5SMatthias Ringwald * \brief Set the source for the Coprocessor Master Clock.
1121*1b2596b5SMatthias Ringwald *
1122*1b2596b5SMatthias Ringwald * \param ul_source Source selection value.
1123*1b2596b5SMatthias Ringwald */
pmc_cpck_set_source(uint32_t ul_source)1124*1b2596b5SMatthias Ringwald void pmc_cpck_set_source(uint32_t ul_source)
1125*1b2596b5SMatthias Ringwald {
1126*1b2596b5SMatthias Ringwald PMC->PMC_MCKR =
1127*1b2596b5SMatthias Ringwald (PMC->PMC_MCKR & (~PMC_MCKR_CPCSS_Msk)) | ul_source;
1128*1b2596b5SMatthias Ringwald }
1129*1b2596b5SMatthias Ringwald #endif
1130*1b2596b5SMatthias Ringwald
1131*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
1132*1b2596b5SMatthias Ringwald /**
1133*1b2596b5SMatthias Ringwald * \brief Switch UDP (USB) clock source selection to PLLA clock.
1134*1b2596b5SMatthias Ringwald *
1135*1b2596b5SMatthias Ringwald * \param ul_usbdiv Clock divisor.
1136*1b2596b5SMatthias Ringwald */
pmc_switch_udpck_to_pllack(uint32_t ul_usbdiv)1137*1b2596b5SMatthias Ringwald void pmc_switch_udpck_to_pllack(uint32_t ul_usbdiv)
1138*1b2596b5SMatthias Ringwald {
1139*1b2596b5SMatthias Ringwald PMC->PMC_USB = PMC_USB_USBDIV(ul_usbdiv);
1140*1b2596b5SMatthias Ringwald }
1141*1b2596b5SMatthias Ringwald #endif
1142*1b2596b5SMatthias Ringwald
1143*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAMG55)
1144*1b2596b5SMatthias Ringwald /**
1145*1b2596b5SMatthias Ringwald * \brief Switch UDP (USB) clock source selection to PLLB clock.
1146*1b2596b5SMatthias Ringwald *
1147*1b2596b5SMatthias Ringwald * \param ul_usbdiv Clock divisor.
1148*1b2596b5SMatthias Ringwald */
pmc_switch_udpck_to_pllbck(uint32_t ul_usbdiv)1149*1b2596b5SMatthias Ringwald void pmc_switch_udpck_to_pllbck(uint32_t ul_usbdiv)
1150*1b2596b5SMatthias Ringwald {
1151*1b2596b5SMatthias Ringwald PMC->PMC_USB = PMC_USB_USBDIV(ul_usbdiv) | PMC_USB_USBS;
1152*1b2596b5SMatthias Ringwald }
1153*1b2596b5SMatthias Ringwald #endif
1154*1b2596b5SMatthias Ringwald
1155*1b2596b5SMatthias Ringwald #if (SAM3XA || SAMV71 || SAMV70 || SAME70 || SAMS70)
1156*1b2596b5SMatthias Ringwald /**
1157*1b2596b5SMatthias Ringwald * \brief Switch UDP (USB) clock source selection to UPLL clock.
1158*1b2596b5SMatthias Ringwald *
1159*1b2596b5SMatthias Ringwald * \param ul_usbdiv Clock divisor.
1160*1b2596b5SMatthias Ringwald */
pmc_switch_udpck_to_upllck(uint32_t ul_usbdiv)1161*1b2596b5SMatthias Ringwald void pmc_switch_udpck_to_upllck(uint32_t ul_usbdiv)
1162*1b2596b5SMatthias Ringwald {
1163*1b2596b5SMatthias Ringwald PMC->PMC_USB = PMC_USB_USBS | PMC_USB_USBDIV(ul_usbdiv);
1164*1b2596b5SMatthias Ringwald }
1165*1b2596b5SMatthias Ringwald #endif
1166*1b2596b5SMatthias Ringwald
1167*1b2596b5SMatthias Ringwald #if (SAM3S || SAM3XA || SAM4S || SAM4E || SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
1168*1b2596b5SMatthias Ringwald /**
1169*1b2596b5SMatthias Ringwald * \brief Enable UDP (USB) clock.
1170*1b2596b5SMatthias Ringwald */
pmc_enable_udpck(void)1171*1b2596b5SMatthias Ringwald void pmc_enable_udpck(void)
1172*1b2596b5SMatthias Ringwald {
1173*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM4E || SAMG55)
1174*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_UDP;
1175*1b2596b5SMatthias Ringwald #elif (SAMV71 || SAMV70 || SAME70 || SAMS70)
1176*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_USBCLK;
1177*1b2596b5SMatthias Ringwald #else
1178*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_UOTGCLK;
1179*1b2596b5SMatthias Ringwald # endif
1180*1b2596b5SMatthias Ringwald }
1181*1b2596b5SMatthias Ringwald
1182*1b2596b5SMatthias Ringwald /**
1183*1b2596b5SMatthias Ringwald * \brief Disable UDP (USB) clock.
1184*1b2596b5SMatthias Ringwald */
pmc_disable_udpck(void)1185*1b2596b5SMatthias Ringwald void pmc_disable_udpck(void)
1186*1b2596b5SMatthias Ringwald {
1187*1b2596b5SMatthias Ringwald #if (SAM3S || SAM4S || SAM4E || SAMG55)
1188*1b2596b5SMatthias Ringwald PMC->PMC_SCDR = PMC_SCDR_UDP;
1189*1b2596b5SMatthias Ringwald #elif (SAMV71 || SAMV70 || SAME70 || SAMS70)
1190*1b2596b5SMatthias Ringwald PMC->PMC_SCDR = PMC_SCDR_USBCLK;
1191*1b2596b5SMatthias Ringwald #else
1192*1b2596b5SMatthias Ringwald PMC->PMC_SCDR = PMC_SCDR_UOTGCLK;
1193*1b2596b5SMatthias Ringwald # endif
1194*1b2596b5SMatthias Ringwald }
1195*1b2596b5SMatthias Ringwald #endif
1196*1b2596b5SMatthias Ringwald
1197*1b2596b5SMatthias Ringwald #if SAMG55
1198*1b2596b5SMatthias Ringwald /**
1199*1b2596b5SMatthias Ringwald * \brief Switch UHP (USB) clock source selection to PLLA clock.
1200*1b2596b5SMatthias Ringwald *
1201*1b2596b5SMatthias Ringwald * \param ul_usbdiv Clock divisor.
1202*1b2596b5SMatthias Ringwald */
pmc_switch_uhpck_to_pllack(uint32_t ul_usbdiv)1203*1b2596b5SMatthias Ringwald void pmc_switch_uhpck_to_pllack(uint32_t ul_usbdiv)
1204*1b2596b5SMatthias Ringwald {
1205*1b2596b5SMatthias Ringwald PMC->PMC_USB = PMC_USB_USBDIV(ul_usbdiv);
1206*1b2596b5SMatthias Ringwald }
1207*1b2596b5SMatthias Ringwald
1208*1b2596b5SMatthias Ringwald /**
1209*1b2596b5SMatthias Ringwald * \brief Switch UHP (USB) clock source selection to PLLB clock.
1210*1b2596b5SMatthias Ringwald *
1211*1b2596b5SMatthias Ringwald * \param ul_usbdiv Clock divisor.
1212*1b2596b5SMatthias Ringwald */
pmc_switch_uhpck_to_pllbck(uint32_t ul_usbdiv)1213*1b2596b5SMatthias Ringwald void pmc_switch_uhpck_to_pllbck(uint32_t ul_usbdiv)
1214*1b2596b5SMatthias Ringwald {
1215*1b2596b5SMatthias Ringwald PMC->PMC_USB = PMC_USB_USBDIV(ul_usbdiv) | PMC_USB_USBS;
1216*1b2596b5SMatthias Ringwald }
1217*1b2596b5SMatthias Ringwald
1218*1b2596b5SMatthias Ringwald /**
1219*1b2596b5SMatthias Ringwald * \brief Enable UHP (USB) clock.
1220*1b2596b5SMatthias Ringwald */
pmc_enable_uhpck(void)1221*1b2596b5SMatthias Ringwald void pmc_enable_uhpck(void)
1222*1b2596b5SMatthias Ringwald {
1223*1b2596b5SMatthias Ringwald PMC->PMC_SCER = PMC_SCER_UHP;
1224*1b2596b5SMatthias Ringwald }
1225*1b2596b5SMatthias Ringwald #endif
1226*1b2596b5SMatthias Ringwald
1227*1b2596b5SMatthias Ringwald /**
1228*1b2596b5SMatthias Ringwald * \brief Enable PMC interrupts.
1229*1b2596b5SMatthias Ringwald *
1230*1b2596b5SMatthias Ringwald * \param ul_sources Interrupt sources bit map.
1231*1b2596b5SMatthias Ringwald */
pmc_enable_interrupt(uint32_t ul_sources)1232*1b2596b5SMatthias Ringwald void pmc_enable_interrupt(uint32_t ul_sources)
1233*1b2596b5SMatthias Ringwald {
1234*1b2596b5SMatthias Ringwald PMC->PMC_IER = ul_sources;
1235*1b2596b5SMatthias Ringwald }
1236*1b2596b5SMatthias Ringwald
1237*1b2596b5SMatthias Ringwald /**
1238*1b2596b5SMatthias Ringwald * \brief Disable PMC interrupts.
1239*1b2596b5SMatthias Ringwald *
1240*1b2596b5SMatthias Ringwald * \param ul_sources Interrupt sources bit map.
1241*1b2596b5SMatthias Ringwald */
pmc_disable_interrupt(uint32_t ul_sources)1242*1b2596b5SMatthias Ringwald void pmc_disable_interrupt(uint32_t ul_sources)
1243*1b2596b5SMatthias Ringwald {
1244*1b2596b5SMatthias Ringwald PMC->PMC_IDR = ul_sources;
1245*1b2596b5SMatthias Ringwald }
1246*1b2596b5SMatthias Ringwald
1247*1b2596b5SMatthias Ringwald /**
1248*1b2596b5SMatthias Ringwald * \brief Get PMC interrupt mask.
1249*1b2596b5SMatthias Ringwald *
1250*1b2596b5SMatthias Ringwald * \return The interrupt mask value.
1251*1b2596b5SMatthias Ringwald */
pmc_get_interrupt_mask(void)1252*1b2596b5SMatthias Ringwald uint32_t pmc_get_interrupt_mask(void)
1253*1b2596b5SMatthias Ringwald {
1254*1b2596b5SMatthias Ringwald return PMC->PMC_IMR;
1255*1b2596b5SMatthias Ringwald }
1256*1b2596b5SMatthias Ringwald
1257*1b2596b5SMatthias Ringwald /**
1258*1b2596b5SMatthias Ringwald * \brief Get current status.
1259*1b2596b5SMatthias Ringwald *
1260*1b2596b5SMatthias Ringwald * \return The current PMC status.
1261*1b2596b5SMatthias Ringwald */
pmc_get_status(void)1262*1b2596b5SMatthias Ringwald uint32_t pmc_get_status(void)
1263*1b2596b5SMatthias Ringwald {
1264*1b2596b5SMatthias Ringwald return PMC->PMC_SR;
1265*1b2596b5SMatthias Ringwald }
1266*1b2596b5SMatthias Ringwald
1267*1b2596b5SMatthias Ringwald /**
1268*1b2596b5SMatthias Ringwald * \brief Set the wake-up inputs for fast startup mode registers
1269*1b2596b5SMatthias Ringwald * (event generation).
1270*1b2596b5SMatthias Ringwald *
1271*1b2596b5SMatthias Ringwald * \param ul_inputs Wake up inputs to enable.
1272*1b2596b5SMatthias Ringwald */
pmc_set_fast_startup_input(uint32_t ul_inputs)1273*1b2596b5SMatthias Ringwald void pmc_set_fast_startup_input(uint32_t ul_inputs)
1274*1b2596b5SMatthias Ringwald {
1275*1b2596b5SMatthias Ringwald ul_inputs &= PMC_FAST_STARTUP_Msk;
1276*1b2596b5SMatthias Ringwald PMC->PMC_FSMR |= ul_inputs;
1277*1b2596b5SMatthias Ringwald }
1278*1b2596b5SMatthias Ringwald
1279*1b2596b5SMatthias Ringwald /**
1280*1b2596b5SMatthias Ringwald * \brief Clear the wake-up inputs for fast startup mode registers
1281*1b2596b5SMatthias Ringwald * (remove event generation).
1282*1b2596b5SMatthias Ringwald *
1283*1b2596b5SMatthias Ringwald * \param ul_inputs Wake up inputs to disable.
1284*1b2596b5SMatthias Ringwald */
pmc_clr_fast_startup_input(uint32_t ul_inputs)1285*1b2596b5SMatthias Ringwald void pmc_clr_fast_startup_input(uint32_t ul_inputs)
1286*1b2596b5SMatthias Ringwald {
1287*1b2596b5SMatthias Ringwald ul_inputs &= PMC_FAST_STARTUP_Msk;
1288*1b2596b5SMatthias Ringwald PMC->PMC_FSMR &= ~ul_inputs;
1289*1b2596b5SMatthias Ringwald }
1290*1b2596b5SMatthias Ringwald
1291*1b2596b5SMatthias Ringwald #if (SAM4C || SAM4CM || SAM4CP)
1292*1b2596b5SMatthias Ringwald /**
1293*1b2596b5SMatthias Ringwald * \brief Set the wake-up inputs of coprocessor for fast startup mode registers
1294*1b2596b5SMatthias Ringwald * (event generation).
1295*1b2596b5SMatthias Ringwald *
1296*1b2596b5SMatthias Ringwald * \param ul_inputs Wake up inputs to enable.
1297*1b2596b5SMatthias Ringwald */
pmc_cp_set_fast_startup_input(uint32_t ul_inputs)1298*1b2596b5SMatthias Ringwald void pmc_cp_set_fast_startup_input(uint32_t ul_inputs)
1299*1b2596b5SMatthias Ringwald {
1300*1b2596b5SMatthias Ringwald ul_inputs &= PMC_FAST_STARTUP_Msk;
1301*1b2596b5SMatthias Ringwald PMC->PMC_CPFSMR |= ul_inputs;
1302*1b2596b5SMatthias Ringwald }
1303*1b2596b5SMatthias Ringwald
1304*1b2596b5SMatthias Ringwald /**
1305*1b2596b5SMatthias Ringwald * \brief Clear the wake-up inputs of coprocessor for fast startup mode registers
1306*1b2596b5SMatthias Ringwald * (remove event generation).
1307*1b2596b5SMatthias Ringwald *
1308*1b2596b5SMatthias Ringwald * \param ul_inputs Wake up inputs to disable.
1309*1b2596b5SMatthias Ringwald */
pmc_cp_clr_fast_startup_input(uint32_t ul_inputs)1310*1b2596b5SMatthias Ringwald void pmc_cp_clr_fast_startup_input(uint32_t ul_inputs)
1311*1b2596b5SMatthias Ringwald {
1312*1b2596b5SMatthias Ringwald ul_inputs &= PMC_FAST_STARTUP_Msk;
1313*1b2596b5SMatthias Ringwald PMC->PMC_CPFSMR &= ~ul_inputs;
1314*1b2596b5SMatthias Ringwald }
1315*1b2596b5SMatthias Ringwald #endif
1316*1b2596b5SMatthias Ringwald
1317*1b2596b5SMatthias Ringwald #if (!(SAMG51 || SAMG53 || SAMG54))
1318*1b2596b5SMatthias Ringwald /**
1319*1b2596b5SMatthias Ringwald * \brief Enable Sleep Mode.
1320*1b2596b5SMatthias Ringwald * Enter condition: (WFE or WFI) + (SLEEPDEEP bit = 0) + (LPM bit = 0)
1321*1b2596b5SMatthias Ringwald *
1322*1b2596b5SMatthias Ringwald * \param uc_type 0 for wait for interrupt, 1 for wait for event.
1323*1b2596b5SMatthias Ringwald * \note For SAM4S, SAM4C, SAM4CM, SAM4CP, SAMV71 and SAM4E series,
1324*1b2596b5SMatthias Ringwald * since only WFI is effective, uc_type = 1 will be treated as uc_type = 0.
1325*1b2596b5SMatthias Ringwald */
pmc_enable_sleepmode(uint8_t uc_type)1326*1b2596b5SMatthias Ringwald void pmc_enable_sleepmode(uint8_t uc_type)
1327*1b2596b5SMatthias Ringwald {
1328*1b2596b5SMatthias Ringwald #if !(SAM4S || SAM4E || SAM4N || SAM4C || SAM4CM || SAM4CP || SAMV71 || SAMV70 || SAME70 || SAMS70)
1329*1b2596b5SMatthias Ringwald PMC->PMC_FSMR &= (uint32_t) ~ PMC_FSMR_LPM; // Enter Sleep mode
1330*1b2596b5SMatthias Ringwald #endif
1331*1b2596b5SMatthias Ringwald SCB->SCR &= (uint32_t) ~ SCB_SCR_SLEEPDEEP_Msk; // Deep sleep
1332*1b2596b5SMatthias Ringwald
1333*1b2596b5SMatthias Ringwald #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CM || SAM4CP || SAMV71 || SAMV70 || SAME70 || SAMS70)
1334*1b2596b5SMatthias Ringwald UNUSED(uc_type);
1335*1b2596b5SMatthias Ringwald __WFI();
1336*1b2596b5SMatthias Ringwald #else
1337*1b2596b5SMatthias Ringwald if (uc_type == 0) {
1338*1b2596b5SMatthias Ringwald __WFI();
1339*1b2596b5SMatthias Ringwald } else {
1340*1b2596b5SMatthias Ringwald __WFE();
1341*1b2596b5SMatthias Ringwald }
1342*1b2596b5SMatthias Ringwald #endif
1343*1b2596b5SMatthias Ringwald }
1344*1b2596b5SMatthias Ringwald #endif
1345*1b2596b5SMatthias Ringwald
1346*1b2596b5SMatthias Ringwald #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CM || SAMG || SAM4CP || SAMV71 || SAMV70 || SAME70 || SAMS70)
1347*1b2596b5SMatthias Ringwald static uint32_t ul_flash_in_wait_mode = PMC_WAIT_MODE_FLASH_DEEP_POWERDOWN;
1348*1b2596b5SMatthias Ringwald /**
1349*1b2596b5SMatthias Ringwald * \brief Set the embedded flash state in wait mode
1350*1b2596b5SMatthias Ringwald *
1351*1b2596b5SMatthias Ringwald * \param ul_flash_state PMC_WAIT_MODE_FLASH_STANDBY flash in standby mode,
1352*1b2596b5SMatthias Ringwald * PMC_WAIT_MODE_FLASH_DEEP_POWERDOWN flash in deep power down mode.
1353*1b2596b5SMatthias Ringwald */
pmc_set_flash_in_wait_mode(uint32_t ul_flash_state)1354*1b2596b5SMatthias Ringwald void pmc_set_flash_in_wait_mode(uint32_t ul_flash_state)
1355*1b2596b5SMatthias Ringwald {
1356*1b2596b5SMatthias Ringwald ul_flash_in_wait_mode = ul_flash_state;
1357*1b2596b5SMatthias Ringwald }
1358*1b2596b5SMatthias Ringwald
1359*1b2596b5SMatthias Ringwald /**
1360*1b2596b5SMatthias Ringwald * \brief Enable Wait Mode. Enter condition: (WAITMODE bit = 1) + FLPM
1361*1b2596b5SMatthias Ringwald *
1362*1b2596b5SMatthias Ringwald * \note In this function, FLPM will retain, WAITMODE bit will be set,
1363*1b2596b5SMatthias Ringwald * Generally, this function will be called by pmc_sleep() in order to
1364*1b2596b5SMatthias Ringwald * complete all sequence entering wait mode.
1365*1b2596b5SMatthias Ringwald * See \ref pmc_sleep() for entering different sleep modes.
1366*1b2596b5SMatthias Ringwald */
pmc_enable_waitmode(void)1367*1b2596b5SMatthias Ringwald void pmc_enable_waitmode(void)
1368*1b2596b5SMatthias Ringwald {
1369*1b2596b5SMatthias Ringwald uint32_t i;
1370*1b2596b5SMatthias Ringwald
1371*1b2596b5SMatthias Ringwald /* Flash in wait mode */
1372*1b2596b5SMatthias Ringwald i = PMC->PMC_FSMR;
1373*1b2596b5SMatthias Ringwald i &= ~PMC_FSMR_FLPM_Msk;
1374*1b2596b5SMatthias Ringwald i |= ul_flash_in_wait_mode;
1375*1b2596b5SMatthias Ringwald PMC->PMC_FSMR = i;
1376*1b2596b5SMatthias Ringwald
1377*1b2596b5SMatthias Ringwald /* Set the WAITMODE bit = 1 */
1378*1b2596b5SMatthias Ringwald PMC->CKGR_MOR |= CKGR_MOR_KEY_PASSWD | CKGR_MOR_WAITMODE;
1379*1b2596b5SMatthias Ringwald
1380*1b2596b5SMatthias Ringwald /* Waiting for Master Clock Ready MCKRDY = 1 */
1381*1b2596b5SMatthias Ringwald while (!(PMC->PMC_SR & PMC_SR_MCKRDY));
1382*1b2596b5SMatthias Ringwald
1383*1b2596b5SMatthias Ringwald /* Waiting for MOSCRCEN bit cleared is strongly recommended
1384*1b2596b5SMatthias Ringwald * to ensure that the core will not execute undesired instructions
1385*1b2596b5SMatthias Ringwald */
1386*1b2596b5SMatthias Ringwald for (i = 0; i < 500; i++) {
1387*1b2596b5SMatthias Ringwald __NOP();
1388*1b2596b5SMatthias Ringwald }
1389*1b2596b5SMatthias Ringwald while (!(PMC->CKGR_MOR & CKGR_MOR_MOSCRCEN));
1390*1b2596b5SMatthias Ringwald
1391*1b2596b5SMatthias Ringwald #if (!SAMG)
1392*1b2596b5SMatthias Ringwald /* Restore Flash in idle mode */
1393*1b2596b5SMatthias Ringwald i = PMC->PMC_FSMR;
1394*1b2596b5SMatthias Ringwald i &= ~PMC_FSMR_FLPM_Msk;
1395*1b2596b5SMatthias Ringwald i |= PMC_WAIT_MODE_FLASH_IDLE;
1396*1b2596b5SMatthias Ringwald PMC->PMC_FSMR = i;
1397*1b2596b5SMatthias Ringwald #endif
1398*1b2596b5SMatthias Ringwald }
1399*1b2596b5SMatthias Ringwald #else
1400*1b2596b5SMatthias Ringwald /**
1401*1b2596b5SMatthias Ringwald * \brief Enable Wait Mode. Enter condition: WFE + (SLEEPDEEP bit = 0) +
1402*1b2596b5SMatthias Ringwald * (LPM bit = 1)
1403*1b2596b5SMatthias Ringwald */
pmc_enable_waitmode(void)1404*1b2596b5SMatthias Ringwald void pmc_enable_waitmode(void)
1405*1b2596b5SMatthias Ringwald {
1406*1b2596b5SMatthias Ringwald uint32_t i;
1407*1b2596b5SMatthias Ringwald
1408*1b2596b5SMatthias Ringwald PMC->PMC_FSMR |= PMC_FSMR_LPM; /* Enter Wait mode */
1409*1b2596b5SMatthias Ringwald SCB->SCR &= (uint32_t) ~ SCB_SCR_SLEEPDEEP_Msk; /* Deep sleep */
1410*1b2596b5SMatthias Ringwald
1411*1b2596b5SMatthias Ringwald __WFE();
1412*1b2596b5SMatthias Ringwald
1413*1b2596b5SMatthias Ringwald /* Waiting for MOSCRCEN bit cleared is strongly recommended
1414*1b2596b5SMatthias Ringwald * to ensure that the core will not execute undesired instructions
1415*1b2596b5SMatthias Ringwald */
1416*1b2596b5SMatthias Ringwald for (i = 0; i < 500; i++) {
1417*1b2596b5SMatthias Ringwald __NOP();
1418*1b2596b5SMatthias Ringwald }
1419*1b2596b5SMatthias Ringwald while (!(PMC->CKGR_MOR & CKGR_MOR_MOSCRCEN));
1420*1b2596b5SMatthias Ringwald
1421*1b2596b5SMatthias Ringwald }
1422*1b2596b5SMatthias Ringwald #endif
1423*1b2596b5SMatthias Ringwald
1424*1b2596b5SMatthias Ringwald #if (!(SAMG51 || SAMG53 || SAMG54))
1425*1b2596b5SMatthias Ringwald /**
1426*1b2596b5SMatthias Ringwald * \brief Enable Backup Mode. Enter condition: WFE/(VROFF bit = 1) +
1427*1b2596b5SMatthias Ringwald * (SLEEPDEEP bit = 1)
1428*1b2596b5SMatthias Ringwald */
pmc_enable_backupmode(void)1429*1b2596b5SMatthias Ringwald void pmc_enable_backupmode(void)
1430*1b2596b5SMatthias Ringwald {
1431*1b2596b5SMatthias Ringwald #if (SAM4C || SAM4CM || SAM4CP)
1432*1b2596b5SMatthias Ringwald uint32_t tmp = SUPC->SUPC_MR & ~(SUPC_MR_BUPPOREN | SUPC_MR_KEY_Msk);
1433*1b2596b5SMatthias Ringwald SUPC->SUPC_MR = tmp | SUPC_MR_KEY_PASSWD;
1434*1b2596b5SMatthias Ringwald while (SUPC->SUPC_SR & SUPC_SR_BUPPORS);
1435*1b2596b5SMatthias Ringwald #endif
1436*1b2596b5SMatthias Ringwald SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
1437*1b2596b5SMatthias Ringwald #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CM || SAM4CP || SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
1438*1b2596b5SMatthias Ringwald SUPC->SUPC_CR = SUPC_CR_KEY_PASSWD | SUPC_CR_VROFF_STOP_VREG;
1439*1b2596b5SMatthias Ringwald __WFE();
1440*1b2596b5SMatthias Ringwald __WFI();
1441*1b2596b5SMatthias Ringwald #else
1442*1b2596b5SMatthias Ringwald __WFE();
1443*1b2596b5SMatthias Ringwald #endif
1444*1b2596b5SMatthias Ringwald }
1445*1b2596b5SMatthias Ringwald #endif
1446*1b2596b5SMatthias Ringwald
1447*1b2596b5SMatthias Ringwald /**
1448*1b2596b5SMatthias Ringwald * \brief Enable Clock Failure Detector.
1449*1b2596b5SMatthias Ringwald */
pmc_enable_clock_failure_detector(void)1450*1b2596b5SMatthias Ringwald void pmc_enable_clock_failure_detector(void)
1451*1b2596b5SMatthias Ringwald {
1452*1b2596b5SMatthias Ringwald uint32_t ul_reg = PMC->CKGR_MOR;
1453*1b2596b5SMatthias Ringwald
1454*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = CKGR_MOR_KEY_PASSWD | CKGR_MOR_CFDEN | ul_reg;
1455*1b2596b5SMatthias Ringwald }
1456*1b2596b5SMatthias Ringwald
1457*1b2596b5SMatthias Ringwald /**
1458*1b2596b5SMatthias Ringwald * \brief Disable Clock Failure Detector.
1459*1b2596b5SMatthias Ringwald */
pmc_disable_clock_failure_detector(void)1460*1b2596b5SMatthias Ringwald void pmc_disable_clock_failure_detector(void)
1461*1b2596b5SMatthias Ringwald {
1462*1b2596b5SMatthias Ringwald uint32_t ul_reg = PMC->CKGR_MOR & (~CKGR_MOR_CFDEN);
1463*1b2596b5SMatthias Ringwald
1464*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = CKGR_MOR_KEY_PASSWD | ul_reg;
1465*1b2596b5SMatthias Ringwald }
1466*1b2596b5SMatthias Ringwald
1467*1b2596b5SMatthias Ringwald #if (SAM4N || SAM4C || SAM4CM || SAM4CP || SAMV71 || SAMV70 || SAME70 || SAMS70)
1468*1b2596b5SMatthias Ringwald /**
1469*1b2596b5SMatthias Ringwald * \brief Enable Slow Crystal Oscillator Frequency Monitoring.
1470*1b2596b5SMatthias Ringwald */
pmc_enable_sclk_osc_freq_monitor(void)1471*1b2596b5SMatthias Ringwald void pmc_enable_sclk_osc_freq_monitor(void)
1472*1b2596b5SMatthias Ringwald {
1473*1b2596b5SMatthias Ringwald uint32_t ul_reg = PMC->CKGR_MOR;
1474*1b2596b5SMatthias Ringwald
1475*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = CKGR_MOR_KEY_PASSWD | CKGR_MOR_XT32KFME | ul_reg;
1476*1b2596b5SMatthias Ringwald }
1477*1b2596b5SMatthias Ringwald
1478*1b2596b5SMatthias Ringwald /**
1479*1b2596b5SMatthias Ringwald * \brief Disable Slow Crystal Oscillator Frequency Monitoring.
1480*1b2596b5SMatthias Ringwald */
pmc_disable_sclk_osc_freq_monitor(void)1481*1b2596b5SMatthias Ringwald void pmc_disable_sclk_osc_freq_monitor(void)
1482*1b2596b5SMatthias Ringwald {
1483*1b2596b5SMatthias Ringwald uint32_t ul_reg = PMC->CKGR_MOR & (~CKGR_MOR_XT32KFME);
1484*1b2596b5SMatthias Ringwald
1485*1b2596b5SMatthias Ringwald PMC->CKGR_MOR = CKGR_MOR_KEY_PASSWD | ul_reg;
1486*1b2596b5SMatthias Ringwald }
1487*1b2596b5SMatthias Ringwald #endif
1488*1b2596b5SMatthias Ringwald
1489*1b2596b5SMatthias Ringwald /**
1490*1b2596b5SMatthias Ringwald * \brief Enable or disable write protect of PMC registers.
1491*1b2596b5SMatthias Ringwald *
1492*1b2596b5SMatthias Ringwald * \param ul_enable 1 to enable, 0 to disable.
1493*1b2596b5SMatthias Ringwald */
pmc_set_writeprotect(uint32_t ul_enable)1494*1b2596b5SMatthias Ringwald void pmc_set_writeprotect(uint32_t ul_enable)
1495*1b2596b5SMatthias Ringwald {
1496*1b2596b5SMatthias Ringwald if (ul_enable) {
1497*1b2596b5SMatthias Ringwald PMC->PMC_WPMR = PMC_WPMR_WPKEY_PASSWD | PMC_WPMR_WPEN;
1498*1b2596b5SMatthias Ringwald } else {
1499*1b2596b5SMatthias Ringwald PMC->PMC_WPMR = PMC_WPMR_WPKEY_PASSWD;
1500*1b2596b5SMatthias Ringwald }
1501*1b2596b5SMatthias Ringwald }
1502*1b2596b5SMatthias Ringwald
1503*1b2596b5SMatthias Ringwald /**
1504*1b2596b5SMatthias Ringwald * \brief Return write protect status.
1505*1b2596b5SMatthias Ringwald *
1506*1b2596b5SMatthias Ringwald * \return Return write protect status.
1507*1b2596b5SMatthias Ringwald */
pmc_get_writeprotect_status(void)1508*1b2596b5SMatthias Ringwald uint32_t pmc_get_writeprotect_status(void)
1509*1b2596b5SMatthias Ringwald {
1510*1b2596b5SMatthias Ringwald return PMC->PMC_WPSR;
1511*1b2596b5SMatthias Ringwald }
1512*1b2596b5SMatthias Ringwald
1513*1b2596b5SMatthias Ringwald #if (SAMG53 || SAMG54 || SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
1514*1b2596b5SMatthias Ringwald /**
1515*1b2596b5SMatthias Ringwald * \brief Enable the specified peripheral clock.
1516*1b2596b5SMatthias Ringwald *
1517*1b2596b5SMatthias Ringwald * \note The ID must NOT be shifted (i.e., 1 << ID_xxx).
1518*1b2596b5SMatthias Ringwald *
1519*1b2596b5SMatthias Ringwald * \param ul_id Peripheral ID (ID_xxx).
1520*1b2596b5SMatthias Ringwald *
1521*1b2596b5SMatthias Ringwald * \retval 0 Success.
1522*1b2596b5SMatthias Ringwald * \retval 1 Fail.
1523*1b2596b5SMatthias Ringwald */
pmc_enable_sleepwalking(uint32_t ul_id)1524*1b2596b5SMatthias Ringwald uint32_t pmc_enable_sleepwalking(uint32_t ul_id)
1525*1b2596b5SMatthias Ringwald {
1526*1b2596b5SMatthias Ringwald uint32_t temp;
1527*1b2596b5SMatthias Ringwald #if (SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
1528*1b2596b5SMatthias Ringwald if ((7 <= ul_id) && (ul_id<= 29)) {
1529*1b2596b5SMatthias Ringwald #else
1530*1b2596b5SMatthias Ringwald if ((8 <= ul_id) && (ul_id<= 29)) {
1531*1b2596b5SMatthias Ringwald #endif
1532*1b2596b5SMatthias Ringwald temp = pmc_get_active_status0();
1533*1b2596b5SMatthias Ringwald if (temp & (1 << ul_id)) {
1534*1b2596b5SMatthias Ringwald return 1;
1535*1b2596b5SMatthias Ringwald }
1536*1b2596b5SMatthias Ringwald PMC->PMC_SLPWK_ER0 = 1 << ul_id;
1537*1b2596b5SMatthias Ringwald temp = pmc_get_active_status0();
1538*1b2596b5SMatthias Ringwald if (temp & (1 << ul_id)) {
1539*1b2596b5SMatthias Ringwald pmc_disable_sleepwalking(ul_id);
1540*1b2596b5SMatthias Ringwald return 1;
1541*1b2596b5SMatthias Ringwald }
1542*1b2596b5SMatthias Ringwald return 0;
1543*1b2596b5SMatthias Ringwald }
1544*1b2596b5SMatthias Ringwald #if (SAMV71 || SAMV70 || SAME70 || SAMS70)
1545*1b2596b5SMatthias Ringwald else if ((32 <= ul_id) && (ul_id<= 60)) {
1546*1b2596b5SMatthias Ringwald ul_id -= 32;
1547*1b2596b5SMatthias Ringwald temp = pmc_get_active_status1();
1548*1b2596b5SMatthias Ringwald if (temp & (1 << ul_id)) {
1549*1b2596b5SMatthias Ringwald return 1;
1550*1b2596b5SMatthias Ringwald }
1551*1b2596b5SMatthias Ringwald PMC->PMC_SLPWK_ER1 = 1 << ul_id;
1552*1b2596b5SMatthias Ringwald temp = pmc_get_active_status1();
1553*1b2596b5SMatthias Ringwald if (temp & (1 << ul_id)) {
1554*1b2596b5SMatthias Ringwald pmc_disable_sleepwalking(ul_id);
1555*1b2596b5SMatthias Ringwald return 1;
1556*1b2596b5SMatthias Ringwald }
1557*1b2596b5SMatthias Ringwald return 0;
1558*1b2596b5SMatthias Ringwald }
1559*1b2596b5SMatthias Ringwald #endif
1560*1b2596b5SMatthias Ringwald else {
1561*1b2596b5SMatthias Ringwald return 1;
1562*1b2596b5SMatthias Ringwald }
1563*1b2596b5SMatthias Ringwald }
1564*1b2596b5SMatthias Ringwald
1565*1b2596b5SMatthias Ringwald /**
1566*1b2596b5SMatthias Ringwald * \brief Disable the sleepwalking of specified peripheral.
1567*1b2596b5SMatthias Ringwald *
1568*1b2596b5SMatthias Ringwald * \note The ID must NOT be shifted (i.e., 1 << ID_xxx).
1569*1b2596b5SMatthias Ringwald *
1570*1b2596b5SMatthias Ringwald * \param ul_id Peripheral ID (ID_xxx).
1571*1b2596b5SMatthias Ringwald *
1572*1b2596b5SMatthias Ringwald * \retval 0 Success.
1573*1b2596b5SMatthias Ringwald * \retval 1 Invalid parameter.
1574*1b2596b5SMatthias Ringwald */
1575*1b2596b5SMatthias Ringwald uint32_t pmc_disable_sleepwalking(uint32_t ul_id)
1576*1b2596b5SMatthias Ringwald {
1577*1b2596b5SMatthias Ringwald #if (SAMG55 || SAMV71 || SAMV70 || SAME70 || SAMS70)
1578*1b2596b5SMatthias Ringwald if ((7 <= ul_id) && (ul_id<= 29)) {
1579*1b2596b5SMatthias Ringwald #else
1580*1b2596b5SMatthias Ringwald if ((8 <= ul_id) && (ul_id<= 29)) {
1581*1b2596b5SMatthias Ringwald #endif
1582*1b2596b5SMatthias Ringwald PMC->PMC_SLPWK_DR0 = 1 << ul_id;
1583*1b2596b5SMatthias Ringwald return 0;
1584*1b2596b5SMatthias Ringwald }
1585*1b2596b5SMatthias Ringwald #if (SAMV71 || SAMV70 || SAME70 || SAMS70)
1586*1b2596b5SMatthias Ringwald else if ((32 <= ul_id) && (ul_id<= 60)) {
1587*1b2596b5SMatthias Ringwald ul_id -= 32;
1588*1b2596b5SMatthias Ringwald PMC->PMC_SLPWK_DR1 = 1 << ul_id;
1589*1b2596b5SMatthias Ringwald return 0;
1590*1b2596b5SMatthias Ringwald }
1591*1b2596b5SMatthias Ringwald #endif
1592*1b2596b5SMatthias Ringwald else {
1593*1b2596b5SMatthias Ringwald return 1;
1594*1b2596b5SMatthias Ringwald }
1595*1b2596b5SMatthias Ringwald }
1596*1b2596b5SMatthias Ringwald
1597*1b2596b5SMatthias Ringwald /**
1598*1b2596b5SMatthias Ringwald * \brief Return peripheral sleepwalking enable status.
1599*1b2596b5SMatthias Ringwald *
1600*1b2596b5SMatthias Ringwald * \return the status register value.
1601*1b2596b5SMatthias Ringwald */
1602*1b2596b5SMatthias Ringwald uint32_t pmc_get_sleepwalking_status0(void)
1603*1b2596b5SMatthias Ringwald {
1604*1b2596b5SMatthias Ringwald return PMC->PMC_SLPWK_SR0;
1605*1b2596b5SMatthias Ringwald }
1606*1b2596b5SMatthias Ringwald
1607*1b2596b5SMatthias Ringwald /**
1608*1b2596b5SMatthias Ringwald * \brief Return peripheral active status.
1609*1b2596b5SMatthias Ringwald *
1610*1b2596b5SMatthias Ringwald * \return the status register value.
1611*1b2596b5SMatthias Ringwald */
1612*1b2596b5SMatthias Ringwald uint32_t pmc_get_active_status0(void)
1613*1b2596b5SMatthias Ringwald {
1614*1b2596b5SMatthias Ringwald return PMC->PMC_SLPWK_ASR0;
1615*1b2596b5SMatthias Ringwald }
1616*1b2596b5SMatthias Ringwald
1617*1b2596b5SMatthias Ringwald #endif
1618*1b2596b5SMatthias Ringwald
1619*1b2596b5SMatthias Ringwald #if (SAMV71 || SAMV70 || SAME70 || SAMS70)
1620*1b2596b5SMatthias Ringwald /**
1621*1b2596b5SMatthias Ringwald * \brief Return peripheral sleepwalking enable status.
1622*1b2596b5SMatthias Ringwald *
1623*1b2596b5SMatthias Ringwald * \return the status register value.
1624*1b2596b5SMatthias Ringwald */
1625*1b2596b5SMatthias Ringwald uint32_t pmc_get_sleepwalking_status1(void)
1626*1b2596b5SMatthias Ringwald {
1627*1b2596b5SMatthias Ringwald return PMC->PMC_SLPWK_SR1;
1628*1b2596b5SMatthias Ringwald }
1629*1b2596b5SMatthias Ringwald
1630*1b2596b5SMatthias Ringwald /**
1631*1b2596b5SMatthias Ringwald * \brief Return peripheral active status.
1632*1b2596b5SMatthias Ringwald *
1633*1b2596b5SMatthias Ringwald * \return the status register value.
1634*1b2596b5SMatthias Ringwald */
1635*1b2596b5SMatthias Ringwald uint32_t pmc_get_active_status1(void)
1636*1b2596b5SMatthias Ringwald {
1637*1b2596b5SMatthias Ringwald return PMC->PMC_SLPWK_ASR1;
1638*1b2596b5SMatthias Ringwald }
1639*1b2596b5SMatthias Ringwald #endif
1640*1b2596b5SMatthias Ringwald
1641*1b2596b5SMatthias Ringwald /// @cond 0
1642*1b2596b5SMatthias Ringwald /**INDENT-OFF**/
1643*1b2596b5SMatthias Ringwald #ifdef __cplusplus
1644*1b2596b5SMatthias Ringwald }
1645*1b2596b5SMatthias Ringwald #endif
1646*1b2596b5SMatthias Ringwald /**INDENT-ON**/
1647*1b2596b5SMatthias Ringwald /// @endcond
1648