Skip to content

Commit 9d625a1

Browse files
aykevldeadprogram
authored andcommitted
nrf: call sd_app_evt_wait when the SoftDevice is enabled
This reduces current consumption from 500-1000µA to very low (<10µA) current consumption. This change is important for battery powered devices, especially devices that may be running for long periods of time.
1 parent b75f042 commit 9d625a1

File tree

8 files changed

+71
-4
lines changed

8 files changed

+71
-4
lines changed

src/runtime/arch_cortexm.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,3 @@ func procPin() {
102102
func procUnpin() {
103103
arm.EnableInterrupts(procPinnedMask)
104104
}
105-
106-
func waitForEvents() {
107-
arm.Asm("wfe")
108-
}

src/runtime/runtime_atsamd21.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,7 @@ func initADCClock() {
345345
sam.GCLK_CLKCTRL_CLKEN)
346346
waitForSync()
347347
}
348+
349+
func waitForEvents() {
350+
arm.Asm("wfe")
351+
}

src/runtime/runtime_atsamd51.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,7 @@ func initADCClock() {
332332
sam.GCLK.PCHCTRL[41].Set((sam.GCLK_PCHCTRL_GEN_GCLK1 << sam.GCLK_PCHCTRL_GEN_Pos) |
333333
sam.GCLK_PCHCTRL_CHEN)
334334
}
335+
336+
func waitForEvents() {
337+
arm.Asm("wfe")
338+
}

src/runtime/runtime_cortexm_qemu.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ var stdoutWrite = (*volatile.Register8)(unsafe.Pointer(uintptr(0x4000c000)))
5050
func putchar(c byte) {
5151
stdoutWrite.Set(uint8(c))
5252
}
53+
54+
func waitForEvents() {
55+
arm.Asm("wfe")
56+
}

src/runtime/runtime_nrf_bare.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build nrf,!softdevice
2+
3+
package runtime
4+
5+
import "device/arm"
6+
7+
func waitForEvents() {
8+
arm.Asm("wfe")
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// +build nrf,softdevice
2+
3+
package runtime
4+
5+
import (
6+
"device/arm"
7+
"device/nrf"
8+
)
9+
10+
//export sd_app_evt_wait
11+
func sd_app_evt_wait()
12+
13+
func waitForEvents() {
14+
// Call into the SoftDevice to sleep. This is necessary here because a
15+
// normal wfe will not put the chip in low power mode (it still consumes
16+
// 500µA-1mA). It is really needed to call sd_app_evt_wait for low power
17+
// consumption.
18+
19+
// First check whether the SoftDevice is enabled. Unfortunately,
20+
// sd_app_evt_wait cannot be called when the SoftDevice is not enabled.
21+
var enabled uint8
22+
arm.SVCall1(0x12, &enabled) // sd_softdevice_is_enabled
23+
24+
if enabled != 0 {
25+
// Now pick the appropriate SVCall number. Hopefully they won't change
26+
// in the future with a different SoftDevice version.
27+
if nrf.DEVICE == "nrf51" {
28+
// sd_app_evt_wait: SOC_SVC_BASE_NOT_AVAILABLE + 29
29+
arm.SVCall0(0x2B + 29)
30+
} else if nrf.DEVICE == "nrf52" || nrf.DEVICE == "nrf52840" {
31+
// sd_app_evt_wait: SOC_SVC_BASE_NOT_AVAILABLE + 21
32+
arm.SVCall0(0x2C + 21)
33+
} else {
34+
sd_app_evt_wait()
35+
}
36+
} else {
37+
// SoftDevice is disabled so we can sleep normally.
38+
arm.Asm("wfe")
39+
}
40+
}

src/runtime/runtime_nxpmk66f18.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,7 @@ func abort() {
265265
machine.PollUART(&machine.UART2)
266266
}
267267
}
268+
269+
func waitForEvents() {
270+
arm.Asm("wfe")
271+
}

src/runtime/runtime_stm32.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package runtime
44

5+
import "device/arm"
6+
57
type timeUnit int64
68

79
func postinit() {}
@@ -12,3 +14,7 @@ func main() {
1214
run()
1315
abort()
1416
}
17+
18+
func waitForEvents() {
19+
arm.Asm("wfe")
20+
}

0 commit comments

Comments
 (0)