# GPIO\_IOF\_SEL in LED Fade Demo

**URL:** <https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838>\
**Category:** HiFive1 Rev B\
**Created:** [October 28, 2017, 7:12pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838 "2017-10-28T19:12:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![knro](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/knro/32/897_2.png) [@knro](https://forums.sifive.com/u/knro)\
**Post date:** [October 28, 2017, 7:12pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/1 "2017-10-28T19:12:06Z")

</div>

I’m carefully examining each line of the code for led\_fade.c and arrived at this (line #83)

GPIO\_REG(GPIO\_IOF\_SEL) &= ~IOF0\_UART0\_MASK;

I read the FE310-G000 documentation regarding GPIO, but do not understand why the IOF\_SEL is inverting the mask above and ANDing it to the register. The net effect is that the mask bits (16 & 17 for UART GPIO) are cleared in the IOF\_SEL register. How is this exactly selecting anything?

---

<div class="post-metadata">

**Author:** ![dgrubb](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/dgrubb/32/23_2.png) [@dgrubb](https://forums.sifive.com/u/dgrubb)\
**Post date:** [October 28, 2017, 8:12pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/2 "2017-10-28T20:12:01Z")

</div>

Most pins have a secondary hardware function (PWM, SPI, UART etc). Setting the corresponding bit in IOF\_SEL allows you to override the associated hardware function and control the pin directly as GPIO instead. E.g.,:

```
// Disable HW functions
GPIO_REG(GPIO_IOF_SEL) |= (1 << PIN_18_OFFSET);
// Enable a GPIO as output
GPIO_REG(GPIO_OUTPUT_EN) |= (1 << PIN_18_OFFSET);
// Write value to pin
GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << PIN_18_OFFSET);

```

Would manually enable pin 18. The corollary is that to use the hardware function (in the case of led\_fade the UART for debug prints) you’d first clear the IOF\_SEL bit for those pins. It’s a little redundant as the UART has previously been enabled in ./bsp/env/freedom-e300-hifive1/init.c but it’s useful to note.

---

<div class="post-metadata">

**Author:** ![knro](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/knro/32/897_2.png) [@knro](https://forums.sifive.com/u/knro)\
**Post date:** [October 28, 2017, 8:46pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/3 "2017-10-28T20:46:58Z")

</div>

Ok, so I guess this what I was missing. **Clearing** the bit **enables** the hardware function and not vice versa. And yes I was wondering why this was done in fade when it was already done in init() already. Thanks!

---

<div class="post-metadata">

**Author:** ![knro](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/knro/32/897_2.png) [@knro](https://forums.sifive.com/u/knro)\
**Post date:** [October 28, 2017, 9:06pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/4 "2017-10-28T21:06:23Z")

</div>

Actually, I don’t see where \_init() is called. It seems you must explicitly call it from your program?

---

<div class="post-metadata">

**Author:** ![dgrubb](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/dgrubb/32/23_2.png) [@dgrubb](https://forums.sifive.com/u/dgrubb)\
**Post date:** [October 28, 2017, 11:02pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/5 "2017-10-28T23:02:00Z")

</div>

I think env/start.S invokes init() and then main() (sorry, on my phone and don’t have the code in front of me 🙂 ) . That’s probably why that NO\_INIT define is present, so you can effectively disable the init() and customise your own high-level startup without having to mess around with the assembly directly.

---

<div class="post-metadata">

**Author:** ![knro](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/knro/32/897_2.png) [@knro](https://forums.sifive.com/u/knro)\
**Post date:** [October 29, 2017, 5:48am UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/6 "2017-10-29T05:48:10Z")

</div>

That’s the thing, no where in start.S does it call \_init. I think it has to be called explicitly unless I’m missing it elsewhere.

---

<div class="post-metadata">

**Author:** ![mwachs5](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/mwachs5/32/13_2.png) [@mwachs5](https://forums.sifive.com/u/mwachs5)\
**Post date:** [October 30, 2017, 1:45pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/7 "2017-10-30T13:45:21Z")

</div>

To clarify, setting GPIO\_IOF\_EN _enables_ the IOF (hardware function). For any GPIO pin, there are 2 possible IOF functions, in addition to the basic GPIO function. GPIO\_IOF\_SEL selects which if the IOF functions you want, which is why the code sets it to 0 for the UART (because it is on IOF 0). In contrast, many of the PWM pins use IOF 1, so they set the GPIO\_IOF\_SEL bits, and also set the GPIO\_IOF\_EN bits.

---

<div class="post-metadata">

**Author:** ![mwachs5](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/mwachs5/32/13_2.png) [@mwachs5](https://forums.sifive.com/u/mwachs5)\
**Post date:** [October 30, 2017, 2:05pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/8 "2017-10-30T14:05:59Z")

</div>

Also, you do not need to explicitly call the \_init and \_fini functions. Those functions have special meaning for GCC.

---

<div class="post-metadata">

**Author:** ![knro](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/knro/32/897_2.png) [@knro](https://forums.sifive.com/u/knro)\
**Post date:** [October 30, 2017, 2:41pm UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/9 "2017-10-30T14:41:28Z")

</div>

Thanks!! That makes perfect sense! I hope documentation is updated with this information just to clarify it for future users. Awesome work Megan!

---

<div class="post-metadata">

**Author:** ![RakeshL](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@RakeshL](https://forums.sifive.com/u/RakeshL)\
**Post date:** [January 31, 2019, 7:05am UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/11 "2019-01-31T07:05:16Z")

</div>

> [@mwachs5](#):
>
> IOF

You guys discussed very usefull thinks ,thanks for the information, and i am trying to configured the UART ,through fredom-e SDk ,can you people just help me to get code

---

<div class="post-metadata">

**Author:** ![RakeshL](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@RakeshL](https://forums.sifive.com/u/RakeshL)\
**Post date:** [February 7, 2019, 6:32am UTC](https://forums.sifive.com/t/gpio-iof-sel-in-led-fade-demo/838/12 "2019-02-07T06:32:51Z")

</div>

hello knor,  
in the below code ,of UART base adress they have specified with UL what it indicates actually.  
#define UART0\_CTRL\_ADDR \_AC(0x10013000,UL).
