Developing RISC-V firmware with Visual Studio

This tutorial shows how to develop firmware for the open-source RISC-V core using Visual Studio and VisualGDB. We will create a basic project for the HiFive1 board that will change the color of the on-board LED and will show how to edit it, program it into the SPI FLASH memory and easily debug it.

Before you begin, install VisualGDB 5.3 or later.

  1. Start Visual Studio and begin creating a project with VisualGDB Embedded Project Wizard:01-newprj
  2. Proceed with the default “Create a new project with MSBuild” settings:02-msbuild
  3. On the next page select the RISC-V toolchain. If you have not installed it yet, simply select “download toolchain for risc-v” and VisualGDB will install and configure it automatically:03-gettoolchain
  4. Once the toolchain is installed, select your device. In this tutorial we will use the HiFive1 board running the FE300 microcontroller (E31 core). Select “E31 (32-bit) and then pick HiFive in the “Board/Configuration” field below:04-device
  5. On the next page proceed with the default settings for the sample project:05-gpio
  6. Connect your board to the computer over USB. VisualGDB will automatically detect it and configure the debugger. Ensure that the interface and device settings match your setup and press “Test” to test the connection:06-test
  7. VisualGDB will automatically install any missing drivers so that OpenOCD can access your board out-of-the-box:07-driver
  8. Once the driver is installed, VisualGDB will confirm that the connection between the JTAG debugger and the chip is successful:08-done
  9. Press “Finish” to generate the project. Then build it with Ctrl-Shift-B:09-build
  10. Set a breakpoint inside the while(1) loop in main() and press F5 to begin debugging. VisualGDB will automatically program the FLASH memory and start debugging your program. The breakpoint will hit:10-bkpt
  11. Remove the breakpoint and resume the program. Observe how the on-board LED changes its color periodically:board
  12. Similarly to the ARM processor, the RISC-V CPU supports reading the target memory without stopping it, enabling features like Live Variables. We will now demonstrate how to use them. Modify the handle_m_time_interrupt() function to increase the counter on each invocation:11-counter
  13. Press F5 to automatically build the firmware and program the new version:12-hifive
  14. Once the firmware is running, open the Live Variables window and add “g_Counter” to it. Then enable plotting for it:13-graphVisualGDB will automatically plot the value of g_Counter without stopping the program. You can add multiple variables to the Live Variables window and plot all of them.
  15. The reset logic of the RISC-V core is not fully integrated with the debug-related logic, so resetting the device during a programming session sometimes doesn’t work. VisualGDB includes several workarounds for this issue (e.g. manual reset mode activated via the Debug Settings page). If nothing works, try using the “Program and Start Without Debugging” command, resetting the device manually and then running the “Attach to Running Embedded Firmware” command:14-menu