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

SNS - ADC Mux #41

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 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
2 changes: 1 addition & 1 deletion boards/stm32f767zi/src/io/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'d, T: Instance> HypedAdc for Stm32f767ziAdc<'d, T> {

impl<'d, T: Instance> Stm32f767ziAdc<'d, T> {
/// Create a new instance of our ADC implementation for the STM32F767ZI
pub fn new(mut adc: Adc<'d, T>, channel: AnyAdcChannel<T>) -> Self {
pub fn new(adc: Adc<'d, T>, channel: AnyAdcChannel<T>) -> Self {
Self { adc, channel }
}
}
55 changes: 55 additions & 0 deletions lib/io/src/adc_mux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::{
adc::HypedAdc,
i2c::{HypedI2c, I2cError},
};

/// ADCMux implements logic to read a channel on the ADC Mux and report any failures
///
/// Data sheet: https://www.ti.com/lit/ds/symlink/adc128d818.pdf
pub struct ADCMuxChannel<T: HypedI2c> {
i2c: T,
device_address: u8,
channel: AdcChannelAddress,
}

impl<T: HypedI2c> ADCMuxChannel<T> {
/// Create a new instance of the ADC mux sensor and configure it
pub fn new(i2c: T, device_address: u8, channel: AdcChannelAddress) -> Result<Self, MuxError> {
Ok(Self {
i2c,
device_address,
channel,
})
}
}

impl<T: HypedI2c> HypedAdc for ADCMuxChannel<T> {
/// Read a value from the ADC Mux channel
fn read_value(&mut self) -> u16 {
self.i2c
.read_byte(self.device_address, self.channel as u8)
.unwrap() as u16
}
}

pub const ADC_MUX_ADDRESS: u8 = 0x1D;

/// ADC Mux channel addresses
#[repr(u8)]
#[derive(Clone, Copy)]
pub enum AdcChannelAddress {
AdcChannel0 = 0x20,
AdcChannel1 = 0x21,
AdcChannel2 = 0x22,
AdcChannel3 = 0x23,
AdcChannel4 = 0x24,
AdcChannel5 = 0x25,
AdcChannel6 = 0x26,
AdcChannel7 = 0x27,
}

/// Possible errors that can occur when interacting with the ADC Mux
#[derive(Debug)]
pub enum MuxError {
I2cError(I2cError),
}
1 change: 1 addition & 0 deletions lib/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]

pub mod adc;
pub mod adc_mux;
pub mod gpio;
pub mod i2c;
pub mod spi;