Morpheus / target-freescale-ksdk

Fork of target-freescale-ksdk by -deleted-

Committer:
screamer
Date:
Wed Mar 23 21:26:50 2016 +0000
Revision:
0:e4d670b91a9a
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:e4d670b91a9a 1 /* mbed Microcontroller Library
screamer 0:e4d670b91a9a 2 * Copyright (c) 2006-2013 ARM Limited
screamer 0:e4d670b91a9a 3 *
screamer 0:e4d670b91a9a 4 * Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:e4d670b91a9a 5 * you may not use this file except in compliance with the License.
screamer 0:e4d670b91a9a 6 * You may obtain a copy of the License at
screamer 0:e4d670b91a9a 7 *
screamer 0:e4d670b91a9a 8 * http://www.apache.org/licenses/LICENSE-2.0
screamer 0:e4d670b91a9a 9 *
screamer 0:e4d670b91a9a 10 * Unless required by applicable law or agreed to in writing, software
screamer 0:e4d670b91a9a 11 * distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:e4d670b91a9a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:e4d670b91a9a 13 * See the License for the specific language governing permissions and
screamer 0:e4d670b91a9a 14 * limitations under the License.
screamer 0:e4d670b91a9a 15 */
screamer 0:e4d670b91a9a 16 #include "sleep_api.h"
screamer 0:e4d670b91a9a 17 #include "cmsis.h"
screamer 0:e4d670b91a9a 18 #include "fsl_mcg_hal.h"
screamer 0:e4d670b91a9a 19 #include "fsl_smc_hal.h"
screamer 0:e4d670b91a9a 20
screamer 0:e4d670b91a9a 21 void sleep(void) {
screamer 0:e4d670b91a9a 22 smc_power_mode_protection_config_t sleep_config = {true};
screamer 0:e4d670b91a9a 23 SMC_HAL_SetProtection(SMC_BASE, &sleep_config);
screamer 0:e4d670b91a9a 24
screamer 0:e4d670b91a9a 25 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
screamer 0:e4d670b91a9a 26 __WFI();
screamer 0:e4d670b91a9a 27 }
screamer 0:e4d670b91a9a 28
screamer 0:e4d670b91a9a 29 void deepsleep(void) {
screamer 0:e4d670b91a9a 30 mcg_clock_select_t mcg_clock = CLOCK_HAL_GetClkSrcMode(MCG_BASE);
screamer 0:e4d670b91a9a 31
screamer 0:e4d670b91a9a 32 smc_power_mode_protection_config_t sleep_config = {true};
screamer 0:e4d670b91a9a 33 SMC_HAL_SetProtection(SMC_BASE, &sleep_config);
screamer 0:e4d670b91a9a 34 SMC->PMCTRL = SMC_PMCTRL_STOPM(2);
screamer 0:e4d670b91a9a 35
screamer 0:e4d670b91a9a 36 //Deep sleep for ARM core:
screamer 0:e4d670b91a9a 37 SCB->SCR = 1 << SCB_SCR_SLEEPDEEP_Pos;
screamer 0:e4d670b91a9a 38
screamer 0:e4d670b91a9a 39 __WFI();
screamer 0:e4d670b91a9a 40
screamer 0:e4d670b91a9a 41 //Switch back to PLL as clock source if needed
screamer 0:e4d670b91a9a 42 //The interrupt that woke up the device will run at reduced speed
screamer 0:e4d670b91a9a 43 if (mcg_clock == kMcgClkSelOut) {
screamer 0:e4d670b91a9a 44 if (CLOCK_HAL_GetPllStatMode(MCG_BASE) == kMcgPllStatPllClkSel) {
screamer 0:e4d670b91a9a 45 while (CLOCK_HAL_GetLock0Mode(MCG_BASE) == kMcgLockUnlocked);
screamer 0:e4d670b91a9a 46 }
screamer 0:e4d670b91a9a 47 CLOCK_HAL_SetClkSrcMode(MCG_BASE, kMcgClkSelOut);
screamer 0:e4d670b91a9a 48 }
screamer 0:e4d670b91a9a 49 }