# 5. Deep Sleep configuration¶

To configure DA14531 device to deep sleep mode, follow the steps mentioned below.

1. Open the proximity reporter project from: …projects/target_apps/ble_examples/prox_reporter/Keil_5
2. Open the file user_proxr.h which is under the user_app folder.
3. Define CFG_APP_GOTO_DEEP_SLEEP to select the sleep mode that the device enters after completion of advertisement.
#define CFG_APP_GOTO_DEEP_SLEEP

1. Select the trigger mechanism to wake-up from deep sleep mode by doing the following,
#if defined(CFG_APP_GOTO_DEEP_SLEEP)
/****************************************************************************************
* Deep Sleep mode and Wake-up from Deep Sleep Configuration                             *
* Selects the trigger mechanism to wake up from deep sleep.                             *
*                                                                                       *
* - CFG_DEEP_SLEEP_WAKEUP_POR       - Wake up from POR pin                              *
*                                                                                       *
* - CFG_DEEP_SLEEP_WAKEUP_GPIO      - Wake up from WakeUp Controller                    *
*                                                                                       *
* - CFG_DEEP_SLEEP_WAKEUP_RTC       - Wake up from RTC Timer (only in DA14531)          *
*                                                                                       *
* - CFG_DEEP_SLEEP_WAKEUP_TIMER1    - Wake up from Timer1 (only in DA14531)             *
*                                                                                       *
* NOTE:                                                                                 *
* A hardware reset or power cycle will always wake up the system from deep sleep.       *
****************************************************************************************/
#undef CFG_DEEP_SLEEP_WAKEUP_POR

#define CFG_DEEP_SLEEP_WAKEUP_GPIO

1. In the same file, configure the retention RAM memory to be retained. To power down any RAM block use PD_SYS_DOWN_RAM_OFF and PD_SYS_DOWN_RAM_ON to retain. When disabling all the RAM blocks, the code should be in Flash or OTP. Disabling RAM blocks will lower the sleep current however it will increase the energy needed to wake-up.
/****************************************************************************************
* Deep sleep mode configuration                                                        *
****************************************************************************************/
#define CFG_DEEP_SLEEP_RAM1            PD_SYS_DOWN_RAM_OFF
#define CFG_DEEP_SLEEP_RAM2            PD_SYS_DOWN_RAM_OFF
#define CFG_DEEP_SLEEP_RAM3            PD_SYS_DOWN_RAM_OFF
#define CFG_DEEP_SLEEP_PAD_LATCH_EN    false

1. In the file user_proxr.c by selecting CFG_DEEP_SLEEP_WAKEUP_GPIO, select the GPIO port_pin to wake the device from deep sleep, by doing this in file ,
#if defined (CFG_DEEP_SLEEP_WAKEUP_GPIO)
wkupct_enable_irq(WKUPCT_PIN_SELECT(GPIO_BUTTON_PORT, GPIO_BUTTON_PIN), // Select pin
WKUPCT_PIN_POLARITY(GPIO_BUTTON_PORT, GPIO_BUTTON_PIN, WKUPCT_PIN_POLARITY_LOW), // Polarity low
1, // 1 event
0); // debouncing time = 0
#endif

1. In the user_periph_setup.h, configure the GPIO_BUTTON_PORT and GPIO_BUTTON_PIN, like so where we have configured P0_5 to wake up the device from deep sleep,
/****************************************************************************************/
/* Button configuration                                                                 */
/****************************************************************************************/
#if defined (__DA14531__)
#define GPIO_BUTTON_PORT        GPIO_PORT_0
#define GPIO_BUTTON_PIN         GPIO_PIN_5
#else

1. To wake up using Power on Reset (POR), define CFG_DEEP_SLEEP_WAKEUP_POR and undefine the rest of the wake-up mechanism. Use the SW1/Reset button on the motherboard to wake up the device.
#define CFG_DEEP_SLEEP_WAKEUP_POR

1. To wake up using RTC (Real Time Clock) timer, define CFG_DEEP_SLEEP_WAKEUP_RTC and undefine the rest of the wake-up mechanism.
#if defined (__DA14531__)
#define CFG_DEEP_SLEEP_WAKEUP_RTC
#undef CFG_DEEP_SLEEP_WAKEUP_TIMER1
#endif


By defining this, the RTC is configured to generate an interrupt after 10 seconds. This would wake up the device from deep sleep mode. The time to interrupt can be modified by changing the alarm_time.sec in function configure_rtc_wakeup in user_proxr.c as shown below,

alarm_time.sec += 10;

1. To wake up using Timer1, define CFG_DEEP_SLEEP_WAKEUP_TIMER1 and undefine the rest of the wake-up mechanism.
#if defined (__DA14531__)
#undef CFG_DEEP_SLEEP_WAKEUP_RTC
#define CFG_DEEP_SLEEP_WAKEUP_TIMER1
#endif

1. Save and compile (F7) the project
2. Open our SmartSnippets toolbox and click on Power Profiler (available from our portal Dialog Semiconductor support portal.) and observe the BLE events and Deep sleep.

## 5.1. Measuring the deep sleep current¶

In order to measure the deep sleep current over a digital multimeter, increase the advertising interval in the proximity reporter project under user_config.h. Follow the steps mentioned below:

1. Open the file user_config.h which is under the user_config folder.

2. Change the default handlers configuration to advertise the device with a timeout, by configuring .adv_scenario to DEF_ADV_WITH_TIMEOUT and changing the timeout value to 3s for example, .advertise_period = MS_TO_TIMERUNITS(18000), as shown below, .. code:

/*
****************************************************************************************
*
* Default handlers configuration (applies only for @app_default_handlers.c)
*
****************************************************************************************
*/
static const struct default_handlers_configuration  user_default_hnd_conf = {
// Configure the advertise operation used by the default handlers
// Possible values:
//  - DEF_ADV_FOREVER
//  - DEF_ADV_WITH_TIMEOUT
.adv_scenario = DEF_ADV_FOREVER, //advertising forever

// Configure the advertise period in case of DEF_ADV_WITH_TIMEOUT.
// It is measured in timer units. Use MS_TO_TIMERUNITS macro to convert
// from milliseconds (ms) to timer units.
.advertise_period = MS_TO_TIMERUNITS(3000), //this is for 3s

// Configure the security start operation of the default handlers
// if the security is enabled (CFG_APP_SECURITY)
// Possible values:
//  - DEF_SEC_REQ_NEVER
//  - DEF_SEC_REQ_ON_CONNECT
.security_request_scenario = DEF_SEC_REQ_NEVER
};


This will configure the advertising period as 3s after which the device will enter the deep sleep mode. The device can wake-up from deep sleep mode by programming the trigger mechanism for wake-up as mentioned in the previous section.

1. Save and Compile the project
2. Follow the steps here in chapter 12 OTP Programmer or chapter 13 SPI Flash programmer to program the device either in Flash or OTP, and then boot from there.
1. Use a digital multimeter, connect the positive of multimeter to J9[4] and the negative to J9[3] of the motherboard, as shown in the figure below,

Figure 9 Deep sleep power measurement

1. Observe the deep sleep current value displayed in the meter. The typical numbers are mentioned below where the IBAT_LOW values are for the Boost mode and the IBAT_HIGH values are for Buck mode.

Figure 10 Deep sleep power numbers in buck and boost mode

1. To change to Boost mode, use the switch on the daughterboard and put it to L. On the motherboard, put the jumper on J4[1-2], as shown in sleep mode overview chapter.
2. Repeat the steps from 1 to 6