11个稳定版本
1.0.10 | 2022年3月21日 |
---|---|
1.0.7 | 2022年3月19日 |
1.0.6 | 2022年3月18日 |
1.0.3 | 2022年3月17日 |
#1518 in 嵌入式开发
每月25次下载
30KB
402 行
Rust Apache NuttX RTOS嵌入式HAL
此crate为Apache NuttX RTOS提供Rust嵌入式HAL接口(GPIO、I2C、SPI和延时)。
有关NuttX Rust应用程序示例,请参阅rust-i2c-nuttx和rust_test
如果您觉得这个crate很有用,请支持我GitHub Sponsors
更多关于NuttX嵌入式HAL的信息...
GPIO输出
// Import Output Pin Trait
use embedded_hal::digital::v2::OutputPin;
// Open /dev/gpio1 for GPIO Output
let mut gpio = nuttx_embedded_hal::OutputPin
::new("/dev/gpio1")
.expect("open gpio failed");
// Set Chip Select to Low
gpio.set_low()
.expect("set gpio failed");
// Set Chip Select to High
gpio.set_high()
.expect("set gpio failed");
GPIO输入
// Import Input Pin Trait
use embedded_hal::digital::v2::InputPin;
// Open /dev/gpio0 for GPIO Input
let gpio = nuttx_embedded_hal::InputPin
::new("/dev/gpio0")
.expect("open gpio failed");
// True if GPIO is High
let is_high = gpio.is_high()
.expect("read gpio failed");
// True if GPIO is Low
let is_low = gpio.is_low()
.expect("read gpio failed");
GPIO中断
尚未支持中断回调。
// Import Input Pin Trait
use embedded_hal::digital::v2::InputPin;
// Open /dev/gpio2 for GPIO Interrupt
let gpio = nuttx_hal::InterruptPin
::new("/dev/gpio2");
.expect("open gpio failed");
// True if GPIO is High
let is_high = gpio.is_high()
.expect("read gpio failed");
// True if GPIO is Low
let is_low = gpio.is_low()
.expect("read gpio failed");
I2C
// Import I2C Trait
use embedded_hal::blocking::i2c;
// Open I2C Port /dev/i2c0
let mut i2c = nuttx_embedded_hal::I2c::new(
"/dev/i2c0", // I2C Port
400000, // I2C Frequency: 400 kHz
).expect("open failed");
// Buffer for received I2C data
let mut buf = [0 ; 1];
// Read register 0xD0 from I2C Address 0x77
i2c.write_read(
0x77, // I2C Address
&[0xD0], // Register ID
&mut buf // Buffer to be received
).expect("read register failed");
// Print the register value
println!("Register value is 0x{:02x}", buf[0]);
// Write 0xA0 to Register 0xF5
i2c.write(
0x77, // I2C Address
&[0xF5, 0xA0] // Register ID and value
).expect("write register failed");
SPI
SPI接口需要安装SPI测试驱动程序(/dev/spitest0)
SPI设置在SPI测试驱动程序中配置。
// Import SPI Trait
use embedded_hal::blocking::spi;
// Open SPI Bus /dev/spitest0
let mut spi = nuttx_embedded_hal::Spi
::new("/dev/spitest0")
.expect("open spi failed");
// Open GPIO Output /dev/gpio1 for Chip Select
let mut cs = nuttx_embedded_hal::OutputPin
::new("/dev/gpio1")
.expect("open gpio failed");
// Set Chip Select to Low
cs.set_low()
.expect("cs failed");
// Transmit and receive SPI data
let mut data: [ u8; 5 ] = [ 0x1d, 0x00, 0x08, 0x00, 0x00 ];
spi.transfer(&mut data)
.expect("spi failed");
// Show the received SPI data
for i in 0..data.len() {
println!("{:02x}", data[i as usize]);
}
// Set Chip Select to High
cs.set_high()
.expect("cs failed");
延时
// Import Delay Trait (milliseconds)
use embedded_hal::blocking::delay::DelayMs;
// Get a Delay Interface
let mut delay = nuttx_embedded_hal::Delay;
// Wait 500 milliseconds
delay.delay_ms(500_u32);
依赖项
~715KB
~13K SLoC