<?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>NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/74340/nrf52832-bluetooth-uart-and-rtc2</link><description>Hello everyone, 
 I&amp;#39;ve been struggling to use the Bluetooth UART example and the RTC2 module but so far I haven&amp;#39;t succeeded. 
 Either I have Bluetooth, or RTC2, but not both. Here is how I&amp;#39;ve modified the UART example code: 
 
 
 
 I&amp;#39;m using nRF5_SDK_17</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 29 Apr 2021 14:06:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/74340/nrf52832-bluetooth-uart-and-rtc2" /><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/307523?ContentTypeID=1</link><pubDate>Thu, 29 Apr 2021 14:06:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:97b2c39c-e5fc-4a9a-85a5-7f691c9331d3</guid><dc:creator>L.B.</dc:creator><description>&lt;p&gt;Hi, again! You&amp;#39;re code works, indeed, I have to enable the RTC2 before the Bluetooth.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Note, however, that in order to stop and start the Bluetooth again, one must use&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;sd_softdevice_disable();&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;to stop it, then this procedure to start it:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    ret_code_t err;

    const nrf_clock_lf_cfg_t sd_lf_config =
    {
    .source       = NRF_SDH_CLOCK_LF_SRC,
    .rc_ctiv      = NRF_SDH_CLOCK_LF_RC_CTIV,
    .rc_temp_ctiv = NRF_SDH_CLOCK_LF_RC_TEMP_CTIV,
    .accuracy     = NRF_SDH_CLOCK_LF_ACCURACY
    };

    err = sd_softdevice_enable(&amp;amp;sd_lf_config, fault_handler);
    APP_ERROR_CHECK(err);

    // Configure the BLE stack using the default settings.
    // Fetch the start address of the application RAM.
    uint32_t ram_start = 0;
    err = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err);

    // Enable BLE stack.
    err = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err);

    advertising_start();
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So, here is an example program that will turn on the Bluetooth for 7 seconds, then turn it off for another 7 seconds, then turn it on again. During all this time, the RTC2 will remain operational.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2); /**&amp;lt; Declaring an instance of nrf_drv_rtc for RTC2. */

#define COMPARE_COUNTERTIME  (3UL)

/** @brief: Function for handling the RTC2 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)
    {
        nrf_gpio_pin_toggle(22);
    }
    else if (int_type == NRF_DRV_RTC_INT_TICK)
    {
    	nrf_gpio_pin_toggle(22);
    }
}

/** @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);
}

void fault_handler(uint32_t id, uint32_t pc, uint32_t info){

}

/**@brief Application main function.
 */
int main(void)
{
	volatile int i = 0;
    bool erase_bonds;

    lfclk_config();
    rtc_config();

    // Initialize.
    uart_init();
    log_init();
    timers_init();
    buttons_leds_init(&amp;amp;erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    nrf_gpio_cfg_output(22);

    // Start execution.
    printf(&amp;quot;\r\nUART started.\r\n&amp;quot;);
    NRF_LOG_INFO(&amp;quot;Debug logging for UART over RTT started.&amp;quot;);

    advertising_start();

    nrf_delay_ms(7000);

    sd_softdevice_disable();

    nrf_delay_ms(7000);

    ret_code_t err;

    const nrf_clock_lf_cfg_t sd_lf_config =
    {
    .source       = NRF_SDH_CLOCK_LF_SRC,
    .rc_ctiv      = NRF_SDH_CLOCK_LF_RC_CTIV,
    .rc_temp_ctiv = NRF_SDH_CLOCK_LF_RC_TEMP_CTIV,
    .accuracy     = NRF_SDH_CLOCK_LF_ACCURACY
    };

    err = sd_softdevice_enable(&amp;amp;sd_lf_config, fault_handler);
    APP_ERROR_CHECK(err);

    // Configure the BLE stack using the default settings.
    // Fetch the start address of the application RAM.
    uint32_t ram_start = 0;
    err = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err);

    // Enable BLE stack.
    err = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err);

    advertising_start();

    // Enter main loop.
    for (;;)
    {
       idle_state_handle();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Note: this code is not low-power because the UART is also working. If you don&amp;#39;t need it, turn it off.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Hope that this will help other folks in the forum.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you for your help, Jared!&lt;/p&gt;
&lt;p&gt;L. B.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/307101?ContentTypeID=1</link><pubDate>Tue, 27 Apr 2021 18:35:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:273d47d6-6713-47b7-9089-5a501f92802f</guid><dc:creator>L.B.</dc:creator><description>&lt;p&gt;OK, I will try this code later in the week /I&amp;#39;m out of office now/ and will write back to you with the result.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However, I suspect that if you:&lt;/p&gt;
&lt;p&gt;- wait a few seconds,&lt;/p&gt;
&lt;p&gt;- then do sd_softdevice_disable()&lt;/p&gt;
&lt;p&gt;- wait some more seconds&lt;/p&gt;
&lt;p&gt;- and then try to enable the bluetooth again&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;it might not work.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;L. B.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/307066?ContentTypeID=1</link><pubDate>Tue, 27 Apr 2021 14:13:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:411650af-4996-4bd0-8946-47119682e72a</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Not sure what you did wrong when you tried to merge it, but I&amp;#39;ve attached an example that has merged ble_app_uart together with the RTC example. I&amp;#39;ve tested it and verified that it works successfully. Notice that it uses LED 3 and 4 instead of 1 and 2 as in the original example, this is because they are already in use in the ble_app_uart example.&lt;/p&gt;
&lt;p&gt;Cheers&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/ble_5F00_app_5F00_uart_5F00_rtc.7z"&gt;devzone.nordicsemi.com/.../ble_5F00_app_5F00_uart_5F00_rtc.7z&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/306794?ContentTypeID=1</link><pubDate>Mon, 26 Apr 2021 12:17:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5a4c860-b8fa-4f3c-90b1-7842702d1e57</guid><dc:creator>L.B.</dc:creator><description>&lt;p&gt;Actually this is where the problem started from - I tried merging&lt;/p&gt;
&lt;p&gt;examples/ble_peripheral/ble_app_uart&lt;/p&gt;
&lt;p&gt;and &lt;/p&gt;
&lt;p&gt;examples/peripheral/rtc&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;But it didn&amp;#39;t work - I can see no RTC2 and no Bluetooth. For example this code builds but doesn&amp;#39;t work:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define COMPARE_COUNTERTIME 1
const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);

