1 Us delay ! Using nRF connect SDK.

I have been trying to achieve a delay of 1 Us , I have tried using Kernel timers and even tried fetching the system clock and getting the difference, But I am unable to figure it out.

#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include <sys/util.h>
#include <sys/printk.h>
#include <hal/nrf_gpio.h>
#include <zephyr/kernel.h>
#include <sys_clock.h>
#include <sys/time_units.h>

#define Data 02

uint32_t start_time;
uint32_t stop_time;
uint32_t cycles_spent;
uint32_t nanoseconds_spent;

   
void Timer(uint32_t time){
	start_time = k_cycle_get_32();
	do{
	stop_time = k_cycle_get_32();
	cycles_spent = stop_time - start_time;
	nanoseconds_spent = k_cyc_to_ns_floor32(cycles_spent);
	}while(nanoseconds_spent>=time);
}


int main(void)
{
    /* Configure board. */
	nrf_gpio_cfg_output(Data);
	
    while(1){
	 nrf_gpio_pin_clear(Data);
	 k_sleep(K_USEC(1));
	 nrf_gpio_pin_set(Data);
	 Timer(1000);	 
    }
}
 

I am new to RTOS and NCS also , any help would be appreciated . 

Parents
  • #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/gpio.h>
    #include <sys/util.h>
    #include <sys/printk.h>
    #include <hal/nrf_gpio.h>
    #include <zephyr/kernel.h>
    
    
    #define Data 02
    
    K_TIMER_DEFINE(my_timer, NULL, NULL);
    
    int main(void)
    {
        /* Configure board. */
     nrf_gpio_cfg_output(Data);
    
    	  while (1)
    	 {
    	 		nrf_gpio_pin_set(Data);
    			k_timer_start(&my_timer, K_USEC(1),K_NO_WAIT);
    			nrf_gpio_pin_clear(Data);
    			k_timer_start(&my_timer, K_USEC(1),K_NO_WAIT);
    	  }
    }

Reply
  • #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/gpio.h>
    #include <sys/util.h>
    #include <sys/printk.h>
    #include <hal/nrf_gpio.h>
    #include <zephyr/kernel.h>
    
    
    #define Data 02
    
    K_TIMER_DEFINE(my_timer, NULL, NULL);
    
    int main(void)
    {
        /* Configure board. */
     nrf_gpio_cfg_output(Data);
    
    	  while (1)
    	 {
    	 		nrf_gpio_pin_set(Data);
    			k_timer_start(&my_timer, K_USEC(1),K_NO_WAIT);
    			nrf_gpio_pin_clear(Data);
    			k_timer_start(&my_timer, K_USEC(1),K_NO_WAIT);
    	  }
    }

Children
No Data
Related