# How to implement sleep?

**URL:** <https://forums.sifive.com/t/how-to-implement-sleep/5762>\
**Category:** HiFive1 Rev B\
**Created:** [July 5, 2022, 5:58pm UTC](https://forums.sifive.com/t/how-to-implement-sleep/5762 "2022-07-05T17:58:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsvtgc](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsvtgc](https://forums.sifive.com/u/bsvtgc)\
**Post date:** [July 5, 2022, 5:58pm UTC](https://forums.sifive.com/t/how-to-implement-sleep/5762/1 "2022-07-05T17:58:31Z")

</div>

What is the best way to implement sleep in hifive1-rev B board?

While waiting for data to arrive at UART rx, how to put the processor to sleep and to be woken by UART1 rx interrupt only. Any help in this regard is appreciated.

---

<div class="post-metadata">

**Author:** ![tgm](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tgm](https://forums.sifive.com/u/tgm)\
**Post date:** [July 6, 2022, 2:49pm UTC](https://forums.sifive.com/t/how-to-implement-sleep/5762/2 "2022-07-06T14:49:43Z")

</div>

WFI (Wait For Interrupt) instruction maybe?  
But it really depends on how a particular RISC-V implementation implements it.  
And what the context is - e.g. bare metal, lightweight [RT]OS, more fully featured OS such as GNU/Linux etc.

---

<div class="post-metadata">

**Author:** ![bsvtgc](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsvtgc](https://forums.sifive.com/u/bsvtgc)\
**Post date:** [July 6, 2022, 3:10pm UTC](https://forums.sifive.com/t/how-to-implement-sleep/5762/3 "2022-07-06T15:10:47Z")

</div>

@tgm Thanks, its ON bare metal.

When I read the manual I came across “Wfi” but don’t know how to use it. Is it an assembly instruction. Can you expand on how to use it?

I also read writing into pmusleep would initiate a sleep sequence from **sleep program memory**. I am not sure how can I program the sleep program memory.

When I

---

<div class="post-metadata">

**Author:** ![tgm](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@tgm](https://forums.sifive.com/u/tgm)\
**Post date:** [July 6, 2022, 5:00pm UTC](https://forums.sifive.com/t/how-to-implement-sleep/5762/4 "2022-07-06T17:00:46Z")

</div>

If you’re using GCC you can use inline assembly.  
There’s an example of invoking WFI using it on this page:

> **[RISC-V Assembly Style Guide | OpenTitan Documentation](https://docs.opentitan.org/doc/rm/asm_coding_style/)**
>
> OpenTitan Documentation

Most or all other compiler toolchains will have a similar mechanism.

---

<div class="post-metadata">

**Author:** ![bsvtgc](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsvtgc](https://forums.sifive.com/u/bsvtgc)\
**Post date:** [July 11, 2022, 11:11am UTC](https://forums.sifive.com/t/how-to-implement-sleep/5762/5 "2022-07-11T11:11:26Z")

</div>

Thanks
