<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>RTC and sleep mode</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/44750/rtc-and-sleep-mode</link><description>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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 22 Mar 2019 07:22:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/44750/rtc-and-sleep-mode" /><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/177719?ContentTypeID=1</link><pubDate>Fri, 22 Mar 2019 07:22:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b1c95ef-3a89-4862-bd75-78888a6f82fb</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Daniel&lt;/p&gt;
&lt;p&gt;Do you have an nRF52840 DK you can test on for reference?&lt;/p&gt;
&lt;p&gt;If not, can you send me the code so I can test it on my end?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/177149?ContentTypeID=1</link><pubDate>Tue, 19 Mar 2019 16:48:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:77eb182d-fabc-4bdb-a626-ebba079f2aa9</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I am working on disabling the RAM and have found the following:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;for (int i = 0; i &amp;lt; 8; i++) {&lt;br /&gt; sd_power_ram_power_set(i,&lt;br /&gt; (POWER_RAM_POWER_S0POWER_Off &amp;lt;&amp;lt; POWER_RAM_POWER_S0POWER_Pos)&lt;br /&gt; | (POWER_RAM_POWER_S1POWER_Off &amp;lt;&amp;lt; POWER_RAM_POWER_S1POWER_Pos)&lt;br /&gt; | (POWER_RAM_POWER_S0RETENTION_Off &amp;lt;&amp;lt; POWER_RAM_POWER_S0RETENTION_Pos)&lt;br /&gt; | (POWER_RAM_POWER_S1RETENTION_Off &amp;lt;&amp;lt; POWER_RAM_POWER_S1RETENTION_Pos));&lt;br /&gt; }&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However, I tried this and see no difference when it comes to the power consumption. Still around 5.5uA.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Am I doing something wrong?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/176640?ContentTypeID=1</link><pubDate>Mon, 18 Mar 2019 09:42:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:474da0a7-c693-4cff-8d57-73fb503bcd58</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Daniel&lt;/p&gt;
&lt;p&gt;Do you also have an nRF52840 DK to test on, or do you only have the dongle?&lt;/p&gt;
&lt;p&gt;Most likely the dongle is less power optimized, but I would have to do some digging internally.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;By default all the RAM is on in system ON idle mode, so most likely you are not disabling anything.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;RAM can be disabled by using the&amp;nbsp;&lt;span&gt;sd_power_ram_power_set(..) function, as documented&amp;nbsp;&lt;a href="https://www.nordicsemi.com/DocLib/Content/SoftDevice_API_Doc/S140/v6-1-0/group_nrf_soc_functions?1864#ga9f1c3f64a4c56f2f6109e214cac02912"&gt;here&lt;/a&gt;&amp;nbsp;and in chapter 5.3.7.22 on page 79 of the nRF52840_PS_v1.1 &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;According to the comments in your code internal LF clock is used, which is fine power wise.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards&lt;br /&gt;Torbjørn&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/176458?ContentTypeID=1</link><pubDate>Fri, 15 Mar 2019 14:40:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:77777c2a-4439-4854-b374-8395c69e55ee</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Hi, to answer your questions:&lt;br /&gt;&lt;br /&gt;I am using the dongle. I am measuring current between a battery and the nRF supply. I have changed the USB jumpers to do this. No other hardware connected.&lt;/p&gt;
&lt;p&gt;I do not think I am disabling any RAM. Not sure on how to do this. How can I check my LFCLK.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;My code is all up there ^^&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/176339?ContentTypeID=1</link><pubDate>Fri, 15 Mar 2019 08:08:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7022aaf7-d392-451a-9489-def59db653f9</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Daniel&lt;/p&gt;
&lt;p&gt;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.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Do you know how much RAM you have enabled, and which LF clock source you are using?&lt;/p&gt;
&lt;p&gt;Are you just using a standard DK, and do you have any custom hardware connected?&lt;/p&gt;
&lt;p&gt;How are you measuring the current consumption?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/176232?ContentTypeID=1</link><pubDate>Thu, 14 Mar 2019 14:25:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e9ba3fa3-87b5-444c-8351-366f042684b4</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Yes, that seems to do the trick. I changed it to use RTC2.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I still see a current draw of about 5uA. Can I decrease this down further in this mode?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/176094?ContentTypeID=1</link><pubDate>Thu, 14 Mar 2019 08:24:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5207ffa2-29eb-4686-a1a7-cba77e791e87</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Daniel&lt;/p&gt;
&lt;p&gt;The SoftDevice uses RTC0. Looking at the code it seems you are also trying to enable RTC0.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Can you try to change it to RTC1 or RTC2 and see if it works better?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/175794?ContentTypeID=1</link><pubDate>Tue, 12 Mar 2019 23:04:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e803a78b-0903-4c97-9048-415cbe500013</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Confirmed. Something is asserting or returning an invalid error code in&amp;nbsp;rtc_config()&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Am I doing something wrong? Ie. setting up timers twice, rtc conflicts&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I cannot debug to the best of my ability as I only have the dongle with me right now.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC and sleep mode</title><link>https://devzone.nordicsemi.com/thread/175793?ContentTypeID=1</link><pubDate>Tue, 12 Mar 2019 22:25:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3e33c649-fce8-4a99-b55a-ce677f86b8f5</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;My code:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;ble.h&amp;quot;
#include &amp;quot;ble_err.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;ble_srv_common.h&amp;quot;
#include &amp;quot;ble_advdata.h&amp;quot;
#include &amp;quot;ble_conn_params.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;nrf_sdh_soc.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;ble_nus.h&amp;quot; // source for Nordic UART service
#include &amp;quot;nrf_ble_gatt.h&amp;quot;
#include &amp;quot;nrf_ble_qwr.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot; // library to drive GPIOs
#include &amp;quot;nrf_drv_rtc.h&amp;quot; // library to utilize RTC
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#include &amp;quot;nrf_drv_timer.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot; // nrf_delay_ms(5000);

