# Time delays using Hifive1 RevB

**URL:** <https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609>\
**Category:** HiFive1 Rev B\
**Created:** [April 7, 2021, 5:00pm UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609 "2021-04-07T17:00:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicola1998](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nicola1998](https://forums.sifive.com/u/nicola1998)\
**Post date:** [April 7, 2021, 5:00pm UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609/1 "2021-04-07T17:00:04Z")

</div>

Hello everyone, to learn to use the bsp I’m trying to develop various libraries for sensors. I’m trying to use an HC-SR04. It needs a 10us pulse on the trigger pin, and I’m using the example from the sifive-welcome project.  
The problem is that the delay is not timed right, in fact if I try to delay 1s, it misses it by 2 to 5ms.  
I’m using the given frequency of 32768Hz and this function to delay:

```
void wait_for_timer() {

    // clear global timer isr flag
    timer_isr_flag = 0;

    // Set timer
    metal_cpu_set_mtimecmp(cpu, (metal_cpu_get_mtime(cpu) + RTC_FREQ));
    // Enable Timer interrupt
    metal_interrupt_enable(tmr_intr, tmr_id);

    // wait till timer triggers and isr is hit
    while (timer_isr_flag == 0){};

    timer_isr_flag = 0;
}
```

---

<div class="post-metadata">

**Author:** ![Jim](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/jim/32/632_2.png) [@Jim](https://forums.sifive.com/u/Jim)\
**Post date:** [April 7, 2021, 5:35pm UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609/2 "2021-04-07T17:35:13Z")

</div>

Hi Nicola,

Would it be better to use a PWM output for a consistent pulse train to the sensor?

How are you measuring the timing?

---

<div class="post-metadata">

**Author:** ![nicola1998](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nicola1998](https://forums.sifive.com/u/nicola1998)\
**Post date:** [April 7, 2021, 6:03pm UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609/3 "2021-04-07T18:03:22Z")

</div>

I’m using the function

`metal_cpu_get_mtime(cpu)`

I don’t think it’d be useful, I need a short 10us pulse on the trigger pin, then I wait for the answer from the echo pin

---

<div class="post-metadata">

**Author:** ![bruce](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/bruce/32/452_2.png) [@bruce](https://forums.sifive.com/u/bruce)\
**Post date:** [April 8, 2021, 12:14am UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609/4 "2021-04-08T00:14:21Z")

</div>

Make sure you run ALL the code (including the timing code) at least once before you do any actual timing, otherwise you’ll be affected by the very slow loading of code from SPI flash into the icache.

---

<div class="post-metadata">

**Author:** ![LetsGrillBananas](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@LetsGrillBananas](https://forums.sifive.com/u/LetsGrillBananas)\
**Post date:** [April 8, 2021, 6:29am UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609/5 "2021-04-08T06:29:16Z")

</div>

Wow, I seem to be working on the exact same problem on the exact same day as you! Also working on an HC-SR04. As far as I have figured out, I think the real-time clock won’t work, as it has a frequency of 32,768 Hz (or a period of ~30 microseconds), which is much too slow for getting a 10 microsecond pulse or for measuring the duration of the echo pulse.

These are other resources in this forum that may be of (minor) help:

- [How to get current system time on HiFive1 board?](https://forums.sifive.com/t/how-to-get-current-system-time-on-hifive1-board/691)
- [FE310 RTC Change Frequency](https://forums.sifive.com/t/fe310-rtc-change-frequency/3254)

I’m going to try `metal_timer_get_cyclecount(hartid, &cyclecount)` or `metal_cpu_get_timer(cpu)` and I’ll update if it works or not.

---

<div class="post-metadata">

**Author:** ![nicola1998](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nicola1998](https://forums.sifive.com/u/nicola1998)\
**Post date:** [April 8, 2021, 3:11pm UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609/6 "2021-04-08T15:11:05Z")

</div>

I’m trying to use PWM as suggested by @Jim, you should try too. The only problem is that I can’t figure out how the PWM gpios are set. pwm2\_0 should be GPIO\_10 or pin 16, as per datasheet, but Thruthfully it seems to be GPIO\_13 or pin 19

---

<div class="post-metadata">

**Author:** ![nicola1998](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nicola1998](https://forums.sifive.com/u/nicola1998)\
**Post date:** [April 13, 2021, 4:57pm UTC](https://forums.sifive.com/t/time-delays-using-hifive1-revb/4609/7 "2021-04-13T16:57:57Z")

</div>

I seem to have figured it out, but I still have a 2/3cm error.  
I’m using this code:

```
metal_pwm_set_freq(pwm2, 1, FREQ);
metal_pwm_set_duty(pwm2, 0, 100, METAL_PWM_PHASE_CORRECT_DISABLE);
metal_pwm_set_duty(pwm2, 2, 100, METAL_PWM_PHASE_CORRECT_DISABLE);
metal_pwm_set_duty(pwm2, 3, 100, METAL_PWM_PHASE_CORRECT_DISABLE);
metal_pwm_set_duty(pwm2, 1, 50, METAL_PWM_PHASE_CORRECT_DISABLE);
metal_pwm_trigger(pwm2, 1, 1);

long r = metal_cpu_get_mtime(cpu);
while(metal_gpio_get_input_pin(button, TRIGGER))
	;
r = metal_cpu_get_mtime(cpu) - r;
printf("%f\n", ((float)r * 1000000.00 / RTC_FREQ) / 58.00);
```
