From 253b28debaf13d839087a2b9160f416f4be22438 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 12 Aug 2023 03:07:13 +0200 Subject: [PATCH] net-enc28j60: fix PHY read unreliable due to missing dummy byte. --- embassy-net-enc28j60/src/lib.rs | 19 ++++++++++++------- embassy-net-enc28j60/src/phy.rs | 3 +-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/embassy-net-enc28j60/src/lib.rs b/embassy-net-enc28j60/src/lib.rs index 4f129b6b2..d77dc2c5e 100644 --- a/embassy-net-enc28j60/src/lib.rs +++ b/embassy-net-enc28j60/src/lib.rs @@ -361,10 +361,16 @@ where fn _read_control_register(&mut self, register: Register) -> u8 { self.change_bank(register); - let mut buffer = [Instruction::RCR.opcode() | register.addr(), 0]; - self.spi.transfer_in_place(&mut buffer).unwrap(); - - buffer[1] + if register.is_eth_register() { + let mut buffer = [Instruction::RCR.opcode() | register.addr(), 0]; + self.spi.transfer_in_place(&mut buffer).unwrap(); + buffer[1] + } else { + // MAC, MII regs need a dummy byte. + let mut buffer = [Instruction::RCR.opcode() | register.addr(), 0, 0]; + self.spi.transfer_in_place(&mut buffer).unwrap(); + buffer[2] + } } fn read_phy_register(&mut self, register: phy::Register) -> u16 { @@ -379,11 +385,10 @@ where // wait until the read operation finishes while self.read_control_register(bank3::Register::MISTAT) & 0b1 != 0 {} - let h = self.read_control_register(bank2::Register::MIRDH); - let l = self.read_control_register(bank2::Register::MIRDL); - self.write_control_register(bank2::Register::MICMD, bank2::MICMD::default().miird(0).bits()); + let l = self.read_control_register(bank2::Register::MIRDL); + let h = self.read_control_register(bank2::Register::MIRDH); (l as u16) | (h as u16) << 8 } diff --git a/embassy-net-enc28j60/src/phy.rs b/embassy-net-enc28j60/src/phy.rs index 7f62b5f27..89144ada3 100644 --- a/embassy-net-enc28j60/src/phy.rs +++ b/embassy-net-enc28j60/src/phy.rs @@ -30,7 +30,6 @@ register!(PHCON2, 0, u16, { }); register!(PHSTAT2, 0, u16, { - // Datasheet says it's bit 10, but it's actually bit 2 ?!?! #[doc = "Link Status bit"] - lstat @ 2, + lstat @ 10, });