Skip to content

Commit

Permalink
Rework driver to avoid taking ownership of spi peripheral.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSprenger committed Dec 9, 2024
1 parent 840a32d commit 6d2ce9f
Show file tree
Hide file tree
Showing 8 changed files with 531 additions and 321 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ members = ["boards/*", "examples/*", "crates/*"]
default-members = ["boards/*", "examples/*"]

[workspace.dependencies.stm32h7xx-hal]
git = "https://github.com/uorocketry/stm32h7xx-hal"
# git = "https://github.com/uorocketry/stm32h7xx-hal"
# We use 35 even though we have the 33.
path = "/home/bonjour/Rocketry/stm32h7xx-hal"
features = ["defmt", "rt", "stm32h735", "can", "rtc"]

[workspace.dependencies.serde]
Expand Down
1 change: 1 addition & 0 deletions boards/temperature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmt-rtt = { workspace = true }
panic-probe = { workspace = true }
chrono = { workspace = true }
messages = {workspace = true}
ads126x = {path = "../../crates/ads126x"}

[dev-dependencies]
defmt-test = { workspace = true }
Expand Down
80 changes: 80 additions & 0 deletions boards/temperature/src/adc_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use ads126x::{
register::{DataRate, Mode1Register, Mode2Register},
ADCCommand, Ads126x,
};

use common_arm::spawn;
use stm32h7xx_hal::{
gpio::{Output, Pin, PushPull},
spi::Spi,
};

use crate::app::delay;

pub struct AdcManager {
pub spi: Spi<stm32h7xx_hal::pac::SPI1, stm32h7xx_hal::spi::Enabled, u8>,
pub adc1: Ads126x<Pin<'A', 0, Output<PushPull>>>,
pub adc2: Ads126x<Pin<'A', 1, Output<PushPull>>>,
pub adc1_cs: Pin<'A', 4, Output<PushPull>>,
pub adc2_cs: Pin<'A', 5, Output<PushPull>>,
}

impl AdcManager {
pub fn new(
spi: Spi<stm32h7xx_hal::pac::SPI1, stm32h7xx_hal::spi::Enabled, u8>,
adc1_pin: Pin<'A', 0, Output<PushPull>>,
adc2_pin: Pin<'A', 1, Output<PushPull>>,
adc1_cs: Pin<'A', 4, Output<PushPull>>,
adc2_cs: Pin<'A', 5, Output<PushPull>>,
) -> Self {
Self {
spi,
adc1: Ads126x::new(adc1_pin),
adc2: Ads126x::new(adc2_pin),
adc1_cs,
adc2_cs,
}
}

pub fn init_adc1(&mut self) -> Result<(), ads126x::error::ADS126xError> {
self.adc2_cs.set_high();
self.adc1_cs.set_low();
self.adc1.reset()?;

spawn!(delay, 1000);

let mut mode1_cfg = Mode1Register::default();
mode1_cfg.set_filter(ads126x::register::DigitalFilter::Sinc1);
self.adc1.set_mode1(&mode1_cfg, &mut self.spi)?;

let mut mode2_cfg = Mode2Register::default();
mode2_cfg.set_dr(DataRate::SPS1200);
self.adc1.set_mode2(&mode2_cfg, &mut self.spi)?;

self.adc1.send_command(ADCCommand::START1, &mut self.spi)?;
self.adc1.send_command(ADCCommand::START2, &mut self.spi)?;

Ok(())
}

pub fn init_adc2(&mut self) -> Result<(), ads126x::error::ADS126xError> {
self.adc1_cs.set_high();
self.adc2_cs.set_low();
self.adc2.reset()?;

spawn!(delay, 1000);

let mut mode1_cfg = Mode1Register::default();
mode1_cfg.set_filter(ads126x::register::DigitalFilter::Sinc1);
self.adc1.set_mode1(&mode1_cfg, &mut self.spi)?;

let mut mode2_cfg = Mode2Register::default();
mode2_cfg.set_dr(DataRate::SPS1200);
self.adc1.set_mode2(&mode2_cfg, &mut self.spi)?;

self.adc1.send_command(ADCCommand::START1, &mut self.spi)?;
self.adc1.send_command(ADCCommand::START2, &mut self.spi)?;

Ok(())
}
}
6 changes: 6 additions & 0 deletions boards/temperature/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![no_main]

mod adc_manager;
mod communication;
mod data_manager;
mod types;
Expand Down Expand Up @@ -380,6 +381,11 @@ mod app {
}
}

#[task(priority = 3)]
async fn delay(_cx: delay::Context, delay: u32) {
Mono::delay(delay.millis()).await;
}

#[task(shared = [data_manager, &em, rtc])]
async fn state_send(mut cx: state_send::Context) {
let state_data = cx
Expand Down
Loading

0 comments on commit 6d2ce9f

Please sign in to comment.