@choas#Devoxx #embeddedRust
Embedded Rust 

Rust on IoT devices
Lars Gregori
SAP Hybris
@choas#Devoxx #embeddedRust
Hybris Labs Prototypes
https://labs.hybris.com/prototype/
@choas#Devoxx #embeddedRust
Motivation
Functional IoT (http://fpiot.metasepi.org/):
Imagine IoT devices are:
• connected to the internet
• developed in a short time
• storing personal data
• secure
• more intelligence
• inexpensive
Can C design IoT devices like that? No, can’t.
@choas#Devoxx #embeddedRust
Agenda
• Rust Overview
• Embedded Rust
• Cross Compiling
• MCU Debugging
@choas#Devoxx #embeddedRust
Rust Overview
@choas#Devoxx #embeddedRust
rust-lang.org
@choas#Devoxx #embeddedRust
rust-lang.org
Rust is a systems programming language
that runs blazingly fast, prevents segfaults,
and guarantees thread safety.
@choas#Devoxx #embeddedRust
Rust Overview
Rust FAQ
@choas#Devoxx #embeddedRust
Rust FAQ
• safe, concurrent, practical system language

• “We do not intend to be 

100% static, 

100% safe, 

100% reflective, 

or too dogmatic in any other sense.”

• Mozilla Servo
@choas#Devoxx #embeddedRust
Rust FAQ
• multi-paradigm
• OO
• no garbage collection
• strong influences from the world of functional programming

• built on LLVM
@choas#Devoxx #embeddedRust
Rust FAQ
• Cross-Platform
• Android, iOS, …

• Conditional compilation

#[cfg(target_os = "macos")]
@choas#Devoxx #embeddedRust
Rust Overview
Control & Safety
@choas#Devoxx #embeddedRust
Control & Safety
C/C++
Safety
Control
Haskell
Go
Java
Python
@choas#Devoxx #embeddedRust
Control & Safety
C/C++
Safety
Control
Haskell
Go
Java
Rust
Python
@choas#Devoxx #embeddedRust
Rust Overview
Rust Concepts
@choas#Devoxx #embeddedRust
Rust Concepts
• ownership, the key concept
• borrowing, and their associated feature ‘references’
• lifetimes, an advanced concept of borrowing



‘fighting with the borrow checker’
https://doc.rust-lang.org/book/
@choas#Devoxx #embeddedRust
let	v	=	vec![1,	2,	3];	
let	v2	=	v;	
//	…	
println!("v[0]	is:	{}",	v[0]);
Ownership
@choas#Devoxx #embeddedRust
let	v	=	vec![1,	2,	3];	
let	v2	=	v;	
//	…	
println!("v[0]	is:	{}",	v[0]);
Ownership
ERROR

use of moved value: `v`
@choas#Devoxx #embeddedRust
let	v	=	vec![1,	2,	3];	
let	v2	=	v;	
take_something(v2);	
println!("v[0]	is:	{}",	v[0]);
Ownership
ERROR

use of moved value: `v`
@choas#Devoxx #embeddedRust
let	v	=	1;	
let	v2	=	v;	
println!("v	is:	{}",	v);
Ownership
@choas#Devoxx #embeddedRust
let	v	=	1;	
let	v2	=	v;	
println!("v	is:	{}",	v);
Ownership
Copy Type
OK
@choas#Devoxx #embeddedRust
fn	foo(v1:	Vec<i32>,	v2:	Vec<i32>)	->	(Vec<i32>,	Vec<i32>,	i32)	{	
				//	do	stuff	with	v1	and	v2	
				(v1,	v2,	42)	//	hand	back	ownership,	and	the	result	
}	
let	v1	=	vec![1,	2,	3];	
let	v2	=	vec![1,	2,	3];	
let	(v1,	v2,	answer)	=	foo(v1,	v2);	
Ownership
@choas#Devoxx #embeddedRust
fn	foo(v1:	&Vec<i32>,	v2:	&Vec<i32>)	->	i32	{	
				//	do	stuff	with	v1	and	v2	
				42	//	return	the	answer	
}	
let	v1	=	vec![1,	2,	3];	
let	v2	=	vec![1,	2,	3];	
let	answer	=	foo(&v1,	&v2);	
//	we	can	use	v1	and	v2	here!
References and Borrowing
@choas#Devoxx #embeddedRust
fn	foo(v:	&Vec<i32>)	{	
					v.push(5);	
}	
let	v	=	vec![];	
foo(&v);
References and Borrowing
@choas#Devoxx #embeddedRust
fn	foo(v:	&Vec<i32>)	{	
					v.push(5);	
}	
let	v	=	vec![];	
foo(&v);
References and Borrowing
ERROR

cannot borrow immutable borrowed content `*v` as mutable
@choas#Devoxx #embeddedRust
let	mut	x	=	5;	
{	
				let	y	=	&mut	x;	
				*y	+=	1;	
}	
println!("{}",	x);
References and Borrowing
@choas#Devoxx #embeddedRust
let	mut	x	=	5;	
{	
				let	y	=	&mut	x;	
				*y	+=	1;	
}	
println!("{}",	x);
References and Borrowing
OK: 6
@choas#Devoxx #embeddedRust
let	mut	x	=	5;	
{	
				let	y	=	&mut	x;	
				*y	+=	1;	
}	
println!("{}",	x);
References and Borrowing
mut
{
}
@choas#Devoxx #embeddedRust
References and Borrowing
The Rules for borrowing:


1.Any borrow must last for a scope no greater than that of the owner.

2. Only one of these kinds of borrows, but not both at the same time:
• one or more references (&T) to a resource
• exactly one mutable reference (&mut T)
@choas#Devoxx #embeddedRust
let	mut	x	=	5;	
				let	y	=	&mut	x;	
				*y	+=	1;	
println!("{}",	x);
References and Borrowing
ERROR

cannot borrow `x` as immutable 

because it is also borrowed as mutable
@choas#Devoxx #embeddedRust
Agenda
• Rust Overview
• Embedded Rust
• Cross Compiling
• MCU Debugging
@choas#Devoxx #embeddedRust
Embedded Rust
@choas#Devoxx #embeddedRust
zinc.rs
Zinc is an experimental attempt to write an ARM stack
that would be similar to CMSIS or mbed in capabilities
but would show rust’s best safety features applied to
embedded development.

Zinc is mostly assembly-free and completely C-free at
the moment.
@choas#Devoxx #embeddedRust
STM32 Nucleo-F411RE
@choas#Devoxx #embeddedRust
STM32 Nucleo-F411RE
Demo
@choas#Devoxx #embeddedRust
@choas#Devoxx #embeddedRust
Agenda
• Rust Overview
• Embedded Rust
• Cross Compiling
• MCU Debugging
@choas#Devoxx #embeddedRust
Cross-Compiling
@choas#Devoxx #embeddedRust
Rustup
https://www.rustup.rs/
rustup is an installer for the systems programming language Rust
> rustup install nightly-2016-09-17
> cd zinc
> rustup override set nightly-2016-09-17
@choas#Devoxx #embeddedRust
GNU ARM Embedded Toolchain
https://launchpad.net/gcc-arm-embedded/+download
arm-none-eabi-gcc
arm-none-eabi-gdb
…
eabi = Embedded Application Binary Interface
@choas#Devoxx #embeddedRust
Building
cargo build --target=thumbv7em-none-eabi --features mcu_stm32f4
Demo
@choas#Devoxx #embeddedRust
@choas#Devoxx #embeddedRust
Agenda
• Rust Overview
• Embedded Rust
• Cross Compiling
• MCU Debugging
@choas#Devoxx #embeddedRust
MCU Debugging
@choas#Devoxx #embeddedRust
zinc.rs Issue #336
Failing to build example. #336
@choas#Devoxx #embeddedRust
>	openocd	…	-c	gdb_port	3333	
>	arm-none-eabi-gdb	
(gdb)	target	remote	:3333	
(gdb)	file	./target/thumbv7em-none-eabi/debug/blink_stm32f4	
(gdb)	break	main	
(gdb)	continue	
(gdb)	next	
(gdb)	set	delay	=	1000	
(gdb)	list	
(gdb)	disassemble	
Debugging
Demo
@choas#Devoxx #embeddedRust
@choas#Devoxx #embeddedRust
What we’ve seen
• Rust Overview ✔
• Embedded Rust ✔
• Cross Compiling ✔
• MCU Debugging ✔
• Hello World
@choas#Devoxx #embeddedRust
What we’ve seen
• Rust Overview ✔
• Embedded Rust ✔
• Cross Compiling ✔
• MCU Debugging ✔
• blinking Hello World ✔
@choas#Devoxx #embeddedRust
Thank you!

Embedded Rust – Rust on IoT devices