Sunday, 30 October 2016

Raspberry Rust

I'm a big fan of Rust, the new systems programming language from Mozilla, and I'm a big fan of the Raspberry Pi, so I thought I'd write a few notes on running Rust on the Raspberry Pi!

Once upon a time installing Rust was a little but messy, but now there's rustup-rs - a Rust install tool written in Rust!

Step 1. Get a Raspberry Pi, boot it up, and get yourself a terminal window.

Step 2. Run this command:

curl -sSf | sh

The installer will check out your architecture (armv7l for a Pi 2 or Pi 3, or armv6 for an original Pi) and download a pre-compiled copy of Rust, ready to run.

pi@boron:~ $ curl -sSf | sh

info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming 
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin 
directory, located at:


This path will then be added to your PATH environment variable by modifying the
profile file located at:


You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

WARNING: This is beta software.

Current installation options:

    default host triple: armv7-unknown-linux-gnueabihf
      default toolchain: stable
   modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation

<press 1 followed by enter here>

info: syncing channel updates for 'stable-armv7-unknown-linux-gnueabihf'
info: downloading component 'rustc'
 44.7 MiB /  44.7 MiB (100 %)   3.7 MiB/s ETA:   0 s                
info: downloading component 'rust-std'
 61.4 MiB /  61.4 MiB (100 %)   4.0 MiB/s ETA:   0 s                 
info: downloading component 'rust-docs'
  7.5 MiB /   7.5 MiB (100 %)   1.2 MiB/s ETA:   0 s                
info: downloading component 'cargo'
  4.1 MiB /   4.1 MiB (100 %) 933.3 KiB/s ETA:   0 s                
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'rust-docs'
info: installing component 'cargo'
info: default toolchain set to 'stable'

   stable installed - rustc 1.12.1 (d4f39402a 2016-10-19)

Rust is installed now. Great!

To get started you need Cargo's bin directory in your PATH environment 
variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env.

Step 3. Write some Rust! First, use Cargo (the Rust package manager) to create you a new 'crate'. A 'crate' is what Rust calls packages, and cargo is how you download crates and build them. Yes, it's all very pun-tastic. Anyway, we use the 'cargo new --bin' command to create a new binary crate - that is, it's a crate that produces a program we can run, as opposed to a library crate we can use in other crates later on.

pi@boron:~ $ source $HOME/.cargo/env
pi@boron:~ $ cargo new --bin my_first_project
     Created binary (application) `my_first_project` project
pi@boron:~ $ cd my_first_project/
pi@boron:~/my_first_project $ cargo run
   Compiling my_first_project v0.1.0 (file:///home/pi/my_first_project)
    Finished debug [unoptimized + debuginfo] target(s) in 8.79 secs
     Running `target/debug/my_first_project`
Hello, world!
pi@boron:~/my_first_project $

Step 4. So what did we just do? Well 'cargo new' create a Hello World example for us, and then 'cargo run' compiled our source code into an executable and then executed it. Let's have a look at the example:
pi@boron:~/my_first_project $ cat src/
fn main() {
    println!("Hello, world!");
If we'd created a library crate instead of a binary create, it would have created a '' instead of a ''.

Step 5. Go learn Rust! Hopefully you've seen that Rust on the Raspberry Pi is very easy to get started with, and there's nothing Pi specific about it. Go and look at the Book, and the API guide, the User Forum, the /r/rust Reddit. If you've got 30 minutes, I've got a brief presentation on Rust from the point of view of a C programmer, and there are many more on YouTube. Stop wasting precious CPU cycles on the Python interpreter and stop writing buggy C programs full of hard-to-find race hazards and buffer overflows - Rust ftw!

Sunday, 23 October 2016

Freescale Kinetis

It appears some of Freescale's Kinetis line of ARM microcontrollers are available in 5V variants. Specifically, the KE02, the KE04 and the KE06.

Lots of I/O, PWM, USB interface/debug via a separate (actually much more powerful) MCU, Cortex-M0+ (which means Rust support, via the thumbv6m-none-eabi target). Could this be the 5V replacement for the Stellaris Launchpad I've been looking for?