1.Using Freedom Studio to Create a New C Project
Direct to Table of Contents:
RISC-V Syllabus
Here, Freedom Studio is used as the development platform. It is based on the industry standard Eclipse IDE, and is compatible with RISC-V hardware boards by SiFive company. Besides, it has prebuilt RISCV GCC toolchain and emulator.
Follow the steps shown below to create a new C project in Freedom Studio.
Firstly, go to File > New > C Project, then a window will pop up as shown in Figure 1. Set the Project name to be test. For the Project type, choose Empty Project, and the Toolchains choose RISC-V Cross GCC. The other settings will remain default. Click two time Next and then Finish the setting.
Figure 1 Create a C project
Figure 2 C project setup
Figure 3 Finish the new C project setting
Right click the new created C project, and select Properties as shown in Figure 4, a window in Figure 5 will pop up.
Figure 4 Project properties
There are lots of items in Figure 5 needed to be notified. To enter the general setting interface, click Settings under C/C++ Build drop-down menu in the left column, then the corresponding setting will occur in the right part.
Firstly, click the Target Processor. Since FII RISCV is 32bit, integer-based instruction set, choose RV32I (-march=rv32i*) for Architecture. Note do not check the Multiple extension or Atomic extension below. For Integer ABI, choose ILP32 (-mabi=ilp32*). ABI (application binary interface) is a format defines the data structure and computational routines. ILP32 means int type, long type, and the pointer type in C project are all 32 bits. For small data limit, fill 4.
Figure 5 Target Processor setting
In Figure 6, for Optimization setting, choose Optimize for debug (-Og) for the Optimization Level. Check Disable builtin (-fno-builtin) to disable all replacement and inlining of standard C library functions with equivalents. The other checkboxes will remain default.
Figure 6 Optimization setting
In Figure 7, click Warnings, and click Enable all common warnings (-Wall). This option will provide common warning information. It is useful since fixing common warning will prevent the occurrence of critical errors.
Click Debugging to enter the debugging setting as shown in Figure 8. Set the Debug level to be Default (-g).
Figure 7 Warning setting
Figure 8 Debugging setting
Find General under GNU RISC-V Cross Linker, and check Do not use standard start files (-nostartfiles) as shown in Figure 9. A customized start file will be used, and will be introduced later.
Figure 9 Linker general setting
In Miscellaneous setting under GNU RISC-V Cross C Linker as shown in Figure 10, check Use newlib-nano (–specs=nano.specs), and set Other linker flags to be -t -nostdinc –entry _start -Wl, -m, elf32lriscv -Wl, -EL, -b, elf32-littleriscv -Wl, –check-sections -Wl, –wrap=printf. See Table 1 for the detailed option explanations. Other options could be found available on GNC official website.
Figure 10 Miscellaneous setting
Option | Description |
–specs=nano.specs | Add newlib-nano, nano is designed for small embedded applications |
-t | Trace file opens |
-nostdinc | Do not search the standard system directories for header files |
–entry _start | Set start address as startup.s |
-Wl | Pass the option behind to the linker |
-m | Set emulation |
Elf32lriscv | Output file in RISCV32I format |
-El | Link little-endian objects |
-b | Specify target for following input files |
Elf32-littleriscv | Output file in little-endian format |
–check-sections | Check section addresses for overlaps |
–wrap=printf | Use wrapper function for printf |
Table 1 Linker option explanation
In Figure 11, choose Raw binary for General setting under GNU RISC-V Cross Create Flash Image. This option outputs raw binary file, which can be used to program the FLASH.
Figure 11 Flash Image setting
Next, start to configure the new C project with some existing files from RISCV_Hello(It is attached at the end, and is available to download). RISCV_Hello basically prints out a sentence. See the source code from main.c for details.
#include <stdio.h> #include "fii_types.h" #include "platform.h" #define NOP_DELAY 0x100 void delay_cnt (int cnt) { unsigned int i; for(i = 0; i < cnt ; i ++ ) asm("nop"); return; }; int main(void) { while ( 1 ) { printf("Hello FII Risc-V !\r\n"); delay_cnt(NOP_DELAY); } }
Select bsp, inc, entry.S, main.S, startup.S, sys.lds, and test_dbg.cfg files, right click to select Copy on the popup menu, and then right click test project, select Paste to paste all copied files. The procedure is shown in Figure 12 -13.
Figure 12 Copy files from RISCV_Hello
Figure 13 Paste files to test
After adding the files to the test project, right click it, and select Properties as shown in Figure 4 as before. In Figure 14, click Includes setting under GNU RISC-V Cross C Compiler. In the Include paths, click the Add icon, then a popup window will show up. In the Directory column, fill ${workspace_loc:/${ProjName}/inc} or select Workspace, and choose inc under test project as shown in Figure 14. Click OK to finish.
Figure 14 Include the header files
Figure 15 Add directory path
In Figure 16, click General setting of GNU RISC-V Cross C Linker, in the right Script files, click Add icon to add path to the linker script. Fill ${workspace_loc:/${ProjName}/sys.lds} to the File , or click Workspace, and find the sys.lds under test project shown in Figure 17. Then click Apply and Close to finish the setting.
Figure 16 Include the linker script
Figure 17 Add file path
After all of the the previous steps have been done, Clean Project, Build Project, and Refresh as shown in Figure 18.
Figure 18 Clean, build, and refresh the project
Figure 19 Debug
Right click test project and select Debug As > Debug Configurations as shown in Figure 19. Figure 20 shows the debug configuration popup window. Under GDB OpenOCD Debugging, click the upper left corner icon, New launch configuration to add test Debug. Usually after building projects, with refresh, the C/C++ Application row under the Main tab will fill up the corresponding *.elf file automatically. If not, click Search Project under the blank, to search for the *.elf file. Or else, use Browse button to find the *.elf file under debug folder (this will be an absolute path).
Figure 20 Debug configurations
Figure 21 shows the Debugger setting. Fill -f test_dbg.cfg in Config options, and fill set mem inaccessible-by-default off in Commands. Then click Debug to start debugging. “set mem inaccessible-by-default off” makes GDB treat the memory not explicitly described by the memory range as RAM.
Figure 21 Debugger setting