Embedded Rust – Rust on IoT devices by Lars Gregori



hi welcome to my talk and better rust on IOT devices my name is Lara GUI I'm working at SP high press in Munich and they are working at hyper slaps team and at hyper slaps team we are building prototypes we're connecting Internet of Things with our e-commerce platform and here you can see on the left side our smart wine shelf for the blinking and lift up of the wine bottles we're using Arduino and for the communication raspberry pi in the middle we are using a light blue beam for the ble communication and there's also an admiral process on it same as in arduino and on the right side it's our infinite card prototype which uses an esp8266 so we are playing around with yeah embedded devices so and the motivation for this talk was this page I found it was about functional IOT and on this page there were three programming languages and also rust and I thought okay rust something functional maybe you have to look at it friend of mine told me yeah you have to look at rust and okay maybe that's the starting point but there also was something interesting on this page with um how imagine how IOT devices are so they are connected to the Internet I mean that's the point internet of things without internet there are only things am they developed in short time storing personal data and also sometimes a Wi-Fi password so therefore they have to be secure and they're getting more intelligent powerful but on the other side cheaper and also with the service and all the old code which it's maybe not updated that's all the problems we are having getting now starting everyday you'll be reading on the new some botnet is up and the ZigBee protocol for philip's you or something was hackable and the page ask can see be designed for Internet of Things like this for the devices and the answer was no it can't I thought okay maybe I have to look more into rust so my talk will be about giving overview about rust looking at embedded rust cross compiling and micro comb micro control of debugging yeah so let's start with an overview when you go to rustling org you can install it and download it and there's also some information or trustees so rust is a system programming language it's fast it prevents segmentation fault that's main problem with the CC plus language and guarantees thread safety okay so I looked at of FAQ page yeah safe concurrent practical system language nice but there's also one sentence they don't want to be 100 percent static safe or reflective not too dogmatic so for example if you have unsafe code you mark this code as unsafe and don't get the help of the compiler but you can use unsafe code if you want and rust is developed by Mozilla for their next browser engine called servo and they already take some parts of this proof of concept and put them into the Firefox so in the actual Firefox you already have some rust code and rust is multi-payer Dickman so it's not only functional programming as a thought in the beginning it has also some old piece code paretic man inside of it but the interesting part is don't use the garbage collector so with the intelligent memory handling they don't need garbage collector and it's built on LLVM and russ is cross-platform so you can use it on Android iOS and let's talk here about embedded devices and you have the conditional compilation so if you have some code especially for a Mac or for some embedded device you can mark this code but what makes rusts also yeah so different is the control and safety so if you look at some rust videos you get an overview with the control this is how you can access the memory so with C C++ you can access every part of a memory but on the other side side you're not safety so language itself doesn't help you really to access parts which shouldn't be accessed because you have something wrong in your code and then you go down with go Java hiding and at the end you have Haskell which totally safety but you don't have really control about it so question is where's rust in this diagram there's a gap between C C++ go maybe no it's totally con you have told control about it and it's absolute safety and we thought this I thought okay let's get more into rust and the reason for this is the rust concepts so the key concept is the ownership and then we have borrowing and references and the lifetime which is with is advanced concept of the borrowing so I will show you now some ownership and powering code examples I skipped the lifetime because yeah when you play around with rust there's this nice sentence in the documentation where you can find here you have to fight borrow tracker so you get error messages anything why is this happening and then you have to think about and it's like a gamification it's like you try to fix it to let it run and at the end here you get more and more into the board checker so here we have the ownership I haven't backed off one two three in rust will use the rust uses and macro for this that's the reason with the exclamation mark then we assign this vector to a second variable do something and at the end we want to print the first element of our vector what will happen you get an error use of a moved value so when you assign your first vector to a second variable you also move the ownership and the reason for this is there could be something inside of it like take something out of vector two so you don't know effect or if your first element exists which value it will have or something like that you have an example I have value one assign it to be two and print it in this case everything is fine the reason for this is integer and primitive types are using a copy type trade so every time you assign a value to another value the value will be copied and the vector itself doesn't have this one of these reasons maybe if someone wants to copy one million items from one side to the other side and around it gets rose yeah you can implement your own Vektor with a copy type on fit or you can use this kind of code so in rust you define a function with the FN key word then we have the function name then the parameter with the type of the parameter in this case we are having them back to within 32-bit integer I set the parameter and after the error you have the return type in this case we have the back two vectors and also an integer 32-bit then we do some stuff with vector one and two and hand back the ownership in our return type with back to one vector two and 42 so in this case we are defining some vectors calling the function and get back to your ownership but it looks a bit complicated to write this kind of code so there's the possibility with references so here you can see the vector has an ampersand now in front of it for reference and we only have to return one type so we can do this now for return 42 as answer and when you call the function you also for the ampersand as a reference for this values and can use it think that's clear but what happens if you want to change something so in this case we're having a vector and put back to value 5 on it and here we're getting an error once again but this time is cannot borrow immutable borrowed content as mutable mean you have to think twice and you get it ok I borrowed it as immutable so I couldn't I'm not allowed to change it but I want to change it the reason for this every variable in rust is per default immutable you couldn't change it so if you want to change it you have to use the mutable keyboard so in this case we define X as a mutable variable then we assign Y with mutable reference to X add to the value 1 and at the end we want to print it so what will happen everything is fine 5 plus 1 is 6 the reason for this is first immutable and curly braces so in this case the code gets a different scope with scope in and after the end scope destroys everything inside it and giving back the reference it's the same if if you would call a function I can see some faces with getting more yeah getting complicated so the rules for borrowing first you have a scope which have to be less or equal than your main scope and the other rule is you have only one or more references in this type are kind AIDS it's immutable or exactly one mutable reference but not both of them so what happens if you take the pray curly braces away we're getting an error once again and this time you're getting an error because you cannot borrow an immutable variable also as mutable so on the top we have a mutable and want to read it immutable and this against the rule where you only have a mutable or immutable variable so at this point I think you get an idea but not understand it completely so you have to play around on the document page you have document page there are runnable examples in browser you click on it and can ya remove the curly braces or something and then you get an error and play around and after half a day you get it a little bit more clear okay now going to the embedded rust part I found a project on the on github it sink gets an experimental project at the moment it's for the ARM processor it shows rust as safety features it's mostly assembler free and completely C free for this example I'm using an stm32 nucleo f4 which you can see here can everyone see it I think oh yeah there's here we are so in the middle the blackboard that's the STM f4 11 re then we have our left and right some sockets also an Arduino compatible socket some LED it's already on it some buttons and a USB connection for it and for this example I'm using the green LED already so pa5 trust remember a 5 and P c7 c7 this is where use for this example so let's take a look at a demo of space great so these are the examples which the sinc project already have so we have most of them uplink examples there's a Bluetooth Low Energy example and some serial communication but I'm staying on on the blink example so in this case with source main I think everyone can see it here so the first line we're using feature this means inside of Rusty's is or everyday new dead of develop or continuously develop so on a nightly build you can use the feature keyboard on a stable build they are not allowed so for this example I need a special version and I'd live a nightly build version so the idea behind this is you you have some new features you put it in a nightly build or in beta version and if they are successful they go into a stable version then we have the null standard keyboard this is for embedded devices it's not using the standard c library we don't have file access or something so you just skip this then used part how to import some something into rust it's a little bit different then in in Java but I think yeah you can also make it big then we have our main function in this case we have to initialize the stack and also the data because we are embedded it's all the same with to some assembler programming but if you do Java it's it's there so you don't have to think about then we have our LED or first one so Yam using port a and pin five so this is a five this case is an unsigned 8-bit integer as an output then I have led to C seven in this case set up the LEDs set up a timer define a variable for the delay and you have the loop set first LED on second off wait and the other way around and wait again and loop through it okay cross compiling so if you download the rest compiler crust on from the internet form from the rest page you have the stable version but if you need some nightly built or something you can use rust up it's an installer for rust and then you just say installed nightly something from September then go into my Russ project and overwrite the default setting so every time I go into this directory I'm using the nightly version and outside I'm having a stable version and we also need some new compiler for this for the ARM version so here you can see arm we on the ARM processor nan means it's bare metal it's no linux or something running around on it and it's an embedded application binary interface that's the EB versus using cargo as package manager with rustbelt we can build it and I just put here the target and feature yeah a parameters on they also can put into some configuration but to make it more clear so we are using some v7 it's some arm code base and the features of the stm32f4 okay so let's go out here I have my built file with this all of them run it and it's already done because I already compiled it if you compile it from scratch it will take a little bit longer so now I've changed some value because you can already see it's blinking I changed the delay to 100 the old it takes a little bit longer and now I upload it it always fails that's cool and now it's blinking faster so with the upload I connect the nuclear board to my laptop and it's getting as this storage device and just copy the binary total to the store storage on this throw it here you can see it on the last line maybe a little bit higher so copy text copy it to the volume note f4 11 re ok so and as you can see it's blinking faster now so I really changed the code it's really rust it's not some some default example or something ok an hour will debug on it so there's some error some failure already inside of the sync project you have to comment out the debug GP script thing and then you can build compatible a debuggable code for it otherwise would release code it's no problem with ya and for the debugging I'm using open OCD so this connects to the microcontroller and open mobs in Port 3333 and then I can open the debugger connect to it remotely on this port load the file and I can do something so I will do it now first of all I start the open OCD as you can see that microcontrollers stopped now and there's lots of flashing here so there's zero communication with this device then I open up the debugger target mode thousand three hundred then I need this file just copy it I want to overwrite it yeah no problem and now can list my RS code so you can see it here I can also set a breakpoint and the main function this is somewhere here at eleven and I want also a breakpoint at thirty two that's the first time I'm setting the LED one too high now I can continue and it stops at main I can now type next so go further and it stops on line 34 with said hi so I just continue once more and then you should see the LED blinks and stops again at thirty-four and now I can step into it so this is the function itself said hi then I get your offset the pin number or can also print self so that's a reference I print self pin it's number five do I already have to port no I think not step further and that's the off no let's see offset true where do I get a port there's some where the port ah it's under get registered so here you can see the PS are are it's it's some memory in on on the device and with changing the flag to true I setting the output to a high and that's when the LED get turns on okay I just continue now because now I can also throw the variable delay 100 just changed I can also change it set delay to do it one second so and when I now continue takes one second and should no force too fast continue I'm okay oh yeah thank you set delay – look what's I always forget it why do I need X equal sign okay now takes longer yeah break12 yeah so I also can change the variable it can also disassemble this quote and I always discuss this I'd copy it I couldn't write this assemble this is arm coat looks like a little bit like Java bytecode and when I go to 600 somewhere here we are with the arrow in front of it then you can see a BL that branch load so it's a call to our said hi function I think that's yeah continue break file everything demo I've already gave so what have we seen today and rest overview embedded ross code some rust code also how to cross compile and debug it what we didn't solve was the hello world the typical hello world but the blinking hello world for embedded devices yeah that's it some question I think there I I couldn't answer borrow check us questions within a short time so if if you have yeah if you have the compiler for it they they don't use they don't come transfer it into C code it's well good question yeah it's economy that's the arm compiler yeah right I'm not sure if they if they're using a C code on it that's a good question didn't looked at it what they're really doing it into the back end on it some other yeah at the moment it's only they started this project and continually developing on it and at the moment there's no Wi-Fi connection or something so this is yeah I think it's also something which will be more complicated to rewrite everything into rust so this is also one main problem with rust there are lots of C code lots of libraries outside of it so you can use it with rust but in this case you are using the unsafe part and yeah then you still have the problem that this library will have some some problems and everything will be unsafe yeah more questions I'm around yeah that's a good thing there's an STM l1 also which this is therefore it's a little bit more powerful but it doesn't have to be powerful um this one has 512 kilobytes laughs and 128 kilobyte SRAM we're talking about kilo not Tara or something yeah and the l1 is also possible and I have it here somewhere okay more questions thank you you you

4 Comments

  1. Mihai Galos said:

    Porbably failing @19:16 because of the file not yet being generated. Try to add a && just before the cp, that way cp will only be executed after the first command is finished.

    June 26, 2019
    Reply
  2. Windkind0 said:

    The speaker explains some rust features till 12:36. He obviously tried to highlight why Rust is safe, but didn't really got the point across, he explained ownership with some examples that are better explained in other talks or in the rust books.
    In the Embedded Part he uses Zinc.rs and shows a basic blink example first and proceeds to explain how to setup Rustup for crosscompilation. In fact it is not too much more than what is in the readme of zinc.rs

    June 26, 2019
    Reply
  3. Just Some Aussie said:

    "Para-dig-man"

    June 26, 2019
    Reply
  4. Michael Cox said:

    The Rust compiler is LLVM-based and generates binary code, so no C compiler is necessary. If LLVM supports the MCU you're using, you should be able to build a cross-compiler, linker, and gdb for it.

    Depending on how much of the zinc library you link in and your code, you should be able to upload your code to very small processors (maybe even single or double digit Kbytes of flash and sram).

    June 26, 2019
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *