diff --git a/cores/nRF5/WVariant.h b/cores/nRF5/WVariant.h index c56d249d..61ce293d 100644 --- a/cores/nRF5/WVariant.h +++ b/cores/nRF5/WVariant.h @@ -22,6 +22,7 @@ #include #include "nrf.h" #include "nrf_soc.h" +#include "nrf_sdm.h" #ifdef __cplusplus extern "C" { diff --git a/cores/nRF5/wiring.c b/cores/nRF5/wiring.c index 0da57c46..85f37efd 100644 --- a/cores/nRF5/wiring.c +++ b/cores/nRF5/wiring.c @@ -20,6 +20,7 @@ #include #include "Arduino.h" +#include "nrf.h" #ifdef __cplusplus extern "C" { @@ -69,6 +70,56 @@ void enterOTADfu(void) NVIC_SystemReset(); } +void waitForEvent(void) +{ +#if 0 + // Set bit 7 and bits 4..0 in the mask to one (0x ...00 1001 1111) + enum { FPU_EXCEPTION_MASK = 0x0000009F }; + + /* Clear exceptions and PendingIRQ from the FPU unit */ + __set_FPSCR(__get_FPSCR() & ~(FPU_EXCEPTION_MASK)); + (void) __get_FPSCR(); + NVIC_ClearPendingIRQ(FPU_IRQn); +#endif + + uint8_t sd_en; + (void) sd_softdevice_is_enabled(&sd_en); + + if ( sd_en ) + { + (void) sd_app_evt_wait(); + }else + { + // SoftDevice is not enabled. + __WFE(); + __SEV(); // Clear Event Register. + __WFE(); + } +} + + +void systemOff(uint32_t pin, uint8_t wake_logic) +{ +// for(int i=0; i<8; i++) +// { +// NRF_POWER->RAM[i].POWERCLR = 0x03UL; +// } + + pinMode(pin, wake_logic ? INPUT_PULLDOWN : INPUT_PULLUP); + NRF_GPIO->PIN_CNF[pin] |= ((uint32_t) (wake_logic ? GPIO_PIN_CNF_SENSE_High : GPIO_PIN_CNF_SENSE_Low) << GPIO_PIN_CNF_SENSE_Pos); + + uint8_t sd_en; + (void) sd_softdevice_is_enabled(&sd_en); + + // Enter System OFF state + if ( sd_en ) + { + sd_power_system_off(); + }else + { + NRF_POWER->SYSTEMOFF = 1; + } +} #ifdef __cplusplus } diff --git a/cores/nRF5/wiring.h b/cores/nRF5/wiring.h index 6eaf6588..7370f837 100644 --- a/cores/nRF5/wiring.h +++ b/cores/nRF5/wiring.h @@ -22,34 +22,17 @@ extern "C" { #endif -extern void init(void); - -extern void enterSerialDfu(void); -extern void enterOTADfu(void); - /* Bootloader version. Assigned by init(). * - value of 0x000500 is version 0.5.0 */ extern uint32_t bootloaderVersion; -// Hint instruction to tell CPU go to low power mode -#if 0 -#define waitForEvent() __WFE() -#else -ATTR_ALWAYS_INLINE static inline void waitForEvent(void) -{ - // Set bit 7 and bits 4..0 in the mask to one (0x ...00 1001 1111) - enum { FPU_EXCEPTION_MASK = 0x0000009F }; +extern void init(void); - /* Clear exceptions and PendingIRQ from the FPU unit */ - __set_FPSCR(__get_FPSCR() & ~(FPU_EXCEPTION_MASK)); - (void) __get_FPSCR(); - NVIC_ClearPendingIRQ(FPU_IRQn); - - /* Call SoftDevice Wait For event */ - (void) sd_app_evt_wait(); -} -#endif +void enterSerialDfu(void); +void enterOTADfu(void); +void waitForEvent(void); +void systemOff(uint32_t pin, uint8_t wake_logic); #ifdef __cplusplus }