HiFive-1 Bare Bones: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
add maintenance template
 
(5 intermediate revisions by 4 users not shown)
Line 1:
{{BeginnersWarning}}
= HiFive1 Bare Bones =
{{FirstPerson}}
{{TutorialExplain}}
{{You}}
{{Rating|2}}
 
If you are looking to get a [[RISC-V]] board, the HiFive1 is a relatively easy (albeit slightly expensive at US$69) way to get a RISC-V board. This Bare Bones tutorial is an attempt at bringing up the HiFive-1 board and getting ready to start some OS development as you would on most other platforms.
Line 15 ⟶ 19:
First, in order to compile for the HiFive-1 Board, you need to download and build the SDK:
 
<syntaxhighlight lang="bash">
<pre>
$ git clone --recursive https://github.com/sifive/freedom-e-sdk.git
$ cd freedom-e-sdk
$ make tools
</syntaxhighlight>
</pre>
 
The SDK builds a bunch of stuff we do not really need (including a [[C Library|Newlib]] port that lets the developer write basic C programs and run it bare-metal). Unfortunately, I did not yet invest the time to turn off those unneeded features, so for now we will need to wait for a full build which does take some time. Grab a tea or coffee.
Line 31 ⟶ 35:
First, create a file named main.c which will serve as both the entry point from the firmware and the starting point for the kernel you will be writing.
 
<sourcesyntaxhighlight lang="c">
#include <stdint.h>
#include <stddef.h>
Line 203 ⟶ 207:
main();
}
</syntaxhighlight>
</source>
 
Next, we need to do some linker script magic to make sure that our code is positioned where it should be. It's worth noting that the firmware seems to perform an unconditional jump to 0x204000000 and expects the application to continue from there. Thus, in the above code we place the _start() function into a special section (aptly named, ".init") which we will move to the correct address via the linker script:
 
<sourcesyntaxhighlight lang="textc">
OUTPUT_ARCH("riscv")
 
Line 291 ⟶ 295:
} >ram AT>ram :ram
}
</syntaxhighlight>
</source>
 
Now this linker script also provides a few extra symbols that can come handy when developing your OS. In particular, it gives your OS a stack location!
Line 297 ⟶ 301:
Now that we have the kernel source code and we have a linker script, what remains is building our image. First, however, let's setup a few environment variables:
 
<sourcesyntaxhighlight lang="bash">
$ export SDK=/the/location/where/your/freedom/sdk/lies
$ export SDK_PREFIX=$(SDK)/work/build/riscv-gnu-toolchain/riscv64-unknown-elf/prefix/bin
$ export CROSS=$(SDK_PREFIX)/riscv64-unknown-elf-
$ export OPENOCD=$(SDK)/work/build/openocd/prefix/bin/openocd
</syntaxhighlight>
</source>
 
Modify the $SDK variable to where your SDK is cloned to.
Line 308 ⟶ 312:
Finally, we can now proceed to build our image, simply:
 
<sourcesyntaxhighlight lang="bash">
$ ${CROSS}gcc main.c \
-g \
Line 317 ⟶ 321:
-T linker.lds \
-nostartfiles
</syntaxhighlight>
</source>
 
Before you execute that command, you might be wondering what those options are.
Line 338 ⟶ 342:
To test on real hardware, you will need to first plug your HiFive-1 board to your computer of choice (preferably the one with the freshly compiled main.img from the previous steps. :-) ).
 
Before you can flash the kernel to the board, however, you will need an OpenOCD specification for the said board. You can find one for the HiFive-1 board [https://github.com/sifive/freedom-e-sdk/blob/mastere582766d596dcaa303634c121638b8f614ffd4c7/bsp/env/freedom-e300sifive-hifive1/openocd.cfg here]. (I am unable to directly include it, because its license is not CC0-compatible, thus you'll need to grab it yourself).
 
Once you have this, the process of flashing an image to the board is relatively quick:
 
<sourcesyntaxhighlight lang="bash">
$ ${OPENOCD} \
-f openocd.cfg \
Line 349 ⟶ 353:
resume 0x20400000;\
exit"
</syntaxhighlight>
</source>
 
In this command, we first turn off the board's flash protection, program the board with the kernel and verify that the hashes match, and resume from the starting point. Finally, we exit the on-chip debugger.
Line 355 ⟶ 359:
If all went well, you are done and the UART (which you can observe via minicom) will now output the following:
 
<pre>
<source lang="text">
Hello, world!
This is myOS on the HiFive-1 Board!
</pre>
 
</source>