Skip to content

Commit

Permalink
add software reset message, update example and fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
ost-ing committed Jun 30, 2022
1 parent bcef97e commit d52d208
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name = "dac8568"
version = "0.0.6"
authors = ["Oliver Stenning <[email protected]>"]
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"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ let spi: Spi<SPI1, Enabled> = interface.spi(
(sck, NoMiso, mosi),
spi::MODE_1, // polarity: Polarity::IdleLow,
// phase: Phase::CaptureOnSecondTransition,
20.mhz(),
10.mhz(),
prec,
clocks,
);
// Initialise SYNC for SPI communications
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
Expand All @@ -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);
Expand Down
48 changes: 39 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d52d208

Please sign in to comment.