diff --git a/Cargo.toml b/Cargo.toml index 482be36..811c537 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,8 @@ name = "dac8568" version = "0.0.6" authors = ["Oliver Stenning "] edition = "2018" -description = "A simple platform agnostic library for the Texas Instruments DAC8568" -keywords = ["embedded-hal", "driver", "dac", "8568", "DAC8568"] +description = "A simple platform agnostic library for the Texas Instruments DAC8568, DAC8168 and DAC7568" +keywords = ["embedded-hal", "driver", "DAC7568", "DAC8168", "DAC8568"] categories = ["embedded", "hardware-support", "no-std"] license = "MIT" readme = "README.md" diff --git a/README.md b/README.md index 7556bc6..0102446 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ let spi: Spi = interface.spi( (sck, NoMiso, mosi), spi::MODE_1, // polarity: Polarity::IdleLow, // phase: Phase::CaptureOnSecondTransition, - 20.mhz(), + 10.mhz(), prec, clocks, ); @@ -44,6 +44,8 @@ let spi: Spi = interface.spi( let sync = sync.into_push_pull_output(); // Initialize the dac instance let mut dac = dac8568::Dac::new(spi, sync); +// Perform a software reset of DAC8568 to clear all registers +dac.reset(); // Configure the DAC to use the internal 2.5v reference dac.use_internal_reference().unwrap(); // Optionally, invert the output signal @@ -52,7 +54,7 @@ dac.set_inverted_output(true); dac.set_voltage(dac8568::Channel::A, voltage).unwrap(); // Alternatively, you can maintain ownership of the SPI and SYNC if you need to use -// asynchronous communication such as Interrupts and/or DMA +// asynchronous communication such as Interrupts and/or DMA. let (spi, sync) = dac.release(); // And then access the desired message directly let message = dac8568::Message::get_voltage_message(dac8568::Channel::A, voltage, false); diff --git a/src/lib.rs b/src/lib.rs index 4c2b681..b41ce5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,15 +111,35 @@ impl Message { } } - /// Get the message payload - pub fn get_payload(&self) -> [u8; 4] { + /// Get software reset message + /// 8.2.10 Software Reset Function + /// The DAC7568, DAC8168, and DAC8568 contain a software reset feature. + /// If the software reset feature is executed, all registers inside the device are reset to default settings; that is, + /// all DAC channels are reset to the power-on reset code (power on reset to zero scale for grades A and C; power on reset to midscale for grades B and D). + pub fn get_software_reset_message() -> Message { + Message { + prefix: 0b0001, + control: 0b1100, + address: 0x00, + data: 0x00, + feature: 0x00, + } + } + + /// Get the message payload word + pub fn get_payload_word(&self) -> u32 { let mut payload: u32 = 0x00; - payload = payload | ((self.prefix as u32) << 28); - payload = payload | ((self.control as u32) << 24); - payload = payload | ((self.address as u32) << 20); - payload = payload | ((self.data as u32) << 4); - payload = payload | ((self.feature as u32) << 0); - payload.to_be_bytes() + payload |= (self.prefix as u32) << 28; + payload |= (self.control as u32) << 24; + payload |= (self.address as u32) << 20; + payload |= (self.data as u32) << 4; + payload |= self.feature as u32; + payload + } + + /// Get the message payload + pub fn get_payload_bytes(&self) -> [u8; 4] { + self.get_payload_word().to_be_bytes() } } @@ -187,9 +207,19 @@ where self.write(message) } + /// Perform a software reset, clearing out all registers + /// 8.2.10 Software Reset Function + /// The DAC7568, DAC8168, and DAC8568 contain a software reset feature. + /// If the software reset feature is executed, all registers inside the device are reset to default settings; that is, + /// all DAC channels are reset to the power-on reset code (power on reset to zero scale for grades A and C; power on reset to midscale for grades B and D). + pub fn reset(&mut self) -> Result<(), DacError> { + let message = Message::get_software_reset_message(); + self.write(message) + } + /// Write to the DAC via a blocking call on the specified SPI interface fn write(&mut self, message: Message) -> Result<(), DacError> { - let command: [u8; 4] = message.get_payload(); + let command: [u8; 4] = message.get_payload_bytes(); self.sync.set_low().unwrap_or_default(); let result = self.spi.write(&command);