Skip to content

Commit 484ccf8

Browse files
aykevldeadprogram
authored andcommitted
nrf: refactor SoftDevice enabled check
Share the code with the runtime.
1 parent 5690204 commit 484ccf8

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/machine/machine_nrf_bare.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ package machine
77
func GetRNG() (ret uint32, err error) {
88
return getRNG()
99
}
10+
11+
func isSoftDeviceEnabled() bool {
12+
return false
13+
}

src/machine/machine_nrf_sd.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import (
1111

1212
// avoid a heap allocation in GetRNG.
1313
var (
14-
softdeviceEnabled uint8
15-
bytesAvailable uint8
16-
buf [4]uint8
14+
bytesAvailable uint8
15+
buf [4]uint8
1716

1817
errNoSoftDeviceSupport = errors.New("rng: softdevice not supported on this device")
1918
errNotEnoughRandomData = errors.New("rng: not enough random data available")
@@ -24,9 +23,7 @@ var (
2423
func GetRNG() (ret uint32, err error) {
2524
// First check whether the SoftDevice is enabled.
2625
// sd_rand_application_bytes_available_get cannot be called when the SoftDevice is not enabled.
27-
arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled
28-
29-
if softdeviceEnabled == 0 {
26+
if !isSoftDeviceEnabled() {
3027
return getRNG()
3128
}
3229

@@ -57,3 +54,8 @@ func GetRNG() (ret uint32, err error) {
5754

5855
return uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24, nil
5956
}
57+
58+
// This function is defined in the runtime, but we need it too.
59+
//
60+
//go:linkname isSoftDeviceEnabled runtime.isSoftDeviceEnabled
61+
func isSoftDeviceEnabled() bool

src/runtime/runtime_nrf_softdevice.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@ func sd_app_evt_wait()
1313
// This is a global variable to avoid a heap allocation in waitForEvents.
1414
var softdeviceEnabled uint8
1515

16+
// Check whether the SoftDevice is currently enabled.
17+
// This function is also called from the machine package, so the signature has
18+
// to stay consistent.
19+
func isSoftDeviceEnabled() bool {
20+
// First check whether the SoftDevice is enabled. Unfortunately,
21+
// sd_app_evt_wait cannot be called when the SoftDevice is not enabled.
22+
arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled
23+
24+
return softdeviceEnabled != 0
25+
}
26+
1627
func waitForEvents() {
1728
// Call into the SoftDevice to sleep. This is necessary here because a
1829
// normal wfe will not put the chip in low power mode (it still consumes
1930
// 500µA-1mA). It is really needed to call sd_app_evt_wait for low power
2031
// consumption.
2132

22-
// First check whether the SoftDevice is enabled. Unfortunately,
23-
// sd_app_evt_wait cannot be called when the SoftDevice is not enabled.
24-
arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled
25-
26-
if softdeviceEnabled != 0 {
33+
if isSoftDeviceEnabled() {
2734
// Now pick the appropriate SVCall number. Hopefully they won't change
2835
// in the future with a different SoftDevice version.
2936
if nrf.Device == "nrf51" {

0 commit comments

Comments
 (0)