# Gpio\_out\_xor

**URL:** <https://forums.sifive.com/t/gpio-out-xor/1480>\
**Category:** HiFive1 Rev B\
**Created:** [August 4, 2018, 11:31am UTC](https://forums.sifive.com/t/gpio-out-xor/1480 "2018-08-04T11:31:27Z")\
**Posts on this page:** 9\
**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:** [August 4, 2018, 11:31am UTC](https://forums.sifive.com/t/gpio-out-xor/1480/1 "2018-08-04T11:31:27Z")

</div>

GPIO\_REG(GPIO\_IOF\_SEL) |= ( (1 \<\< GREEN\_LED\_OFFSET) | (1 \<\< BLUE\_LED\_OFFSET) | (1 \<\< RED\_LED\_OFFSET));  
GPIO\_REG(GPIO\_IOF\_EN ) |= ( (1 \<\< GREEN\_LED\_OFFSET) | (1 \<\< BLUE\_LED\_OFFSET) | (1 \<\< RED\_LED\_OFFSET));  
GPIO\_REG(GPIO\_OUTPUT\_XOR) &= ~( (1 \<\< GREEN\_LED\_OFFSET) | (1 \<\< BLUE\_LED\_OFFSET));  
GPIO\_REG(GPIO\_OUTPUT\_XOR) |= (1 \<\< RED\_LED\_OFFSET);

what does it mean? (last two lines) can some one explain?

---

<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:** [August 5, 2018, 10:19pm UTC](https://forums.sifive.com/t/gpio-out-xor/1480/2 "2018-08-05T22:19:41Z")

</div>

Have you read chapter 11 of the E300 Platform Reference Manual? Although to be fair there isn’t a lot of tutorial material there. I guess it assumes familiarity with other microcontrollers.

I believe the GPIO\_OUTPUT\_XOR register should toggle any GPIO pin whose bit it set to 1 when a write is made to the register.

In which case those last two lines look like a bug. I don’t know what is supposed to happen if you read from that register. Maybe it should be:

```
GPIO_REG(GPIO_OUTPUT_XOR) = (1 << GREEN_LED_OFFSET) | (1 << BLUE_LED_OFFSET);
GPIO_REG(GPIO_OUTPUT_XOR) = (1 << RED_LED_OFFSET);

```

Which could be combined into one assignment, after initial set up.

---

<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:** [August 6, 2018, 8:27am UTC](https://forums.sifive.com/t/gpio-out-xor/1480/3 "2018-08-06T08:27:48Z")

</div>

Yeah I read.yes i understood !Well ,the information was good and helpful and i feel that i should improve my understanding over micro controllers.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:** [August 6, 2018, 9:50am UTC](https://forums.sifive.com/t/gpio-out-xor/1480/4 "2018-08-06T09:50:31Z")

</div>

A lot of the ARM microcontrollers have individual “set” and “clear” registers that let you do things like…

```
GPIO_REG(GPIO_OUTPUT_CLEAR) = (1 << GREEN_LED_OFFSET) | (1 << BLUE_LED_OFFSET);
GPIO_REG(GPIO_OUTPUT_SET) = (1 << RED_LED_OFFSET);

```

… to turn off the green and blue LED’s GPIOs and turn on the red LED’s GPIO without having to read the whole IO register to a CPU register, turn bits on and off, and write the new value back.

We don’t seem to have that, but I guess the XOR register will often allow the same kind of things, possibly even with slightly less code if you implicitly already know the previous state.  
.

---

<div class="post-metadata">

**Author:** ![Krste](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/krste/32/11_2.png) [@Krste](https://forums.sifive.com/u/Krste)\
**Post date:** [August 6, 2018, 4:09pm UTC](https://forums.sifive.com/t/gpio-out-xor/1480/5 "2018-08-06T16:09:26Z")

</div>

As opposed to reserving special differently-addressed memory regions for atomic set and clear of device bits (bit banding), the RISC-V amoor and amoand instructions allow you to set and clear bits in a memory-mapped device with a single instruction using the normal device address. Amoxor lets you toggle the bits as described by Bruce.

---

<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:** [August 7, 2018, 4:24am UTC](https://forums.sifive.com/t/gpio-out-xor/1480/6 "2018-08-07T04:24:50Z")

</div>

great!

---

<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:** [August 9, 2018, 7:12am UTC](https://forums.sifive.com/t/gpio-out-xor/1480/7 "2018-08-09T07:12:35Z")

</div>

should we connect IOREF 3v or 5v J1 jumper to use the digital pins?  
and if so, can i use F-F jumper wire instead? i dont have the jumper 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:** [August 9, 2018, 7:25am UTC](https://forums.sifive.com/t/gpio-out-xor/1480/8 "2018-08-09T07:25:55Z")

</div>

That depends what voltage peripherals you want to connect.

It doesn’t matter what kind of wire you use to connect the pins as long as there is electrical continuity.

---

<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:** [August 9, 2018, 7:30am UTC](https://forums.sifive.com/t/gpio-out-xor/1480/9 "2018-08-09T07:30:33Z")

</div>

Okay then.  
Thank you Bruce.
