Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to embedded-hal 1.0. #37

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ description = "A generic rust driver to support RFM69 family wireless chips"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = "0.2"
embedded-hal = "1.0.0"

[dev-dependencies]
anyhow = "1.0"
linux-embedded-hal = "^0.3.2"
embedded-hal-bus = { version = "0.1.0", features = ["std"] }
linux-embedded-hal = "0.4.0"
utilities = { path = "utilities" }
15 changes: 7 additions & 8 deletions examples/low_power_lab_receive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use embedded_hal_bus::spi::ExclusiveDevice;
use linux_embedded_hal::spidev::{SpiModeFlags, SpidevOptions};
use linux_embedded_hal::sysfs_gpio::Direction;
use linux_embedded_hal::{Spidev, SysfsPin};
use linux_embedded_hal::{Delay, SpidevBus, SysfsPin};
use rfm69::{low_power_lab_defaults, Rfm69};
use utilities::{rfm_error, Packet};

Expand All @@ -12,20 +13,18 @@ fn main() -> Result<()> {
cs.set_direction(Direction::High)?;

// Configure SPI 8 bits, Mode 0
let mut spi = Spidev::open("/dev/spidev0.0")?;
let mut spi_bus = SpidevBus::open("/dev/spidev0.0")?;
let options = SpidevOptions::new()
.bits_per_word(8)
.max_speed_hz(1_000_000)
.mode(SpiModeFlags::SPI_MODE_0)
.build();
spi.configure(&options)?;
spi_bus.configure(&options)?;

let spi = ExclusiveDevice::new(spi_bus, cs, Delay);

// Create rfm struct with default compatible with LowPowerLabs
let mut rfm = rfm_error!(low_power_lab_defaults(
Rfm69::new(spi, cs),
100,
433_000_000
))?;
let mut rfm = rfm_error!(low_power_lab_defaults(Rfm69::new(spi), 100, 433_000_000))?;

// Print content of all RFM registers
for (index, val) in rfm_error!(rfm.read_all_regs())?.iter().enumerate() {
Expand Down
15 changes: 7 additions & 8 deletions examples/low_power_lab_send.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use embedded_hal_bus::spi::ExclusiveDevice;
use linux_embedded_hal::spidev::{SpiModeFlags, SpidevOptions};
use linux_embedded_hal::sysfs_gpio::Direction;
use linux_embedded_hal::{Spidev, SysfsPin};
use linux_embedded_hal::{Delay, SpidevBus, SysfsPin};
use rfm69::{low_power_lab_defaults, Rfm69};
use utilities::{rfm_error, Packet};

Expand All @@ -12,20 +13,18 @@ fn main() -> Result<()> {
cs.set_direction(Direction::High)?;

// Configure SPI 8 bits, Mode 0
let mut spi = Spidev::open("/dev/spidev0.0")?;
let mut spi_bus = SpidevBus::open("/dev/spidev0.0")?;
let options = SpidevOptions::new()
.bits_per_word(8)
.max_speed_hz(1_000_000)
.mode(SpiModeFlags::SPI_MODE_0)
.build();
spi.configure(&options)?;
spi_bus.configure(&options)?;

let spi = ExclusiveDevice::new(spi_bus, cs, Delay);

// Create rfm struct with default compatible with LowPowerLabs
let mut rfm = rfm_error!(low_power_lab_defaults(
Rfm69::new(spi, cs),
100,
433_000_000
))?;
let mut rfm = rfm_error!(low_power_lab_defaults(Rfm69::new(spi), 100, 433_000_000))?;

// Print content of all RFM registers
for (index, val) in rfm_error!(rfm.read_all_regs())?.iter().enumerate() {
Expand Down
15 changes: 3 additions & 12 deletions examples/receive.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use anyhow::Result;
use linux_embedded_hal::spidev::{SpiModeFlags, SpidevOptions};
use linux_embedded_hal::sysfs_gpio::Direction;
use linux_embedded_hal::{Spidev, SysfsPin};
use linux_embedded_hal::SpidevDevice;
use rfm69::Rfm69;
use utilities::rfm_error;

fn main() -> Result<()> {
// Configure CS pin
let cs = SysfsPin::new(25);
almusil marked this conversation as resolved.
Show resolved Hide resolved
cs.export()?;
cs.set_direction(Direction::High)?;

// Configure SPI 8 bits, Mode 0
let mut spi = Spidev::open("/dev/spidev0.0")?;
let mut spi = SpidevDevice::open("/dev/spidev0.0")?;
let options = SpidevOptions::new()
.bits_per_word(8)
.max_speed_hz(1_000_000)
Expand All @@ -21,7 +15,7 @@ fn main() -> Result<()> {
spi.configure(&options)?;

// Create rfm struct with defaults that are set after reset
let mut rfm = Rfm69::new(spi, cs);
let mut rfm = Rfm69::new(spi);

// Print content of all RFM registers
for (index, val) in rfm_error!(rfm.read_all_regs())?.iter().enumerate() {
Expand All @@ -36,8 +30,5 @@ fn main() -> Result<()> {
println!("Value at {} = {}", index, val)
}

// Un-export the CS pin
SysfsPin::new(25).unexport()?;

Ok(())
}
15 changes: 3 additions & 12 deletions examples/send.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use anyhow::Result;
use linux_embedded_hal::spidev::{SpiModeFlags, SpidevOptions};
use linux_embedded_hal::sysfs_gpio::Direction;
use linux_embedded_hal::{Spidev, SysfsPin};
use linux_embedded_hal::SpidevDevice;
use rfm69::Rfm69;
use utilities::rfm_error;

fn main() -> Result<()> {
// Configure CS pin
let cs = SysfsPin::new(25);
almusil marked this conversation as resolved.
Show resolved Hide resolved
cs.export()?;
cs.set_direction(Direction::High)?;

// Configure SPI 8 bits, Mode 0
let mut spi = Spidev::open("/dev/spidev0.0")?;
let mut spi = SpidevDevice::open("/dev/spidev0.0")?;
let options = SpidevOptions::new()
.bits_per_word(8)
.max_speed_hz(1_000_000)
Expand All @@ -21,7 +15,7 @@ fn main() -> Result<()> {
spi.configure(&options)?;

// Create rfm struct with defaults that are set after reset
let mut rfm = Rfm69::new(spi, cs);
let mut rfm = Rfm69::new(spi);

// Print content of all RFM registers
for (index, val) in rfm_error!(rfm.read_all_regs())?.iter().enumerate() {
Expand All @@ -32,8 +26,5 @@ fn main() -> Result<()> {
let buffer = Vec::from(b"Hello, world!".as_ref());
rfm_error!(rfm.send(&buffer))?;

// Un-export the CS pin
SysfsPin::new(25).unexport()?;

Ok(())
}
62 changes: 0 additions & 62 deletions src/cs.rs

This file was deleted.

9 changes: 3 additions & 6 deletions src/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use embedded_hal::digital::v2::OutputPin;

use crate::error::Result;
use crate::registers::{
ContinuousDagc, DataMode, DccCutoff, FifoMode, InterPacketRxDelay, LnaConfig, LnaGain,
Expand All @@ -11,13 +9,12 @@ use crate::Rfm69;

/// Configures RFM69 according to [LowPowerLab](https://github.com/LowPowerLab/RFM69) Arduino
/// library
pub fn low_power_lab_defaults<T, S, Ecs, Espi>(
mut rfm: Rfm69<T, S>,
pub fn low_power_lab_defaults<S, Espi>(
mut rfm: Rfm69<S>,
network_id: u8,
frequency: u32,
) -> Result<Rfm69<T, S>, Ecs, Espi>
) -> Result<Rfm69<S>, Espi>
where
T: OutputPin<Error = Ecs>,
S: ReadWrite<Error = Espi>,
{
rfm.mode(Mode::Standby)?;
Expand Down
6 changes: 2 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
pub(crate) type Result<T, Ecs, Espi> = core::result::Result<T, Error<Ecs, Espi>>;
pub(crate) type Result<T, Espi> = core::result::Result<T, Error<Espi>>;

#[derive(Debug)]
pub enum Error<Ecs, Espi> {
/// Chip select pin error
Cs(Ecs),
pub enum Error<Espi> {
/// SPI bus error
Spi(Espi),
/// Timeout exceeded
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@

#![cfg_attr(not(test), no_std)]

pub use crate::cs::NoCs;
pub use crate::defaults::low_power_lab_defaults;
pub use crate::error::Error;
pub use crate::rfm::Rfm69;
pub use crate::rw::{ReadWrite, SpiTransactional};
pub use crate::rw::ReadWrite;

mod cs;
mod defaults;
mod error;
mod rfm;
Expand Down
Loading
Loading