#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#define ADVERTISING_LED                 BSP_BOARD_LED_0                         /**&amp;lt; Is on when device is advertising. */
#define CONNECTED_LED                   BSP_BOARD_LED_1                         /**&amp;lt; Is on when device has connected. */

/**&amp;lt; 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                 &amp;quot;ABB_LOW&amp;quot;
#define DEVICE_NAME_MED                 &amp;quot;ABB_MED&amp;quot;
#define DEVICE_NAME_HIGH                &amp;quot;ABB_HIG&amp;quot;
#define DEVICE_NAME_DEF                 &amp;quot;ABB_DEF&amp;quot;
#define DEVICE_NAME_ERR                 &amp;quot;ABB_ERR&amp;quot;

// 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                                       /**&amp;lt; Application&amp;#39;s BLE observer priority. You shouldn&amp;#39;t need to modify this value. */
#define APP_BLE_CONN_CFG_TAG            1                                       /**&amp;lt; A tag identifying the SoftDevice BLE configuration. */

#define APP_ADV_INTERVAL                64                                      /**&amp;lt; 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)        /**&amp;lt; Minimum acceptable connection interval (0.5 seconds). */
#define MAX_CONN_INTERVAL               MSEC_TO_UNITS(200, UNIT_1_25_MS)        /**&amp;lt; Maximum acceptable connection interval (1 second). */
#define SLAVE_LATENCY                   0                                       /**&amp;lt; Slave latency. */
#define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)         /**&amp;lt; Connection supervisory time-out (4 seconds). */

#define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(20000)                  /**&amp;lt; 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)                   /**&amp;lt; Time between each call to sd_ble_gap_conn_param_update after the first call (5 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT    3                                       /**&amp;lt; Number of attempts before giving up the connection parameter negotiation. */

#define DEAD_BEEF                       0xDEADBEEF                              /**&amp;lt; 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;        /**&amp;lt; 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);                               /**&amp;lt; BLE NUS service instance. */
//BLE_LBS_DEF(m_lbs);                                                           /**&amp;lt; LED Button Service instance. */
NRF_BLE_GATT_DEF(m_gatt);                                                       /**&amp;lt; GATT module instance. */
NRF_BLE_QWR_DEF(m_qwr);                                                         /**&amp;lt; Context for the Queued Write module.*/

static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;                        /**&amp;lt; Handle of the current connection. */

