August 12, 2022

Raspberry Pi Pico - Rust, Pico and GDB

This page is all about seeing if I can follow what this fellow does in this article: I am not doing anything with the Mac M1, so I hope that doesn't turn out to be a show stopper.

So far I have avoided anything to do with Rust. Mostly I just don't want to add yet another language to the dozen or so I regularly work with.

But as a brief aside, where does Rust fit in the C and C++ world? C++ is a mess. My take is that if you are doing embedded work, you will avoid C++ like the plague and use C. Rust offers the potential for using a language with object oriented features in an embedded project. Just the fact that the linux kernel developers have now started using Rust for some things says something. That aside, Rust is a new language with modern features, while C++ has always been a kludge at best (the old "nail six legs on a dog to create an octopus joke").

A rp2040 rust project

The following will be my starting point: They say you will need the standard "rust tooling" as in cargo and rustup and tell you to install them from here: On the other hand, I see rust in the fedora packages. The following instructions show two different paths. The first says that rust is not in the Fedora 38 packages, but I see both cargo and rust there (although not "rustup"). I apparently already have rust installed as "rustc", so I install cargo via "dnf install cargo". The rustup package is another approach that uses the standard rust distribution channels, bypassing the Fedora package system. I intend to avoid it, at least for now.

So I clone the "template" project:

cd /u1/Projects/rp2040
git clone --depth=1 git@github.com:rp-rs/rp2040-project-template.git
ln -s rp2040-project-template rust
cd rust
I bravely try "cargo build", but it fails, in particular it mentions that "thumbv6m-none-eabi" target is not installed. The "lesson" I am following tells me to do "rustup target add thumbv6m-none-eabi" to get that. So, let's try the other way of doing things.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
It warns me that I have an existing rust installed and asks if I want to continue. I say no, and do this:
su
dnf erase rust cargo
exit
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
I am doing this as user "tom" and it tells me it will put stuff in /home/tom/.rustup and /home/tom/.cargo and that it will modify my .bashrc to add .cargo/bin to my path.
I can revert changes via: "rustup self uninstall".
The install goes quickly and of course I need to start a new shell to get the stuff in .bashrc.

Now I am back to the instructions in the "lesson", I do this:

cd /home/tom
rustup target add thumbv6m-none-eabi
cargo install flip-link
cargo install probe-run
cargo install elf2uf2-rs --locked
These all pull in and even compile quite a lot. But without any errors.
Now:
cd /u1/Projects/rp2040/rust
cargo build
It all goes just fine. When done, the result is an elf file:
/u1/Projects/rp2040/rust/target/thumbv6m-none-eabi/debug
-rwxr-xr-x  2 tom tom 2291980 Aug 12 14:17 rp2040-project-template

Pretty big, eh?
So in one terminal I run:

openocd -f interface/cmsis-dap.cfg -c "adapter speed 5000" -f target/rp2040.cfg
In another I run:
cd /u1/Projects/rp2040/rust
arm-none-eabi-gdb -q -ex "target extended-remote :3333" target/thumbv6m-none-eabi/debug/rp2040-project-template
Once I see the "(gdb) " prompt, I do:
(gdb) load
Loading section .boot2, size 0x100 lma 0x10000000
Loading section .vector_table, size 0xc0 lma 0x10000100
Loading section .text, size 0x2338 lma 0x100001c0
Loading section .rodata, size 0x6c0 lma 0x10002500
Loading section .data, size 0x38 lma 0x10002bc0
Start address 0x100001c0, load size 11248
Transfer rate: 11 KB/sec, 2249 bytes/write.
(gdb) continue
Continuing.
And indeed the LED on my target board begins blinking

Conclusion

It all seems to work. And now I can play with rust should I care to. I have also done something a bit more exotic with openocd and gdb, though I am certainly not doing any gdb debugging.


Have any comments? Questions? Drop me a line!

Tom's electronics pages / tom@mmto.org