Skip to content

Commit

Permalink
Add ADC manager to all boards.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSprenger committed Dec 13, 2024
1 parent 2f99606 commit 9d80d85
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions boards/pressure/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
81 changes: 81 additions & 0 deletions boards/pressure/src/adc_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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;

// There is an option to use interrupts using the data ready pins, but for now we will poll.
pub struct AdcManager {

Check warning on line 15 in boards/pressure/src/adc_manager.rs

View workflow job for this annotation

GitHub Actions / All

struct `AdcManager` is never constructed
pub spi: Spi<stm32h7xx_hal::pac::SPI4, stm32h7xx_hal::spi::Enabled, u8>,
pub adc1: Ads126x<Pin<'C', 11, Output<PushPull>>>,
pub adc2: Ads126x<Pin<'E', 0, Output<PushPull>>>,
pub adc1_cs: Pin<'C', 10, Output<PushPull>>,
pub adc2_cs: Pin<'D', 2, Output<PushPull>>,
}

impl AdcManager {
pub fn new(
spi: Spi<stm32h7xx_hal::pac::SPI4, stm32h7xx_hal::spi::Enabled, u8>,
adc1_rst: Pin<'C', 11, Output<PushPull>>,
adc2_rst: Pin<'E', 0, Output<PushPull>>,
adc1_cs: Pin<'C', 10, Output<PushPull>>,
adc2_cs: Pin<'D', 2, Output<PushPull>>,
) -> Self {
Self {
spi,
adc1: Ads126x::new(adc1_rst),
adc2: Ads126x::new(adc2_rst),
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/pressure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod communication;
mod data_manager;
mod adc_manager;
mod types;

use chrono::NaiveDate;
Expand Down Expand Up @@ -322,6 +323,11 @@ mod app {
)
}

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

#[task(priority = 3, shared = [&em, rtc])]
async fn generate_random_messages(mut cx: generate_random_messages::Context) {
loop {
Expand Down
1 change: 1 addition & 0 deletions boards/strain/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
81 changes: 81 additions & 0 deletions boards/strain/src/adc_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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;

// There is an option to use interrupts using the data ready pins, but for now we will poll.
pub struct AdcManager {

Check warning on line 15 in boards/strain/src/adc_manager.rs

View workflow job for this annotation

GitHub Actions / All

struct `AdcManager` is never constructed
pub spi: Spi<stm32h7xx_hal::pac::SPI4, stm32h7xx_hal::spi::Enabled, u8>,
pub adc1: Ads126x<Pin<'C', 11, Output<PushPull>>>,
pub adc2: Ads126x<Pin<'E', 0, Output<PushPull>>>,
pub adc1_cs: Pin<'C', 10, Output<PushPull>>,
pub adc2_cs: Pin<'D', 2, Output<PushPull>>,
}

impl AdcManager {
pub fn new(

Check warning on line 24 in boards/strain/src/adc_manager.rs

View workflow job for this annotation

GitHub Actions / All

associated items `new`, `init_adc1`, and `init_adc2` are never used
spi: Spi<stm32h7xx_hal::pac::SPI4, stm32h7xx_hal::spi::Enabled, u8>,
adc1_rst: Pin<'C', 11, Output<PushPull>>,
adc2_rst: Pin<'E', 0, Output<PushPull>>,
adc1_cs: Pin<'C', 10, Output<PushPull>>,
adc2_cs: Pin<'D', 2, Output<PushPull>>,
) -> Self {
Self {
spi,
adc1: Ads126x::new(adc1_rst),
adc2: Ads126x::new(adc2_rst),
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);

Check warning on line 45 in boards/strain/src/adc_manager.rs

View workflow job for this annotation

GitHub Actions / All

unused `Result` that must be used

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);

Check warning on line 66 in boards/strain/src/adc_manager.rs

View workflow job for this annotation

GitHub Actions / All

unused `Result` that must be used

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/strain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod communication;
mod data_manager;
mod adc_manager;
mod types;

use chrono::NaiveDate;
Expand Down Expand Up @@ -322,6 +323,11 @@ mod app {
)
}

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

#[task(priority = 3, shared = [&em, rtc])]
async fn generate_random_messages(mut cx: generate_random_messages::Context) {
loop {
Expand Down
1 change: 0 additions & 1 deletion boards/temperature/src/adc_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use stm32h7xx_hal::{

use crate::app::delay;


// There is an option to use interrupts using the data ready pins, but for now we will poll.
pub struct AdcManager {
pub spi: Spi<stm32h7xx_hal::pac::SPI4, stm32h7xx_hal::spi::Enabled, u8>,

Check warning on line 16 in boards/temperature/src/adc_manager.rs

View workflow job for this annotation

GitHub Actions / All

fields `spi`, `adc1`, `adc2`, `adc1_cs`, and `adc2_cs` are never read
Expand Down
18 changes: 9 additions & 9 deletions boards/temperature/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use fdcan::{
filter::{StandardFilter, StandardFilterSlot},
};
use messages::command::RadioRate;
use messages::Message;
use messages::{sensor, Data};
use panic_probe as _;
use rtic_monotonics::systick::prelude::*;
Expand All @@ -28,10 +29,13 @@ use stm32h7xx_hal::gpio::Speed;
use stm32h7xx_hal::gpio::{Output, PushPull};
use stm32h7xx_hal::prelude::*;
use stm32h7xx_hal::rtc;
use stm32h7xx_hal::{
gpio::{Alternate, Pin},
hal::spi,
};
use stm32h7xx_hal::{rcc, rcc::rec};
use messages::Message;
use stm32h7xx_hal::{gpio::{Alternate, Pin}, hal::spi};
use types::COM_ID; // global logger
use adc_manager::AdcManager;

const DATA_CHANNEL_CAPACITY: usize = 10;

Expand All @@ -46,10 +50,6 @@ fn panic() -> ! {

#[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true, dispatchers = [EXTI0, EXTI1, EXTI2, SPI3, SPI2])]
mod app {


use adc_manager::AdcManager;

use super::*;

#[shared]
Expand All @@ -59,7 +59,7 @@ mod app {
// sd_manager: SdManager<
// stm32h7xx_hal::spi::Spi<stm32h7xx_hal::pac::SPI1, stm32h7xx_hal::spi::Enabled>,
// PA4<Output<PushPull>>,
// >,
// >,
radio_manager: RadioManager,
can_command_manager: CanCommandManager,
can_data_manager: CanDataManager,
Expand Down Expand Up @@ -256,7 +256,7 @@ mod app {

// let sd_manager = SdManager::new(spi_sd, cs_sd);

// ADC setup
// ADC setup
let adc_spi: stm32h7xx_hal::spi::Spi<
stm32h7xx_hal::stm32::SPI4,
stm32h7xx_hal::spi::Enabled,
Expand Down Expand Up @@ -344,7 +344,7 @@ mod app {
can_data_manager,
sbg_power,
rtc,
adc_manager
adc_manager,
},
LocalResources {
led_red,
Expand Down

0 comments on commit 9d80d85

Please sign in to comment.