# Interfacing with the Pins

**URL:** https://forums.sifive.com/t/interfacing-with-the-pins/3631
**Category:** HiFive1 Rev B
**Created:** [May 14, 2020, 5:16am UTC](https://forums.sifive.com/t/interfacing-with-the-pins/3631 "2020-05-14T05:16:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Jonas](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonas](https://forums.sifive.com/u/Jonas)
#### Post date: [May 14, 2020, 5:16am UTC](https://forums.sifive.com/t/interfacing-with-the-pins/3631/1 "2020-05-14T05:16:19Z")

</div>

I am using Freedom Studio with a HiFive1 rev b. I would like to build a simple program that blinks an LED on a solderless breadboard. Is there any way for me to interface with pins 1-19 like an Arduino?

Arduino provides simple commands like  
pinMode(2,INPUT);  
digitalWrite(0, HIGH);

Of course, given the fact that as of the latest release of the HiFive board, there is no way to run Arduino code directly on the board directly, I don’t expect this to be simple, especially since this product is new. I am wondering, however, if it can be done. How simple is it in C vs Rust? Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![al3x](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/al3x/32/800_2.png) [@al3x](https://forums.sifive.com/u/al3x)
#### Post date: [May 14, 2020, 5:55am UTC](https://forums.sifive.com/t/interfacing-with-the-pins/3631/2 "2020-05-14T05:55:38Z")

</div>

Hi Jonas,

I don’t know about the Freedom Studio but you want to accomplish is surely possible there. Another possibility would be to use the [Snowfox](https://snowfox-project.org/) framework where accessing GPIO pins can be done quite simple in a high-level way quite similar to Arduino.

A minimal example might look like this:

```auto
#include <snowfox/hal/riscv64/FE310/Io.h>
#include <snowfox/hal/riscv64/FE310/Clock.h>
#include <snowfox/hal/riscv64/FE310/Delay.h>
#include <snowfox/hal/riscv64/FE310/DigitalOutPin.h>
/* ... */
using namespace snowfox::hal;
/* ... */
static uint32_t const HFXOSCIN_FREQ_Hz = 16000000UL;
static uint32_t const CORECLK_FREQ_Hz = 200000000UL;
/* ... */
static uint8_t const LED_BLUE_GPIO_NUMBER = 21;
/* ... */
FE310::Delay delay;
FE310::Clock clock (&PRCI_HFXOSCCFG, &PRCI_PLLCFG, &PRCI_PLLOUTDIV, HFXOSCIN_FREQ_Hz);
FE310::DigitalOutPin led_blue (&GPIO0_INPUT_EN, &GPIO0_OUTPUT_EN, &GPIO0_IOF_EN, &GPIO0_OUTPUT_VAL, LED_BLUE_GPIO_NUMBER );
/* ... */
int snowfox_main()
{
  clock.setClockFreq(static_cast<uint8_t>(FE310::ClockId::coreclk), CORECLK_FREQ_Hz);

  for(;;)
  {
    led_blue.set();
    delay.delay_ms(250);
    led_blue.clr();
    delay.delay_ms(250);
  }

  return 0;
}

```

For a full working example take a look [here](https://github.com/snowfox-project/snowfox-examples/blob/master/hal/FE310/hal-fe310-digital-out-pin/hal-fe310-digital-out-pin.cpp).

If you want to compile it for your HiFive 1 Rev. B you’ve got to follow the following steps:

- Download/Install RISCV64 Compiler (you probably already did that)

```bash
sudo wget https://static.dev.sifive.com/dev-tools/riscv64-unknown-elf-gcc-8.2.0-2019.02.0-x86_64-linux-ubuntu14.tar.gz
sudo tar -xzvf riscv64-unknown-elf-gcc-8.2.0-2019.02.0-x86_64-linux-ubuntu14.tar.gz
export PATH=$PATH:/opt/riscv64-unknown-elf-gcc-8.2.0-2019.02.0-x86_64-linux-ubuntu14/bin

```

- Clone the Snowfox Repository with all examples

```bash
git clone --recurse-submodules https://github.com/snowfox-project/snowfox.git
cd snowfox

```

- Build the SiFive FE310-G002 GPIO example (the compiled binary is located in `build/bin/hal-fe310-digital-out-pin )

```bash
.ci/script/run-build-example.sh examples/hal/FE310/hal-fe310-digital-out-pin

```

As for getting the number of a specific pin exposed on the HiFive 1 Rev. B header - you can look up the exact GPIO number by taking a look at the [schematic](https://sifive.cdn.prismic.io/sifive%2Fa4546ced-0922-4d87-9334-e97c1a9fd9a5_hifive1.b01.schematics.pdf) / page 3. It would be an easy thing to create a header file containing constants for each GPIO pin exposed on a header, e.g.

```auto
/* ... */
enum class HiFive1RevBGpioPin : uint32_t
{
  D2 = 18, /* GPIO_18 */
  D3 = 19, /* GPIO_19 */
  /* etc. */
};
/* ... */

```

[Pull requests](https://github.com/snowfox-project/snowfox/pulls) are always welcome 😉

---

<div class="post-metadata">

### Author: ![Jonas](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonas](https://forums.sifive.com/u/Jonas)
#### Post date: [May 14, 2020, 9:48pm UTC](https://forums.sifive.com/t/interfacing-with-the-pins/3631/3 "2020-05-14T21:48:36Z")

</div>

It’s interesting. I actually imported these as source files in scifive to see if the libraries would work in the IDE. The only problem seems to be that the code above is written in c++ rather than C. Does snowfox support C? I think that’s all I would need. Granted, I haven’t tried it the way you suggested. I’m not complainin, I’m just curious.

---

<div class="post-metadata">

### Author: ![al3x](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/al3x/32/800_2.png) [@al3x](https://forums.sifive.com/u/al3x)
#### Post date: [May 15, 2020, 4:17am UTC](https://forums.sifive.com/t/interfacing-with-the-pins/3631/4 "2020-05-15T04:17:57Z")

</div>

Hi Jonas 👋 You’ll be definitely able to integrate Snowfox code within the Freedom SDK / Studio (and compile them) but I doubt it’s the wisest course of action. Both Freedom SDK and Snowfox provide HAL (Hardware Abstraction Layer) functionality but each of those projects is self-contained and follows its own design philosophy. Wildly mixing together without knowing what you are doing is a guarantee for a headache 😉

---

<div class="post-metadata">

### Author: ![Jonas](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonas](https://forums.sifive.com/u/Jonas)
#### Post date: [May 15, 2020, 4:29am UTC](https://forums.sifive.com/t/interfacing-with-the-pins/3631/5 "2020-05-15T04:29:10Z")

</div>

Noted. Naturally, I like working within a GUI workspace because of all the tools and convenience, but I will give your advice a try in the terminal.

---

<div class="post-metadata">

### Author: ![al3x](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/al3x/32/800_2.png) [@al3x](https://forums.sifive.com/u/al3x)
#### Post date: [May 15, 2020, 5:23am UTC](https://forums.sifive.com/t/interfacing-with-the-pins/3631/6 "2020-05-15T05:23:29Z")

</div>

Hi Jonas 👋

I can understand your reluctance to deviate from GUI tools because I myself like to work with a good GUI when its available. Fortunately the HiFive 1 Rev. B is supported by [Segger Ozone](https://www.segger.com/products/development-tools/ozone-j-link-debugger/) which is one of the best debugging IDEs that’s around. In order to get started you have to:

- Download & Install [Ozone](https://www.segger.com/downloads/jlink/#Ozone)

- Start it up and select the FE310 MCU

![fe310-startup-01](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/6/6589c754f8dfcb4b0802cd68859143831e209099.png)

- Connect the HiFive 1 Rev. B and check if the JLink on the HiFive is detected

![fe310-startup-02](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/5/532dd89068bd3d21a2875803e7f4914f6f61dcc5.png)

- Select the binary file for upload/debugging ( `snowfox/build/bin/hal-fe310-digital-out-pin` )

![fe310-startup-03](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/d/dde222e5346de7c1733e64b7c48539dbf801e767.png)

- Download binary via `Download & Reset Programm` and perform a reset and halt in order to start debugging from the very beginning via `Reset & Halt`

 ![fe310-startup-04](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/e/e42cc7927445cf9d9d19f728cbba94e36dba839e.png)

 ![Screenshot from 2020-05-15 07-22-08](https://us1.discourse-cdn.com/flex020/uploads/sifive/original/2X/5/50d3b8dec55fb56034dc440a20adc6e7168b79d2.png)

Have fun 😉
