# Delay function in freedom sdk

**URL:** <https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580>\
**Category:** HiFive1 Rev B\
**Created:** [September 6, 2018, 7:03am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580 "2018-09-06T07:03:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://forums.sifive.com/u/daniel)\
**Post date:** [September 6, 2018, 7:03am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/1 "2018-09-06T07:03:47Z")

</div>

Is there any **delay()** function available in Freedom-E-SDK ? i want to use it for seconds delay.  
Since i am not going to write code in arduino ide ,i am searching for that function or similar to it.

---

<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:** [September 6, 2018, 7:55am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/2 "2018-09-06T07:55:44Z")

</div>

If you look in `freedom-e-sdk/bsp/env/freedom-e300-hifive1/init.c` you’ll see `uint64_t get_timer_value()`. In `platform.h’ you’ll see

```
unsigned long get_cpu_freq(void);
unsigned long get_timer_freq(void);
uint64_t get_timer_value(void);

```

On the HiFive1 the timer always increments at 32 MHz. On other boards it may be different. `get_timer_freq()` returns how many timer counts there are in a ms.

So you can multiply the delay you want in ms by `get_timer_freq()`, add that to the result of `get_timer_value()`, and then do whatever you want until the result of `get_timer_value()` is greater than that.

There’s no operating system, so if you simply loop waiting then nothing else can happen (except interrupts) until your delay ends. That’s usually not very useful for delays over a few us so it’s better to put it in your main loop and do other things. Or use an RTOS, several of which have been ported.

It’ll take 18000 years for the timer to overflow so you probably don’t need to worry about that.

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://forums.sifive.com/u/daniel)\
**Post date:** [September 6, 2018, 8:19am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/3 "2018-09-06T08:19:59Z")

</div>

Great ! Very useful.

> [@bruce](#):
>
> 18000 years for the timer to overflow so you probably don’t need to worry about that.

😅😅 well that was good to hear!

Anyways thank you.

---

<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:** [September 11, 2018, 6:23pm UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/4 "2018-09-11T18:23:20Z")

</div>

Oops! Sorry, that timer increments at 32 kHz, not 32 MHz, and so `get_timer_freq()` returns the number of counts in a second, not millisecond.

Which means the 64 bit overflow time is 18 _million_ years.

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://forums.sifive.com/u/daniel)\
**Post date:** [September 12, 2018, 5:36am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/5 "2018-09-12T05:36:19Z")

</div>

Here i have made it like this for a normal seconds delay.  
is that fine?

void delay(int sec){

uint64\_t ticks = sec \* RTC\_FREQ;  
mtime\_wait(ticks);

}  
void mtime\_wait( uint64\_t ticks)  
{

```
volatile uint64_t * mtime = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIME);
uint64_t now = *mtime;
  uint64_t then = now + ticks;
 
  while(*mtime<then) {
  }

```

}

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://forums.sifive.com/u/daniel)\
**Post date:** [September 12, 2018, 5:37am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/6 "2018-09-12T05:37:31Z")

</div>

where RTC\_FREQ is 32768

---

<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:** [September 12, 2018, 5:58am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/7 "2018-09-12T05:58:51Z")

</div>

Looks good. I even just tested it and it works.

A general note for such functions: if you write the loops as…

```
while((*mtime - now) < ticks) {}

```

… then it will work correctly over that 18 million year counter rollover too.

Not an issue in this case, but important if you use the 32 bit clock cycle counter which rolls over every 10 or 12 seconds on HiFive1 at the higher clock speed settings.

---

<div class="post-metadata">

**Author:** ![daniel](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@daniel](https://forums.sifive.com/u/daniel)\
**Post date:** [September 12, 2018, 6:04am UTC](https://forums.sifive.com/t/delay-function-in-freedom-sdk/1580/8 "2018-09-12T06:04:51Z")

</div>

Oh ! okay.  
Thank you.🙂
