USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Zaitsev 10:41552d038a69 1 /* mbed Microcontroller Library
Zaitsev 10:41552d038a69 2 * Copyright (c) 2006-2013 ARM Limited
Zaitsev 10:41552d038a69 3 *
Zaitsev 10:41552d038a69 4 * Licensed under the Apache License, Version 2.0 (the "License");
Zaitsev 10:41552d038a69 5 * you may not use this file except in compliance with the License.
Zaitsev 10:41552d038a69 6 * You may obtain a copy of the License at
Zaitsev 10:41552d038a69 7 *
Zaitsev 10:41552d038a69 8 * http://www.apache.org/licenses/LICENSE-2.0
Zaitsev 10:41552d038a69 9 *
Zaitsev 10:41552d038a69 10 * Unless required by applicable law or agreed to in writing, software
Zaitsev 10:41552d038a69 11 * distributed under the License is distributed on an "AS IS" BASIS,
Zaitsev 10:41552d038a69 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Zaitsev 10:41552d038a69 13 * See the License for the specific language governing permissions and
Zaitsev 10:41552d038a69 14 * limitations under the License.
Zaitsev 10:41552d038a69 15 */
Zaitsev 10:41552d038a69 16 #include "sleep_api.h"
Zaitsev 10:41552d038a69 17 #include "cmsis.h"
Zaitsev 10:41552d038a69 18 #include "PeripheralPins.h"
Zaitsev 10:41552d038a69 19
Zaitsev 10:41552d038a69 20 //Normal wait mode
Zaitsev 10:41552d038a69 21 void sleep(void)
Zaitsev 10:41552d038a69 22 {
Zaitsev 10:41552d038a69 23 SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
Zaitsev 10:41552d038a69 24
Zaitsev 10:41552d038a69 25 //Normal sleep mode for ARM core:
Zaitsev 10:41552d038a69 26 SCB->SCR = 0;
Zaitsev 10:41552d038a69 27 __WFI();
Zaitsev 10:41552d038a69 28 }
Zaitsev 10:41552d038a69 29
Zaitsev 10:41552d038a69 30 //Very low-power stop mode
Zaitsev 10:41552d038a69 31 void deepsleep(void)
Zaitsev 10:41552d038a69 32 {
Zaitsev 10:41552d038a69 33 //Check if ADC is enabled and HS mode is set, if yes disable it (lowers power consumption by 60uA)
Zaitsev 10:41552d038a69 34 uint8_t ADC_HSC = 0;
Zaitsev 10:41552d038a69 35 if (SIM->SCGC6 & SIM_SCGC6_ADC0_MASK) {
Zaitsev 10:41552d038a69 36 if (ADC0->CFG2 & ADC_CFG2_ADHSC_MASK) {
Zaitsev 10:41552d038a69 37 ADC_HSC = 1;
Zaitsev 10:41552d038a69 38 ADC0->CFG2 &= ~(ADC_CFG2_ADHSC_MASK);
Zaitsev 10:41552d038a69 39 }
Zaitsev 10:41552d038a69 40 }
Zaitsev 10:41552d038a69 41
Zaitsev 10:41552d038a69 42 #if ! defined(TARGET_KL43Z)
Zaitsev 10:41552d038a69 43 //Check if PLL/FLL is enabled:
Zaitsev 10:41552d038a69 44 uint32_t PLL_FLL_en = (MCG->C1 & MCG_C1_CLKS_MASK) == MCG_C1_CLKS(0);
Zaitsev 10:41552d038a69 45 #endif
Zaitsev 10:41552d038a69 46
Zaitsev 10:41552d038a69 47 SMC->PMPROT = SMC_PMPROT_AVLLS_MASK | SMC_PMPROT_ALLS_MASK | SMC_PMPROT_AVLP_MASK;
Zaitsev 10:41552d038a69 48 SMC->PMCTRL = SMC_PMCTRL_STOPM(2);
Zaitsev 10:41552d038a69 49
Zaitsev 10:41552d038a69 50 //Deep sleep for ARM core:
Zaitsev 10:41552d038a69 51 SCB->SCR = 1<<SCB_SCR_SLEEPDEEP_Pos;
Zaitsev 10:41552d038a69 52
Zaitsev 10:41552d038a69 53 __WFI();
Zaitsev 10:41552d038a69 54
Zaitsev 10:41552d038a69 55 #if ! defined(TARGET_KL43Z)
Zaitsev 10:41552d038a69 56 //Switch back to PLL as clock source if needed
Zaitsev 10:41552d038a69 57 //The interrupt that woke up the device will run at reduced speed
Zaitsev 10:41552d038a69 58 if (PLL_FLL_en) {
Zaitsev 10:41552d038a69 59 #ifdef MCG_C5_PLLCLKEN0_MASK //PLL available
Zaitsev 10:41552d038a69 60 if (MCG->C6 & (1<<MCG_C6_PLLS_SHIFT) != 0) /* If PLL */
Zaitsev 10:41552d038a69 61 while((MCG->S & MCG_S_LOCK0_MASK) == 0x00U); /* Wait until locked */
Zaitsev 10:41552d038a69 62 #endif
Zaitsev 10:41552d038a69 63 MCG->C1 &= ~MCG_C1_CLKS_MASK;
Zaitsev 10:41552d038a69 64 }
Zaitsev 10:41552d038a69 65 #endif
Zaitsev 10:41552d038a69 66
Zaitsev 10:41552d038a69 67 if (ADC_HSC) {
Zaitsev 10:41552d038a69 68 ADC0->CFG2 |= (ADC_CFG2_ADHSC_MASK);
Zaitsev 10:41552d038a69 69 }
Zaitsev 10:41552d038a69 70 }