static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;                   /**&amp;lt; Advertising handle used to identify an advertising set. */
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];                    /**&amp;lt; Buffer for storing an encoded advertising set. */
static uint8_t m_enc_scan_response_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];         /**&amp;lt; 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(&amp;amp;sec_mode);

    err_code = sd_ble_gap_device_name_set(&amp;amp;sec_mode,
                                          (const uint8_t *)DEVICE_NAME_DEF,
                                          strlen(DEVICE_NAME_DEF));
    APP_ERROR_CHECK(err_code);

    memset(&amp;amp;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(&amp;amp;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-&amp;gt;conn_handle) &amp;amp;&amp;amp; (p_evt-&amp;gt;evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
    {
        m_ble_nus_max_data_len = p_evt-&amp;gt;params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
        NRF_LOG_INFO(&amp;quot;Data len is set to 0x%X(%d)&amp;quot;, m_ble_nus_max_data_len, m_ble_nus_max_data_len);
    }
    NRF_LOG_DEBUG(&amp;quot;ATT MTU exchange completed. central 0x%x peripheral 0x%x&amp;quot;,
                  p_gatt-&amp;gt;att_mtu_desired_central,
                  p_gatt-&amp;gt;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(&amp;amp;m_gatt, gatt_evt_handler);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_gatt_att_mtu_periph_set(&amp;amp;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(&amp;amp;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(&amp;amp;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(&amp;amp;advdata, m_adv_data.adv_data.p_data, &amp;amp;m_adv_data.adv_data.len);
    APP_ERROR_CHECK(err_code);

    err_code = ble_advdata_encode(&amp;amp;srdata, m_adv_data.scan_rsp_data.p_data, &amp;amp;m_adv_data.scan_rsp_data.len);
    APP_ERROR_CHECK(err_code);

    // Set advertising parameters.
    memset(&amp;amp;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(&amp;amp;m_adv_handle, &amp;amp;m_adv_data, &amp;amp;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-&amp;gt;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-&amp;gt;params.rx_data.p_data, &amp;quot;red&amp;quot;, 3) == 0)
        {
            bsp_board_led_on(1);
            ble_nus_data_send(&amp;amp;m_nus, &amp;quot;one&amp;quot;, &amp;amp;len, m_conn_handle);
        }
        else if(strncmp(p_evt-&amp;gt;params.rx_data.p_data, &amp;quot;green&amp;quot;, 5) == 0)
        {
            bsp_board_led_on(2);
            ble_nus_data_send(&amp;amp;m_nus, &amp;quot;two&amp;quot;, &amp;amp;len, m_conn_handle);
        }
        else if(strncmp(p_evt-&amp;gt;params.rx_data.p_data, &amp;quot;blue&amp;quot;, 4) == 0)
        {
            bsp_board_led_on(3);
            ble_nus_data_send(&amp;amp;m_nus, &amp;quot;thr&amp;quot;, &amp;amp;len, m_conn_handle);
        }

        // random test
        if(p_evt-&amp;gt;params.rx_data.length == 3 || p_evt-&amp;gt;params.rx_data.length == 11){
            bsp_board_led_on(0);
        }

        // loopback test
        ble_nus_data_send(&amp;amp;m_nus, p_evt-&amp;gt;params.rx_data.p_data, &amp;amp;p_evt-&amp;gt;params.rx_data.length, m_conn_handle);

        // Turn on LED based on length
        //uint8_t num = (int) strtol(&amp;amp;p_evt-&amp;gt;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(&amp;amp;m_qwr, &amp;amp;qwr_init);
    APP_ERROR_CHECK(err_code);

    // Initialize Nordic UART Service
    memset(&amp;amp;nus_init, 0, sizeof(nus_init));
    nus_init.data_handler = nus_data_handler;

    err_code = ble_nus_init(&amp;amp;m_nus, &amp;amp;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-&amp;gt;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(&amp;amp;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(&amp;amp;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-&amp;gt;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-&amp;gt;evt.gap_evt.conn_handle;
            err_code = nrf_ble_qwr_conn_handle_assign(&amp;amp;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-&amp;gt;evt.gap_evt.conn_handle, &amp;amp;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-&amp;gt;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-&amp;gt;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, &amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    // Enable BLE stack.
    err_code = nrf_sdh_ble_enable(&amp;amp;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(&amp;amp;sec_mode);
    
    // Set the device name
    switch(status)
    {
        case STATUS_LOW:
            sd_ble_gap_device_name_set(&amp;amp;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(&amp;amp;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(&amp;amp;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(&amp;amp;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(&amp;amp;advdata, m_adv_data.adv_data.p_data, &amp;amp;m_adv_data.adv_data.len);
    sd_ble_gap_adv_set_configure(&amp;amp;m_adv_handle, &amp;amp;m_adv_data, &amp;amp;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 &amp;amp;&amp;amp; stateMed == false)
    {
        return STATUS_LOW;
    }
    else if(stateLow == true &amp;amp;&amp;amp; stateMed == false)
    {
        return STATUS_MED;
    }
    else if(stateLow == true &amp;amp;&amp;amp; 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)                                        /**&amp;lt; Get Compare event COMPARE_TIME seconds after the counter starts from 0. */

const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**&amp;lt; 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(&amp;amp;rtc, &amp;amp;config, rtc_handler);
    APP_ERROR_CHECK(err_code);

    //Enable tick event &amp;amp; interrupt
    nrf_drv_rtc_tick_enable(&amp;amp;rtc,true);

    //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
    err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc,0,COMPARE_COUNTERTIME * 8,true);
    APP_ERROR_CHECK(err_code);

    //Power on RTC instance
    nrf_drv_rtc_enable(&amp;amp;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);
//        }
//    }
}

&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>