Rust on Longan Nano
Yesterday, I tried embedded Rust on the Feather M4. Today I thought I would try out embedded Rust on a slightly more exotic board, the Longan Nano. It is a 32-bit RISC-V board which is very inexpensive, and has a 160x80 color LCD built in.
This was mostly just a matter of following the instructions in the longan-nano crate. The only thing that stymied me for a couple minutes was that they forgot to mention that dfu-util
has to be run as root
.
I already have Rust installed on my Ubuntu 20.04 machine:
$ rustc --version
rustc 1.46.0 (04488afe3 2020-08-24)
If you don’t, you’ll need to install Rust first.
Next, following the instructions in the longan-nano crate:
$ rustup target add riscv32imac-unknown-none-elf
The next step is to install a RISC-V toolchain. The longan-nano crate recommends installing a toolchain from SiFive, but I opted to install the Ubuntu package gcc-riscv64-unknown-elf
instead. Note that despite having “64” in the name, gcc-riscv64-unknown-elf
supports both 32-bit and 64-bit RISC-V processors.
$ sudo apt-get install gcc-riscv64-unknown-elf
The next step is to install dfu-util. Because of a bug in some versions of the Longan Nano bootloader, the longan-nano crate recommends installing the development version of dfu-util
from git, which has a workaround for the bug, rather than installing a stable release of dfu-util
.
$ cd ~/src
$ git clone git://git.code.sf.net/p/dfu-util/dfu-util
$ cd dfu-util
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install
And now, back to the longan-nano crate:
$ cd ~/src
$ git clone https://github.com/riscv-rust/longan-nano
$ cd longan-nano
$ cargo build --examples --release --all-features
Because I installed a different RISC-V toolchain than they did, I had to use riscv64-unknown-elf-objcopy
instead of riscv-nuclei-elf-objcopy
:
$ riscv64-unknown-elf-objcopy -O binary target/riscv32imac-unknown-none-elf/release/examples/ferris ferris.bin
Now hold down the “BOOT0” button on the Longan Nano while connecting it to the computer’s USB port. Uploading the code to the board must be done as root
:
$ sudo dfu-util -a 0 -s 0x08000000:leave -D ferris.bin
This should display a picture of Ferris, the Rust mascot.
Update: By doing the following setup once, it’s possible to avoid having to run dfu-util
as root.
$ sudo sh -c 'cat > /etc/udev/rules.d/99-gigadevice.rules'
ATTRS{idVendor}=="28e9", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="usb", ATTRS{idVendor}=="28e9", MODE="0666"
SUBSYSTEM=="tty", ATTRS{idVendor}=="28e9", MODE="0666"
^D
$ sudo udevadm control --reload-rules
$ sudo udevadm trigger