How do i use SysTick for NRF52840 using nRF Connect SDK v2.0.0

I want to keep perform some task periodically .. probably using tick timer of every 1 ms, so that only after every 4000ms, i can perform some task.

How do i implement it ?

Any API / example i can refer to ?

Parents
  • Hi Deep,

    Have you had a look at the blinky sample? An LED is toggled every 1000ms. You could redefine the sleep time interval at the top to 4000, and then use the while loop as a template for your periodic task.

    Or is this not what you are looking for?

    /*
     * Copyright (c) 2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <drivers/gpio.h>
    
    /* 1000 msec = 1 sec */
    #define SLEEP_TIME_MS   1000
    
    /* The devicetree node identifier for the "led0" alias. */
    #define LED0_NODE DT_ALIAS(led0)
    
    /*
     * A build error on this line means your board is unsupported.
     * See the sample documentation for information on how to fix this.
     */
    static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
    
    void main(void)
    {
    	int ret;
    
    	if (!device_is_ready(led.port)) {
    		return;
    	}
    
    	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
    	if (ret < 0) {
    		return;
    	}
    
    	while (1) {
    		ret = gpio_pin_toggle_dt(&led);
    		if (ret < 0) {
    			return;
    		}
    		k_msleep(SLEEP_TIME_MS);
    	}
    }

    Best regards,

    Håkon

  • I dont think i am looking for this exactly.

    This is my implementation in different MCU, how to use it in nRF Connect ?

     

    //Current Implementation on different MCU with no OS
    //Want to port this implementation on nordic MCU with nRF Connect SDK 2.0.0
    
    #define TIMEOUT 1000 //in ms
    
    volatile uint32_t systick_count = 0;
    
    //Function to read
    uint32_t get_systick( void )
    {
    	return systick_count;
    }
    
    // Systick interrupt handler
    //Function that updates, periodically in interrupt, triggered every 1 ms
    //How do i implement this in nRF Connect SDK ?
    void SysTick_Handler( void )
    {
    	systick_count++;
    }
    
    main()
    {
    	uint32_t myTimer = 0;
    
        while(1)
        {
            //Do something every 1ms, indefinetely
        	if (myTimer > (get_systick() + TIMEOUT))
    	    {
    	        myTimer = get_systick();
    		    //do something
    	    }
        }
    }

Reply
  • I dont think i am looking for this exactly.

    This is my implementation in different MCU, how to use it in nRF Connect ?

     

    //Current Implementation on different MCU with no OS
    //Want to port this implementation on nordic MCU with nRF Connect SDK 2.0.0
    
    #define TIMEOUT 1000 //in ms
    
    volatile uint32_t systick_count = 0;
    
    //Function to read
    uint32_t get_systick( void )
    {
    	return systick_count;
    }
    
    // Systick interrupt handler
    //Function that updates, periodically in interrupt, triggered every 1 ms
    //How do i implement this in nRF Connect SDK ?
    void SysTick_Handler( void )
    {
    	systick_count++;
    }
    
    main()
    {
    	uint32_t myTimer = 0;
    
        while(1)
        {
            //Do something every 1ms, indefinetely
        	if (myTimer > (get_systick() + TIMEOUT))
    	    {
    	        myTimer = get_systick();
    		    //do something
    	    }
        }
    }

Children
Related