-
Notifications
You must be signed in to change notification settings - Fork 12
/
sleep.c
75 lines (66 loc) · 2.97 KB
/
sleep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* mbed Microcontroller Library
* Copyright (c) 2006-2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sleep_api.h"
#include "cmsis.h"
#include "mbed-drivers/mbed_interface.h"
#include "nrf_soc.h"
#include "nrf_sdm.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 mbed_enter_sleep(sleep_t *obj)
{
(void)obj;
// ensure debug is disconnected if semihost is enabled....
NRF_POWER->TASKS_LOWPWR = 1;
SCB->SCR |= SCB_SCR_SEVONPEND_Msk; /* send an event when an interrupt is pending.
* This helps with the wakeup from the following app_evt_wait(). */
uint8_t sd_enabled;
// 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_app_evt_wait();
} else {
// Note: it is not possible to just use WFE at this stage because WFE
// will read the event register (not accessible) and if an event occured,
// in the past, it will just clear the event register and continue execution.
// SVC call like sd_softdevice_is_enabled set the event register to 1.
// This means that a making an SVC call followed by WFE will never put the
// CPU to sleep.
// Our startegy here is to clear the event register then, if there is any
// interrupt, return from here. If no interrupts are pending, just call
// WFE.
// 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 mbed_exit_sleep(sleep_t *obj)
{
(void)obj;
}