Embedded Rust

I want to become fluent in Rust, and I have a few things I'd like to do with embedded systems, so I figured I'd combine the two. This has got a lot easier since I first thought about it: there's a book.

The thing I specifically want to do is regularly sample the ADC, using non-blocking APIs.

This is sort of supported: There are some standardised HAL APIs, and they use nb which is a generalised non-blocking mechanism.

This does mean, however, that you end up needing a spin loop that tries everything you are waiting for. You can handle interrupts, so perhaps there's a sleep function that wakes up after an interrupt has been processed? Then I could add something like Chibios events: the interrupt would set the right event flag, and the main loop would act on each event flag that was set, each time it woke up.

I don't need to do this, because the RTFM framework does it already, and statically analyses your program for deadlocks.

RTFM lets you define 'tasks' which are functions that are invoked to process messages. You can send a message to a task from an interrupt, and the scheduler will invoke the task function with that message later.

This means everything will have to be written as a state machine: Rust doesn't have co-routines, although it may in future.

As far as I can tell, if you want to the processor to sleep when Idle, you just don't specify an idle function.

There are drivers for a couple of peripherals I have: the accelerometer and gyroscope in the stm32f3 discovery board, and the ENC28J60 ethernet adapter.

I couldn't immediately get an example to build, but then I don't know much about rust or cargo, which is obviously a handicap.
I found an example which uses rtfm with an stm32f103: rusty-clock. It uses a released version of RTFM, and the RTFM documentation refers to master, which has some API changes that affect the trivial init.rs example. Anyway, Cargo can just use git repositories, so I'm going with that.

RTFM also doesn't expect memory.x to be required - that those details are provided by the PAC crate. This doesn't seem to be the norm, and definitely isn't true of the stm32f1 create. Anyway, I just grabbed memory.x from the rusty clock project.

The result builds.

Other references:
Cortex M Quick start
Awesome embedded Rust

Popular posts from this blog

3D Printer ramblings

Balance Bot V2

Robot arm