From d666f3d5730e30d3a63dd02f575238596231cfae Mon Sep 17 00:00:00 2001 From: Stephan Maka Date: Tue, 13 Aug 2019 00:33:11 +0200 Subject: [PATCH] firmware: factor out mod pca9548 from si5324 orepares for further i2c devices. --- artiq/firmware/libboard_artiq/lib.rs | 5 +++++ artiq/firmware/libboard_artiq/pca9548.rs | 13 ++++++++++++ artiq/firmware/libboard_artiq/si5324.rs | 27 ++++++------------------ 3 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 artiq/firmware/libboard_artiq/pca9548.rs diff --git a/artiq/firmware/libboard_artiq/lib.rs b/artiq/firmware/libboard_artiq/lib.rs index bcfdf8d9e..1900719ec 100644 --- a/artiq/firmware/libboard_artiq/lib.rs +++ b/artiq/firmware/libboard_artiq/lib.rs @@ -25,6 +25,11 @@ pub mod mailbox; #[cfg(has_kernel_cpu)] pub mod rpc_queue; +#[cfg(any(soc_platform = "kasli", + soc_platform = "sayma_amc", + soc_platform = "sayma_rtm", + soc_platform = "kc705"))] +mod pca9548; #[cfg(has_si5324)] pub mod si5324; diff --git a/artiq/firmware/libboard_artiq/pca9548.rs b/artiq/firmware/libboard_artiq/pca9548.rs new file mode 100644 index 000000000..684620d0e --- /dev/null +++ b/artiq/firmware/libboard_artiq/pca9548.rs @@ -0,0 +1,13 @@ +use i2c; + +pub fn select(busno: u8, address: u8, channels: u8) -> Result<(), &'static str> { + i2c::start(busno).unwrap(); + if !i2c::write(busno, address << 1).unwrap() { + return Err("PCA9548 failed to ack write address") + } + if !i2c::write(busno, channels).unwrap() { + return Err("PCA9548 failed to ack control word") + } + i2c::stop(busno).unwrap(); + Ok(()) +} diff --git a/artiq/firmware/libboard_artiq/si5324.rs b/artiq/firmware/libboard_artiq/si5324.rs index e722c44d3..d02bec693 100644 --- a/artiq/firmware/libboard_artiq/si5324.rs +++ b/artiq/firmware/libboard_artiq/si5324.rs @@ -3,28 +3,13 @@ use board_misoc::clock; #[cfg(not(si5324_soft_reset))] use board_misoc::csr; use i2c; +use pca9548; type Result = result::Result; const BUSNO: u8 = 0; const ADDRESS: u8 = 0x68; -#[cfg(any(soc_platform = "kasli", - soc_platform = "sayma_amc", - soc_platform = "sayma_rtm", - soc_platform = "kc705"))] -fn pca9548_select(address: u8, channels: u8) -> Result<()> { - i2c::start(BUSNO).unwrap(); - if !i2c::write(BUSNO, address << 1).unwrap() { - return Err("PCA9548 failed to ack write address") - } - if !i2c::write(BUSNO, channels).unwrap() { - return Err("PCA9548 failed to ack control word") - } - i2c::stop(BUSNO).unwrap(); - Ok(()) -} - #[cfg(not(si5324_soft_reset))] fn hard_reset() { unsafe { csr::si5324_rst_n::out_write(0); } @@ -196,15 +181,15 @@ fn init() -> Result<()> { #[cfg(soc_platform = "kasli")] { - pca9548_select(0x70, 0)?; - pca9548_select(0x71, 1 << 3)?; + pca9548::select(BUSNO, 0x70, 0)?; + pca9548::select(BUSNO, 0x71, 1 << 3)?; } #[cfg(soc_platform = "sayma_amc")] - pca9548_select(0x70, 1 << 4)?; + pca9548::select(BUSNO, 0x70, 1 << 4)?; #[cfg(soc_platform = "sayma_rtm")] - pca9548_select(0x77, 1 << 5)?; + pca9548::select(BUSNO, 0x77, 1 << 5)?; #[cfg(soc_platform = "kc705")] - pca9548_select(0x74, 1 << 7)?; + pca9548::select(BUSNO, 0x74, 1 << 7)?; if ident()? != 0x0182 { return Err("Si5324 does not have expected product number");