diff --git a/Cargo.lock b/Cargo.lock index 44886bb..f6e7f56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,6 +178,24 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "enum-iterator" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "enum-iterator-derive 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "enum-iterator-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.30 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "generic-array" version = "0.12.3" @@ -392,6 +410,7 @@ dependencies = [ "cortex-m-rt 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", "cortex-m-rtfm 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "enum-iterator 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "heapless 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "mcp23017 0.1.1 (git+https://github.com/mrd0ll4r/mcp23017.git)", @@ -524,6 +543,8 @@ dependencies = [ "checksum cortex-m-rtfm-macros 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c62092f6ff344e9b0adb748f0302ed69889ba2fae1fce446e3788d4726ea73bb" "checksum cortex-m-semihosting 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "113ef0ecffee2b62b58f9380f4469099b30e9f9cbee2804771b4203ba1762cfa" "checksum embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4908a155094da7723c2d60d617b820061e3b4efcc3d9e293d206a5a76c170b" +"checksum enum-iterator 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c79a6321a1197d7730510c7e3f6cb80432dfefecb32426de8cea0aa19b4bb8d7" +"checksum enum-iterator-derive 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1e94aa31f7c0dc764f57896dc615ddd76fc13b0d5dca7eb6cc5e018a5a09ec06" "checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" "checksum generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0ed1e761351b56f54eb9dcd0cfaca9fd0daecf93918e1cfc01c8a3d26ee7adcd" "checksum hash32 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc" diff --git a/Cargo.toml b/Cargo.toml index acaad2f..f82a952 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ cortex-m-rtfm = "0.5" embedded-hal = "0.2.3" nb = "0.1.2" asm-delay = "0.7.0" +enum-iterator = "0.6.0" [dependencies.mcp23017] git = "https://github.com/mrd0ll4r/mcp23017.git" diff --git a/src/afe.rs b/src/afe.rs index c3d9f3c..dfe0b00 100644 --- a/src/afe.rs +++ b/src/afe.rs @@ -1,6 +1,8 @@ use embedded_hal; +use core::convert::TryFrom; +use enum_iterator::IntoEnumIterator; -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, IntoEnumIterator)] pub enum Gain { G1 = 0b00, G2 = 0b01, @@ -13,6 +15,20 @@ pub struct ProgrammableGainAmplifier { a1: A1 } +impl TryFrom for Gain { + type Error = (); + + fn try_from(value: u8) -> Result { + for gain in Gain::into_enum_iter() { + if value == gain as u8 { + return Ok(gain) + } + } + + Err(()) + } +} + impl ProgrammableGainAmplifier where A0: embedded_hal::digital::v2::StatefulOutputPin, @@ -43,22 +59,15 @@ where } } - pub fn get_gain(&self) -> Gain { - let lsb_set = self.a0.is_set_high().unwrap(); - let msb_set = self.a1.is_set_high().unwrap(); - - if msb_set { - if lsb_set { - Gain::G10 - } else { - Gain::G5 - } - } else { - if lsb_set { - Gain::G2 - } else { - Gain::G1 - } + pub fn get_gain(&self) -> Result { + let mut code: u8 = 0; + if self.a0.is_set_high().unwrap() { + code |= 0b1; } + if self.a1.is_set_high().unwrap() { + code |= 0b10; + } + + Gain::try_from(code) } } diff --git a/src/main.rs b/src/main.rs index 4401bfe..f6018c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -252,7 +252,7 @@ const APP: () = { .frame_size(16) .swap_mosi_miso(); - dp.SPI4.spi((spi_sck, spi_miso, hal::spi::NoMosi), config, 25.mhz(), &clocks) + dp.SPI4.spi((spi_sck, spi_miso, hal::spi::NoMosi), config, 50.mhz(), &clocks) }; let dac2_spi = { @@ -269,7 +269,7 @@ const APP: () = { .frame_size(16) .swap_mosi_miso(); - dp.SPI5.spi((spi_sck, spi_miso, hal::spi::NoMosi), config, 25.mhz(), &clocks) + dp.SPI5.spi((spi_sck, spi_miso, hal::spi::NoMosi), config, 50.mhz(), &clocks) }; let pounder_devices = { @@ -487,6 +487,7 @@ const APP: () = { y0 as i16 as u16 ^ 0x8000 }; c.resources.adc1.spi.ifcr.write(|w| w.eotc().set_bit()); + c.resources.dac1.send(output_ch1).unwrap(); let output_ch2 = { let a: u16 = nb::block!(c.resources.adc2.read()).unwrap(); @@ -495,8 +496,6 @@ const APP: () = { y0 as i16 as u16 ^ 0x8000 }; c.resources.adc2.spi.ifcr.write(|w| w.eotc().set_bit()); - - c.resources.dac1.send(output_ch1).unwrap(); c.resources.dac2.send(output_ch2).unwrap(); c.resources.dac_pin.set_low().unwrap();