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();

Parents
  • 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:

    __SEV();
    __WFE();
    __WFE();
    

    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.

Reply
  • 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:

    __SEV();
    __WFE();
    __WFE();
    

    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.

Children
  • From the example that you sent, this is the line "NRF_POWER->SYSTEMOFF = 1;" that make the system go into sleep? and what is this line

    NRF_POWER->RAMON = POWER_RAMON_ONRAM0_RAM0On   << POWER_RAMON_ONRAM0_Pos
                     | POWER_RAMON_ONRAM1_RAM1On   << POWER_RAMON_ONRAM1_Pos
                     | POWER_RAMON_OFFRAM0_RAM0Off << POWER_RAMON_OFFRAM0_Pos
                     | POWER_RAMON_OFFRAM1_RAM1Off << POWER_RAMON_OFFRAM1_Pos;
    

    doing? Kindly advice

  • Btw, how the sleep is disable too?

  • "NRF_POWER->SYSTEMOFF = 1" will put the chip in system OFF sleep mode, which means that the chip will only wake up on gpio (this needs to be set up, like in the example). When the chip wakes up from system OFF sleep mode, the system will do a full reset. The RAMON part is to specify which RAM blocks should be on when system is ON, and which RAM blocks should be retained when the system goes to OFF mode. See the Reference Manual for more information on this part. The example configures 16 KB of RAM to be enabled in ON mode and retained in OFF mode.

    I don't know what you mean by disable sleep. There is no function for this as it is not needed. If you don't want to go to sleep, don't call __WFE/sd_app_evt_wait() or NRF_POWER->SYSTEMOFF = 1.

  • 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()?

  • 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.

Related