Interfacing with the Pins

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.

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 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:

#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.

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)
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
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 )
.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 / 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.

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

Pull requests are always welcome :wink:

1 Like

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.

Hi Jonas :wave: 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 :wink:

1 Like

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.

Hi Jonas :wave:

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 which is one of the best debugging IDEs that’s around. In order to get started you have to:

  • Download & Install Ozone

  • Start it up and select the FE310 MCU

fe310-startup-01

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

fe310-startup-02

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

fe310-startup-03

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

Have fun :wink:

1 Like