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

DAC: Add wrapper around RIOTs DAC-interface #36

Merged
merged 5 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 60 additions & 0 deletions src/dac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use riot_sys::dac_t;

#[derive(Debug)]
pub struct DACLine {
line: dac_t,
}

#[derive(Debug)]
chrysn marked this conversation as resolved.
Show resolved Hide resolved
pub enum DACError {
/// The given dac_t line did not exist
NoLine,
/// An unknown error did occur
Unknown,
}

impl DACLine {
/// Creates and intializes a new [`DACLine`] from the given
/// unitialized [`dac_t`].
///
/// Returns an Error if the given line does not exist
/// on the board.
pub fn new(line: dac_t) -> Result<Self, DACError> {
Copy link
Member

@chrysn chrysn Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind trying this out with #37 (comment) ?

[edit addition]: It'll need to use the latest riot-sys master version, but if your test code is in the RIOT tree, its .cargo/config should allow a cargo update riot-sys to pick up on that version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that DAC_LINE is missing from riot_sys/build.rs which seems to be the equivalent for *_DEV in the DAC interface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed that reply. A version of -sys that adds DAC_LINE is being merged, and should thus be usable easily in a few mintues

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, is now done in 25dead1

let res = unsafe { riot_sys::dac_init(line) } as i32;

const DAC_OK: i32 = riot_sys::DAC_OK as i32;
const DAC_NOLINE: i32 = riot_sys::DAC_NOLINE as i32;

match res {
DAC_OK => Ok(DACLine { line }),
DAC_NOLINE => Err(DACError::NoLine),
_ => Err(DACError::Unknown),
}
}

/// Builds a [`DACLine`] from an already initialized [`dac_t`].
///
/// Providing a not initialized [`dac_t`] results in undefined behavior.
pub unsafe fn new_without_init(line: dac_t) -> Self {
DACLine { line }
}

/// Writes the given value to this [`DACLine`]
///
/// The `value` is internally scaled to the underlying
/// dac device so that the maximum voltage output
/// is always equivalent to [`u16::MAX`]
pub fn set(&self, value: u16) {
chrysn marked this conversation as resolved.
Show resolved Hide resolved
unsafe { riot_sys::dac_set(self.line, value) }
}

/// Turns the [`DACLine`] on after `DACLine::power_off`
pub fn power_on(&self) {
unsafe { riot_sys::dac_poweron(self.line) }
}

/// Turns the [`DACLine`] off
pub fn power_off(&self) {
unsafe { riot_sys::dac_poweroff(self.line) }
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ pub mod spi;
#[cfg(riot_module_periph_adc)]
pub mod adc;

#[cfg(riot_module_periph_dac)]
pub mod dac;

#[cfg(riot_module_ztimer)]
pub mod ztimer;

Expand Down