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

NRF52832 Bluetooth UART and RTC2

Hello everyone,

I've been struggling to use the Bluetooth UART example and the RTC2 module but so far I haven't succeeded.

Either I have Bluetooth, or RTC2, but not both. Here is how I've modified the UART example code:

void RTC2_IRQHandler(void){
    if(NRF_RTC2->EVENTS_COMPARE[0]){
        NRF_RTC2->EVENTS_COMPARE[0] = 0;
        NRF_RTC2->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(&erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    // Start execution.
    printf("\r\nUART started.\r\n");
    NRF_LOG_INFO("Debug logging for UART over RTT started.");
    advertising_start();

    nrf_gpio_cfg_output(22);	
 
/*  This code alone works, but when added here
    it doesn't!

    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->LFCLKSRC = 0x00; 
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
    NRF_RTC2->PRESCALER = 0xFFF;
    NRF_RTC2->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
    NRF_RTC2->INTENSET = RTC_INTENSET_COMPARE0_Msk;
    NRF_RTC2->TASKS_CLEAR = 1;
    NRF_RTC2->TASKS_START = 1;
    NRF_RTC2->CC[0] = 8 * 1;
    NVIC_SetPriority(RTC2_IRQn, 3);
    NVIC_ClearPendingIRQ(RTC2_IRQn);
    NVIC_EnableIRQ(RTC2_IRQn);
*/



    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

I'm using nRF5_SDK_17.0.0_9d13099

Thank you in advance.

L. B.

  • Hello again!

    I've just discovered, that if I comment out the RC oscillator initialization, the code works.

    void RTC2_IRQHandler(void){
        if(NRF_RTC2->EVENTS_COMPARE[0]){
            NRF_RTC2->EVENTS_COMPARE[0] = 0;
            NRF_RTC2->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(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
    
        nrf_gpio_cfg_output(22);	
     
    
    /*  Works without this one
        NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
        NRF_CLOCK->LFCLKSRC = 0x00; 
        NRF_CLOCK->TASKS_LFCLKSTART = 1;
        while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);*/
        
        NRF_RTC2->PRESCALER = 0xFFF;
        NRF_RTC2->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
        NRF_RTC2->INTENSET = RTC_INTENSET_COMPARE0_Msk;
        NRF_RTC2->TASKS_CLEAR = 1;
        NRF_RTC2->TASKS_START = 1;
        NRF_RTC2->CC[0] = 8 * 1;
        NVIC_SetPriority(RTC2_IRQn, 3);
        NVIC_ClearPendingIRQ(RTC2_IRQn);
        NVIC_EnableIRQ(RTC2_IRQn);
    
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }

    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.

    Regards,

    L. B.

  • Hi,

    Could you try decreasing the priority to 6 or 7?

    regards

    Jared 

  • It did not work, neither with 6 nor with 7. I'm turning off the Bluetooth with sd_softdevice_disable( ) btw ... This way I can achieve 2 - 3 uA IDLE state + RTC2.

  • Hi,

    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. See the RTC example if you want an example on how to use the driver.

    regards

    Jared 

  • Actually this is where the problem started from - I tried merging

    examples/ble_peripheral/ble_app_uart

    and

    examples/peripheral/rtc

    But it didn't work - I can see no RTC2 and no Bluetooth. For example this code builds but doesn't work:

    #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(&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);
    }
    
    //!!!!!! THIS CODE DOESN'T WORK!!!!!!
    
    /**@brief Application main function.
     */
    int main(void)
    {
        bool erase_bonds;
    
        // Initialize.
        uart_init();
        log_init();
        timers_init();
        buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
    
        nrf_gpio_cfg_output(22);	
    
        lfclk_config();
        rtc_config();
    
        // Enter main loop.
        for (;;)
        {
           idle_state_handle();
        }
    }
    

    I'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'm on the wrong track, please let me know ... maybe I'm not doing it right.

    Regards,

    L. B.

Related