why so many
There are a couple libraries that you might use.
They are:
avr-hal
: Hardware Abstraction Layer for AVR microcontrollers and common boards (for example Arduino)avr-rust
/ruduino
Reusable components for the Arduino Uno.
These three projects all effectively do the same thing at different abstraction levels.
This is my recommendation for developing an Arduino application in Rust. avr-hal
has the low level control needed for real projects but provides an easy to write and reasonably safe API to interact with the system.
What is HAL?
HAL Design Patterns
This is a set of common and recommended patterns for writing hardware abstraction layers (HALs) for microcontrollers in Rust. These patterns are intended to be used in addition to the existing Rust API Guidelines when writing HALs for microcontrollers.
Simply, HALs are abstractions over embedded hardware. The most basic is the embdedded-hal crate which defines many traits applicable to most embedded systems. avr-hal
is a HAL that abstracts many AVR boards like Arduino boards.
In my opinion, this seems like the correct solution to Arduino Rust development. Additionally, avr-hal
has lots of examples.
ruduino
seems to be a separate, more high level take compared to avr-hal. While there is still access to registers, serial, and timers, etc., the overall feel is very rustian and hand-holdy.
For example, a timer configuration:
const DESIRED_HZ_TIM1: f64 = 2.0;
const TIM1_PRESCALER: u64 = 1024;
const INTERRUPT_EVERY_1_HZ_1024_PRESCALER: u16 =
((ruduino::config::CPU_FREQUENCY_HZ as f64 / (DESIRED_HZ_TIM1 * TIM1_PRESCALER as f64)) as u64 - 1) as u16;
timer1::Timer::new()
.waveform_generation_mode(timer1::WaveformGenerationMode::ClearOnTimerMatchOutputCompare)
.clock_source(timer1::ClockSource::Prescale1024)
.output_compare_1(Some(INTERRUPT_EVERY_1_HZ_1024_PRESCALER))
.configure();
Timer becomes a struct that can be have it's properties modified using method chaining. In contrary, avr-hal gives you organized register definitions but has you write each register individually.
Additionally it appears the ruduino
is only for the Arduino Uno.
avr-rust
refers to the fork of the Rust compiler to add AVR support. Nowadays, AVR support is included in Rust nightly so these projects are deprecated e.g. rust-legacy-fork.
See Arduino UNO.