From 4bc936f071341840900f1445dcd19cba18747189 Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Wed, 15 Feb 2023 14:37:55 +0800 Subject: [PATCH 1/4] Copy io expander from kasli Signed-off-by: Egor Savkin --- src/runtime/src/io_expander.rs | 165 +++++++++++++++++++++++++++++++++ src/runtime/src/main.rs | 1 + 2 files changed, 166 insertions(+) create mode 100644 src/runtime/src/io_expander.rs diff --git a/src/runtime/src/io_expander.rs b/src/runtime/src/io_expander.rs new file mode 100644 index 0000000..eecb0fa --- /dev/null +++ b/src/runtime/src/io_expander.rs @@ -0,0 +1,165 @@ +use crate::pl::csr; +use crate::i2c; + +// Only the bare minimum registers. Bits/IO connections equivalent between IC types. +struct Registers { + // PCA9539 equivalent register names in comments + iodira: u8, // Configuration Port 0 + iodirb: u8, // Configuration Port 1 + gpioa: u8, // Output Port 0 + gpiob: u8, // Output Port 1 +} + +pub struct IoExpander { + busno: u8, + port: u8, + address: u8, + virtual_led_mapping: &'static [(u8, u8, u8)], + iodir: [u8; 2], + out_current: [u8; 2], + out_target: [u8; 2], + registers: Registers, +} + +impl IoExpander { + #[cfg(all(soc_platform = "kasli", hw_rev = "v2.0"))] + pub fn new(index: u8) -> Result { + const VIRTUAL_LED_MAPPING0: [(u8, u8, u8); 2] = [(0, 0, 6), (1, 1, 6)]; + const VIRTUAL_LED_MAPPING1: [(u8, u8, u8); 2] = [(2, 0, 6), (3, 1, 6)]; + + // Both expanders on SHARED I2C bus + let mut io_expander = match index { + 0 => IoExpander { + busno: 0, + port: 11, + address: 0x40, + virtual_led_mapping: &VIRTUAL_LED_MAPPING0, + iodir: [0xff; 2], + out_current: [0; 2], + out_target: [0; 2], + registers: Registers { + iodira: 0x00, + iodirb: 0x01, + gpioa: 0x12, + gpiob: 0x13, + }, + }, + 1 => IoExpander { + busno: 0, + port: 11, + address: 0x42, + virtual_led_mapping: &VIRTUAL_LED_MAPPING1, + iodir: [0xff; 2], + out_current: [0; 2], + out_target: [0; 2], + registers: Registers { + iodira: 0x00, + iodirb: 0x01, + gpioa: 0x12, + gpiob: 0x13, + }, + }, + _ => return Err("incorrect I/O expander index"), + }; + if !io_expander.check_ack()? { + #[cfg(feature = "log")] + log::info!( + "MCP23017 io expander {} not found. Checking for PCA9539.", + index + ); + io_expander.address += 0xa8; // translate to PCA9539 addresses (see schematic) + io_expander.registers = Registers { + iodira: 0x06, + iodirb: 0x07, + gpioa: 0x02, + gpiob: 0x03, + }; + if !io_expander.check_ack()? { + return Err("Neither MCP23017 nor PCA9539 io expander found."); + }; + } + Ok(io_expander) + } + + #[cfg(soc_platform = "kasli")] + fn select(&self) -> Result<(), &'static str> { + let mask: u16 = 1 << self.port; + i2c::switch_select(self.busno, 0x70, mask as u8)?; + i2c::switch_select(self.busno, 0x71, (mask >> 8) as u8)?; + Ok(()) + } + + fn write(&self, addr: u8, value: u8) -> Result<(), &'static str> { + i2c::start(self.busno)?; + i2c::write(self.busno, self.address)?; + i2c::write(self.busno, addr)?; + i2c::write(self.busno, value)?; + i2c::stop(self.busno)?; + Ok(()) + } + + fn check_ack(&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)?; + Ok(ack) + } + + fn update_iodir(&self) -> Result<(), &'static str> { + self.write(self.registers.iodira, self.iodir[0])?; + self.write(self.registers.iodirb, self.iodir[1])?; + Ok(()) + } + + pub fn init(&mut self) -> Result<(), &'static str> { + self.select()?; + + for (_led, port, bit) in self.virtual_led_mapping.iter() { + self.iodir[*port as usize] &= !(1 << *bit); + } + self.update_iodir()?; + + self.out_current[0] = 0x00; + self.write(self.registers.gpioa, 0x00)?; + self.out_current[1] = 0x00; + self.write(self.registers.gpiob, 0x00)?; + Ok(()) + } + + pub fn set_oe(&mut self, port: u8, outputs: u8) -> Result<(), &'static str> { + self.iodir[port as usize] &= !outputs; + self.update_iodir()?; + Ok(()) + } + + pub fn set(&mut self, port: u8, bit: u8, high: bool) { + if high { + self.out_target[port as usize] |= 1 << bit; + } else { + self.out_target[port as usize] &= !(1 << bit); + } + } + + pub fn service(&mut self) -> Result<(), &'static str> { + for (led, port, bit) in self.virtual_led_mapping.iter() { + let level = unsafe { (csr::virtual_leds::status_read() >> led) & 1 }; + self.set(*port, *bit, level != 0); + } + + if self.out_target != self.out_current { + self.select()?; + if self.out_target[0] != self.out_current[0] { + self.write(self.registers.gpioa, self.out_target[0])?; + self.out_current[0] = self.out_target[0]; + } + if self.out_target[1] != self.out_current[1] { + self.write(self.registers.gpiob, self.out_target[1])?; + self.out_current[1] = self.out_target[1]; + } + } + + Ok(()) + } +} diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index 9b707da..fce660e 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -46,6 +46,7 @@ mod mgmt; mod analyzer; mod irq; mod i2c; +mod io_expander; static mut SEEN_ASYNC_ERRORS: u8 = 0; From 8230a01701d7885f241c42cdcbe448b5e5d34a6c Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Wed, 15 Feb 2023 15:31:22 +0800 Subject: [PATCH 2/4] Build io_expander Signed-off-by: Egor Savkin --- src/runtime/src/io_expander.rs | 39 +++++++++++++++++++--------------- src/runtime/src/main.rs | 1 + 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/runtime/src/io_expander.rs b/src/runtime/src/io_expander.rs index eecb0fa..e83f763 100644 --- a/src/runtime/src/io_expander.rs +++ b/src/runtime/src/io_expander.rs @@ -11,9 +11,9 @@ struct Registers { } pub struct IoExpander { - busno: u8, + busno: i32, port: u8, - address: u8, + address: i32, virtual_led_mapping: &'static [(u8, u8, u8)], iodir: [u8; 2], out_current: [u8; 2], @@ -22,7 +22,7 @@ pub struct IoExpander { } impl IoExpander { - #[cfg(all(soc_platform = "kasli", hw_rev = "v2.0"))] + #[cfg(feature = "target_kasli_soc")] pub fn new(index: u8) -> Result { const VIRTUAL_LED_MAPPING0: [(u8, u8, u8); 2] = [(0, 0, 6), (1, 1, 6)]; const VIRTUAL_LED_MAPPING1: [(u8, u8, u8); 2] = [(2, 0, 6), (3, 1, 6)]; @@ -81,29 +81,34 @@ impl IoExpander { Ok(io_expander) } - #[cfg(soc_platform = "kasli")] + #[cfg(feature = "target_kasli_soc")] fn select(&self) -> Result<(), &'static str> { - let mask: u16 = 1 << self.port; - i2c::switch_select(self.busno, 0x70, mask as u8)?; - i2c::switch_select(self.busno, 0x71, (mask >> 8) as u8)?; + let mask: i32 = 1 << self.port; + i2c::switch_select(self.busno, 0x70, mask); + i2c::switch_select(self.busno, 0x71, (mask >> 8)); Ok(()) } + #[cfg(not(feature = "target_kasli_soc"))] + fn select(&self) -> Result<(), &'static str> { + Err("the target is not supported yet") + } + fn write(&self, addr: u8, value: u8) -> Result<(), &'static str> { - i2c::start(self.busno)?; - i2c::write(self.busno, self.address)?; - i2c::write(self.busno, addr)?; - i2c::write(self.busno, value)?; - i2c::stop(self.busno)?; + 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); Ok(()) } fn check_ack(&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)?; + i2c::start(self.busno); + let ack = i2c::write(self.busno, self.address); + i2c::stop(self.busno); Ok(ack) } @@ -144,8 +149,8 @@ impl IoExpander { pub fn service(&mut self) -> Result<(), &'static str> { for (led, port, bit) in self.virtual_led_mapping.iter() { - let level = unsafe { (csr::virtual_leds::status_read() >> led) & 1 }; - self.set(*port, *bit, level != 0); + //let level = unsafe { (csr::virtual_leds::status_read() >> led) & 1 }; + self.set(*port, *bit, true); } if self.out_target != self.out_current { diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index fce660e..5ee7e5d 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -46,6 +46,7 @@ mod mgmt; mod analyzer; mod irq; mod i2c; +#[cfg(feature = "target_kasli_soc")] mod io_expander; static mut SEEN_ASYNC_ERRORS: u8 = 0; From b4b7912c40afdf06fcbff75d704ce48d546d4347 Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Wed, 15 Feb 2023 17:44:01 +0800 Subject: [PATCH 3/4] Port tx_disable-related code from Kasli Signed-off-by: Egor Savkin --- src/runtime/src/comms.rs | 8 +++++++- src/runtime/src/io_expander.rs | 34 +++++++++++++++++++--------------- src/runtime/src/main.rs | 22 ++++++++++++++++++++-- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index e14e7c6..2fa3c8e 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -32,6 +32,7 @@ use crate::moninj; use crate::mgmt; use crate::analyzer; use crate::rtio_mgt::{self, resolve_channel_name}; +use crate::io_expander; #[cfg(has_drtio)] use crate::pl; @@ -380,7 +381,7 @@ async fn handle_connection(stream: &mut TcpStream, control: Rc Result { + Ok(IoExpander {}) + } +} + +#[cfg(feature = "target_kasli_soc")] impl IoExpander { - #[cfg(feature = "target_kasli_soc")] pub fn new(index: u8) -> Result { const VIRTUAL_LED_MAPPING0: [(u8, u8, u8); 2] = [(0, 0, 6), (1, 1, 6)]; const VIRTUAL_LED_MAPPING1: [(u8, u8, u8); 2] = [(2, 0, 6), (3, 1, 6)]; @@ -62,8 +74,7 @@ impl IoExpander { _ => return Err("incorrect I/O expander index"), }; if !io_expander.check_ack()? { - #[cfg(feature = "log")] - log::info!( + info!( "MCP23017 io expander {} not found. Checking for PCA9539.", index ); @@ -81,19 +92,13 @@ impl IoExpander { Ok(io_expander) } - #[cfg(feature = "target_kasli_soc")] fn select(&self) -> Result<(), &'static str> { - let mask: i32 = 1 << self.port; - i2c::switch_select(self.busno, 0x70, mask); - i2c::switch_select(self.busno, 0x71, (mask >> 8)); + 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); Ok(()) } - #[cfg(not(feature = "target_kasli_soc"))] - fn select(&self) -> Result<(), &'static str> { - Err("the target is not supported yet") - } - fn write(&self, addr: u8, value: u8) -> Result<(), &'static str> { i2c::start(self.busno); i2c::write(self.busno, self.address as i32); @@ -148,8 +153,7 @@ impl IoExpander { } pub fn service(&mut self) -> Result<(), &'static str> { - for (led, port, bit) in self.virtual_led_mapping.iter() { - //let level = unsafe { (csr::virtual_leds::status_read() >> led) & 1 }; + for (_led, port, bit) in self.virtual_led_mapping.iter() { self.set(*port, *bit, true); } diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index 5ee7e5d..09e272e 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -46,7 +46,6 @@ mod mgmt; mod analyzer; mod irq; mod i2c; -#[cfg(feature = "target_kasli_soc")] mod io_expander; static mut SEEN_ASYNC_ERRORS: u8 = 0; @@ -116,6 +115,25 @@ pub fn main_core0() { i2c::init(); + let (mut io_expander0, mut io_expander1) = (io_expander::IoExpander::new(0).unwrap(), io_expander::IoExpander::new(1).unwrap()); + #[cfg(feature = "target_kasli_soc")] + { + 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 cfg = match Config::new() { Ok(cfg) => cfg, Err(err) => { @@ -128,5 +146,5 @@ pub fn main_core0() { task::spawn(report_async_rtio_errors()); - comms::main(timer, cfg); + comms::main(timer, cfg, io_expander0, io_expander1); } From ca6e0d13ad7ab3649455c20cbfbb42a53565471b Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Wed, 15 Feb 2023 18:14:05 +0800 Subject: [PATCH 4/4] Remove virtual LEDs from io_expander Signed-off-by: Egor Savkin --- src/runtime/src/comms.rs | 8 +------- src/runtime/src/io_expander.rs | 25 ------------------------- src/runtime/src/main.rs | 5 +++-- 3 files changed, 4 insertions(+), 34 deletions(-) diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index 2fa3c8e..e14e7c6 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -32,7 +32,6 @@ use crate::moninj; use crate::mgmt; use crate::analyzer; use crate::rtio_mgt::{self, resolve_channel_name}; -use crate::io_expander; #[cfg(has_drtio)] use crate::pl; @@ -381,7 +380,7 @@ async fn handle_connection(stream: &mut TcpStream, control: Rc Result { - Ok(IoExpander {}) - } -} - -#[cfg(feature = "target_kasli_soc")] -impl IoExpander { - pub fn new(index: u8) -> Result { - const VIRTUAL_LED_MAPPING0: [(u8, u8, u8); 2] = [(0, 0, 6), (1, 1, 6)]; - const VIRTUAL_LED_MAPPING1: [(u8, u8, u8); 2] = [(2, 0, 6), (3, 1, 6)]; // Both expanders on SHARED I2C bus let mut io_expander = match index { @@ -45,7 +30,6 @@ impl IoExpander { busno: 0, port: 11, address: 0x40, - virtual_led_mapping: &VIRTUAL_LED_MAPPING0, iodir: [0xff; 2], out_current: [0; 2], out_target: [0; 2], @@ -60,7 +44,6 @@ impl IoExpander { busno: 0, port: 11, address: 0x42, - virtual_led_mapping: &VIRTUAL_LED_MAPPING1, iodir: [0xff; 2], out_current: [0; 2], out_target: [0; 2], @@ -125,10 +108,6 @@ impl IoExpander { pub fn init(&mut self) -> Result<(), &'static str> { self.select()?; - - for (_led, port, bit) in self.virtual_led_mapping.iter() { - self.iodir[*port as usize] &= !(1 << *bit); - } self.update_iodir()?; self.out_current[0] = 0x00; @@ -153,10 +132,6 @@ impl IoExpander { } pub fn service(&mut self) -> Result<(), &'static str> { - for (_led, port, bit) in self.virtual_led_mapping.iter() { - self.set(*port, *bit, true); - } - if self.out_target != self.out_current { self.select()?; if self.out_target[0] != self.out_current[0] { diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index 09e272e..a925be9 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -46,6 +46,7 @@ mod mgmt; mod analyzer; mod irq; mod i2c; +#[cfg(feature = "target_kasli_soc")] mod io_expander; static mut SEEN_ASYNC_ERRORS: u8 = 0; @@ -115,9 +116,9 @@ pub fn main_core0() { i2c::init(); - let (mut io_expander0, mut io_expander1) = (io_expander::IoExpander::new(0).unwrap(), io_expander::IoExpander::new(1).unwrap()); #[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"); @@ -146,5 +147,5 @@ pub fn main_core0() { task::spawn(report_async_rtio_errors()); - comms::main(timer, cfg, io_expander0, io_expander1); + comms::main(timer, cfg); }