dds: add register macro

This commit is contained in:
occheung 2020-08-17 11:45:42 +08:00
parent f32de647d3
commit afe00402b7
1 changed files with 55 additions and 1 deletions

View File

@ -57,6 +57,9 @@ construct_bitmask!(CFR3Mask; u32;
DRV0, 28, 2
)
const WRITE_MASK :u8 = 0x00;
const READ_MASK :u8 = 0x80;
pub struct DDS<SPI> {
spi: SPI,
}
@ -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<SPI, E> DDS<SPI>
where
SPI: Transfer<u8, Error = E>
{
pub fn write_register(&mut self, addr: u8, bytes: &mut[u8]) -> Result<(), Error<E>> {
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<E>) {
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
);