diff --git a/src/machine/machine_nrf_bare.go b/src/machine/machine_nrf_bare.go index b94886ed9..fc0892853 100644 --- a/src/machine/machine_nrf_bare.go +++ b/src/machine/machine_nrf_bare.go @@ -7,3 +7,7 @@ package machine func GetRNG() (ret uint32, err error) { return getRNG() } + +func isSoftDeviceEnabled() bool { + return false +} diff --git a/src/machine/machine_nrf_sd.go b/src/machine/machine_nrf_sd.go index b816e62ee..b8fc781eb 100644 --- a/src/machine/machine_nrf_sd.go +++ b/src/machine/machine_nrf_sd.go @@ -11,9 +11,8 @@ import ( // avoid a heap allocation in GetRNG. var ( - softdeviceEnabled uint8 - bytesAvailable uint8 - buf [4]uint8 + bytesAvailable uint8 + buf [4]uint8 errNoSoftDeviceSupport = errors.New("rng: softdevice not supported on this device") errNotEnoughRandomData = errors.New("rng: not enough random data available") @@ -24,9 +23,7 @@ var ( func GetRNG() (ret uint32, err error) { // First check whether the SoftDevice is enabled. // sd_rand_application_bytes_available_get cannot be called when the SoftDevice is not enabled. - arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled - - if softdeviceEnabled == 0 { + if !isSoftDeviceEnabled() { return getRNG() } @@ -57,3 +54,8 @@ func GetRNG() (ret uint32, err error) { return uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24, nil } + +// This function is defined in the runtime, but we need it too. +// +//go:linkname isSoftDeviceEnabled runtime.isSoftDeviceEnabled +func isSoftDeviceEnabled() bool diff --git a/src/runtime/runtime_nrf_softdevice.go b/src/runtime/runtime_nrf_softdevice.go index 3fb6550ba..849ecb507 100644 --- a/src/runtime/runtime_nrf_softdevice.go +++ b/src/runtime/runtime_nrf_softdevice.go @@ -13,17 +13,24 @@ func sd_app_evt_wait() // This is a global variable to avoid a heap allocation in waitForEvents. var softdeviceEnabled uint8 +// Check whether the SoftDevice is currently enabled. +// This function is also called from the machine package, so the signature has +// to stay consistent. +func isSoftDeviceEnabled() bool { + // First check whether the SoftDevice is enabled. Unfortunately, + // sd_app_evt_wait cannot be called when the SoftDevice is not enabled. + arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled + + return softdeviceEnabled != 0 +} + func waitForEvents() { // Call into the SoftDevice to sleep. This is necessary here because a // normal wfe will not put the chip in low power mode (it still consumes // 500µA-1mA). It is really needed to call sd_app_evt_wait for low power // consumption. - // First check whether the SoftDevice is enabled. Unfortunately, - // sd_app_evt_wait cannot be called when the SoftDevice is not enabled. - arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled - - if softdeviceEnabled != 0 { + if isSoftDeviceEnabled() { // Now pick the appropriate SVCall number. Hopefully they won't change // in the future with a different SoftDevice version. if nrf.Device == "nrf51" {