Rust for Low Power Digital Signal Processing | Interrupt

Like many firmware developers, I’ve been curious about the potential Rust can have in the embedded space. After reading James Munns’ great article, I decided to find a project I could use Rust for to learn.


This is a companion discussion topic for the original entry at https://interrupt.memfault.com/blog/rust-for-digital-signal-processing

Did you try using crate-type = ["staticlib", "rlib"]?

Excellent work!!!
Just a clarification, I dont know the inner-workings of APP_ERROR_CHECK_BOOL, but in the mean_rs == mean_c comparison, floats should never compare by value directly due the lack of precision of that type of data.

The difference between the values should be compared to be less than an acceptable error tolerance.

Something like:

const float32 TOLERANCE = 0.0001;
 float32 diff = abs(mean_rs - mean_c);
 APP_ERROR_CHECK_BOOL(diff <= TOLERANCE );

Regards

You’re correct. In this case, it works because we’re running on the same MCU with the same FPU. FP math is imprecise, but it is deterministic.

I think that would work, though what we really want is the ability to set crate type based on target. This is an open issue against cargo: https://github.com/rust-lang/cargo/issues/4881

Hi @cyril

I couldn’t believe that Rust could be as low memory consuming as C. But do you see other tools for Rust available like gdb ? And the language support on major vendors(ARM, RISC-V,etc) and SDK is still C mostly. Do you still recommend to learn Rust?

Hi!

Rust has pretty good tooling, with new tools being developed actively (checkout probe-rs or knurling-rs for example).
To my mind, it will take time before we can see SDKs being written in Rust. The binding between C and Rust is quite easy though. Still, the focus of this article is on developing libraries using Rust because I think this is a great initial step to try out Rust and enjoy the language modernity and safety.
To answer your question: Rust is not easy to learn compared to other languages like Go, but I think this language should be taught at school because it makes you realize how to correctly write code. So even if you don’t have any project to apply Rust now, you’ll get benefits from learning the language.
I have to admit I am still learning it and it’s quite exciting :slight_smile:

Best,
Cyril

1 Like

It have not tried with the cmsis dsp intristics but looking at godbolt codegen it seems that the obvious C++ implementation produces much better code:

Compare this with the codegen of the above rust snipper:

I would be surprised if this code outperformed the C++ snippet. Maybe I am missing some optimization flags?

1 Like

idk, smaller code size does not necessarily translate to fewer cycles