mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
targets/TARGET_Silicon_Labs/TARGET_EFM32/sleep.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /***************************************************************************//** |
elessair | 0:f269e3021894 | 2 | * @file sleep.c |
elessair | 0:f269e3021894 | 3 | ******************************************************************************* |
elessair | 0:f269e3021894 | 4 | * @section License |
elessair | 0:f269e3021894 | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
elessair | 0:f269e3021894 | 6 | ******************************************************************************* |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * SPDX-License-Identifier: Apache-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
elessair | 0:f269e3021894 | 11 | * not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 12 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 13 | * |
elessair | 0:f269e3021894 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 15 | * |
elessair | 0:f269e3021894 | 16 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
elessair | 0:f269e3021894 | 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 19 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 20 | * limitations under the License. |
elessair | 0:f269e3021894 | 21 | * |
elessair | 0:f269e3021894 | 22 | ******************************************************************************/ |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | #include "device.h" |
elessair | 0:f269e3021894 | 25 | #if DEVICE_SLEEP |
elessair | 0:f269e3021894 | 26 | |
elessair | 0:f269e3021894 | 27 | #include "sleep_api.h" |
elessair | 0:f269e3021894 | 28 | #include "sleepmodes.h" |
elessair | 0:f269e3021894 | 29 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 30 | #include "em_emu.h" |
elessair | 0:f269e3021894 | 31 | #include "critical.h" |
elessair | 0:f269e3021894 | 32 | |
elessair | 0:f269e3021894 | 33 | uint32_t sleep_block_counter[NUM_SLEEP_MODES] = {0}; |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | /** |
elessair | 0:f269e3021894 | 36 | * Sleep mode. |
elessair | 0:f269e3021894 | 37 | * Enter the lowest possible sleep mode that is not blocked by ongoing activity. |
elessair | 0:f269e3021894 | 38 | */ |
elessair | 0:f269e3021894 | 39 | void sleep(void) |
elessair | 0:f269e3021894 | 40 | { |
elessair | 0:f269e3021894 | 41 | if (sleep_block_counter[0] > 0) { |
elessair | 0:f269e3021894 | 42 | /* Blocked everything below EM0, so just return */ |
elessair | 0:f269e3021894 | 43 | return; |
elessair | 0:f269e3021894 | 44 | } else if (sleep_block_counter[1] > 0) { |
elessair | 0:f269e3021894 | 45 | /* Blocked everything below EM1, enter EM1 */ |
elessair | 0:f269e3021894 | 46 | EMU_EnterEM1(); |
elessair | 0:f269e3021894 | 47 | } else if (sleep_block_counter[2] > 0) { |
elessair | 0:f269e3021894 | 48 | /* Blocked everything below EM2, enter EM2 */ |
elessair | 0:f269e3021894 | 49 | EMU_EnterEM2(true); |
elessair | 0:f269e3021894 | 50 | } else { |
elessair | 0:f269e3021894 | 51 | /* Blocked everything below EM3, enter EM3 */ |
elessair | 0:f269e3021894 | 52 | EMU_EnterEM3(true); |
elessair | 0:f269e3021894 | 53 | } /* Never enter EM4, as mbed has no way of configuring EM4 wakeup */ |
elessair | 0:f269e3021894 | 54 | return; |
elessair | 0:f269e3021894 | 55 | } |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | /** |
elessair | 0:f269e3021894 | 58 | * Deep Sleep mode. |
elessair | 0:f269e3021894 | 59 | * Enter Energy Mode 2, turning off all high-frequency clocks. |
elessair | 0:f269e3021894 | 60 | * |
elessair | 0:f269e3021894 | 61 | * In EM2 the high frequency oscillator is turned off, but with the 32.768 kHz |
elessair | 0:f269e3021894 | 62 | * oscillator running, selected low energy peripherals (LCD, RTC, LETIMER, |
elessair | 0:f269e3021894 | 63 | * PCNT, LEUART, I2C, LESENSE, OPAMP, USB, WDOG and ACMP) are still |
elessair | 0:f269e3021894 | 64 | * available. This gives a high degree of autonomous operation with a current |
elessair | 0:f269e3021894 | 65 | * consumption as low as 1.1 μA with RTC enabled. Power-on Reset, Brown-out |
elessair | 0:f269e3021894 | 66 | * Detection and full RAM and CPU retention is also included. |
elessair | 0:f269e3021894 | 67 | */ |
elessair | 0:f269e3021894 | 68 | void deepsleep(void) |
elessair | 0:f269e3021894 | 69 | { |
elessair | 0:f269e3021894 | 70 | EMU_EnterEM2(true); |
elessair | 0:f269e3021894 | 71 | } |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | /** Block the microcontroller from sleeping below a certain mode |
elessair | 0:f269e3021894 | 74 | * |
elessair | 0:f269e3021894 | 75 | * This will block sleep() from entering an energy mode below the one given. |
elessair | 0:f269e3021894 | 76 | * -- To be called by peripheral HAL's -- |
elessair | 0:f269e3021894 | 77 | * |
elessair | 0:f269e3021894 | 78 | * After the peripheral is finished with the operation, it should call unblock with the same state |
elessair | 0:f269e3021894 | 79 | * |
elessair | 0:f269e3021894 | 80 | */ |
elessair | 0:f269e3021894 | 81 | void blockSleepMode(sleepstate_enum minimumMode) |
elessair | 0:f269e3021894 | 82 | { |
elessair | 0:f269e3021894 | 83 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 84 | sleep_block_counter[minimumMode]++; |
elessair | 0:f269e3021894 | 85 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 86 | } |
elessair | 0:f269e3021894 | 87 | |
elessair | 0:f269e3021894 | 88 | /** Unblock the microcontroller from sleeping below a certain mode |
elessair | 0:f269e3021894 | 89 | * |
elessair | 0:f269e3021894 | 90 | * This will unblock sleep() from entering an energy mode below the one given. |
elessair | 0:f269e3021894 | 91 | * -- To be called by peripheral HAL's -- |
elessair | 0:f269e3021894 | 92 | * |
elessair | 0:f269e3021894 | 93 | * This should be called after all transactions on a peripheral are done. |
elessair | 0:f269e3021894 | 94 | */ |
elessair | 0:f269e3021894 | 95 | void unblockSleepMode(sleepstate_enum minimumMode) |
elessair | 0:f269e3021894 | 96 | { |
elessair | 0:f269e3021894 | 97 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 98 | if(sleep_block_counter[minimumMode] > 0) { |
elessair | 0:f269e3021894 | 99 | sleep_block_counter[minimumMode]--; |
elessair | 0:f269e3021894 | 100 | } |
elessair | 0:f269e3021894 | 101 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 102 | } |
elessair | 0:f269e3021894 | 103 | |
elessair | 0:f269e3021894 | 104 | #endif |