Time delays using Hifive1 RevB

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;

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?

1 Like

I’m using the function


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

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.

1 Like

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:

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.

1 Like

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

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);