{"id":3435,"date":"2020-06-22T21:09:52","date_gmt":"2020-06-23T04:09:52","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=3435"},"modified":"2020-06-23T18:32:48","modified_gmt":"2020-06-24T01:32:48","slug":"developing-risc-v-firmware-with-visual-studio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/risc-v\/","title":{"rendered":"Developing RISC-V firmware with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to develop firmware for the open-source <a href=\"https:\/\/riscv.org\/\">RISC-V core<\/a> using Visual Studio and VisualGDB. We will create a basic project for the <a href=\"https:\/\/www.sifive.com\/products\/hifive1\/\">HiFive1 board<\/a> 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.<\/p>\n<p>Before you begin, install VisualGDB 5.5 or later.<\/p>\n<ol>\n<li>Start Visual Studio and begin creating a project with VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/01-newprj-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6363\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/01-newprj-1.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>Enter the name and location for your project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/02-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6364\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/02-name.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>Proceed with the default &#8220;Create a new project with MSBuild&#8221; settings:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/03-msbuild.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6365\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/03-msbuild.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select the RISC-V toolchain. If you have not installed it yet, simply select &#8220;download toolchain for risc-v&#8221; and VisualGDB will install and configure it automatically:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/04-riscv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6366\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/04-riscv.png\" alt=\"\" width=\"887\" height=\"694\" \/><\/a>Make sure you are using the toolchain based on GCC 10.1 or later, as the earlier versions are less reliable.<\/li>\n<li>Once the toolchain is installed, VisualGDB will show the devices compatible with it and will allow automatically downloading the related SDKs. Select the SIFIVE-HIFIVE1 device and click &#8220;Install&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/05-install.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6368\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/05-install.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Once the HiFive SDK is installed, proceed with the default device settings:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/06-hifive.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6369\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/06-hifive.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page proceed with the default settings for the sample project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/07-gpio.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6370\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/07-gpio.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>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 &#8220;Test&#8221; to test the connection:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/08-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6371\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/08-debug.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a>VisualGDB will automatically install any missing drivers so that OpenOCD can access your board out-of-the-box.<\/li>\n<li>Once the driver is installed, VisualGDB will confirm that the connection between the JTAG debugger and the chip is successful:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/tested.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6372\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/tested.png\" alt=\"\" width=\"786\" height=\"593\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Then build it with Ctrl-Shift-B:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/09-build-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6373\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/09-build-1.png\" alt=\"\" width=\"1075\" height=\"746\" \/><\/a><\/li>\n<li>Set a breakpoint inside the <strong>main()<\/strong> function and press F5 to begin debugging. VisualGDB will automatically program the FLASH memory and start debugging your program. The breakpoint will hit:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/10-bkpt-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6374\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/10-bkpt-1.png\" alt=\"\" width=\"1075\" height=\"746\" \/><\/a><\/li>\n<li>Remove the breakpoint and resume the program. Observe how the on-board LED changes its color periodically:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/board2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3452\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/board2.jpg\" alt=\"board\" width=\"1604\" height=\"805\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/board2.jpg 1604w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/board2-300x151.jpg 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/board2-1024x514.jpg 1024w\" sizes=\"(max-width: 1604px) 100vw, 1604px\" \/><\/a><\/li>\n<li>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 <strong>timer_isr()<\/strong> function to increase the counter on each invocation. Then press F5 to automatically build the updated program and begin debugging it. Then use the Live Watch window to plot the value of <strong>g_Counter<\/strong> while the program is running:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/11-live.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6375\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/11-live.png\" alt=\"\" width=\"1075\" height=\"746\" \/><\/a>You can add multiple variables to the Live Variables window and plot all of them.<\/li>\n<li>The VisualGDB project includes the SiFive bare metal library. You can use CodeJumps to quickly explore the relations between different functions in the library and in your program:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/12-jumps.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6376\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/12-jumps.png\" alt=\"\" width=\"1075\" height=\"746\" \/><\/a><\/li>\n<li>You can get a detailed memory utilization report using the VisualGDB&#8217;s Embedded Memory Explorer window. It can show per-file, per-section and per-symbol statistics, and also highlight differences between similar builds:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/13-memexp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6377\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/10\/13-memexp.png\" alt=\"\" width=\"1075\" height=\"746\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to develop firmware for the open-source RISC-V core using Visual Studio and VisualGDB. We will create<\/p>\n","protected":false},"author":1,"featured_media":3450,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[59,151],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3435"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=3435"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3435\/revisions"}],"predecessor-version":[{"id":6378,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/3435\/revisions\/6378"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media\/3450"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=3435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=3435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=3435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}