This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

RTC and sleep mode

Hi everyone,

I am developing a quick evaluation demo for a low-power sensor node which consists of the following steps:

- read a couple of GPIOs

- set advertising name based on their status and advertise for X amount of seconds

- Once advertising is done, setup RTC/ other timer to generate an interrupt after Y time

- Go to lowest sleep state while keeping the timer on

- Wake up and start the process again

Does anyone know how to do the last three steps? Note that this module may be put to sleep for weeks for the application.

I am using the S140 device.

Thank you all

  • I have been able to accomplish this (mostly). Unfortunately, as soon as I enable the RTC, my system restarts. Does anyone know why? I am using a mix of the ble blinky and rtc examples.

    My code:

    #include <stdint.h>
    #include <string.h>
    #include "nordic_common.h"
    #include "nrf.h"
    #include "app_error.h"
    #include "ble.h"
    #include "ble_err.h"
    #include "ble_hci.h"
    #include "ble_srv_common.h"
    #include "ble_advdata.h"
    #include "ble_conn_params.h"
    #include "nrf_sdh.h"
    #include "nrf_sdh_ble.h"
    #include "nrf_sdh_soc.h"
    #include "boards.h"
    #include "app_timer.h"
    #include "ble_nus.h" // source for Nordic UART service
    #include "nrf_ble_gatt.h"
    #include "nrf_ble_qwr.h"
    #include "nrf_pwr_mgmt.h"
    #include "nrf_gpio.h" // library to drive GPIOs
    #include "nrf_drv_rtc.h" // library to utilize RTC
    #include "nrf_drv_clock.h"
    #include "nrf_drv_timer.h"
    #include "nrf_delay.h" // nrf_delay_ms(5000);
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #define ADVERTISING_LED                 BSP_BOARD_LED_0                         /**< Is on when device is advertising. */
    #define CONNECTED_LED                   BSP_BOARD_LED_1                         /**< Is on when device has connected. */
    
    /**< Name of device. Will be included in the advertising data. */
    // Advertising names must all be the same length
    // Enventually, we can add some sort of ID to the name
    #define DEVICE_NAME_LOW                 "ABB_LOW"
    #define DEVICE_NAME_MED                 "ABB_MED"
    #define DEVICE_NAME_HIGH                "ABB_HIG"
    #define DEVICE_NAME_DEF                 "ABB_DEF"
    #define DEVICE_NAME_ERR                 "ABB_ERR"
    
    // GPIO definitions
    // For power consumption purposes, drive with one gpio ?
    #define GPIO_OUT_MED_LEVEL              22
    #define GPIO_IN_MED_LEVEL               20
    #define GPIO_OUT_LOW_LEVEL              15
    #define GPIO_IN_LOW_LEVEL               13
    
    #define APP_BLE_OBSERVER_PRIO           3                                       /**< Application's BLE observer priority. You shouldn't need to modify this value. */
    #define APP_BLE_CONN_CFG_TAG            1                                       /**< A tag identifying the SoftDevice BLE configuration. */
    
    #define APP_ADV_INTERVAL                64                                      /**< The advertising interval (in units of 0.625 ms; this value corresponds to 40 ms). */
    
    #define MIN_CONN_INTERVAL               MSEC_TO_UNITS(100, UNIT_1_25_MS)        /**< Minimum acceptable connection interval (0.5 seconds). */
    #define MAX_CONN_INTERVAL               MSEC_TO_UNITS(200, UNIT_1_25_MS)        /**< Maximum acceptable connection interval (1 second). */
    #define SLAVE_LATENCY                   0                                       /**< Slave latency. */
    #define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)         /**< Connection supervisory time-out (4 seconds). */
    
    #define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(20000)                  /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (15 seconds). */
    #define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000)                   /**< Time between each call to sd_ble_gap_conn_param_update after the first call (5 seconds). */
    #define MAX_CONN_PARAMS_UPDATE_COUNT    3                                       /**< Number of attempts before giving up the connection parameter negotiation. */
    
    #define DEAD_BEEF                       0xDEADBEEF                              /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
    
    static uint16_t   m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3;        /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
    
    // Ensure that SDK config has BLE_NUS_ENABLED enabled
    BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);                               /**< BLE NUS service instance. */
    //BLE_LBS_DEF(m_lbs);                                                           /**< LED Button Service instance. */
    NRF_BLE_GATT_DEF(m_gatt);                                                       /**< GATT module instance. */
    NRF_BLE_QWR_DEF(m_qwr);                                                         /**< Context for the Queued Write module.*/
    
    static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                        /**< Handle of the current connection. */
    
    static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;                   /**< Advertising handle used to identify an advertising set. */
    static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];                    /**< Buffer for storing an encoded advertising set. */
    static uint8_t m_enc_scan_response_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];         /**< Buffer for storing an encoded scan data. */
    static ble_gap_adv_params_t adv_params;
    static ble_advdata_t advdata;
    
    typedef enum {
        STATUS_LOW = 0,
        STATUS_MED,
        STATUS_HIGH,
        STATUS_ERR
    } deviceStatus_t;
    
    
    /**@brief Struct that contains pointers to the encoded advertising data. */
    static ble_gap_adv_data_t m_adv_data =
    {
        .adv_data =
        {
            .p_data = m_enc_advdata,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = m_enc_scan_response_data,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
    
        }
    };
    
    /**@brief Function for assert macro callback.
     *
     * @details This function will be called in case of an assert in the SoftDevice.
     *
     * @warning This handler is an example only and does not fit a final product. Add code here which should run if product crashes
     * @warning On assert from the SoftDevice, the system can only recover on reset.
     *
     * @param[in] line_num    Line number of the failing ASSERT call.
     * @param[in] p_file_name File name of the failing ASSERT call.
     */
    void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
    {
        app_error_handler(DEAD_BEEF, line_num, p_file_name);
    }
    
    /**@brief Initializes the timer module. */
    static void timers_init(void)
    {
        // Initialize timer module, making it use the scheduler
        ret_code_t err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief GAP initialization. This function sets up all the necessary GAP (Generic Access Profile) parameters of the device. */
    static void gap_params_init(void)
    {
        ret_code_t              err_code;
        ble_gap_conn_params_t   gap_conn_params;
        ble_gap_conn_sec_mode_t sec_mode;
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
    
        err_code = sd_ble_gap_device_name_set(&sec_mode,
                                              (const uint8_t *)DEVICE_NAME_DEF,
                                              strlen(DEVICE_NAME_DEF));
        APP_ERROR_CHECK(err_code);
    
        memset(&gap_conn_params, 0, sizeof(gap_conn_params));
    
        gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
        gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
        gap_conn_params.slave_latency     = SLAVE_LATENCY;
        gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;
    
        err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for handling events from the GATT library. */
    void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
    {
        if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
        {
            m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
            NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
        }
        NRF_LOG_DEBUG("ATT MTU exchange completed. central 0x%x peripheral 0x%x",
                      p_gatt->att_mtu_desired_central,
                      p_gatt->att_mtu_desired_periph);
    }
    
    /**@brief Function for initializing the GATT module. */
    static void gatt_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for initializing the Advertising functionality.
     *
     * @details Encodes the required advertising data and passes it to the stack.
     *          Also builds a structure to be passed to the stack when starting advertising.
     *
     * @param[in]   duration How long to advertise for (in 10ms units). Pass in 0 if unlimited advertising is required
     *
     */
    static void advertising_init(uint16_t duration)
    {
        ret_code_t    err_code;
        ble_advdata_t srdata;
    
        // Use the Nordic UART Service (NUS)
        // Set UUID type for the Nordic UART Service (vendor specific)
        ble_uuid_t adv_uuids[] = {{BLE_UUID_NUS_SERVICE, BLE_UUID_TYPE_VENDOR_BEGIN}};
    
        // Build and set advertising data.
        memset(&advdata, 0, sizeof(advdata));
    
        advdata.name_type          = BLE_ADVDATA_FULL_NAME;
        advdata.include_appearance = true;
        advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    
        memset(&srdata, 0, sizeof(srdata));
        srdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
        srdata.uuids_complete.p_uuids  = adv_uuids;
    
        err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
        err_code = ble_advdata_encode(&srdata, m_adv_data.scan_rsp_data.p_data, &m_adv_data.scan_rsp_data.len);
        APP_ERROR_CHECK(err_code);
    
        // Set advertising parameters.
        memset(&adv_params, 0, sizeof(adv_params));
    
        adv_params.primary_phy     = BLE_GAP_PHY_1MBPS;
        adv_params.duration        = duration;
        adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
        adv_params.p_peer_addr     = NULL;
        adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
        adv_params.interval        = APP_ADV_INTERVAL;
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for handling Queued Write Module errors.
     *
     * @details A pointer to this function will be passed to each service which may need to inform the
     *          application about an error.
     *
     * @param[in]   nrf_error   Error code containing information about what went wrong.
     */
    static void nrf_qwr_error_handler(uint32_t nrf_error)
    {
        APP_ERROR_HANDLER(nrf_error);
    }
    
    /**@brief Function for handling the data from the Nordic UART Service.
     *
     * @details This function will process the data received from the Nordic UART BLE Service
     *
     * @param[in] p_evt       Nordic UART Service event.
     */
    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
    
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
            // PARSE INCOMING STRING - DO SOMETHING
            bsp_board_leds_off();
            
            uint16_t len = 3;
    
            // Note: to do the following, must be 4 byte alligned???????
            if(strncmp(p_evt->params.rx_data.p_data, "red", 3) == 0)
            {
                bsp_board_led_on(1);
                ble_nus_data_send(&m_nus, "one", &len, m_conn_handle);
            }
            else if(strncmp(p_evt->params.rx_data.p_data, "green", 5) == 0)
            {
                bsp_board_led_on(2);
                ble_nus_data_send(&m_nus, "two", &len, m_conn_handle);
            }
            else if(strncmp(p_evt->params.rx_data.p_data, "blue", 4) == 0)
            {
                bsp_board_led_on(3);
                ble_nus_data_send(&m_nus, "thr", &len, m_conn_handle);
            }
    
            // random test
            if(p_evt->params.rx_data.length == 3 || p_evt->params.rx_data.length == 11){
                bsp_board_led_on(0);
            }
    
            // loopback test
            ble_nus_data_send(&m_nus, p_evt->params.rx_data.p_data, &p_evt->params.rx_data.length, m_conn_handle);
    
            // Turn on LED based on length
            //uint8_t num = (int) strtol(&p_evt->params.rx_data.p_data[0], (char **)NULL, 10);
            //bsp_board_led_on(num);
        }
    
    }
    
    /**@brief Function for initializing services that will be used by the application. */
    static void services_init(void)
    {
        ret_code_t         err_code;
        ble_nus_init_t     nus_init;
        nrf_ble_qwr_init_t qwr_init = {0};
    
        // Initialize Queued Write Module.
        qwr_init.error_handler = nrf_qwr_error_handler;
    
        err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
        APP_ERROR_CHECK(err_code);
    
        // Initialize Nordic UART Service
        memset(&nus_init, 0, sizeof(nus_init));
        nus_init.data_handler = nus_data_handler;
    
        err_code = ble_nus_init(&m_nus, &nus_init);
        APP_ERROR_CHECK(err_code);
    }
     
    /**@brief Function for handling the Connection Parameters Module.
     *
     * @details This function will be called for all events in the Connection Parameters Module that
     *          are passed to the application.
     *
     * @note All this function does is to disconnect. This could have been done by simply
     *       setting the disconnect_on_fail config parameter, but instead we use the event
     *       handler mechanism to demonstrate its use.
     *
     * @param[in] p_evt  Event received from the Connection Parameters Module.
     */
    static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
    {
        ret_code_t err_code;
    
        if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
        {
            err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
            APP_ERROR_CHECK(err_code);
        }
    }
    
    /**@brief Function for handling a Connection Parameters error.
     *
     * @param[in] nrf_error  Error code containing information about what went wrong.
     */
    static void conn_params_error_handler(uint32_t nrf_error)
    {
        APP_ERROR_HANDLER(nrf_error);
    }
    
    /**@brief Function for initializing the Connection Parameters module. */
    static void conn_params_init(void)
    {
        ret_code_t             err_code;
        ble_conn_params_init_t cp_init;
    
        memset(&cp_init, 0, sizeof(cp_init));
    
        cp_init.p_conn_params                  = NULL;
        cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
        cp_init.next_conn_params_update_delay  = NEXT_CONN_PARAMS_UPDATE_DELAY;
        cp_init.max_conn_params_update_count   = MAX_CONN_PARAMS_UPDATE_COUNT;
        cp_init.start_on_notify_cccd_handle    = BLE_GATT_HANDLE_INVALID;
        cp_init.disconnect_on_fail             = false;
        cp_init.evt_handler                    = on_conn_params_evt;
        cp_init.error_handler                  = conn_params_error_handler;
    
        err_code = ble_conn_params_init(&cp_init);
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for starting advertising. */
    static void advertising_start(void)
    {
        ret_code_t           err_code;
    
        err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for handling BLE events.
     *
     * @param[in]   p_ble_evt   Bluetooth stack event.
     * @param[in]   p_context   Unused.
     */
    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ret_code_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                bsp_board_led_on(CONNECTED_LED);
                bsp_board_led_off(ADVERTISING_LED);
                m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                bsp_board_led_off(CONNECTED_LED);
                m_conn_handle = BLE_CONN_HANDLE_INVALID;
                APP_ERROR_CHECK(err_code);
                advertising_start();
                break;
    
            case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
                // Pairing not supported
                err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
                                                       BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP,
                                                       NULL,
                                                       NULL);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
            {
                ble_gap_phys_t const phys =
                {
                    .rx_phys = BLE_GAP_PHY_AUTO,
                    .tx_phys = BLE_GAP_PHY_AUTO,
                };
                err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
                APP_ERROR_CHECK(err_code);
            } break;
    
            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                // No system attributes have been stored.
                err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTC_EVT_TIMEOUT:
                // Disconnect on GATT Client timeout event.
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_TIMEOUT:
                // Disconnect on GATT Server timeout event.
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }
    
    /**@brief Function for initializing the BLE stack. Initializes the SoftDevice and the BLE event interrupt. */
    static void ble_stack_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_sdh_enable_request();
        APP_ERROR_CHECK(err_code);
    
        // Configure the BLE stack using the default settings.
        // Fetch the start address of the application RAM.
        uint32_t ram_start = 0;
        err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
        APP_ERROR_CHECK(err_code);
    
        // Enable BLE stack.
        err_code = nrf_sdh_ble_enable(&ram_start);
        APP_ERROR_CHECK(err_code);
    
        // Register a handler for BLE events.
        NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
    }
    
    /**@brief Function for initializing power management. */
    static void power_management_init(void)
    {
        ret_code_t err_code;
        err_code = nrf_pwr_mgmt_init();
        APP_ERROR_CHECK(err_code);
    }
    
    /**@brief Function for setting up the nRF GPIO pins. */
    static void gpioSetup(void)
    {
        // Setup 2 GPIO pins as outputs
        nrf_gpio_cfg_output(GPIO_OUT_MED_LEVEL);
        nrf_gpio_cfg_output(GPIO_OUT_LOW_LEVEL);
        
        // Drive the GPIOs high
        nrf_gpio_pin_set(GPIO_OUT_MED_LEVEL);
        nrf_gpio_pin_set(GPIO_OUT_LOW_LEVEL);
    
        // Setup 2 GPIO pins as inputs
        nrf_gpio_cfg_input(GPIO_IN_MED_LEVEL, NRF_GPIO_PIN_PULLDOWN);
        nrf_gpio_cfg_input(GPIO_IN_LOW_LEVEL, NRF_GPIO_PIN_PULLDOWN);
    }
    
    /**@brief Function for setting the device name based on a status
     *
     * @param[in]   status   Device status
     */
    static void setDeviceName(deviceStatus_t status)
    {
        ble_gap_conn_sec_mode_t sec_mode;
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
        
        // Set the device name
        switch(status)
        {
            case STATUS_LOW:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_LOW, strlen(DEVICE_NAME_LOW));
                bsp_board_led_on(BSP_BOARD_LED_1);
                break;
            case STATUS_MED:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_MED, strlen(DEVICE_NAME_MED));
                bsp_board_led_on(BSP_BOARD_LED_3);
                break;
            case STATUS_HIGH:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_HIGH, strlen(DEVICE_NAME_HIGH));
                bsp_board_led_on(BSP_BOARD_LED_2);
                break;
            case STATUS_ERR:
                sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME_ERR, strlen(DEVICE_NAME_ERR));
                bsp_board_led_on(BSP_BOARD_LED_0);
                break;   
        }
    
        // Encode the advertising packet and start advertising
        ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
        sd_ble_gap_adv_stop(m_adv_handle);
        sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    }
    
    /**@brief Function for getting the device status
     *
     * @param[out]   status   Device status
     */
    static deviceStatus_t getDeviceStatus(void)
    {
        bool stateLow  = nrf_gpio_pin_read(GPIO_IN_LOW_LEVEL);
        bool stateMed = nrf_gpio_pin_read(GPIO_IN_MED_LEVEL);
    
        // Return status based on the status of the two connections
        if(stateLow == false && stateMed == false)
        {
            return STATUS_LOW;
        }
        else if(stateLow == true && stateMed == false)
        {
            return STATUS_MED;
        }
        else if(stateLow == true && stateMed == true)
        {
            return STATUS_HIGH;
        }
        else
        {
            // Not a valid GPIO combination
            // For this to occur, the LOW connection must be disconnected while the MED connection is still connected
            return STATUS_ERR;
        }
    }
    
    
    
    
    
    #define COMPARE_COUNTERTIME  (3UL)                                        /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
    
    const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
    
    /** @brief: Function for handling the RTC0 interrupts.
     * Triggered on TICK and COMPARE0 match.
     */
    static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
    {
        if (int_type == NRF_DRV_RTC_INT_COMPARE0)
        {
            bsp_board_led_invert(1);
        }
        else if (int_type == NRF_DRV_RTC_INT_TICK)
        {
            bsp_board_led_invert(0);
        }
    }
    
    /** @brief Function starting the internal LFCLK XTAL oscillator.
     */
    static void lfclk_config(void)
    {
        ret_code_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_clock_lfclk_request(NULL);
    }
    
    /** @brief Function initialization and configuration of RTC driver instance.
     */
    static void rtc_config(void)
    {
        uint32_t err_code;
    
        //Initialize RTC instance
        nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
        config.prescaler = 4095;
        err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
        APP_ERROR_CHECK(err_code);
    
        //Enable tick event & interrupt
        nrf_drv_rtc_tick_enable(&rtc,true);
    
        //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
        err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
        APP_ERROR_CHECK(err_code);
    
        //Power on RTC instance
        nrf_drv_rtc_enable(&rtc);
    }
    
    
    
    /**@brief Function for application main entry. */
    int main(void)
    {
        // Initialize.
        bsp_board_init(BSP_INIT_LEDS);
        timers_init();
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init(500);
        conn_params_init();
        // Start execution.
        advertising_start();
        gpioSetup();
    
        deviceStatus_t last = 10;
        deviceStatus_t current;
    
        // Wait on advertising to finish
        sd_app_evt_wait();
        nrf_gpio_pin_clear(GPIO_OUT_MED_LEVEL);
        nrf_gpio_pin_clear(GPIO_OUT_LOW_LEVEL);
    
        // Advertising done. Configure clock to interrupt us in X seconds
        bsp_board_led_on(BSP_BOARD_LED_0);
        nrf_delay_ms(5000);
        
        lfclk_config();
        rtc_config();
    
        // power down and wait for RTC interrupt
        while(1){
             sd_app_evt_wait();
        }
    
        // Enter main loop.
    //    while(1)
    //    {
    //        if (NRF_LOG_PROCESS() == false)
    //        {
    //            //nrf_pwr_mgmt_run(); // If there is no pending log operation, then sleep until next the next event occurs.
    //        }
    //        current = getDeviceStatus();
    //        if(last != current)
    //        {
    //            last = current;
    //            bsp_board_leds_off();
    //            setDeviceName(current);
    //        }
    //    }
    }
    
    

  • Confirmed. Something is asserting or returning an invalid error code in rtc_config()

    Am I doing something wrong? Ie. setting up timers twice, rtc conflicts

    I cannot debug to the best of my ability as I only have the dongle with me right now.

  • Hi Daniel

    The SoftDevice uses RTC0. Looking at the code it seems you are also trying to enable RTC0. 

    Can you try to change it to RTC1 or RTC2 and see if it works better?

    Best regards
    Torbjørn

  • Yes, that seems to do the trick. I changed it to use RTC2.

    I still see a current draw of about 5uA. Can I decrease this down further in this mode?

  • Hi Daniel

    Yes, 5uA sounds a bit hig. Depending on how much RAM you have enabled the system ON sleep current with RTC wakeup enabled should be in the 1.5-3.2 uA range. 

    Do you know how much RAM you have enabled, and which LF clock source you are using?

    Are you just using a standard DK, and do you have any custom hardware connected?

    How are you measuring the current consumption?

    Best regards
    Torbjørn

Related