From 3d4d42a2ee8cc6d0928db7417e1e850202a3710c Mon Sep 17 00:00:00 2001 From: Joel Aschmann Date: Mon, 6 Feb 2023 13:55:17 +0100 Subject: [PATCH 1/5] DAC: Add wrapper around RIOTs DAC-interface --- src/dac.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ 2 files changed, 63 insertions(+) create mode 100644 src/dac.rs diff --git a/src/dac.rs b/src/dac.rs new file mode 100644 index 00000000..0d81ab18 --- /dev/null +++ b/src/dac.rs @@ -0,0 +1,60 @@ +use riot_sys::dac_t; + +#[derive(Debug)] +pub struct DACLine { + line: dac_t, +} + +#[derive(Debug)] +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 { + 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) { + 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) } + } +} diff --git a/src/lib.rs b/src/lib.rs index 9837ab5a..08e93f65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; From 540bbe422a0cb3d75b424c509e67299bfc5d4c7e Mon Sep 17 00:00:00 2001 From: Joel Aschmann Date: Tue, 7 Feb 2023 17:10:08 +0100 Subject: [PATCH 2/5] DAC: error: non_exhaustive, use &mut refs --- src/dac.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/dac.rs b/src/dac.rs index 0d81ab18..e5495bc2 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -6,6 +6,7 @@ pub struct DACLine { } #[derive(Debug)] +#[non_exhaustive] pub enum DACError { /// The given dac_t line did not exist NoLine, @@ -44,17 +45,17 @@ impl 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) { + pub fn set(&mut self, value: u16) { unsafe { riot_sys::dac_set(self.line, value) } } /// Turns the [`DACLine`] on after `DACLine::power_off` - pub fn power_on(&self) { + pub fn power_on(&mut self) { unsafe { riot_sys::dac_poweron(self.line) } } /// Turns the [`DACLine`] off - pub fn power_off(&self) { + pub fn power_off(&mut self) { unsafe { riot_sys::dac_poweroff(self.line) } } } From 8d66f72ee177bf109b3e760d319c115e3ebbea40 Mon Sep 17 00:00:00 2001 From: Joel Aschmann Date: Sat, 25 Feb 2023 10:42:03 +0100 Subject: [PATCH 3/5] DAC: implement https://github.com/RIOT-OS/rust-riot-wrappers/issues/37 --- src/dac.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/dac.rs b/src/dac.rs index e5495bc2..651a232d 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -15,12 +15,16 @@ pub enum DACError { } impl DACLine { - /// Creates and intializes a new [`DACLine`] from the given - /// unitialized [`dac_t`]. + /// Creates and intializes a new [`DACLine`]. + /// + /// The `index` indicates which dac device from the current board should be used. + /// For information on how many such devices are available for this board please + /// refer to its RIOT documentation. /// /// Returns an Error if the given line does not exist /// on the board. - pub fn new(line: dac_t) -> Result { + pub fn new(index: usize) -> Result { + let line = unsafe { riot_sys::macro_DAC_LINE(index as u32) }; let res = unsafe { riot_sys::dac_init(line) } as i32; const DAC_OK: i32 = riot_sys::DAC_OK as i32; From c9cf3f2195af4d8c3342ec11078d3d9dc21afa4d Mon Sep 17 00:00:00 2001 From: chrysn Date: Sat, 25 Feb 2023 13:23:28 +0100 Subject: [PATCH 4/5] DAC: Add test --- tests/dac/Cargo.toml | 19 +++++++++++++++++++ tests/dac/Makefile | 14 ++++++++++++++ tests/dac/src/lib.rs | 11 +++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/dac/Cargo.toml create mode 100644 tests/dac/Makefile create mode 100644 tests/dac/src/lib.rs diff --git a/tests/dac/Cargo.toml b/tests/dac/Cargo.toml new file mode 100644 index 00000000..f4e0ef37 --- /dev/null +++ b/tests/dac/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "riot-wrappers-test-dac" +version = "0.1.0" +authors = ["Christian Amsüss "] +edition = "2021" +publish = false + +[lib] +crate-type = ["staticlib"] + +[profile.release] +panic = "abort" + +[dependencies] +riot-wrappers = { version = "*", features = [ "set_panic_handler" ] } +riot-sys = "*" + +[patch.crates-io] +riot-sys = { git = "https://github.com/RIOT-OS/rust-riot-sys" } diff --git a/tests/dac/Makefile b/tests/dac/Makefile new file mode 100644 index 00000000..3e39f4c3 --- /dev/null +++ b/tests/dac/Makefile @@ -0,0 +1,14 @@ +# name of your application +APPLICATION = riot-wrappers-test-dac +APPLICATION_RUST_MODULE = riot_wrappers_test_dac +BASELIBS += $(APPLICATION_RUST_MODULE).module +FEATURES_REQUIRED += rust_target +FEATURES_REQUIRED += periph_dac + +# Of these boards it is known that DAC0 may be driven arbitrarily because it's +# just a pin on the extension header. (It's probably true for most boards, but +# so far nobody guarantees that. Just add your board here if your DAC0 pin is +# good to use). +BOARD_WHITELIST = stk3700 + +include $(RIOTBASE)/Makefile.include diff --git a/tests/dac/src/lib.rs b/tests/dac/src/lib.rs new file mode 100644 index 00000000..12c5247b --- /dev/null +++ b/tests/dac/src/lib.rs @@ -0,0 +1,11 @@ +#![no_std] + +use riot_wrappers::dac; +use riot_wrappers::riot_main; + +riot_main!(main); + +fn main() { + let mut dac = dac::DACLine::new(0).unwrap(); + dac.set(655); // 1% of maximum value +} From b9b56eed685a119ee357745970fe95023f2bc939 Mon Sep 17 00:00:00 2001 From: chrysn Date: Sat, 25 Feb 2023 13:23:48 +0100 Subject: [PATCH 5/5] CI: Add a board that has a DAC to the tests --- .github/workflows/buildtest.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/buildtest.yml b/.github/workflows/buildtest.yml index 89fb631e..bba6f9e7 100644 --- a/.github/workflows/buildtest.yml +++ b/.github/workflows/buildtest.yml @@ -37,7 +37,7 @@ jobs: # update` is much faster the second time (so a parallel execution may # still be faster but uses 3x the resources) run: | - export BOARDS='native sltb001a samr21-xpro' + export BOARDS='native sltb001a samr21-xpro stk3700' DIRS='examples/rust-hello-world examples/rust-gcoap tests/rust_minimal' # It appears that there has to be output before :: commands really catch on echo "Building ${DIRS} on ${BOARDS}" @@ -56,7 +56,7 @@ jobs: echo "::echo ::off" - name: Build tests run: | - export BOARDS='native sltb001a samr21-xpro' + export BOARDS='native sltb001a samr21-xpro stk3700' DIRS=$(echo tests/*/) export RIOTBASE=$(pwd)/RIOT # It appears that there has to be output before :: commands really catch on