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

Adc driver #7

Merged
merged 22 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
18 changes: 18 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(())
}
}
4 changes: 2 additions & 2 deletions boards/pressure/src/data_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl DataManager {
pub fn handle_data(&mut self, data: Message) {
match data.data {
messages::Data::Sensor(ref sensor) => match sensor.data {
messages::sensor::SensorData::SbgData(ref sbg_data) => match sbg_data{
messages::sensor::SensorData::SbgData(ref sbg_data) => match sbg_data {
messages::sensor::SbgData::EkfNavAcc(_) => {
self.ekf_nav_acc = Some(data);
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl DataManager {
messages::sensor::SbgData::GpsPos2(_) => {
self.gps_pos_2 = Some(data);
}
}
},
messages::sensor::SensorData::RecoverySensing(_) => {
self.recovery_sensing = Some(data);
}
Expand Down
39 changes: 25 additions & 14 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,14 +323,19 @@ 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 {
cx.shared.em.run(|| {
let message = Message::new(
cx.shared.rtc.lock(|rtc| {
messages::FormattedNaiveDateTime(rtc.date_time().unwrap())
}),
cx.shared
.rtc
.lock(|rtc| messages::FormattedNaiveDateTime(rtc.date_time().unwrap())),
COM_ID,
messages::state::State::new(messages::state::StateData::Initializing),
);
Expand Down Expand Up @@ -363,10 +369,13 @@ mod app {
stm32h7xx_hal::rcc::ResetReason::Unknown { rcc_rsr } => sensor::ResetReason::Unknown { rcc_rsr },
stm32h7xx_hal::rcc::ResetReason::WindowWatchdogReset => sensor::ResetReason::WindowWatchdogReset,
};
let message =
messages::Message::new(cx.shared.rtc.lock(|rtc| {
messages::FormattedNaiveDateTime(rtc.date_time().unwrap())
}), COM_ID, sensor::Sensor::new(x));
let message = messages::Message::new(
cx.shared
.rtc
.lock(|rtc| messages::FormattedNaiveDateTime(rtc.date_time().unwrap())),
COM_ID,
sensor::Sensor::new(x),
);

cx.shared.em.run(|| {
spawn!(send_gs, message)?;
Expand All @@ -385,10 +394,13 @@ mod app {
.lock(|data_manager| data_manager.state.clone());
cx.shared.em.run(|| {
if let Some(x) = state_data {
let message =
Message::new(cx.shared.rtc.lock(|rtc| {
messages::FormattedNaiveDateTime(rtc.date_time().unwrap())
}), COM_ID, messages::state::State::new(x));
let message = Message::new(
cx.shared
.rtc
.lock(|rtc| messages::FormattedNaiveDateTime(rtc.date_time().unwrap())),
COM_ID,
messages::state::State::new(x),
);
spawn!(send_gs, message)?;
} // if there is none we still return since we simply don't have data yet.
Ok(())
Expand Down Expand Up @@ -442,8 +454,7 @@ mod app {
}

#[task(priority = 3, shared = [rtc, &em])]
async fn send_gs_intermediate(mut cx: send_gs_intermediate::Context, m: Data)
{
async fn send_gs_intermediate(mut cx: send_gs_intermediate::Context, m: Data) {
cx.shared.em.run(|| {
cx.shared.rtc.lock(|rtc| {
let message = messages::Message::new(
Expand Down Expand Up @@ -572,4 +583,4 @@ mod app {
sbg.set_low();
});
}
}
}
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(())
}
}
4 changes: 2 additions & 2 deletions boards/strain/src/data_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl DataManager {
pub fn handle_data(&mut self, data: Message) {
match data.data {
messages::Data::Sensor(ref sensor) => match sensor.data {
messages::sensor::SensorData::SbgData(ref sbg_data) => match sbg_data{
messages::sensor::SensorData::SbgData(ref sbg_data) => match sbg_data {
messages::sensor::SbgData::EkfNavAcc(_) => {
self.ekf_nav_acc = Some(data);
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl DataManager {
messages::sensor::SbgData::GpsPos2(_) => {
self.gps_pos_2 = Some(data);
}
}
},
messages::sensor::SensorData::RecoverySensing(_) => {
self.recovery_sensing = Some(data);
}
Expand Down
Loading
Loading