From 2a71b2fc62f98e54d5e023e7d5aeb3dafcc24361 Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Fri, 17 Feb 2023 17:19:30 +0800 Subject: [PATCH] satman: drive SFP TX_DISABLE --- .../src/io_expander.rs | 48 +++++++++---------- src/libboard_artiq/src/lib.rs | 2 + src/runtime/src/main.rs | 30 +++++------- src/satman/src/main.rs | 15 ++++++ 4 files changed, 52 insertions(+), 43 deletions(-) rename src/{runtime => libboard_artiq}/src/io_expander.rs (77%) diff --git a/src/runtime/src/io_expander.rs b/src/libboard_artiq/src/io_expander.rs similarity index 77% rename from src/runtime/src/io_expander.rs rename to src/libboard_artiq/src/io_expander.rs index a199f766..5e76867f 100644 --- a/src/runtime/src/io_expander.rs +++ b/src/libboard_artiq/src/io_expander.rs @@ -1,4 +1,4 @@ -use crate::i2c; +use libboard_zynq::i2c; use log::info; // Only the bare minimum registers. Bits/IO connections equivalent between IC types. @@ -10,10 +10,9 @@ struct Registers { gpiob: u8, // Output Port 1 } -pub struct IoExpander { - busno: i32, - port: u8, - address: i32, +pub struct IoExpander<'a> { + i2c: &'a mut i2c::I2c, + address: u8, iodir: [u8; 2], out_current: [u8; 2], out_target: [u8; 2], @@ -21,14 +20,13 @@ pub struct IoExpander { } -impl IoExpander { - pub fn new(index: u8) -> Result { +impl<'a> IoExpander<'a> { + pub fn new(i2c: &'a mut i2c::I2c, index: u8) -> Result { // Both expanders on SHARED I2C bus let mut io_expander = match index { 0 => IoExpander { - busno: 0, - port: 11, + i2c, address: 0x40, iodir: [0xff; 2], out_current: [0; 2], @@ -41,8 +39,7 @@ impl IoExpander { }, }, 1 => IoExpander { - busno: 0, - port: 11, + i2c, address: 0x42, iodir: [0xff; 2], out_current: [0; 2], @@ -75,32 +72,31 @@ impl IoExpander { Ok(io_expander) } - fn select(&self) -> Result<(), &'static str> { - let mask: u16 = 1 << self.port; - i2c::switch_select(self.busno, 0x70, mask as u8 as i32); - i2c::switch_select(self.busno, 0x71, (mask >> 8) as u8 as i32); + fn select(&mut self) -> Result<(), &'static str> { + self.i2c.pca954x_select(0x70, None)?; + self.i2c.pca954x_select(0x71, Some(3))?; Ok(()) } - fn write(&self, addr: u8, value: u8) -> Result<(), &'static str> { - i2c::start(self.busno); - i2c::write(self.busno, self.address as i32); - i2c::write(self.busno, addr as i32); - i2c::write(self.busno, value as i32); - i2c::stop(self.busno); + fn write(&mut self, addr: u8, value: u8) -> Result<(), &'static str> { + self.i2c.start()?; + self.i2c.write(self.address)?; + self.i2c.write(addr)?; + self.i2c.write(value)?; + self.i2c.stop()?; Ok(()) } - fn check_ack(&self) -> Result { + fn check_ack(&mut self) -> Result { // Check for ack from io expander self.select()?; - i2c::start(self.busno); - let ack = i2c::write(self.busno, self.address); - i2c::stop(self.busno); + self.i2c.start()?; + let ack = self.i2c.write(self.address)?; + self.i2c.stop()?; Ok(ack) } - fn update_iodir(&self) -> Result<(), &'static str> { + fn update_iodir(&mut self) -> Result<(), &'static str> { self.write(self.registers.iodira, self.iodir[0])?; self.write(self.registers.iodirb, self.iodir[1])?; Ok(()) diff --git a/src/libboard_artiq/src/lib.rs b/src/libboard_artiq/src/lib.rs index b85481c6..8abbb675 100644 --- a/src/libboard_artiq/src/lib.rs +++ b/src/libboard_artiq/src/lib.rs @@ -27,6 +27,8 @@ pub mod drtioaux_async; #[cfg(has_drtio)] #[path = "../../../build/mem.rs"] pub mod mem; +#[cfg(feature = "target_kasli_soc")] +pub mod io_expander; use core::{cmp, str}; use libboard_zynq::slcr; diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index 0f7d6749..3a74223e 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -21,6 +21,8 @@ use void::Void; use libconfig::Config; use libcortex_a9::l2c::enable_l2_cache; use libboard_artiq::{logger, identifier_read, init_gateware, pl}; +#[cfg(feature = "target_kasli_soc")] +use libboard_artiq::io_expander; const ASYNC_ERROR_COLLISION: u8 = 1 << 0; const ASYNC_ERROR_BUSY: u8 = 1 << 1; @@ -45,8 +47,6 @@ mod mgmt; mod analyzer; mod irq; mod i2c; -#[cfg(feature = "target_kasli_soc")] -mod io_expander; static mut SEEN_ASYNC_ERRORS: u8 = 0; @@ -117,21 +117,17 @@ pub fn main_core0() { #[cfg(feature = "target_kasli_soc")] { - let (mut io_expander0, mut io_expander1) = (io_expander::IoExpander::new(0).unwrap(), io_expander::IoExpander::new(1).unwrap()); - io_expander0.init().expect("I2C I/O expander #0 initialization failed"); - io_expander1.init().expect("I2C I/O expander #1 initialization failed"); - - // Actively drive TX_DISABLE to false on SFP0..3 - io_expander0.set_oe(0, 1 << 1).unwrap(); - io_expander0.set_oe(1, 1 << 1).unwrap(); - io_expander1.set_oe(0, 1 << 1).unwrap(); - io_expander1.set_oe(1, 1 << 1).unwrap(); - io_expander0.set(0, 1, false); - io_expander0.set(1, 1, false); - io_expander1.set(0, 1, false); - io_expander1.set(1, 1, false); - io_expander0.service().unwrap(); - io_expander1.service().unwrap(); + let i2c = unsafe { (&mut i2c::I2C_BUS).as_mut().unwrap() }; + for expander_i in 0..=1 { + let mut io_expander = io_expander::IoExpander::new(i2c, expander_i).unwrap(); + io_expander.init().expect("I2C I/O expander #0 initialization failed"); + // Actively drive TX_DISABLE to false on SFP0..3 + io_expander.set_oe(0, 1 << 1).unwrap(); + io_expander.set_oe(1, 1 << 1).unwrap(); + io_expander.set(0, 1, false); + io_expander.set(1, 1, false); + io_expander.service().unwrap(); + } } let cfg = match Config::new() { diff --git a/src/satman/src/main.rs b/src/satman/src/main.rs index 9544f011..fe97a906 100644 --- a/src/satman/src/main.rs +++ b/src/satman/src/main.rs @@ -22,6 +22,8 @@ use libboard_zynq::{i2c::I2c, timer::GlobalTimer, time::Milliseconds, print, pri use libsupport_zynq::ram; #[cfg(has_si5324)] use libboard_artiq::si5324; +#[cfg(feature = "target_kasli_soc")] +use libboard_artiq::io_expander; use libboard_artiq::{pl::csr, drtio_routing, drtioaux, logger, identifier_read, init_gateware}; use libcortex_a9::{spin_lock_yield, interrupt_handler, regs::{MPIDR, SP}, notify_spin_lock, asm, l2c::enable_l2_cache}; use libregister::{RegisterW, RegisterR}; @@ -468,6 +470,19 @@ pub extern fn main_core0() -> i32 { let mut i2c = I2c::i2c0(); i2c.init().expect("I2C initialization failed"); + #[cfg(feature = "target_kasli_soc")] + { + for expander_i in 0..=1 { + let mut io_expander = io_expander::IoExpander::new(&mut i2c, expander_i).unwrap(); + io_expander.init().expect("I2C I/O expander #0 initialization failed"); + // Actively drive TX_DISABLE to false on SFP0..3 + io_expander.set_oe(0, 1 << 1).unwrap(); + io_expander.set_oe(1, 1 << 1).unwrap(); + io_expander.set(0, 1, false); + io_expander.set(1, 1, false); + io_expander.service().unwrap(); + } + } #[cfg(has_si5324)] si5324::setup(&mut i2c, &SI5324_SETTINGS, si5324::Input::Ckin1, &mut timer).expect("cannot initialize Si5324");