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

icm20689: Remove manual CS control #94

Merged
merged 2 commits into from
Dec 10, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ads1x1x = "0.2.2"
ak09915_rs= "0.2.0"
approx = "0.5.1"
bmp280 = "0.4.0"
dummy-pin = "0.1.1"
embedded-hal = "0.2.7"
icm20689 = "0.1.1"
linux-embedded-hal = "0.3.2"
Expand Down
36 changes: 5 additions & 31 deletions src/icm20689.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use std::{error::Error, thread::sleep, time::Duration};
use std::error::Error;

use dummy_pin::DummyPin;
use icm20689::{self, Builder as ImuBuilder, SpiInterface, ICM20689};
use linux_embedded_hal::spidev::{SpiModeFlags, SpidevOptions};
use linux_embedded_hal::{
gpio_cdev::{Chip, LineRequestFlags},
CdevPin,
};
use linux_embedded_hal::{Delay, Spidev};

use crate::peripherals::{
AccelerometerSensor, AnyHardware, GyroscopeSensor, PeripheralClass, PeripheralInfo, Peripherals,
};

pub struct Icm20689Device {
imu: ICM20689<SpiInterface<Spidev, CdevPin>>,
imu: ICM20689<SpiInterface<Spidev, DummyPin>>,
info: PeripheralInfo,
}

Expand All @@ -39,15 +36,13 @@ impl AnyHardware for Icm20689Device {

pub struct Icm20689Builder {
spi_device: String,
cs_pin_number: u32,
info: PeripheralInfo,
}

impl Icm20689Builder {
pub fn new() -> Self {
Icm20689Builder {
spi_device: "/dev/spidev1.0".to_string(),
cs_pin_number: 16,
spi_device: "/dev/spidev1.2".to_string(),
info: PeripheralInfo {
peripheral: Peripherals::Icm20689,
class: vec![PeripheralClass::Accelerometer, PeripheralClass::Gyroscope],
Expand All @@ -65,16 +60,6 @@ impl Icm20689Builder {
self
}

/// Sets the Chip Select (CS) pin number.
///
/// # Arguments
///
/// * `pin_number` - The SPI pin number (e.g., 16).
pub fn with_cs_pin(mut self, pin_number: u32) -> Self {
self.cs_pin_number = pin_number;
self
}

pub fn with_peripheral_info(mut self, info: PeripheralInfo) -> Self {
self.info = info;
self
Expand All @@ -89,18 +74,7 @@ impl Icm20689Builder {
.build();
spi.configure(&options)?;

let cs_pin = {
let mut chip = Chip::new("/dev/gpiochip0").unwrap();
let line = chip
.get_line(self.cs_pin_number)
.unwrap()
.request(LineRequestFlags::OUTPUT, 1, "cs-icm20689")
.expect("Failed to request CS pin");
let pin = CdevPin::new(line)?;
sleep(Duration::from_millis(30));
pin.set_value(1)?;
pin
};
let cs_pin = dummy_pin::DummyPin::new_low();

let mut imu = ImuBuilder::new_spi(spi, cs_pin);

Expand Down
Loading