Rust Embedded HAL for Apache NuttX RTOS
This crate provides Rust Embedded HAL interfaces (GPIO, I2C, SPI and Delay) for Apache NuttX RTOS.
For sample NuttX Rust apps, see rust-i2c-nuttx and rust_test
If you find this crate useful, please support me on GitHub Sponsors
More about NuttX Embedded HAL...
GPIO Output
// Import Output Pin Trait
use OutputPin;
// Open /dev/gpio1 for GPIO Output
let mut gpio = OutputPin
new
.expect;
// Set Chip Select to Low
gpio.set_low
.expect;
// Set Chip Select to High
gpio.set_high
.expect;
GPIO Input
// Import Input Pin Trait
use InputPin;
// Open /dev/gpio0 for GPIO Input
let gpio = InputPin
new
.expect;
// True if GPIO is High
let is_high = gpio.is_high
.expect;
// True if GPIO is Low
let is_low = gpio.is_low
.expect;
GPIO Interrupt
Interrupt callbacks are not supported yet.
// Import Input Pin Trait
use InputPin;
// Open /dev/gpio2 for GPIO Interrupt
let gpio = InterruptPin
new;
.expect;
// True if GPIO is High
let is_high = gpio.is_high
.expect;
// True if GPIO is Low
let is_low = gpio.is_low
.expect;
I2C
// Import I2C Trait
use i2c;
// Open I2C Port /dev/i2c0
let mut i2c = new.expect;
// Buffer for received I2C data
let mut buf = ;
// Read register 0xD0 from I2C Address 0x77
i2c.write_read.expect;
// Print the register value
println!;
SPI
The SPI interface requires the SPI Test Driver (/dev/spitest0) to be installed:
SPI settings are configured in the SPI Test Driver.
// Import SPI Trait
use spi;
// Open SPI Bus /dev/spitest0
let mut spi = Spi
new
.expect;
// Open GPIO Output /dev/gpio1 for Chip Select
let mut cs = OutputPin
new
.expect;
// Set Chip Select to Low
cs.set_low
.expect;
// Transmit and receive SPI data
let mut data: = ;
spi.transfer
.expect;
// Show the received SPI data
for i in 0..data.len
// Set Chip Select to High
cs.set_high
.expect;
Delay
// Import Delay Trait (milliseconds)
use DelayMs;
// Get a Delay Interface
let mut delay = Delay;
// Wait 500 milliseconds
delay.delay_ms;