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
    	    }
        }
    }

  • Hi, the code you posted keeps the CPU busy all the time, waiting in a loop. Does it use the Arm SysTick to keep track of time? https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.0.0/nrfx/drivers/systick/driver.html It seems to me like in the code you posted, the CPU itself keeps track of time using the SysTick API, and you use conditional statements in a loop checking if a task is supposed to run or not. This requires the CPU to run all the time and will consume a lot of current (3-4 mA).

    This is not how things work in the nRF connect SDK (zephyr). It uses the RTC peripheral (which runs of the low frequency clock, 32.768 kHz), to keep track of time. And the RTC ticks will be used as the basis for scheduling tasks, waking up the CPU if needed. The CPU will be in idle mode if not interrupted by a task, which usually means that the CPU is in idle most of the time.

    k_msleep(SLEEP_TIME_MS); as shown in the example code from Håkon is a good way to schedule a task in the main loop. The CPU will be in idle as soon as it enters the k_msleep function, and will wake up again on an RTC interrupt SLEEP_TIME_MS milliseconds later.

    Other ways to schedule tasks is to use the work queue: http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/kernel/services/threads/workqueue.html?highlight=k_work#

    You can schedule a task to happen at a given time in the future, or you can schedule periodic tasks.

    You can also check out the timer API: http://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/kernel/services/timing/timers.html

    Best regards,
    Stian

Reply Children
No Data
Related