github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/runtime_nrf_softdevice.go (about)

     1  //go: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  // This is a global variable to avoid a heap allocation in waitForEvents.
    14  var softdeviceEnabled uint8
    15  
    16  func waitForEvents() {
    17  	// Call into the SoftDevice to sleep. This is necessary here because a
    18  	// normal wfe will not put the chip in low power mode (it still consumes
    19  	// 500µA-1mA). It is really needed to call sd_app_evt_wait for low power
    20  	// consumption.
    21  
    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 {
    27  		// Now pick the appropriate SVCall number. Hopefully they won't change
    28  		// in the future with a different SoftDevice version.
    29  		if nrf.Device == "nrf51" {
    30  			// sd_app_evt_wait: SOC_SVC_BASE_NOT_AVAILABLE + 29
    31  			arm.SVCall0(0x2B + 29)
    32  		} else if nrf.Device == "nrf52" || nrf.Device == "nrf52840" || nrf.Device == "nrf52833" {
    33  			// sd_app_evt_wait: SOC_SVC_BASE_NOT_AVAILABLE + 21
    34  			arm.SVCall0(0x2C + 21)
    35  		} else {
    36  			sd_app_evt_wait()
    37  		}
    38  	} else {
    39  		// SoftDevice is disabled so we can sleep normally.
    40  		arm.Asm("wfe")
    41  	}
    42  }