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

Timer 0 interface program

Hi,

 i am trying to interface ultrasonic sensor with nrf9160, but i dont get any examples regarding ultrasonic. so i tried the interrupt method to measure the time of gpio high state, i used

k_cycle_get_32(); function to get starting timing and also i used the same function while the high is getting low. then i send the count to SYS_CLOCK_HW_CYCLES_TO_NS() function, but it doesnt give the exact value. kindly help me to sort this out. here i attached the interrupt callback function.

static void UltraSonicData(struct device *port, struct gpio_callback *cb, u32_t pins)
{
  static uint8_t DataFlag;
  float Distance;
  if(DataFlag == 0)
  {
      Time =  k_cycle_get_32();
      printk("UltraSonic high Started\n starting time: %d \n ",Time);
     int err;
   gpio_pin_configure(gpio_dev, dpin1,
			   (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | 
			    GPIO_INT_ACTIVE_LOW));
                            gpio_init_callback(&ultrasonic_cb, UltraSonicData, BIT(dpin1));
                            err = gpio_add_callback(gpio_dev, &ultrasonic_cb);
                            
        if (err) 
        {
          printk("UltraSonicConfig Cannot add callbacks");
	}
        err = gpio_pin_enable_callback(gpio_dev, dpin1);
        if (err) 
        {
          printk("UltraSonicConfig Cannot enable callbacks");
	}
    DataFlag = 1;
  }
  else
  {
       Time =  k_cycle_get_32() - Time;
       printf("Time:%d\n ",Time);
       Distance = (((SYS_CLOCK_HW_CYCLES_TO_NS(Time)/10.0)/5882.0));//*0.0343)/2.0); 
       printf("Distance:%f\n ",Distance);
   int err;
   gpio_pin_configure(gpio_dev, dpin1,
			   (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_PUD_PULL_DOWN |
			    GPIO_INT_ACTIVE_HIGH));
                            gpio_init_callback(&ultrasonic_cb, UltraSonicData, BIT(dpin1));
                            err = gpio_add_callback(gpio_dev, &ultrasonic_cb);
                            
        if (err) 
        {
          printk("UltraSonicConfig Cannot add callbacks");
	}

     DataFlag = 0;
  }
}

thanks in advance

hmdra 

  • Hi,

     

    What is the required accuracy for the signal? Right now you're using the RTC (the kernel timer), which is 32.768 kHz base frequency, which might not be accurate enough for your application.

    In order to do this as accurate as possible, it would require:

    * NRF_TIMER

    * 2 x GPIOTE IN (One on rising, one on falling edge)

    * Two PPI channels (one to start, one to stop TIMER)

     

    Connect the signal you want to time to two GPIOs (configured as IN), as "GPIOTE IN Toggle" will not be able to differentiate between the start and stop of the signal here.

    This method will require either bare-metal or the usage of nrfx drivers directly.

     

    Kind regards,

    Håkon

  • Hi Hakon,
     Thank you for your valuable response,

     i need micro second timer accuracy, ultrasonic need two GPIOs, one for tirgger and another is for echo, i configure trigger as output and echo pin as interrupt, as per the ultrasonic datasheet the echo pin returns the high state for a short period of time which is help to measure the distance. so that i start measure timing for initial high level and end of the high level, then i converts it as nanosecond.

     the posted program is now working good to me, the only thing make my timing different is printing statement. it tooks more time so i have missed the original count.

     as per your suggestion i have doubt how to configure GPIOTE as toggle. i didnt have an API for that.

    Thanks 

    hmdra

  • Hi,

     

    Could you try this sample and see if it works on your end? It uses TIMER in 16M resolution.

    https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/nrfx_timed_signal

     

    You might have to change the pins to match your use-case. Its GPIOs 6 and 7 now, which are active low at my end.

    Note the spm diff file, where DPPIC is set to non-secure.

     

    Kind regards,

    Håkon

Related