FE310G: an open source RISC-V microcontroller – IDE

In my previous articles I presented the RISC-V CPU, its interrupt system and FE310G’s main features. Now it’s time to take a look at Freedom Studio, SiFive’s Eclipse-based IDE which integrates a full GCC-based RISC-V toolchain. We will also take a look at HiFive1, a small and cheap FE310G-based development board.

Freedom Studio

As I said before, Freedom Studio is an Eclipse-based IDE which is a “De facto” IDE standard nowadays so I believe most people will feel pretty comfortable using it. Freedom Studio includes a GCC-based toolchain including a GDB debugger. SiFive also provides an OpenOCD version supporting RISC-V processors. There are versions for Linux, MacOS and Windows, in this article we are using Freedom Studio v20170616 (beta 2) which includes a new CDT build plugin.

Figure 1 – Freedom Studio

The IDE includes a wizard to create new projects, but unfortunately it doesn’t generate all the tens of options needed to properly configure a project, nor it generates debugger configuration files and the necessary BSP files in order to work with the internal peripherals. So what I recommend is to copy an existing project whenever you want to start a new project. All you have to do is to open the desired project, right-click on the project name and select “copy” on the pop-up menu. Then right-click on an empty area within the project explorer and select “paste” on the pop-up menu. Eclipse will create a full copy of the project which you can rename and start using!

As an example, we are going to use a simple Hello World application which writes a string on the standard output device (UART0). The main source code for our hello application (available at our repository) is show below:

#include <stdio.h>
#include <stdint.h>
#include "platform.h"

void RISCV_DelayMs(uint32_t time)
{
	uint64_t targetTime = ((time * 32768) / 1000) + get_timer_value();
	while (get_timer_value() < targetTime);
}

int main()
{
	while (1){
		printf("Hello World from RISC-V!\r\n");
		RISCV_DelayMs(1000);
	}
}

UART0 is configured by the BSP to operate at 115,200bps  (function _init() in bsp/env/freedom-e300-hifive1/init.c) as shown below:

void _init()
{
  #ifndef NO_INIT
  use_default_clocks();
  use_pll(0, 0, 1, 31, 1);
  uart_init(115200);
 
  printf("core freq at %d Hz\n", get_cpu_freq());
 
  write_csr(mtvec, &trap_entry);
  if (read_csr(misa) & (1 << ('F' - 'A'))) { // if F extension is present
    write_csr(mstatus, MSTATUS_FS); // allow FPU instructions without trapping
    write_csr(fcsr, 0); // initialize rounding mode, undefined at reset
  }
  #endif
}

The same function also includes a printf() which prints out the current core frequency. You can comment out that function to reduce code size if printf() is not being used within your application.

Figure 2 shows the project settings window, where the user can select several project options. Notice it is possible to select the desired target RISC-V architecture as well as enable/disable hardware extensions. Also notice the hardware floating point support is disabled (none) as there is no hardware FPU in the FE310G microcontroller.

Figure 2 – Build settings

Debugging the FE310G is simple but presents some limitations. In order to debug a project, first build it (build all option/button) and then go to the debug configurations and select a configuration under GDB OpenOCD Debugging (or create one). Make sure you select the right executable and linkable format (elf) file. If no elf file is shown, press “Search Project…” button to locate the file.

Figure 3 – Selecting the right debugging configuration

Once you set the debug configuration file, press “Debug” button to initiate a debugging session. OpenOCD driver will communicate with the HiFive1 board to program the microcontroller. After code downloading, Freedom Studio IDE should present a screen such as the one in figure 4. You can then press resume button (F8) to resume code execution, run the program step-by-step, inspect variables, etc.


Figure 4 – Debug session successfully started

Running the program will result in a message to be sent through FE310G’s UART0. The UART is wired to the  serial-to-USB FTDI converter chip which will create a virtual comm port (VCP) on the host computer. You can use your preferred serial terminal program to communicate with the board, I chose moserial, which includes some interesting features such as ASCII/HEX transmission/receiving, file transfer, etc.

Figure 5 – RISC-V says hi!

Some important remarks about Freedom Studio debugging capabilities:

  1. Peripheral registers currently are not shown within peripherals view (making it useless). Right now, the only ways to inspect peripheral registers are by directly inspecting memory or by using a variable (such as a pointer) to map a peripheral register as a user variable;
  2. Scrolling down CPU registers beyond register x31 results in an error when the debugger tries to read non-existent CPU registers;
  3. Sometimes the debugger cannot communicate with the target microcontroller. In such cases, you must put the board into a safe boot mode by pressing the reset button one time and as soon as the RGB LED blinks green, press reset button again, the board will enter a safe mode where user code is not executed (RGB LED quickly fading red), allowing the debugger successfully communicating with the target microcontroller (see figure 6).

Figure 6 – Boot in safe mode

HiFive1 board

SiFive currently showcases its RISC-V silicon on a small and cheap board named HiFive1. It includes an FE310G microcontroller paired with a 128Mbit QSPI Flash memory (IS25LP128) which acts as program memory storage, a 16MHz oscillator (the main external oscillator) and a 32,768Hz oscillator (which feeds the internal machine timer).

Figure 7 – HiFive1 development board

The board includes Arduino headers which are connected to the FE310G by using three level shifters (they can be configured to work at 3.3 or 5V). The Arduino headers can be used to connect most Arduino shields to the board (excepting those which rely on A/D inputs which are not implemented on the FE310G).

An RGB LED is connected to GPIO 19 (green), 21 (blue) and 22 (red) and can provide simple feedback on applications. There are two onboard push-buttons, one for reset and the other for wake-up (it is connected to FE310G’s PMU wake-up input).

Finally, there is also an FTDI FT2232 USB chip which manages debugging and VCP communication.

Closing

RISC-V is a very nice platform with a powerful and flexible royalty-free instruction set, it is supported by major silicon manufacturers (such as Google, AMD, Qualcomm, Samsung, etc), has a nice open-source toolchain with a good IDE and also supports running Linux, FreeRTOS, etc.

RISC-V is being selected as part of several SoC projects and maybe, sometime in the future, we will see a major manufacturer releasing a commercial general-purpose microcontroller family.

My next article on RISC-V will show how to use the timer to generate periodic interrupts and PWM, see you there!

References

Leave a Reply