From afe00402b7e56cea4ebdd36ed0c0fd4cf045c438 Mon Sep 17 00:00:00 2001 From: occheung Date: Mon, 17 Aug 2020 11:45:42 +0800 Subject: [PATCH] dds: add register macro --- src/dds.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/dds.rs b/src/dds.rs index e276395..a806cf3 100644 --- a/src/dds.rs +++ b/src/dds.rs @@ -57,13 +57,16 @@ construct_bitmask!(CFR3Mask; u32; DRV0, 28, 2 ) +const WRITE_MASK :u8 = 0x00; +const READ_MASK :u8 = 0x80; + pub struct DDS { spi: SPI, } impl DDS where - SPI: Transfer + SPI: Transfer { pub fn new(spi: SPI) -> Self { DDS { @@ -82,3 +85,54 @@ where self.spi.transfer(words).map_err(Error::SPI) } } + +macro_rules! impl_register_io { + ($($reg_addr: expr, $reg_byte_size: expr),+) => { + impl DDS + where + SPI: Transfer + { + pub fn write_register(&mut self, addr: u8, bytes: &mut[u8]) -> Result<(), Error> { + match addr { + $( + $reg_addr => { + assert_eq!(bytes.len(), $reg_byte_size); + let arr: [u8; $reg_byte_size + 1] = [addr, 0; $reg_byte_size]; + for i in 0..$reg_byte_size { + arr[i+1] = bytes[i]; + } + match self.spi.transfer(words).map_err(Error) { + Ok(v) => Ok(()), + Err(e) => Err(e), + }, + }, + )* + } + } + } + } +} + +impl_register_io!( + 0x00, 4, + 0x01, 4, + 0x02, 4, + 0x03, 4, + 0x04, 4, + 0x07, 4, + 0x08, 2, + 0x09, 4, + 0x0A, 4, + 0x0B, 8, + 0x0C, 8, + 0x0D, 4, + 0x0E, 8, + 0x0F, 8, + 0x10, 8, + 0x11, 8, + 0x12, 8, + 0x13, 8, + 0x14, 8, + 0x15, 8, + 0x16, 4 +);