static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
	nrf_gpio_pin_toggle(22);
}

/** @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);
}

//!!!!!! THIS CODE DOESN&amp;#39;T WORK!!!!!!

/**@brief Application main function.
 */
int main(void)
{
    bool erase_bonds;

    // Initialize.
    uart_init();
    log_init();
    timers_init();
    buttons_leds_init(&amp;amp;erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    // Start execution.
    printf(&amp;quot;\r\nUART started.\r\n&amp;quot;);
    NRF_LOG_INFO(&amp;quot;Debug logging for UART over RTT started.&amp;quot;);
    advertising_start();

    nrf_gpio_cfg_output(22);	

    lfclk_config();
    rtc_config();

    // Enter main loop.
    for (;;)
    {
       idle_state_handle();
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m trying to achieve a very simple task - I want to be able to turn off and on the Bluetooth on demand, and I want at the same time an RTC to keep track of time (my application needs it) all the time. If you think I&amp;#39;m on the wrong track, please let me know ... maybe I&amp;#39;m not doing it right.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;L. B.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/306769?ContentTypeID=1</link><pubDate>Mon, 26 Apr 2021 11:26:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:654188ab-4f75-4bf5-9b34-7209befced8a</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Instead of writing directly to the peripheral. Can you try to use our RTC driver? It will take into consideration if the Softdevice is present and enabled. &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/rtc_example.html?cp=7_1_4_6_34"&gt;See the RTC example if you want an example on how to use the driver.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/306563?ContentTypeID=1</link><pubDate>Fri, 23 Apr 2021 13:43:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0a7a482-1e04-481c-9c9a-c043e5b4c4f7</guid><dc:creator>L.B.</dc:creator><description>&lt;p&gt;It did not work, neither with 6 nor with 7. I&amp;#39;m turning off the Bluetooth with sd_softdevice_disable( ) btw ... This way I can achieve 2 - 3 uA IDLE state + RTC2.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/306560?ContentTypeID=1</link><pubDate>Fri, 23 Apr 2021 13:40:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3788e4b2-9ef4-489f-8326-2a0ff6a0dcae</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Could you try decreasing the priority to 6 or 7?&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52832 Bluetooth UART and RTC2</title><link>https://devzone.nordicsemi.com/thread/306486?ContentTypeID=1</link><pubDate>Fri, 23 Apr 2021 08:47:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ba76e2b-3bdd-4dfb-972b-c17be19672a9</guid><dc:creator>L.B.</dc:creator><description>&lt;p&gt;Hello again!&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve just discovered, that if I comment out the RC oscillator initialization, the code works.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void RTC2_IRQHandler(void){
    if(NRF_RTC2-&amp;gt;EVENTS_COMPARE[0]){
        NRF_RTC2-&amp;gt;EVENTS_COMPARE[0] = 0;
        NRF_RTC2-&amp;gt;TASKS_CLEAR = 1;
	nrf_gpio_pin_toggle(22);
    }
}

/**@brief Application main function.
 */
int main(void)
{
    bool erase_bonds;

    // Initialize.
    uart_init();
    log_init();
    timers_init();
    buttons_leds_init(&amp;amp;erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    // Start execution.
    printf(&amp;quot;\r\nUART started.\r\n&amp;quot;);
    NRF_LOG_INFO(&amp;quot;Debug logging for UART over RTT started.&amp;quot;);
    advertising_start();

    nrf_gpio_cfg_output(22);	
 

/*  Works without this one
    NRF_CLOCK-&amp;gt;EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK-&amp;gt;LFCLKSRC = 0x00; 
    NRF_CLOCK-&amp;gt;TASKS_LFCLKSTART = 1;
    while(NRF_CLOCK-&amp;gt;EVENTS_LFCLKSTARTED == 0);*/
    
    NRF_RTC2-&amp;gt;PRESCALER = 0xFFF;
    NRF_RTC2-&amp;gt;EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
    NRF_RTC2-&amp;gt;INTENSET = RTC_INTENSET_COMPARE0_Msk;
    NRF_RTC2-&amp;gt;TASKS_CLEAR = 1;
    NRF_RTC2-&amp;gt;TASKS_START = 1;
    NRF_RTC2-&amp;gt;CC[0] = 8 * 1;
    NVIC_SetPriority(RTC2_IRQn, 3);
    NVIC_ClearPendingIRQ(RTC2_IRQn);
    NVIC_EnableIRQ(RTC2_IRQn);


    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However I cannot turn on and off the Bluetooth. Basically I want the RTC2 to work always, and just turn on and off the Bluetooth at specific periods.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;L. B.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>