embedded rust – rust on iot devices

50
@choas #Devoxx #embeddedRust Embedded Rust Rust on IoT devices Lars Gregori SAP Hybris

Upload: lars-gregori

Post on 12-Apr-2017

338 views

Category:

Technology


10 download

TRANSCRIPT

@choas#Devoxx #embeddedRust

Embedded Rust Rust on IoT devices

Lars Gregori SAP Hybris

@choas#Devoxx #embeddedRust

Hybris Labs Prototypeshttps://labs.hybris.com/prototype/

@choas#Devoxx #embeddedRust

MotivationFunctional 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

letv=vec![1,2,3];letv2=v;//…println!("v[0]is:{}",v[0]);

Ownership

@choas#Devoxx #embeddedRust

letv=vec![1,2,3];letv2=v;//…println!("v[0]is:{}",v[0]);

Ownership

ERROR use of moved value: `v`

@choas#Devoxx #embeddedRust

letv=vec![1,2,3];letv2=v;take_something(v2);println!("v[0]is:{}",v[0]);

Ownership

ERROR use of moved value: `v`

@choas#Devoxx #embeddedRust

letv=1;letv2=v;println!("vis:{}",v);

Ownership

@choas#Devoxx #embeddedRust

letv=1;letv2=v;println!("vis:{}",v);

Ownership

Copy Type

OK

@choas#Devoxx #embeddedRust

fnfoo(v1:Vec<i32>,v2:Vec<i32>)->(Vec<i32>,Vec<i32>,i32){//dostuffwithv1andv2(v1,v2,42)//handbackownership,andtheresult}

letv1=vec![1,2,3];letv2=vec![1,2,3];let(v1,v2,answer)=foo(v1,v2);

Ownership

@choas#Devoxx #embeddedRust

fnfoo(v1:&Vec<i32>,v2:&Vec<i32>)->i32{//dostuffwithv1andv242//returntheanswer}

letv1=vec![1,2,3];letv2=vec![1,2,3];letanswer=foo(&v1,&v2);//wecanusev1andv2here!

References and Borrowing

@choas#Devoxx #embeddedRust

fnfoo(v:&Vec<i32>){v.push(5);}letv=vec![];foo(&v);

References and Borrowing

@choas#Devoxx #embeddedRust

fnfoo(v:&Vec<i32>){v.push(5);}letv=vec![];foo(&v);

References and Borrowing

ERROR cannot borrow immutable borrowed content `*v` as mutable

@choas#Devoxx #embeddedRust

letmutx=5;{lety=&mutx;*y+=1;}println!("{}",x);

References and Borrowing

@choas#Devoxx #embeddedRust

letmutx=5;{lety=&mutx;*y+=1;}println!("{}",x);

References and Borrowing

OK: 6

@choas#Devoxx #embeddedRust

letmutx=5;{lety=&mutx;*y+=1;}println!("{}",x);

References and Borrowing

mut{

}

@choas#Devoxx #embeddedRust

References and BorrowingThe 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

letmutx=5;

lety=&mutx;*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.rsZinc 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

Rustuphttps://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 Toolchainhttps://launchpad.net/gcc-arm-embedded/+download

arm-none-eabi-gccarm-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…-cgdb_port3333

>arm-none-eabi-gdb(gdb)targetremote:3333(gdb)file./target/thumbv7em-none-eabi/debug/blink_stm32f4(gdb)breakmain(gdb)continue(gdb)next(gdb)setdelay=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!