Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using timer interrupt and flash storage in nRF51

Hi,

I am using ble_app_uart sample code. In that I want to run a timer based on the commands received in BLE. i.e., if command received is 'one' , LED1 should ON for one hour and the timing values should be written into flash storage. If any power reset is there, particular LED should run for remaining time. The timer counter value should be updated for every one hour into flash.

How it can be done. any sample code is there for similar kind?

  • See my code below.

    uint32_t timeout = 0,count=0;
    
    APP_TIMER_DEF(m_led_a_timer_id);
    
    // Timeout handler for the  timer
    static void timer_a_handler(void * p_context)
    {
    
    		nrf_gpio_pin_clear(3);
    
    }
    
    
    // Create timers
    static void create_timers()
    {   
        uint32_t err_code;
    
        // Create timers
        err_code = app_timer_create(&m_led_a_timer_id,
                                    APP_TIMER_MODE_SINGLE_SHOT,
                                    timer_a_handler);
        APP_ERROR_CHECK(err_code);
    	
    	err_code = app_timer_start(m_led_a_timer_id, APP_TIMER_TICKS(timeout, APP_TIMER_PRESCALER), NULL);
       APP_ERROR_CHECK(err_code);
    }
    
    
    static void check_rcv_data(uint8_t * rcv_p_data)
    {
    	  uint32_t time_ms = 5000; //Time(in miliseconds) between consecutive compare events.
        uint32_t time_ticks;
        uint32_t err_code = NRF_SUCCESS;
         
    
    		if(strstr((char *)rcv_p_data, "Dev"))
    		{
    //			printf("dev\n\r");
    		update_device_name(rcv_p_data);
    		}
    		
    		else if((strcmp((char *)rcv_p_data, "one") == 0))
    		{
    			nrf_gpio_pin_set(3);
    			timeout+=60000;
    		  count++;
    			create_timers();
    	}
    	
    	else if((strcmp((char *)rcv_p_data, "two") == 0))
    		{
    			nrf_gpio_pin_set(3);
    			timeout+=120000;
    			count++;
    			create_timers();
            
        }
    	
    	else if((strcmp((char *)rcv_p_data, "three") == 0))
    		{
    			nrf_gpio_pin_set(3);
    			timeout+=180000;
    			count++;
    			create_timers();
    	}
    	
    	else if((strcmp((char *)rcv_p_data, "four") == 0))
    		{
    			nrf_gpio_pin_set(3);
    			timeout+=240000;
    			count++;
    			create_timers();
    	}
    	
    	else if((strcmp((char *)rcv_p_data, "five") == 0))
    		{
    			nrf_gpio_pin_set(3);
    			timeout+=300000;
    			count++;
    			create_timers();
    	}
    	
    		else 
    		{
    			invalid_cmd();
    		}
    	}
    
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
    	
    		uint32_t i;
        for (i = 0; i < length; i++)
        {
         
        }
    		p_data[i] = '\0';
    
    		
    		printf("%s",p_data);
    		
    		check_rcv_data(p_data);
    
    }

  • I was thinking more like this:

    uint32_t 1_minute_counter = 0, max_timeout = 60;
    
    APP_TIMER_DEF(m_led_a_timer_id);
    
    // 1 minute handler for the timer
    static void timer_a_handler(void * p_context)
    {
    	1_minute_counter++;
    
    	if(1_minute_counter >= max_timeout)
    	{
    		nrf_gpio_pin_clear(3);
    	}
    }
    
    // Start timer
    static void create_timers()
    {   
    	uint32_t err_code;
    
    	err_code = app_timer_create(&m_led_a_timer_id,APP_TIMER_MODE_INTERVAL,timer_a_handler);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = app_timer_start(m_led_a_timer_id, APP_TIMER_TICKS(60_seconds_timeout, APP_TIMER_PRESCALER), NULL);
    	APP_ERROR_CHECK(err_code);
    }
    
    // The application can now clear 1_minute_counter or change max_timeout to adjust when pin should be cleared.
    

Related