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

current time gets 0 after connectiong with bluetooth

hi, i am using ble_uart example to take time when data arrives and time when data being sent over bluetooth. when i prints the time without connecting to bluetooth. then the time gives a value. but after connecting with bluetooth it gives 0. i dont know what makes it 0.

below is my code:

/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
		uint8_t data[5];
    static uint8_t index,push = 0;
    uint32_t       err_code;
		static uint32_t previous_time,current_time,duration_time,waiting_time;
		waiting_time=450;
		

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;
						push++;
						if(index%3 == 0)
						{
							count++;
							err_code = app_timer_cnt_get(&current_time);
							APP_ERROR_CHECK(err_code);
							
							err_code = app_timer_cnt_diff_compute(current_time,previous_time,&duration_time);
							APP_ERROR_CHECK(err_code);
							current_time = app_timer_ms(current_time);
							previous_time = app_timer_ms(previous_time);
							duration_time = app_timer_ms(duration_time);
							SEGGER_RTT_printf(0, "current:%d\tprevious:%d\tduration:%d\n",current_time,previous_time,duration_time);
							
							SEGGER_RTT_printf(0, "%x\t%x\t%x\n",data_array[0],data_array[1],data_array[2]);
									sendData(data_array,index);
									previous_time = current_time;
							index = 0;
							preCount=count;
						}						
							
            break;
}

what i am doing wrong here.

thanks!!

  • FormerMember
    0 FormerMember

    How do you initialize the timer?

    What is the prescaler set to?

    Do you manage the RTC "on your own" somewhere?

    To get the current value of the timer, I would recommend you to use the function app_timer_cnt_get(..).

    When I test a very similar code of yours here, it works:

      case APP_UART_DATA_READY:
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
    						
    						if(index%3 == 0)
                {
                    //count++;
                    err_code = app_timer_cnt_get(&current_time);
    		        APP_ERROR_CHECK(err_code);
    								
    								
    		        err_code  = app_timer_cnt_get(&current_time); 
    			   nrf_delay_ms(20);
    			   err_code  = app_timer_cnt_get(&previous_time);
    			   err_code  = app_timer_cnt_get(&duration_time);
    			   err_code = app_timer_cnt_diff_compute(current_time,previous_time,&duration_time);
    			   APP_ERROR_CHECK(err_code);
    			   printf("current:%d\tprevious:%d\tduration:%d\n\r",current_time,previous_time,duration_time);
    								
    								
    			   previous_time = current_time;
                   
                }   
    
  • Hi, I have the same problem using sdk 9.0 and an application based on ble_app_uart. Timer init is identical as ble_app_uart (APP_TIMER_INIT called with same APP_TIMER_.. values)

    Call to app_timer_cnt_get() returns a correct value (around 33k tics/per sec) when the softdevice is not connected and returns 0 when softdevice is connected. If I disconect the app_timer_cnt_get() works again

    How did you fix this problem ? Thanks

    Edit: does app_timer_cnt_get requires to start a timer with timer_start to work ?

  • FormerMember
    0 FormerMember in reply to energy

    The app_timer module uses RTC1 and the softdevice uses RTC0, the softdevice should therefore not have any effect of the RTC1 value.

    When you are using app_timer with the softdevice, could you try to explicitly start app_timer using app_timer_start(..), and see if that changes anything?

  • could you show how to declare current_time, and let it be the RTC count? thanks.

  • FormerMember
    0 FormerMember in reply to FormerMember

    "current_time" is defined the following way: static uint32_t current_time

    In the code snippet above, current_time gets its value from app_timer_cnt_get(..). For how app_timer works, it may be useful to take a look at the app timer documentation, it can be found here.

Related