41 #if (SAM3S || SAM3N || SAM3XA || SAM3U || SAM4S || SAM4E || SAM4N || SAM4C || \ 42 SAM4CM || SAMG || SAM4CP || SAMV71 || SAMV70 || SAMS70 || SAME70) 47 #if !defined(BOARD_OSC_STARTUP_US) 48 # warning The board main clock xtal statup time has not been defined. Using default settings. 49 # define BOARD_OSC_STARTUP_US (15625UL) 59 __always_inline
static void pmc_save_clock_settings(
60 uint32_t *p_osc_setting,
61 uint32_t *p_pll0_setting,
62 uint32_t *p_pll1_setting,
63 uint32_t *p_mck_setting,
64 uint32_t *p_fmr_setting,
66 uint32_t *p_fmr_setting1,
68 const bool disable_xtal)
70 uint32_t mor =
PMC->CKGR_MOR;
71 uint32_t mckr =
PMC->PMC_MCKR;
72 uint32_t fmr = EFC0->EEFC_FMR;
74 uint32_t fmr1 = EFC1->EEFC_FMR;
81 *p_pll0_setting =
PMC->CKGR_PLLAR;
84 #if (SAM3S || SAM4S || SAM4C || SAM4CM || SAM4CP) 85 *p_pll1_setting =
PMC->CKGR_PLLBR;
86 #elif (SAM3U || SAM3XA) 87 *p_pll1_setting =
PMC->CKGR_UCKR;
93 *p_mck_setting = mckr;
100 *p_fmr_setting1 = fmr1;
110 PMC->PMC_MCKR = mckr;
115 mckr = (mckr & (~PMC_MCKR_PRES_Msk));
116 PMC->PMC_MCKR = mckr;
121 #if (SAM3S || SAM4S || SAM4C || SAM4CM || SAM4CP) 122 pmc_disable_pllbck();
123 #elif (SAM3U || SAM3XA) 124 pmc_disable_upll_clock();
166 __always_inline
static void pmc_restore_clock_setting(
167 const uint32_t osc_setting,
168 const uint32_t pll0_setting,
169 const uint32_t pll1_setting,
170 const uint32_t mck_setting,
171 const uint32_t fmr_setting
173 ,
const uint32_t fmr_setting1
191 if (!(
PMC->CKGR_MOR & CKGR_MOR_MOSCXTEN)) {
192 PMC->CKGR_MOR = (
PMC->CKGR_MOR & ~CKGR_MOR_MOSCXTBY) |
209 #if (SAM4C || SAM4CM || SAMG || SAM4CP) 210 PMC->CKGR_PLLAR = pll0_setting;
216 #if (SAM3S || SAM4S || SAM4C || SAM4CM || SAM4CP) 217 if (pll1_setting & CKGR_PLLBR_MULB_Msk) {
218 PMC->CKGR_PLLBR = pll1_setting;
219 pll_sr |= PMC_SR_LOCKB;
221 #elif (SAM3U || SAM3XA) 223 PMC->CKGR_UCKR = pll1_setting;
230 switch(mck_setting & PMC_MCKR_CSS_Msk) {
234 #if (SAM3S || SAM4S || SAM4C || SAM4CM || SAM4CP) 235 case PMC_MCKR_CSS_PLLB_CLK:
236 while (!(
PMC->PMC_SR & PMC_SR_LOCKB));
238 #elif (SAM3U || SAM3XA) 246 mckr =
PMC->PMC_MCKR;
249 PMC->PMC_MCKR = (mckr & ~PMC_MCKR_PRES_Msk)
250 | (mck_setting & PMC_MCKR_PRES_Msk);
254 EFC0->EEFC_FMR = fmr_setting;
256 EFC1->EEFC_FMR = fmr_setting1;
260 PMC->PMC_MCKR = mck_setting;
264 while (!(
PMC->PMC_SR & pll_sr));
268 static volatile bool b_is_sleep_clock_used =
false;
270 static pmc_callback_wakeup_clocks_restored_t callback_clocks_restored =
NULL;
272 void pmc_sleep(
int sleep_mode)
274 switch (sleep_mode) {
275 case SAM_PM_SMODE_SLEEP_WFI:
276 case SAM_PM_SMODE_SLEEP_WFE:
277 #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CM || SAM4CP || SAMG || SAMV71 || SAMV70 || SAMS70 || SAME70) 278 SCB->SCR &= (uint32_t)~SCR_SLEEPDEEP;
285 SCB->SCR &= (uint32_t)~SCR_SLEEPDEEP;
287 if (sleep_mode == SAM_PM_SMODE_SLEEP_WFI) {
297 case SAM_PM_SMODE_WAIT_FAST:
298 case SAM_PM_SMODE_WAIT: {
299 uint32_t mor, pllr0, pllr1, mckr;
304 #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CM || SAM4CP || SAMG || SAMV71 || SAMV70 || SAMS70 || SAME70) 305 (sleep_mode == SAM_PM_SMODE_WAIT_FAST) ?
310 b_is_sleep_clock_used =
true;
312 #if (SAM4C || SAM4CM || SAM4CP) 314 uint32_t cpclk_backup =
PMC->PMC_SCSR &
315 (PMC_SCSR_CPCK | PMC_SCSR_CPBMCK);
316 PMC->PMC_SCDR = cpclk_backup | PMC_SCDR_CPKEY_PASSWD;
318 pmc_save_clock_settings(&mor, &pllr0, &pllr1, &mckr, &fmr,
322 (sleep_mode == SAM_PM_SMODE_WAIT));
330 pmc_restore_clock_setting(mor, pllr0, pllr1, mckr, fmr
336 #if (SAM4C || SAM4CM || SAM4CP) 338 PMC->PMC_SCER = cpclk_backup | PMC_SCER_CPKEY_PASSWD;
340 b_is_sleep_clock_used =
false;
341 if (callback_clocks_restored) {
342 callback_clocks_restored();
343 callback_clocks_restored =
NULL;
349 #if (!(SAMG51 || SAMG53 || SAMG54)) 350 case SAM_PM_SMODE_BACKUP:
351 SCB->SCR |= SCR_SLEEPDEEP;
352 #if (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CM || SAM4CP || SAMG55 || SAMV71 || SAMV70 || SAMS70 || SAME70) 365 bool pmc_is_wakeup_clocks_restored(
void)
367 return !b_is_sleep_clock_used;
370 void pmc_wait_wakeup_clocks_restore(
371 pmc_callback_wakeup_clocks_restored_t callback)
373 if (b_is_sleep_clock_used) {
375 callback_clocks_restored = callback;
376 }
else if (callback) {
#define cpu_irq_disable()
Disable interrupts globally.
#define CKGR_MOR_MOSCSEL
(CKGR_MOR) Main Oscillator Selection
#define CKGR_MOR_MOSCXTBY
(CKGR_MOR) Main Crystal Oscillator Bypass
#define PMC
(PMC ) Base Address
#define CKGR_MOR_MOSCRCF_Msk
(CKGR_MOR) Main On-Chip RC Oscillator Frequency Selection
#define UNUSED(v)
Marking v as a unused parameter or value.
#define CKGR_PLLAR_MULA_Msk
(CKGR_PLLAR) PLLA Multiplier
#define PMC_FSMR_FLPM_FLASH_DEEP_POWERDOWN
(PMC_FSMR) Flash is in Deep-power-down mode when system enters Wait Mode
#define PMC_FSMR_FLPM_FLASH_STANDBY
(PMC_FSMR) Flash is in Standby Mode when system enters Wait Mode
#define CKGR_MOR_MOSCRCEN
(CKGR_MOR) Main On-Chip RC Oscillator Enable
void pmc_enable_waitmode(void)
Enable Wait Mode. Enter condition: WFE + (SLEEPDEEP bit = 0) + (LPM bit = 1)
#define CKGR_UCKR_UPLLEN
(CKGR_UCKR) UTMI PLL Enable
#define PMC_SR_MOSCXTS
(PMC_SR) Main Crystal Oscillator Status
#define PMC_SR_MOSCRCS
(PMC_SR) Main On-Chip RC Oscillator Status
Commonly used includes, types and macros.
#define PMC_FSMR_LPM
(PMC_FSMR) Low-power Mode
#define EEFC_FMR_FWS(value)
#define PMC_MCKR_CSS_MAIN_CLK
(PMC_MCKR) Main Clock is selected
#define CKGR_PLLAR_ONE
(CKGR_PLLAR) Must Be Set to 1
#define CKGR_MOR_MOSCXTEN
(CKGR_MOR) Main Crystal Oscillator Enable
#define PMC_SR_LOCKA
(PMC_SR) PLLA Lock Status
#define PMC_SR_MOSCSELS
(PMC_SR) Main Oscillator Selection Status
#define PMC_SR_MCKRDY
(PMC_SR) Master Clock Status
#define PMC_MCKR_CSS_PLLA_CLK
(PMC_MCKR) PLLA Clock is selected
#define SUPC
(SUPC ) Base Address
#define PMC_SR_LOCKU
(PMC_SR) UTMI PLL Lock Status
#define PMC_MCKR_PRES_Msk
(PMC_MCKR) Processor Clock Prescaler
#define PMC_MCKR_CSS_UPLL_CLK
(PMC_MCKR) Divided UPLL Clock is selected
#define SUPC_CR_KEY_PASSWD
#define SUPC_CR_VROFF_STOP_VREG
(SUPC_CR) If KEY is correct, VROFF asserts the vddcore_nreset and stops the voltage regulator...
#define EEFC_FMR_FWS_Msk
(EEFC_FMR) Flash Wait State
Standard board header file.
#define PMC_MCKR_CSS_Msk
(PMC_MCKR) Master Clock Source Selection
void pmc_disable_pllack(void)
Disable PLLA clock.
#define cpu_irq_enable()
Enable interrupts globally.
#define CKGR_MOR_KEY_PASSWD