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

facing issues in timer

hi..... i am using nrf52832 , merged the uart and  timer example program . while i am debugging , debugged successfully , but timer is not working

actually i set the timer for one minute after completing the one minute it should send the data .  i am getting the data very quickly in this case i couldn't  found any timer 

Parents
  • is it possible you can post your code / setup or pieces of your code?

  • static void create_timers()
    {  
       uint32_t err_code;
    
        // Create timers
    
        err_code = app_timer_create(&m_led_a_timer_id,
                                    APP_TIMER_MODE_REPEATED,
                                   timer_sensor_event_handler);
      APP_ERROR_CHECK(err_code);
    }
    
    static timer_start(void)
    {
       uint32_t err_code;
        //uint32_t APP_TIMER_TICKS(50000);
       
           
        // Start application timers
        err_code = app_timer_start(m_led_a_timer_id, APP_TIMER_TICKS(50000), NULL);
        APP_ERROR_CHECK(err_code);
         NRF_LOG_INFO("SUCCESS1");          
    
    }
    
    
    
    /**@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();
    
    app_timer_init();
         create_timers();
        
        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
       timer_start();
       
         twi_init();
         MAX30_set_mode();
         
    
        
       while (true)
        {
            nrf_delay_ms(6000);
      uint32_t err_code;
           while (m_xfer_done == false);
    
             m_xfer_done = false;
                    
    		uint8_t reg[2] = {0x00U};
            err_code = nrf_drv_twi_tx(&m_twi,LM75B_ADDR, reg, sizeof(reg), true);
    		APP_ERROR_CHECK(err_code);
    		while (m_xfer_done == false);
    		nrf_delay_ms(5);
    		
    	        m_xfer_done = false;
    		ret_code_t err_code1 = nrf_drv_twi_rx(&m_twi,LM75B_ADDR, &m_sample, sizeof(m_sample));
    		APP_ERROR_CHECK(err_code1);
                    NRF_LOG_INFO("%d", m_sample);
                     
     
    }
                  for (;;)
        {
            idle_state_handle();
        }
    
          
          }
             
       

  • thanks  alot its working. in this case how can put in sleep mode ,its like for one  minute it should sleep and another one minute it should send data ,how to do

  • Glad to hear it is working on your side!! Your sleep request is going to be a little tricky without knowing a little bit more about your use case. Here is why.

    Currently you above code by default goes into sleep mode by using the idle_state_handle function. Since your application is currently using BLE, you will only sleep between the advertisement intervals when not connected to a device and only sleep between the connection intervals when you are connected to a device. Since the softdevice will generate events in the background to be serviced. 

    If you only need to be connected to the device when it needs to send information then you could only advertise and connect to a central when data is needed to be sent.

    So instead of advertising continuously, you would advertise based on your timer, connect to central, send the information for your duration and then disconnect and restart or sleep timer and repeat.

    Or some scheme similar to the above. Basically you need to stop the softdevice from generating BLE events in order to get a longer sleep period or better CPU idle usage.

  • Is it possible to connect,when we need to send information . More over while during advertising and paring it will consume more power I guess . How to do that ,anyway we try this 

  • Yes it is possible and some of that is done of the central side to automatically send out the connection request once it appears the advertisement of your device. normally done by uuid filtering but there are other ways to do it as well.

    you can reduce the power consumption during advertising and connection by increasing the advertisement and connection intervals. You can also depending of the distance of the rx and tx reduce the radio power from 0 down to a low power. 

    Connecting only when you need to send information would be the best solution as you dont have to change any of the intervals. You only advertise when you want to connect and if you adjust your central to automatically connect to the device we it advertises then the peripheral can terminate the connect once it is done sending or maybe a better way it to have a end of transmission message and allow the central to perform the disconnect. Either way works. 

  • ok, but to implement  this . i am not that much good at program, is there any sample program like this so that i can get some idea 

Reply Children
  • I dont believe there is sample exactly but the above code you provide is very close to the functionality described. 

    You already have the 50s timer. instead of toggling an led you toggle a variable. I believe you mentioned 60s on while sending data and then 60s off. 120s period between on times. 

    bool yourflag = false;

    to toggle => yourflag = !yourflag. This would be the logic inside the timer handler.

    static void timer_sensor_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
    yourflag = !yourflag;
    }

    Then in your main you would only advertise when your flag is true and when your flag is false stop and sleep.

    Note "sleep" still will not be deep power off sleep because you still need your timer to run so the RTC clock will still be ticking away. Power will be much less than advertising the entire time.

    When flag is set you call start advertising making sure that it is only called once.

    then once timer toggles flag you disconnect gatt and then idle.

    If you need more assistance or some rough pseudo code please let me know.

  • yeah sure i will look into and get back to you 

  • how to set the flag ,if  i want to check the some data if it reaches certain limit , and then  it should start advertise and  send the data ,and enter into the sleep ,  only when it reaches the certain level it should advertise and connect

  • i have tried but i can't able control the ble advertising , i have written the code when it reaches the threshold (beyond the limit value) then it should start advertise and send the data over ble ,how to connect automatically 

Related