mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/sleep.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
- Child:
- 22:9c52de9bc1d7
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /***************************************************************************//** |
bogdanm | 0:9b334a45a8ff | 2 | * @file sleep.c |
bogdanm | 0:9b334a45a8ff | 3 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 4 | * @section License |
bogdanm | 0:9b334a45a8ff | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
bogdanm | 0:9b334a45a8ff | 6 | ******************************************************************************* |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * Permission is granted to anyone to use this software for any purpose, |
bogdanm | 0:9b334a45a8ff | 9 | * including commercial applications, and to alter it and redistribute it |
bogdanm | 0:9b334a45a8ff | 10 | * freely, subject to the following restrictions: |
bogdanm | 0:9b334a45a8ff | 11 | * |
bogdanm | 0:9b334a45a8ff | 12 | * 1. The origin of this software must not be misrepresented; you must not |
bogdanm | 0:9b334a45a8ff | 13 | * claim that you wrote the original software. |
bogdanm | 0:9b334a45a8ff | 14 | * 2. Altered source versions must be plainly marked as such, and must not be |
bogdanm | 0:9b334a45a8ff | 15 | * misrepresented as being the original software. |
bogdanm | 0:9b334a45a8ff | 16 | * 3. This notice may not be removed or altered from any source distribution. |
bogdanm | 0:9b334a45a8ff | 17 | * |
bogdanm | 0:9b334a45a8ff | 18 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
bogdanm | 0:9b334a45a8ff | 19 | * obligation to support this Software. Silicon Labs is providing the |
bogdanm | 0:9b334a45a8ff | 20 | * Software "AS IS", with no express or implied warranties of any kind, |
bogdanm | 0:9b334a45a8ff | 21 | * including, but not limited to, any implied warranties of merchantability |
bogdanm | 0:9b334a45a8ff | 22 | * or fitness for any particular purpose or warranties against infringement |
bogdanm | 0:9b334a45a8ff | 23 | * of any proprietary rights of a third party. |
bogdanm | 0:9b334a45a8ff | 24 | * |
bogdanm | 0:9b334a45a8ff | 25 | * Silicon Labs will not be liable for any consequential, incidental, or |
bogdanm | 0:9b334a45a8ff | 26 | * special damages, or any other relief, or for any claim by any third party, |
bogdanm | 0:9b334a45a8ff | 27 | * arising from your use of this Software. |
bogdanm | 0:9b334a45a8ff | 28 | * |
bogdanm | 0:9b334a45a8ff | 29 | ******************************************************************************/ |
bogdanm | 0:9b334a45a8ff | 30 | |
bogdanm | 0:9b334a45a8ff | 31 | #include "device.h" |
bogdanm | 0:9b334a45a8ff | 32 | #if DEVICE_SLEEP |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | #include "sleep_api.h" |
bogdanm | 0:9b334a45a8ff | 35 | #include "sleepmodes.h" |
bogdanm | 0:9b334a45a8ff | 36 | #include "cmsis.h" |
bogdanm | 0:9b334a45a8ff | 37 | #include "em_emu.h" |
bogdanm | 0:9b334a45a8ff | 38 | #include "em_int.h" |
bogdanm | 0:9b334a45a8ff | 39 | |
bogdanm | 0:9b334a45a8ff | 40 | uint32_t sleep_block_counter[NUM_SLEEP_MODES] = {0}; |
bogdanm | 0:9b334a45a8ff | 41 | |
bogdanm | 0:9b334a45a8ff | 42 | /** |
bogdanm | 0:9b334a45a8ff | 43 | * Sleep mode. |
bogdanm | 0:9b334a45a8ff | 44 | * Enter Energy Mode 1, which turns off the clock to the CPU. |
bogdanm | 0:9b334a45a8ff | 45 | * |
bogdanm | 0:9b334a45a8ff | 46 | * In EM1, the CPU is sleeping and the power consumption is only 50 μA/MHz. |
bogdanm | 0:9b334a45a8ff | 47 | * All peripherals, including DMA, PRS and memory system, are still available. |
bogdanm | 0:9b334a45a8ff | 48 | */ |
bogdanm | 0:9b334a45a8ff | 49 | void sleep(void) |
bogdanm | 0:9b334a45a8ff | 50 | { |
bogdanm | 0:9b334a45a8ff | 51 | if (sleep_block_counter[0] > 0) { |
bogdanm | 0:9b334a45a8ff | 52 | /* Blocked everything below EM0, so just return */ |
bogdanm | 0:9b334a45a8ff | 53 | return; |
bogdanm | 0:9b334a45a8ff | 54 | } else if (sleep_block_counter[1] > 0) { |
bogdanm | 0:9b334a45a8ff | 55 | /* Blocked everything below EM1, enter EM1 */ |
bogdanm | 0:9b334a45a8ff | 56 | EMU_EnterEM1(); |
bogdanm | 0:9b334a45a8ff | 57 | } else if (sleep_block_counter[2] > 0) { |
bogdanm | 0:9b334a45a8ff | 58 | /* Blocked everything below EM2, enter EM2 */ |
bogdanm | 0:9b334a45a8ff | 59 | EMU_EnterEM2(true); |
bogdanm | 0:9b334a45a8ff | 60 | } else if (sleep_block_counter[3] > 0) { |
bogdanm | 0:9b334a45a8ff | 61 | /* Blocked everything below EM3, enter EM3 */ |
bogdanm | 0:9b334a45a8ff | 62 | EMU_EnterEM3(true); |
bogdanm | 0:9b334a45a8ff | 63 | } else{ |
bogdanm | 0:9b334a45a8ff | 64 | /* Nothing is blocked, enter EM4 */ |
bogdanm | 0:9b334a45a8ff | 65 | EMU_EnterEM4(); |
bogdanm | 0:9b334a45a8ff | 66 | } |
bogdanm | 0:9b334a45a8ff | 67 | return; |
bogdanm | 0:9b334a45a8ff | 68 | } |
bogdanm | 0:9b334a45a8ff | 69 | |
bogdanm | 0:9b334a45a8ff | 70 | /** |
bogdanm | 0:9b334a45a8ff | 71 | * Deep Sleep mode. |
bogdanm | 0:9b334a45a8ff | 72 | * Enter Energy Mode 2, turning off all high-frequency clocks. |
bogdanm | 0:9b334a45a8ff | 73 | * |
bogdanm | 0:9b334a45a8ff | 74 | * In EM2 the high frequency oscillator is turned off, but with the 32.768 kHz |
bogdanm | 0:9b334a45a8ff | 75 | * oscillator running, selected low energy peripherals (LCD, RTC, LETIMER, |
bogdanm | 0:9b334a45a8ff | 76 | * PCNT, LEUART, I2C, LESENSE, OPAMP, USB, WDOG and ACMP) are still |
bogdanm | 0:9b334a45a8ff | 77 | * available. This gives a high degree of autonomous operation with a current |
bogdanm | 0:9b334a45a8ff | 78 | * consumption as low as 1.1 μA with RTC enabled. Power-on Reset, Brown-out |
bogdanm | 0:9b334a45a8ff | 79 | * Detection and full RAM and CPU retention is also included. |
bogdanm | 0:9b334a45a8ff | 80 | */ |
bogdanm | 0:9b334a45a8ff | 81 | void deepsleep(void) |
bogdanm | 0:9b334a45a8ff | 82 | { |
bogdanm | 0:9b334a45a8ff | 83 | EMU_EnterEM2(true); |
bogdanm | 0:9b334a45a8ff | 84 | } |
bogdanm | 0:9b334a45a8ff | 85 | |
bogdanm | 0:9b334a45a8ff | 86 | /** Block the microcontroller from sleeping below a certain mode |
bogdanm | 0:9b334a45a8ff | 87 | * |
bogdanm | 0:9b334a45a8ff | 88 | * This will block sleep() from entering an energy mode below the one given. |
bogdanm | 0:9b334a45a8ff | 89 | * -- To be called by peripheral HAL's -- |
bogdanm | 0:9b334a45a8ff | 90 | * |
bogdanm | 0:9b334a45a8ff | 91 | * After the peripheral is finished with the operation, it should call unblock with the same state |
bogdanm | 0:9b334a45a8ff | 92 | * |
bogdanm | 0:9b334a45a8ff | 93 | */ |
bogdanm | 0:9b334a45a8ff | 94 | void blockSleepMode(sleepstate_enum minimumMode) |
bogdanm | 0:9b334a45a8ff | 95 | { |
bogdanm | 0:9b334a45a8ff | 96 | INT_Disable(); |
bogdanm | 0:9b334a45a8ff | 97 | sleep_block_counter[minimumMode]++; |
bogdanm | 0:9b334a45a8ff | 98 | INT_Enable(); |
bogdanm | 0:9b334a45a8ff | 99 | } |
bogdanm | 0:9b334a45a8ff | 100 | |
bogdanm | 0:9b334a45a8ff | 101 | /** Unblock the microcontroller from sleeping below a certain mode |
bogdanm | 0:9b334a45a8ff | 102 | * |
bogdanm | 0:9b334a45a8ff | 103 | * This will unblock sleep() from entering an energy mode below the one given. |
bogdanm | 0:9b334a45a8ff | 104 | * -- To be called by peripheral HAL's -- |
bogdanm | 0:9b334a45a8ff | 105 | * |
bogdanm | 0:9b334a45a8ff | 106 | * This should be called after all transactions on a peripheral are done. |
bogdanm | 0:9b334a45a8ff | 107 | */ |
bogdanm | 0:9b334a45a8ff | 108 | void unblockSleepMode(sleepstate_enum minimumMode) |
bogdanm | 0:9b334a45a8ff | 109 | { |
bogdanm | 0:9b334a45a8ff | 110 | INT_Disable(); |
bogdanm | 0:9b334a45a8ff | 111 | if(sleep_block_counter[minimumMode] > 0) { |
bogdanm | 0:9b334a45a8ff | 112 | sleep_block_counter[minimumMode]--; |
bogdanm | 0:9b334a45a8ff | 113 | } |
bogdanm | 0:9b334a45a8ff | 114 | INT_Enable(); |
bogdanm | 0:9b334a45a8ff | 115 | } |
bogdanm | 0:9b334a45a8ff | 116 | |
bogdanm | 0:9b334a45a8ff | 117 | #endif |