Can someone explain to me how nordic nrf51822 sleep mode works?

Hi i'm a newbie in nordic chip. Can someone please explain to me that how to enable and disable sleep in nrf51822 and what kind of setup need to be done? Example in Arduino : set_sleep_mode(SLEEP_MODE_PWR_SAVE);
sleep_enable(); sleep_mode(); sleep_disable();

  • There are two sleep modes on nRF51822, System ON sleep mode and System OFF sleep mode. In System ON sleep mode the CPU will wake up on any event. In System OFF sleep mode the CPU can only wake up on gpio, high value or voltage input crossing a certain value.

    System On sleep mode is the most used one since all peripherals can be active. This sleep mode is entered with the function __WFE() which is short for Wait For Event. However you will often see this code in examples:


    This is because if the event flag is set when __WFE() is called, the function will clear the event flag and return immediately, therefore not go to sleep. __SEV() (Set EVent flag) sets the event flag, so __SEV() followed by __WFE() will clear the event flag without going to sleep. Then we are sure that in the next __WFE() the CPU will go to sleep. The SoftDevice function sd_app_evt_wait() will do the same as these calls and put the CPU to System ON sleep mode.

    sd_app_evt_wait() or the SEV WFE calls are often done inside the main while loop. After this call we can add code to be executed when the CPU wakes up from an event. You can also run code inside interrupts connected to the event, as these will run when the specific event happens and the system wakes up.

    See these examples on github.

  • I went into the code for sd_app_evt_wait() and the only command in it was a single __WFE(); Can you explain how it is doing the same thing as the __SEV(); __WFE(); __WFE(); without the SEV() or second WFE()?

Reply Children
  • It actually makes sense. Whoever invented that "__SEV(); __WFE(); __WFE();" sequence was seriously confused about the purpose of WFE, IMHO. I mean, if there is a pending event already, why would you hide it by calling SEV? And if there is no pending event then single WFE works exactly as expected.