mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NORDIC/TARGET_NRF5/sleep.c@150:da61ba4e9755, 2016-11-25 (annotated)
- Committer:
- sivasuren
- Date:
- Fri Nov 25 07:57:40 2016 +0000
- Revision:
- 150:da61ba4e9755
- Parent:
- 149:156823d33999
surendar changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "sleep_api.h" |
<> | 149:156823d33999 | 17 | #include "cmsis.h" |
<> | 149:156823d33999 | 18 | #include "mbed_interface.h" |
<> | 149:156823d33999 | 19 | #include "softdevice_handler.h" |
<> | 149:156823d33999 | 20 | #include "nrf_soc.h" |
<> | 149:156823d33999 | 21 | |
<> | 149:156823d33999 | 22 | // Mask of reserved bits of the register ICSR in the System Control Block peripheral |
<> | 149:156823d33999 | 23 | // In this case, bits which are equal to 0 are the bits reserved in this register |
<> | 149:156823d33999 | 24 | #define SCB_ICSR_RESERVED_BITS_MASK 0x9E43F03F |
<> | 149:156823d33999 | 25 | |
<> | 149:156823d33999 | 26 | void sleep(void) |
<> | 149:156823d33999 | 27 | { |
<> | 149:156823d33999 | 28 | // ensure debug is disconnected if semihost is enabled.... |
<> | 149:156823d33999 | 29 | |
<> | 149:156823d33999 | 30 | // Trigger an event when an interrupt is pending. This allows to wake up |
<> | 149:156823d33999 | 31 | // the processor from disabled interrupts. |
<> | 149:156823d33999 | 32 | SCB->SCR |= SCB_SCR_SEVONPEND_Msk; |
<> | 149:156823d33999 | 33 | |
<> | 149:156823d33999 | 34 | // If the SoftDevice is enabled, its API must be used to go to sleep. |
<> | 149:156823d33999 | 35 | if (softdevice_handler_isEnabled()) { |
<> | 149:156823d33999 | 36 | sd_power_mode_set(NRF_POWER_MODE_LOWPWR); |
<> | 149:156823d33999 | 37 | sd_app_evt_wait(); |
<> | 149:156823d33999 | 38 | } else { |
<> | 149:156823d33999 | 39 | NRF_POWER->TASKS_LOWPWR = 1; |
<> | 149:156823d33999 | 40 | |
<> | 149:156823d33999 | 41 | // Note: it is not sufficient to just use WFE here, since the internal |
<> | 149:156823d33999 | 42 | // event register may be already set from an event that occurred in the |
<> | 149:156823d33999 | 43 | // past (like an SVC call to the SoftDevice) and in such case WFE will |
<> | 149:156823d33999 | 44 | // just clear the register and continue execution. |
<> | 149:156823d33999 | 45 | // Therefore, the strategy here is to first clear the event register |
<> | 149:156823d33999 | 46 | // by using SEV/WFE pair, and then execute WFE again, unless there is |
<> | 149:156823d33999 | 47 | // a pending interrupt. |
<> | 149:156823d33999 | 48 | |
<> | 149:156823d33999 | 49 | // Set an event and wake up whatsoever, this will clear the event |
<> | 149:156823d33999 | 50 | // register from all previous events set (SVC call included) |
<> | 149:156823d33999 | 51 | __SEV(); |
<> | 149:156823d33999 | 52 | __WFE(); |
<> | 149:156823d33999 | 53 | |
<> | 149:156823d33999 | 54 | // Test if there is an interrupt pending (mask reserved regions) |
<> | 149:156823d33999 | 55 | if (SCB->ICSR & (SCB_ICSR_RESERVED_BITS_MASK)) { |
<> | 149:156823d33999 | 56 | // Ok, there is an interrut pending, no need to go to sleep |
<> | 149:156823d33999 | 57 | return; |
<> | 149:156823d33999 | 58 | } else { |
<> | 149:156823d33999 | 59 | // next event will wakeup the CPU |
<> | 149:156823d33999 | 60 | // If an interrupt occured between the test of SCB->ICSR and this |
<> | 149:156823d33999 | 61 | // instruction, WFE will just not put the CPU to sleep |
<> | 149:156823d33999 | 62 | __WFE(); |
<> | 149:156823d33999 | 63 | } |
<> | 149:156823d33999 | 64 | } |
<> | 149:156823d33999 | 65 | } |
<> | 149:156823d33999 | 66 | |
<> | 149:156823d33999 | 67 | void deepsleep(void) |
<> | 149:156823d33999 | 68 | { |
<> | 149:156823d33999 | 69 | sleep(); |
<> | 149:156823d33999 | 70 | // NRF_POWER->SYSTEMOFF=1; |
<> | 149:156823d33999 | 71 | } |