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

RTC stops after disconnet from Android

Hi, I am using nrf51822 chip. I am using RTC to generate irq with CC. Everything works fine for IOS and old Android version. New android version makes 2 things when I disconnect:

  1. change register of CC[0] value so I changed to generate IRQ from CC[1]:

    void lfclk_config(void) { ret_code_t err_code = nrf_drv_clock_init(NULL); APP_ERROR_CHECK(err_code);

     nrf_drv_clock_lfclk_request();
    

    }

     uint32_t err_code;
    
     //Initialize RTC instance
     err_code = nrf_drv_rtc_init(&rtc, NULL, rtc_handler);
     APP_ERROR_CHECK(err_code);
    
     //Enable tick event & interrupt
     nrf_drv_rtc_tick_enable(&rtc,false);
    
     //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
    
     err_code = nrf_drv_rtc_cc_set(&rtc,1,P*60*8,true);
     APP_ERROR_CHECK(err_code);
    
     //Power on RTC instance
     nrf_drv_rtc_enable(&rtc);
    
  2. stops RTC after disconnect.

I don't have any idea why on IOS works fine but on new Android it doesn't.

Thanks for help in advance.

  • Sorry but it doesn't make sense that any external BLE device by connecting influences state of nRF5x registers like this. It must be code running on nRF5x chip which causes it, e.g. Soft Device. What SD are you running? What RTC instance are you using, how you initialize it etc.?

  • I know it doesn't have any sense. I use soft device 110, I use RTC 1 to get irq. This is my initialization code. It's weird that at IOS or Android with old version everything works fine, but when I getting disconnet with new Android the register is changing and RTC stops. Main loop and other tasks works fine. It`s happens even when I am using NRF uart application.

    void lfclk_config(void)
    {
        ret_code_t err_code = nrf_drv_clock_init(NULL);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_clock_lfclk_request();
    }
    
    const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(1);
    
    void rtc_config(void)
    {
        uint32_t err_code;
    
        //Initialize RTC instance
        err_code = nrf_drv_rtc_init(&rtc, NULL, rtc_handler);
        APP_ERROR_CHECK(err_code);
    
        //Enable tick event & interrupt
        nrf_drv_rtc_tick_enable(&rtc,false);
    
        //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
    
    	err_code = nrf_drv_rtc_cc_set(&rtc,1,2*60*8,true);
    	APP_ERROR_CHECK(err_code);
    
        //Power on RTC instance
        nrf_drv_rtc_enable(&rtc);
    
    
    }
    

    BLE config here:

    uint32_t BLE_config(void)
    {
    	uint32_t err_code;
    
    	APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    
    
    	ble_stack_init();
    	gap_params_init();
    	services_init();
    	advertising_init(APP_ADV_START_TIMEOUT_IN_SECONDS);
    	conn_params_init();
    
    	//err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    	//APP_ERROR_CHECK(err_code);
    
        return err_code;
    }
    
    static void ble_stack_init(void)
    {
        uint32_t err_code;
    
        // Initialize SoftDevice.
        SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);
    
        // Enable BLE stack.
        ble_enable_params_t ble_enable_params;
        memset(&ble_enable_params, 0, sizeof(ble_enable_params));
    #if (defined(S130) || defined(S132))
        ble_enable_params.gatts_enable_params.attr_tab_size   = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
    #endif
        ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
        err_code = sd_ble_enable(&ble_enable_params);
        APP_ERROR_CHECK(err_code);
    
        // Subscribe for BLE events.
        err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
        APP_ERROR_CHECK(err_code);
    }
    
    static void gap_params_init(void)
    {
        uint32_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,
                                              strlen(DEVICE_NAME));
        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);
    }
    
    static void services_init(void)
    {
        uint32_t       err_code;
        ble_nus_init_t nus_init;
    
        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);
    }
    
    void advertising_init(uint16_t timeout)
    {
        uint32_t      err_code;
        ble_advdata_t advdata;
        ble_advdata_t scanrsp;
    
        // Build advertising data struct to pass into @ref ble_advertising_init.
        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;
        advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
        advdata.uuids_complete.p_uuids  = m_adv_uuids;
        ble_adv_modes_config_t options = {0};
        options.ble_adv_fast_enabled  = BLE_ADV_FAST_ENABLED;
        options.ble_adv_fast_interval = APP_ADV_INTERVAL;
        options.ble_adv_fast_timeout  = timeout;
        options.ble_adv_slow_enabled = BLE_ADV_SLOW_ENABLED;
        options.ble_adv_slow_interval = 2056;
        options.ble_adv_slow_timeout = timeout;
    
        err_code = ble_advertising_init(&advdata, NULL, &options, on_adv_evt, NULL);
        APP_ERROR_CHECK(err_code);
    }
    
    static void conn_params_init(void)
    {
        uint32_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);
    }
    

    I post the logs from terminal from 3 phones.

    rtc counter CC

    Android 4.3 Samsung gt-i9300 with CC[0]: image description

    Iphone 6S with system 10.2.1 with CC[0]: image description

    and now Android 7.0 Samsung s7 edge with CC[0]: image description

    Android 7.0 Samsung s7 edge with CC[1]: image description

    Here is my code, sorry for a mess but this is just a test code: BLE_NEW_mod.rar

    Now it`s working. Here is log:

    image description

    The problem was in using app_timer library.

    Thanks a lot for help!

  • Hi Marcin,

    Could you make sure you don't have any other code or other module that may use the RTC ?

    Which value the CC[0] changed to after you disconnect from the new Android phone ?

    Which Android version was that and which phone did you use ?

    Could you provide the nRF UART version with your modification that can reproduce the issue ?

  • @Marcin: Please provide code that reproduce the issue. From the log, doesn't seem the CC changed when it's disconnected but when in connection.

  • @Marcin: It would be easier if you have a Keil project so I can test here.

    Are you sure there is nothing else in the application that can affect RTC? Could you try to modify the ble_app_uart to show the issue ?

    You can also try to sniff a sniffer trace and compare between Android 4.3 and Android 7.0 on what they do after connection and spot the different.

    Which app did you use on the phone ?

    I noticed that CC changed before disconnection.

Related