TARGET_NORDIC: simplified the checking if SD is enabled in 'sleep()', used the new implementation in legacy HAL for NRF51822.

pull/2865/head
Głąbek, Andrzej 2016-10-05 09:10:29 +02:00
parent 10419cd3f5
commit 1b593b125a
2 changed files with 62 additions and 22 deletions

View File

@ -16,13 +16,55 @@
#include "sleep_api.h" #include "sleep_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "mbed_interface.h" #include "mbed_interface.h"
#include "softdevice_handler.h"
#include "nrf_soc.h"
// Mask of reserved bits of the register ICSR in the System Control Block peripheral
// In this case, bits which are equal to 0 are the bits reserved in this register
#define SCB_ICSR_RESERVED_BITS_MASK 0x9E43F03F
void sleep(void) void sleep(void)
{ {
// ensure debug is disconnected if semihost is enabled.... // ensure debug is disconnected if semihost is enabled....
NRF_POWER->TASKS_LOWPWR = 1;
// wait for interrupt // Trigger an event when an interrupt is pending. This allows to wake up
__WFE(); // the processor from disabled interrupts.
SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
// If the SoftDevice is enabled, its API must be used to go to sleep.
if (softdevice_handler_isEnabled())
{
sd_power_mode_set(NRF_POWER_MODE_LOWPWR);
sd_app_evt_wait();
}
else
{
NRF_POWER->TASKS_LOWPWR = 1;
// Note: it is not sufficient to just use WFE here, since the internal
// event register may be already set from an event that occurred in the
// past (like an SVC call to the SoftDevice) and in such case WFE will
// just clear the register and continue execution.
// Therefore, the strategy here is to first clear the event register
// by using SEV/WFE pair, and then execute WFE again, unless there is
// a pending interrupt.
// Set an event and wake up whatsoever, this will clear the event
// register from all previous events set (SVC call included)
__SEV();
__WFE();
// Test if there is an interrupt pending (mask reserved regions)
if (SCB->ICSR & (SCB_ICSR_RESERVED_BITS_MASK)) {
// Ok, there is an interrut pending, no need to go to sleep
return;
} else {
// next event will wakeup the CPU
// If an interrupt occured between the test of SCB->ICSR and this
// instruction, WFE will just not put the CPU to sleep
__WFE();
}
}
} }
void deepsleep(void) void deepsleep(void)

View File

@ -16,8 +16,8 @@
#include "sleep_api.h" #include "sleep_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "mbed_interface.h" #include "mbed_interface.h"
#include "softdevice_handler.h"
#include "nrf_soc.h" #include "nrf_soc.h"
#include "nrf_sdm.h"
// Mask of reserved bits of the register ICSR in the System Control Block peripheral // Mask of reserved bits of the register ICSR in the System Control Block peripheral
// In this case, bits which are equal to 0 are the bits reserved in this register // In this case, bits which are equal to 0 are the bits reserved in this register
@ -27,29 +27,27 @@ void sleep(void)
{ {
// ensure debug is disconnected if semihost is enabled.... // ensure debug is disconnected if semihost is enabled....
SCB->SCR |= SCB_SCR_SEVONPEND_Msk; /* send an event when an interrupt is pending. // Trigger an event when an interrupt is pending. This allows to wake up
* This helps with the wakeup from the following app_evt_wait(). */ // the processor from disabled interrupts.
SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
uint8_t sd_enabled; // If the SoftDevice is enabled, its API must be used to go to sleep.
if (softdevice_handler_isEnabled())
// look if exceptions are enabled or not, if they are, it is possible to make an SVC call {
// and check if the soft device is running
if ((__get_PRIMASK() == 0) && (sd_softdevice_is_enabled(&sd_enabled) == NRF_SUCCESS) && (sd_enabled == 1)) {
// soft device is enabled, use the primitives from the soft device to go to sleep
sd_power_mode_set(NRF_POWER_MODE_LOWPWR); sd_power_mode_set(NRF_POWER_MODE_LOWPWR);
sd_app_evt_wait(); sd_app_evt_wait();
} else { }
else
{
NRF_POWER->TASKS_LOWPWR = 1; NRF_POWER->TASKS_LOWPWR = 1;
// Note: it is not possible to just use WFE at this stage because WFE // Note: it is not sufficient to just use WFE here, since the internal
// will read the event register (not accessible) and if an event occured, // event register may be already set from an event that occurred in the
// in the past, it will just clear the event register and continue execution. // past (like an SVC call to the SoftDevice) and in such case WFE will
// SVC call like sd_softdevice_is_enabled set the event register to 1. // just clear the register and continue execution.
// This means that a making an SVC call followed by WFE will never put the // Therefore, the strategy here is to first clear the event register
// CPU to sleep. // by using SEV/WFE pair, and then execute WFE again, unless there is
// Our startegy here is to clear the event register then, if there is any // a pending interrupt.
// interrupt, return from here. If no interrupts are pending, just call
// WFE.
// Set an event and wake up whatsoever, this will clear the event // Set an event and wake up whatsoever, this will clear the event
// register from all previous events set (SVC call included) // register from all previous events set (SVC call included)