aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lenfesty <[email protected]>2022-04-25 19:57:09 -0600
committerDario Nieuwenhuis <[email protected]>2022-04-30 04:49:24 +0200
commit2e7b42fc5b62f134bc1c477e4817e0c512c399b3 (patch)
tree2243583283e74d58a3aac3e4c1128aa9cecf925e
parent905b40e212e794895824906cffaf9df9b9900dd3 (diff)
embassy-stm32/eth: convert LAN8742 driver to generic SMI driver
SMI Ethernet PHYs all share a common base set of registers that can do 90% of all tasks. The LAN8742 driver used some vendor-specific registers to check link negotiation status, but the need for that was debatable, so I migrated it to a generic driver instead, anybody who wants extra functionality can copy it and impl their own on top of it.
-rw-r--r--embassy-stm32/src/eth/generic_smi.rs (renamed from embassy-stm32/src/eth/lan8742a.rs)29
-rw-r--r--embassy-stm32/src/eth/mod.rs2
-rw-r--r--examples/stm32f7/src/bin/eth.rs8
-rw-r--r--examples/stm32h7/src/bin/eth.rs8
4 files changed, 15 insertions, 32 deletions
diff --git a/embassy-stm32/src/eth/lan8742a.rs b/embassy-stm32/src/eth/generic_smi.rs
index 74d0ca5de..5a323bf5a 100644
--- a/embassy-stm32/src/eth/lan8742a.rs
+++ b/embassy-stm32/src/eth/generic_smi.rs
@@ -1,4 +1,4 @@
1//! SMSC LAN8742A Ethernet PHY 1//! Generic SMI Ethernet PHY
2 2
3use super::{StationManagement, PHY}; 3use super::{StationManagement, PHY};
4 4
@@ -13,7 +13,6 @@ mod phy_consts {
13 pub const PHY_REG_ANEXP: u8 = 0x06; 13 pub const PHY_REG_ANEXP: u8 = 0x06;
14 pub const PHY_REG_ANNPTX: u8 = 0x07; 14 pub const PHY_REG_ANNPTX: u8 = 0x07;
15 pub const PHY_REG_ANNPRX: u8 = 0x08; 15 pub const PHY_REG_ANNPRX: u8 = 0x08;
16 pub const PHY_REG_SSR: u8 = 0x1F; // Special Status Register
17 pub const PHY_REG_CTL: u8 = 0x0D; // Ethernet PHY Register Control 16 pub const PHY_REG_CTL: u8 = 0x0D; // Ethernet PHY Register Control
18 pub const PHY_REG_ADDAR: u8 = 0x0E; // Ethernet PHY Address or Data 17 pub const PHY_REG_ADDAR: u8 = 0x0E; // Ethernet PHY Address or Data
19 18
@@ -33,20 +32,13 @@ mod phy_consts {
33 pub const PHY_REG_BSR_UP: u16 = 1 << 2; 32 pub const PHY_REG_BSR_UP: u16 = 1 << 2;
34 pub const PHY_REG_BSR_FAULT: u16 = 1 << 4; 33 pub const PHY_REG_BSR_FAULT: u16 = 1 << 4;
35 pub const PHY_REG_BSR_ANDONE: u16 = 1 << 5; 34 pub const PHY_REG_BSR_ANDONE: u16 = 1 << 5;
36
37 pub const PHY_REG_SSR_ANDONE: u16 = 1 << 12;
38 pub const PHY_REG_SSR_SPEED: u16 = 0b111 << 2;
39 pub const PHY_REG_SSR_10BASE_HD: u16 = 0b001 << 2;
40 pub const PHY_REG_SSR_10BASE_FD: u16 = 0b101 << 2;
41 pub const PHY_REG_SSR_100BASE_HD: u16 = 0b010 << 2;
42 pub const PHY_REG_SSR_100BASE_FD: u16 = 0b110 << 2;
43} 35}
44use self::phy_consts::*; 36use self::phy_consts::*;
45 37
46/// SMSC LAN8742A Ethernet PHY 38/// Generic SMI Ethernet PHY
47pub struct LAN8742A; 39pub struct GenericSMI;
48 40
49unsafe impl PHY for LAN8742A { 41unsafe impl PHY for GenericSMI {
50 /// Reset PHY and wait for it to come out of reset. 42 /// Reset PHY and wait for it to come out of reset.
51 fn phy_reset<S: StationManagement>(sm: &mut S) { 43 fn phy_reset<S: StationManagement>(sm: &mut S) {
52 sm.smi_write(PHY_REG_BCR, PHY_REG_BCR_RESET); 44 sm.smi_write(PHY_REG_BCR, PHY_REG_BCR_RESET);
@@ -67,7 +59,6 @@ unsafe impl PHY for LAN8742A {
67 59
68 fn poll_link<S: StationManagement>(sm: &mut S) -> bool { 60 fn poll_link<S: StationManagement>(sm: &mut S) -> bool {
69 let bsr = sm.smi_read(PHY_REG_BSR); 61 let bsr = sm.smi_read(PHY_REG_BSR);
70 let ssr = sm.smi_read(PHY_REG_SSR);
71 62
72 // No link without autonegotiate 63 // No link without autonegotiate
73 if bsr & PHY_REG_BSR_ANDONE == 0 { 64 if bsr & PHY_REG_BSR_ANDONE == 0 {
@@ -77,22 +68,14 @@ unsafe impl PHY for LAN8742A {
77 if bsr & PHY_REG_BSR_UP == 0 { 68 if bsr & PHY_REG_BSR_UP == 0 {
78 return false; 69 return false;
79 } 70 }
80 // No link if autonegotiate incomplete
81 if ssr & PHY_REG_SSR_ANDONE == 0 {
82 return false;
83 }
84 // No link if other side isn't 100Mbps full duplex
85 if ssr & PHY_REG_SSR_SPEED != PHY_REG_SSR_100BASE_FD {
86 return false;
87 }
88 71
89 // Got link 72 // Got link
90 true 73 true
91 } 74 }
92} 75}
93 76
94/// Public functions for the LAN8742A 77/// Public functions for the PHY
95impl LAN8742A { 78impl GenericSMI {
96 // Writes a value to an extended PHY register in MMD address space 79 // Writes a value to an extended PHY register in MMD address space
97 fn smi_write_ext<S: StationManagement>(sm: &mut S, reg_addr: u16, reg_data: u16) { 80 fn smi_write_ext<S: StationManagement>(sm: &mut S, reg_addr: u16, reg_data: u16) {
98 sm.smi_write(PHY_REG_CTL, 0x0003); // set address 81 sm.smi_write(PHY_REG_CTL, 0x0003); // set address
diff --git a/embassy-stm32/src/eth/mod.rs b/embassy-stm32/src/eth/mod.rs
index ef5424e59..1c0d13fea 100644
--- a/embassy-stm32/src/eth/mod.rs
+++ b/embassy-stm32/src/eth/mod.rs
@@ -5,7 +5,7 @@
5#[cfg_attr(eth_v2, path = "v2/mod.rs")] 5#[cfg_attr(eth_v2, path = "v2/mod.rs")]
6#[cfg_attr(eth_v1, path = "v1.rs")] 6#[cfg_attr(eth_v1, path = "v1.rs")]
7mod _version; 7mod _version;
8pub mod lan8742a; 8pub mod generic_smi;
9 9
10pub use _version::*; 10pub use _version::*;
11 11
diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs
index 446756c29..33e41de9c 100644
--- a/examples/stm32f7/src/bin/eth.rs
+++ b/examples/stm32f7/src/bin/eth.rs
@@ -11,7 +11,7 @@ use embassy::util::Forever;
11use embassy_net::{ 11use embassy_net::{
12 Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket, 12 Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket,
13}; 13};
14use embassy_stm32::eth::lan8742a::LAN8742A; 14use embassy_stm32::eth::generic_smi::GenericSMI;
15use embassy_stm32::eth::{Ethernet, State}; 15use embassy_stm32::eth::{Ethernet, State};
16use embassy_stm32::interrupt; 16use embassy_stm32::interrupt;
17use embassy_stm32::peripherals::ETH; 17use embassy_stm32::peripherals::ETH;
@@ -26,7 +26,7 @@ use panic_probe as _;
26 26
27#[embassy::task] 27#[embassy::task]
28async fn main_task( 28async fn main_task(
29 device: &'static mut Ethernet<'static, ETH, LAN8742A, 4, 4>, 29 device: &'static mut Ethernet<'static, ETH, GenericSMI, 4, 4>,
30 config: &'static mut StaticConfigurator, 30 config: &'static mut StaticConfigurator,
31 spawner: Spawner, 31 spawner: Spawner,
32) { 32) {
@@ -82,7 +82,7 @@ static mut RNG_INST: Option<Rng<RNG>> = None;
82 82
83static EXECUTOR: Forever<Executor> = Forever::new(); 83static EXECUTOR: Forever<Executor> = Forever::new();
84static STATE: Forever<State<'static, ETH, 4, 4>> = Forever::new(); 84static STATE: Forever<State<'static, ETH, 4, 4>> = Forever::new();
85static ETH: Forever<Ethernet<'static, ETH, LAN8742A, 4, 4>> = Forever::new(); 85static ETH: Forever<Ethernet<'static, ETH, GenericSMI, 4, 4>> = Forever::new();
86static CONFIG: Forever<StaticConfigurator> = Forever::new(); 86static CONFIG: Forever<StaticConfigurator> = Forever::new();
87static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new(); 87static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
88 88
@@ -112,7 +112,7 @@ fn main() -> ! {
112 let eth = unsafe { 112 let eth = unsafe {
113 ETH.put(Ethernet::new( 113 ETH.put(Ethernet::new(
114 state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13, 114 state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13,
115 p.PG11, LAN8742A, mac_addr, 0, 115 p.PG11, GenericSMI, mac_addr, 0,
116 )) 116 ))
117 }; 117 };
118 118
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs
index 4eb5421a8..9a2e7a33d 100644
--- a/examples/stm32h7/src/bin/eth.rs
+++ b/examples/stm32h7/src/bin/eth.rs
@@ -14,7 +14,7 @@ use embassy::util::Forever;
14use embassy_net::{ 14use embassy_net::{
15 Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket, 15 Config as NetConfig, Ipv4Address, Ipv4Cidr, StackResources, StaticConfigurator, TcpSocket,
16}; 16};
17use embassy_stm32::eth::lan8742a::LAN8742A; 17use embassy_stm32::eth::generic_smi::GenericSMI;
18use embassy_stm32::eth::{Ethernet, State}; 18use embassy_stm32::eth::{Ethernet, State};
19use embassy_stm32::interrupt; 19use embassy_stm32::interrupt;
20use embassy_stm32::peripherals::ETH; 20use embassy_stm32::peripherals::ETH;
@@ -26,7 +26,7 @@ use heapless::Vec;
26 26
27#[embassy::task] 27#[embassy::task]
28async fn main_task( 28async fn main_task(
29 device: &'static mut Ethernet<'static, ETH, LAN8742A, 4, 4>, 29 device: &'static mut Ethernet<'static, ETH, GenericSMI, 4, 4>,
30 config: &'static mut StaticConfigurator, 30 config: &'static mut StaticConfigurator,
31 spawner: Spawner, 31 spawner: Spawner,
32) { 32) {
@@ -82,7 +82,7 @@ static mut RNG_INST: Option<Rng<RNG>> = None;
82 82
83static EXECUTOR: Forever<Executor> = Forever::new(); 83static EXECUTOR: Forever<Executor> = Forever::new();
84static STATE: Forever<State<'static, ETH, 4, 4>> = Forever::new(); 84static STATE: Forever<State<'static, ETH, 4, 4>> = Forever::new();
85static ETH: Forever<Ethernet<'static, ETH, LAN8742A, 4, 4>> = Forever::new(); 85static ETH: Forever<Ethernet<'static, ETH, GenericSMI, 4, 4>> = Forever::new();
86static CONFIG: Forever<StaticConfigurator> = Forever::new(); 86static CONFIG: Forever<StaticConfigurator> = Forever::new();
87static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new(); 87static NET_RESOURCES: Forever<StackResources<1, 2, 8>> = Forever::new();
88 88
@@ -114,7 +114,7 @@ fn main() -> ! {
114 let eth = unsafe { 114 let eth = unsafe {
115 ETH.put(Ethernet::new( 115 ETH.put(Ethernet::new(
116 state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13, 116 state, p.ETH, eth_int, p.PA1, p.PA2, p.PC1, p.PA7, p.PC4, p.PC5, p.PG13, p.PB13,
117 p.PG11, LAN8742A, mac_addr, 0, 117 p.PG11, GenericSMI, mac_addr, 0,
118 )) 118 ))
119 }; 119 };
120 120