9 ms·
Raspberry Pi Bare Metal Programming with Rust
- kalimatas 11y agoThanks for sharing!
- s986s 11y agoVery cool! Rust has been impressing me alot these past few months.
- deleted 11y ago[deleted]
- mintplant 11y agoDon't hold your breath. The kernel is huge and Linus is happy with C.
- sremani 11y agoRust needs a Unikernel, there is no need for full blown OS.
- pjmlp 11y agoI think the MirageOS guys are thinking into eventually replacing the C parts with Rust.
- steveklabnik 11y agoRumprun already has Rust support.
- nickysielicki 11y agoI remember reading a couple months ago that bare metal using Rust was in a bad state. If I recall correctly the outputs were prohibitively large, among other issues. Has that changed?
- steveklabnik 11y agoThe real reason that Rust isn't necessarily wonderful for bare metal is that a lot of the things you need to do aren't stable yet. Binary size isn't a problem, though. A lot of the threads talking about sizes aren't doing all the stuff you need to get truly small binaries. That's because, as this stuff is on nightly, it has much worse (or no) docs, so it's easy to miss things. I'm working on a little kernel, the one linked in the first line of the post, and it's great.
- Aeolos 11y agoI have the perfect project for this! Generate a signal using the GPIO ports to drive a mirror and camera in a synchronized fashion. Does anyone know if the RPi GPIOs can be driven at around 80KHz? I've seen reports that this is possible, but that the USB or video driver tends to lock the CPU for long times, messing with timings - but hopefully running on bare metal would take care of that.
- asandweech 11y agoThis might be of interest: http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/ http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-...
- Aeolos 11y agoExcellent, thanks! So GPIO performance won't be a problem, but they note that OS multitasking can cause issues: "What is not evident from the snapshots, however, is that due to multitasking nature of Linux, the GPIO manipulation is constantly interrupted for short periods when the CPU is doing something else, such as receiving or sending data over network, writing log files, etc" Running on bare metal should take care of that, so the only remaining problem is how to synchronize the signal generation on the RPi with the recording of data on the host computer. (Sigh, if only RPi had a USB3 port...)
- timow1337 11y agoThe new raspberry pi zero can be run as a device, so you could use that.
- Aeolos 11y agoO.o this $5 puppy could essentially replace the 1.5K National Instruments DAQ I'm using right now. Even better, I won't have to deal with their horrendous API documentation anymore. Of course that would require a Linux kernel, so not bare metal anymore, but there might be a way to make this work! Now I just need to get my hands on one, it's out of stock everywhere within a radius of 300km...
- akerro 11y agoIs Rust the future? As a C++ (hobby), Java (full time) developer, should I invest my time in Rust?
- jacquesm 11y agoRust, Go, Java, Scala, Clojure, Javascript... (leaving out quite a few other choices for brevity). They say choice is a good thing but too much choice is actually really annoying and fragments the community across a very large number of ecosystems.
- akerro 11y agoI can't stand JS and Go syntax, I tried a few times... This is the worst thing that happened to humanity after in XXI century. I feel more comfortable with Prolog than with JS.
- jacquesm 11y agoIn a way Go is a step backwards from C towards Pascal. Since a number of people that had extensive C experience and were in fact present at the birth of the C language made that decision I figure they are doing it for a good reason. I can read Go easily enough, I haven't done any major writing in it (yet), other than some minor work on Hugo trying to help to nail down/replicate a bug. It was easy enough that I think I'd pick it up quite fast, the syntax feels 'weird' to me but that's most likely just unfamiliarity.
- deleted 11y ago[deleted]
- pdpi 11y agoI see the point you're trying to make, yet: - Nobody in their right mind would choose Rust or Javascript for a project where the other is more appropriate. Though (AFAIK) Scala and Clojure both interop quite well with Java, and Scala/Clojure interop isn't terrible either, plus there's definitely a culture of reusing in Clojure/Scala all the stuff that the Java ecosystem already had. So this cluster isn't causing much fragmentation at all. You have something of an argument that Go and the JVM languages clash for mindshare, but at that point the argument around really annoying fragmentation has kind of lost steam.
- deleted 11y ago[deleted]
- Animats 11y agoVery nice. A few more months and that should be stable.
- jcoffland 11y agoAs a long time C programmer this is not very convincing. A C program to do the same requires far less voodoo. All you need to do is take the address of the GPIO register then toggle the bit. No name mangling. No error handlers to override. Don't get me wrong I know rust does have some compelling features. It does seem odd to me that so many of what would be compiler options in C are hard coded.
- steveklabnik 11y agoWe generally try to put options in source code or configuration files, rather than in command-line flags, because that's more repeatable. It makes the CLI much simpler.
- izacus 11y agoOf course that same ability lets you clobber anything else in your address space and leads to those lovely debugging sessions playing "track down what corrupted my datastructure".
- Manishearth 11y agoWhilst having to debug this is still undesirable, I recommend trying `rr` (http://rr-project.org/ http://rr-project.org/) to track this if you're ever in such a situation. It's as simple as setting a watchpoint and reverse-continue-ing! I don't do much C/++ these days, but when I do (and it's a large codebase which is hard to debug), `rr` is invaluable. It works with Rust too.
- pjmlp 11y agoSeems quite restrict in terms of platform support.
- hatsunearu 11y agoAs the author said at the end, the general "good practice" for Rust is to encapsulate the weird stuff and unsafe blocks into a safe lib that other people can use.
- notabot 11y agoAs a C programmer who fiddles with low level stuff I wonder why people get exciting about such trivial thing. Don't get me wrong. Rust is an interesting language. The thing described in this post is well within its capability, i.e. IMO there isn't really anything that worths bragging about. Such trivial thing neither demonstrates the real potential of Rust, nor answers important questions from real world engineering perspective. I'm all for having better tool to write low level stuff. I have dabbled with Rust and the experience was eye-opening. I think Rust still have a lot to catch up though.
- rdc12 11y agoHas hello world ever been exciting, it is only really a building point. You can't do anything intersting until you know the basics, of building and flashing the board. The question is what does Rust have to offer as your embedded program grows.
- notabot 11y ago> Has hello world ever been exciting The HN upvote says otherwise. :-) But I guess had there been an HN-equivalent in assembly age people would get excited about C, too. > The question is what does Rust have to offer as your embedded program grows. Exactly. We are aware of the good, bad and ugly bits of C. The industry has built extensive tooling around it. Rust has a long way to go. I certainly wish to see more pioneering projects from Rust.
- Manishearth 11y agoI don't think the post is bragging about this. It's well known amongst the Rust community that this stuff is pretty easy to do) I read it as a primer for writing simple Rust programs that can run baremetal on the Pi (I've cross compiled for a Pi-with-OS before but never tried this) and access GPIO. If you're talking about the HN upvotes, HN just tends to upvote posts mentioning Rust a lot :) Have you looked at https://zinc.rs/ https://zinc.rs/?
- notabot 11y ago
- akhilcacharya 11y agoOutside of C, are there any other languages or platforms that can do this? I'd like something modern, but I haven't liked what I've seen with Rust personally.
- pjmlp 11y agoStill alive: Ada, SPARK, ATS, FreePascal, D Controversial: Go (if someone ports the runtime to bare metal), embedded JVMs, embedded, Swift (depending how Apple drives it), .NET Native (if C# gets missing features from System C#) Faded away: Algol, PL/I, CPL, Mesa, Modula-2, Modula-2+, Modula-3, Oberon, Oberon-2, Active Oberon, Component Pascal, Turbo Pascal, Forth, Sing#, System C#
- jeremyjh 11y agoI think he was asking which languages could actually be used to do this today, without writing a new cross-compiler first.
- akhilcacharya 11y agoThat's exactly right. I didn't know that D would be an appropriate language for things like this - I'll have to check it out.
- pjmlp 11y agoCheck some of the videos here http://wiki.dlang.org/Videos http://wiki.dlang.org/Videos Namely "Tiny, Ubiquitous Machines Powered by D" and "x86 Bare Metal and Custom Runtime Programming".
- pjmlp 11y agoYou always need a cross-compiler, or a VM. There is no way around it.
- jeremyjh 11y ago
- kod 11y agoIf you want to play around with the code in this post that requires nightly, multirust makes it much more pleasant to use nightly and / or stable rust. https://github.com/brson/multirust https://github.com/brson/multirust