change io_expander not to borrow i2c indefinitely and create io_expander0..1 to store out_current data #255

Merged
sb10q merged 1 commits from morgan/artiq-zynq:refactor_expander into release-7 2023-08-31 15:50:23 +08:00
3 changed files with 77 additions and 58 deletions
Showing only changes of commit 5597927df9 - Show all commits

View File

@ -10,8 +10,7 @@ struct Registers {
gpiob: u8, // Output Port 1 gpiob: u8, // Output Port 1
} }
pub struct IoExpander<'a> { pub struct IoExpander {
i2c: &'a mut i2c::I2c,
address: u8, address: u8,
iodir: [u8; 2], iodir: [u8; 2],
out_current: [u8; 2], out_current: [u8; 2],
@ -19,14 +18,12 @@ pub struct IoExpander<'a> {
registers: Registers, registers: Registers,
} }
impl IoExpander {
impl<'a> IoExpander<'a> { pub fn new(i2c: &mut i2c::I2c, index: u8) -> Result<Self, &'static str> {
pub fn new(i2c: &'a mut i2c::I2c, index: u8) -> Result<Self, &'static str> {
// Both expanders on SHARED I2C bus // Both expanders on SHARED I2C bus
let mut io_expander = match index { let mut io_expander = match index {
0 => IoExpander { 0 => IoExpander {
i2c,
address: 0x40, address: 0x40,
iodir: [0xff; 2], iodir: [0xff; 2],
out_current: [0; 2], out_current: [0; 2],
@ -39,7 +36,6 @@ impl<'a> IoExpander<'a> {
}, },
}, },
1 => IoExpander { 1 => IoExpander {
i2c,
address: 0x42, address: 0x42,
iodir: [0xff; 2], iodir: [0xff; 2],
out_current: [0; 2], out_current: [0; 2],
@ -53,7 +49,7 @@ impl<'a> IoExpander<'a> {
}, },
_ => return Err("incorrect I/O expander index"), _ => return Err("incorrect I/O expander index"),
}; };
if !io_expander.check_ack()? { if !io_expander.check_ack(i2c)? {
info!( info!(
"MCP23017 io expander {} not found. Checking for PCA9539.", "MCP23017 io expander {} not found. Checking for PCA9539.",
index index
@ -65,57 +61,57 @@ impl<'a> IoExpander<'a> {
gpioa: 0x02, gpioa: 0x02,
gpiob: 0x03, gpiob: 0x03,
}; };
if !io_expander.check_ack()? { if !io_expander.check_ack(i2c)? {
return Err("Neither MCP23017 nor PCA9539 io expander found."); return Err("Neither MCP23017 nor PCA9539 io expander found.");
}; };
} }
Ok(io_expander) Ok(io_expander)
} }
fn select(&mut self) -> Result<(), &'static str> { fn select(&self, i2c: &mut i2c::I2c) -> Result<(), &'static str> {
self.i2c.pca954x_select(0x70, None)?; i2c.pca954x_select(0x70, None)?;
self.i2c.pca954x_select(0x71, Some(3))?; i2c.pca954x_select(0x71, Some(3))?;
Ok(()) Ok(())
} }
fn write(&mut self, addr: u8, value: u8) -> Result<(), &'static str> { fn write(&self, i2c: &mut i2c::I2c, addr: u8, value: u8) -> Result<(), &'static str> {
self.i2c.start()?; i2c.start()?;
self.i2c.write(self.address)?; i2c.write(self.address)?;
self.i2c.write(addr)?; i2c.write(addr)?;
self.i2c.write(value)?; i2c.write(value)?;
self.i2c.stop()?; i2c.stop()?;
Ok(()) Ok(())
} }
fn check_ack(&mut self) -> Result<bool, &'static str> { fn check_ack(&self, i2c: &mut i2c::I2c) -> Result<bool, &'static str> {
// Check for ack from io expander // Check for ack from io expander
self.select()?; self.select(i2c)?;
self.i2c.start()?; i2c.start()?;
let ack = self.i2c.write(self.address)?; let ack = i2c.write(self.address)?;
self.i2c.stop()?; i2c.stop()?;
Ok(ack) Ok(ack)
} }
fn update_iodir(&mut self) -> Result<(), &'static str> { fn update_iodir(&self, i2c: &mut i2c::I2c) -> Result<(), &'static str> {
self.write(self.registers.iodira, self.iodir[0])?; self.write(i2c, self.registers.iodira, self.iodir[0])?;
self.write(self.registers.iodirb, self.iodir[1])?; self.write(i2c, self.registers.iodirb, self.iodir[1])?;
Ok(()) Ok(())
} }
pub fn init(&mut self) -> Result<(), &'static str> { pub fn init(&mut self, i2c: &mut i2c::I2c) -> Result<(), &'static str> {
self.select()?; self.select(i2c)?;
self.update_iodir()?; self.update_iodir(i2c)?;
self.out_current[0] = 0x00; self.out_current[0] = 0x00;
self.write(self.registers.gpioa, 0x00)?; self.write(i2c, self.registers.gpioa, 0x00)?;
self.out_current[1] = 0x00; self.out_current[1] = 0x00;
self.write(self.registers.gpiob, 0x00)?; self.write(i2c, self.registers.gpiob, 0x00)?;
Ok(()) Ok(())
} }
pub fn set_oe(&mut self, port: u8, outputs: u8) -> Result<(), &'static str> { pub fn set_oe(&mut self, i2c: &mut i2c::I2c, port: u8, outputs: u8) -> Result<(), &'static str> {
self.iodir[port as usize] &= !outputs; self.iodir[port as usize] &= !outputs;
self.update_iodir()?; self.update_iodir(i2c)?;
Ok(()) Ok(())
} }
@ -127,15 +123,15 @@ impl<'a> IoExpander<'a> {
} }
} }
pub fn service(&mut self) -> Result<(), &'static str> { pub fn service(&mut self, i2c: &mut i2c::I2c) -> Result<(), &'static str> {
if self.out_target != self.out_current { if self.out_target != self.out_current {
self.select()?; self.select(i2c)?;
if self.out_target[0] != self.out_current[0] { if self.out_target[0] != self.out_current[0] {
self.write(self.registers.gpioa, self.out_target[0])?; self.write(i2c, self.registers.gpioa, self.out_target[0])?;
self.out_current[0] = self.out_target[0]; self.out_current[0] = self.out_target[0];
} }
if self.out_target[1] != self.out_current[1] { if self.out_target[1] != self.out_current[1] {
self.write(self.registers.gpiob, self.out_target[1])?; self.write(i2c, self.registers.gpiob, self.out_target[1])?;
self.out_current[1] = self.out_target[1]; self.out_current[1] = self.out_target[1];
} }
} }

View File

@ -114,20 +114,31 @@ pub fn main_core0() {
info!("gateware ident: {}", identifier_read(&mut [0; 64])); info!("gateware ident: {}", identifier_read(&mut [0; 64]));
i2c::init(); i2c::init();
let i2c_bus = unsafe { (i2c::I2C_BUS).as_mut().unwrap() };
#[cfg(feature = "target_kasli_soc")]
let (mut io_expander0, mut io_expander1);
#[cfg(feature = "target_kasli_soc")] #[cfg(feature = "target_kasli_soc")]
{ {
let i2c = unsafe { (&mut i2c::I2C_BUS).as_mut().unwrap() }; io_expander0 = io_expander::IoExpander::new(i2c_bus, 0).unwrap();
for expander_i in 0..=1 { io_expander1 = io_expander::IoExpander::new(i2c_bus, 1).unwrap();
let mut io_expander = io_expander::IoExpander::new(i2c, expander_i).unwrap(); io_expander0
io_expander.init().expect("I2C I/O expander #0 initialization failed"); .init(i2c_bus)
.expect("I2C I/O expander #0 initialization failed");
io_expander1
.init(i2c_bus)
.expect("I2C I/O expander #1 initialization failed");
// Actively drive TX_DISABLE to false on SFP0..3 // Actively drive TX_DISABLE to false on SFP0..3
io_expander.set_oe(0, 1 << 1).unwrap(); io_expander0.set_oe(i2c_bus, 0, 1 << 1).unwrap();
io_expander.set_oe(1, 1 << 1).unwrap(); io_expander1.set_oe(i2c_bus, 0, 1 << 1).unwrap();
io_expander.set(0, 1, false); io_expander0.set_oe(i2c_bus, 1, 1 << 1).unwrap();
io_expander.set(1, 1, false); io_expander1.set_oe(i2c_bus, 1, 1 << 1).unwrap();
io_expander.service().unwrap(); io_expander0.set(0, 1, false);
} io_expander1.set(0, 1, false);
io_expander0.set(1, 1, false);
io_expander1.set(1, 1, false);
io_expander0.service(i2c_bus).unwrap();
io_expander1.service(i2c_bus).unwrap();
} }
let cfg = match Config::new() { let cfg = match Config::new() {

View File

@ -470,18 +470,30 @@ pub extern fn main_core0() -> i32 {
let mut i2c = I2c::i2c0(); let mut i2c = I2c::i2c0();
i2c.init().expect("I2C initialization failed"); i2c.init().expect("I2C initialization failed");
#[cfg(feature = "target_kasli_soc")]
let (mut io_expander0, mut io_expander1);
#[cfg(feature = "target_kasli_soc")] #[cfg(feature = "target_kasli_soc")]
{ {
for expander_i in 0..=1 { io_expander0 = io_expander::IoExpander::new(&mut i2c, 0).unwrap();
let mut io_expander = io_expander::IoExpander::new(&mut i2c, expander_i).unwrap(); io_expander1 = io_expander::IoExpander::new(&mut i2c, 1).unwrap();
io_expander.init().expect("I2C I/O expander #0 initialization failed"); io_expander0
.init(&mut i2c)
.expect("I2C I/O expander #0 initialization failed");
io_expander1
.init(&mut i2c)
.expect("I2C I/O expander #1 initialization failed");
// Actively drive TX_DISABLE to false on SFP0..3 // Actively drive TX_DISABLE to false on SFP0..3
io_expander.set_oe(0, 1 << 1).unwrap(); io_expander0.set_oe(&mut i2c, 0, 1 << 1).unwrap();
io_expander.set_oe(1, 1 << 1).unwrap(); io_expander1.set_oe(&mut i2c, 0, 1 << 1).unwrap();
io_expander.set(0, 1, false); io_expander0.set_oe(&mut i2c, 1, 1 << 1).unwrap();
io_expander.set(1, 1, false); io_expander1.set_oe(&mut i2c, 1, 1 << 1).unwrap();
io_expander.service().unwrap(); io_expander0.set(0, 1, false);
} io_expander1.set(0, 1, false);
io_expander0.set(1, 1, false);
io_expander1.set(1, 1, false);
io_expander0.service(&mut i2c).unwrap();
io_expander1.service(&mut i2c).unwrap();
} }
#[cfg(has_si5324)] #[cfg(has_si5324)]