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

Embedded Rust – Rust on IoT devices

Apr 12, 2017

Download

Technology

Lars Gregori
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Embedded Rust Rust on IoT devices

Lars Gregori SAP Hybris

Page 2: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

Page 3: Embedded Rust – Rust on IoT devices

@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.

Page 4: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

Page 5: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Rust Overview

Page 6: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

rust-lang.org

Page 7: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

rust-lang.org

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

Page 8: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Rust Overview

Rust FAQ

Page 9: Embedded Rust – Rust on IoT devices

@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

Page 10: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Rust FAQ

• multi-paradigm

• OO

• no garbage collection

• strong influences from the world of functional programming

• built on LLVM

Page 11: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Rust FAQ

• Cross-Platform

• Android, iOS, …

• Conditional compilation#[cfg(target_os = "macos")]

Page 12: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Rust Overview

Control & Safety

Page 13: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Control & Safety

C/C++

Safety

Control

Haskell

Go

Java

Python

Page 14: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Control & Safety

C/C++

Safety

Control

Haskell

Go

Java

Rust

Python

Page 15: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Rust Overview

Rust Concepts

Page 16: Embedded Rust – Rust on IoT devices

@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/

Page 17: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

Ownership

Page 18: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

Ownership

ERROR use of moved value: `v`

Page 19: Embedded Rust – Rust on IoT devices

@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`

Page 20: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

Ownership

Page 21: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

Ownership

Copy Type

OK

Page 22: Embedded Rust – Rust on IoT devices

@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

Page 23: Embedded Rust – Rust on IoT devices

@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

Page 24: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

References and Borrowing

Page 25: Embedded Rust – Rust on IoT devices

@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

Page 26: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

References and Borrowing

Page 27: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

References and Borrowing

OK: 6

Page 28: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

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

References and Borrowing

mut{

}

Page 29: Embedded Rust – Rust on IoT devices

@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)

Page 30: Embedded Rust – Rust on IoT devices

@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

Page 31: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

Page 32: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Embedded Rust

Page 33: Embedded Rust – Rust on IoT devices

@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.

Page 34: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

STM32 Nucleo-F411RE

Page 35: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

STM32 Nucleo-F411RE

Page 36: Embedded Rust – Rust on IoT devices

Demo

@choas#Devoxx #embeddedRust

Page 37: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

Page 38: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Cross-Compiling

Page 39: Embedded Rust – Rust on IoT devices

@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

Page 40: Embedded Rust – Rust on IoT devices

@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

Page 41: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Building

cargo build --target=thumbv7em-none-eabi --features mcu_stm32f4

Page 42: Embedded Rust – Rust on IoT devices

Demo

@choas#Devoxx #embeddedRust

Page 43: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Agenda

• Rust Overview

• Embedded Rust

• Cross Compiling

• MCU Debugging

Page 44: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

MCU Debugging

Page 45: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

zinc.rs Issue #336 Failing to build example. #336

Page 46: Embedded Rust – Rust on IoT devices

@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

Page 47: Embedded Rust – Rust on IoT devices

Demo

@choas#Devoxx #embeddedRust

Page 48: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

What we’ve seen

• Rust Overview ✔

• Embedded Rust ✔

• Cross Compiling ✔

• MCU Debugging ✔

• Hello World

Page 49: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

What we’ve seen

• Rust Overview ✔

• Embedded Rust ✔

• Cross Compiling ✔

• MCU Debugging ✔

• blinking Hello World ✔

Page 50: Embedded Rust – Rust on IoT devices

@choas#Devoxx #embeddedRust

Thank you!