Skip to content

Commit 0c1c9e7

Browse files
bors[bot]olback
andauthored
Merge #279 #283
279: Set ulpi pin speed to VeryHigh r=richardeoin a=olback I don't know if there are any downsides to automatically setting the pin speed to VeryHigh apart from power consumption. 283: Impl ethernet::new r=richardeoin a=olback Co-authored-by: Edwin Svensson <git@olback.net>
3 parents 9469cf4 + d21e1eb + 5f3d7f2 commit 0c1c9e7

File tree

8 files changed

+348
-123
lines changed

8 files changed

+348
-123
lines changed

examples/ethernet-nucleo-h743zi2.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ fn main() -> ! {
8686
let mut link_led = gpiob.pb0.into_push_pull_output(); // LED1, green
8787
link_led.set_high().ok();
8888

89-
let _rmii_ref_clk = gpioa.pa1.into_alternate_af11().set_speed(VeryHigh);
90-
let _rmii_mdio = gpioa.pa2.into_alternate_af11().set_speed(VeryHigh);
91-
let _rmii_mdc = gpioc.pc1.into_alternate_af11().set_speed(VeryHigh);
92-
let _rmii_crs_dv = gpioa.pa7.into_alternate_af11().set_speed(VeryHigh);
93-
let _rmii_rxd0 = gpioc.pc4.into_alternate_af11().set_speed(VeryHigh);
94-
let _rmii_rxd1 = gpioc.pc5.into_alternate_af11().set_speed(VeryHigh);
95-
let _rmii_tx_en = gpiog.pg11.into_alternate_af11().set_speed(VeryHigh);
96-
let _rmii_txd0 = gpiog.pg13.into_alternate_af11().set_speed(VeryHigh);
97-
let _rmii_txd1 = gpiob.pb13.into_alternate_af11().set_speed(VeryHigh);
89+
let rmii_ref_clk = gpioa.pa1.into_alternate_af11();
90+
let rmii_mdio = gpioa.pa2.into_alternate_af11();
91+
let rmii_mdc = gpioc.pc1.into_alternate_af11();
92+
let rmii_crs_dv = gpioa.pa7.into_alternate_af11();
93+
let rmii_rxd0 = gpioc.pc4.into_alternate_af11();
94+
let rmii_rxd1 = gpioc.pc5.into_alternate_af11();
95+
let rmii_tx_en = gpiog.pg11.into_alternate_af11();
96+
let rmii_txd0 = gpiog.pg13.into_alternate_af11();
97+
let rmii_txd1 = gpiob.pb13.into_alternate_af11();
9898

9999
// Initialise ethernet...
100100
assert_eq!(ccdr.clocks.hclk().0, 200_000_000); // HCLK 200MHz
@@ -104,10 +104,21 @@ fn main() -> ! {
104104

105105
let mac_addr = smoltcp::wire::EthernetAddress::from_bytes(&MAC_ADDRESS);
106106
let (_eth_dma, eth_mac) = unsafe {
107-
ethernet::new_unchecked(
107+
ethernet::new(
108108
dp.ETHERNET_MAC,
109109
dp.ETHERNET_MTL,
110110
dp.ETHERNET_DMA,
111+
(
112+
rmii_ref_clk,
113+
rmii_mdio,
114+
rmii_mdc,
115+
rmii_crs_dv,
116+
rmii_rxd0,
117+
rmii_rxd1,
118+
rmii_tx_en,
119+
rmii_txd0,
120+
rmii_txd1,
121+
),
111122
&mut DES_RING,
112123
mac_addr,
113124
ccdr.peripheral.ETH1MAC,

examples/ethernet-rtic-stm32h735g-dk.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use smoltcp::socket::{SocketSet, SocketSetItem};
2727
use smoltcp::time::Instant;
2828
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Cidr};
2929

30-
use gpio::Speed::*;
3130
use stm32h7xx_hal::gpio;
3231
use stm32h7xx_hal::hal::digital::v2::OutputPin;
3332
use stm32h7xx_hal::rcc::CoreClocks;
@@ -150,15 +149,15 @@ const APP: () = {
150149
let mut link_led = gpioc.pc3.into_push_pull_output(); // USR LED1
151150
link_led.set_high().ok();
152151

153-
let _rmii_ref_clk = gpioa.pa1.into_alternate_af11().set_speed(VeryHigh);
154-
let _rmii_mdio = gpioa.pa2.into_alternate_af11().set_speed(VeryHigh);
155-
let _rmii_mdc = gpioc.pc1.into_alternate_af11().set_speed(VeryHigh);
156-
let _rmii_crs_dv = gpioa.pa7.into_alternate_af11().set_speed(VeryHigh);
157-
let _rmii_rxd0 = gpioc.pc4.into_alternate_af11().set_speed(VeryHigh);
158-
let _rmii_rxd1 = gpioc.pc5.into_alternate_af11().set_speed(VeryHigh);
159-
let _rmii_tx_en = gpiob.pb11.into_alternate_af11().set_speed(VeryHigh);
160-
let _rmii_txd0 = gpiob.pb12.into_alternate_af11().set_speed(VeryHigh);
161-
let _rmii_txd1 = gpiob.pb13.into_alternate_af11().set_speed(VeryHigh);
152+
let rmii_ref_clk = gpioa.pa1.into_alternate_af11();
153+
let rmii_mdio = gpioa.pa2.into_alternate_af11();
154+
let rmii_mdc = gpioc.pc1.into_alternate_af11();
155+
let rmii_crs_dv = gpioa.pa7.into_alternate_af11();
156+
let rmii_rxd0 = gpioc.pc4.into_alternate_af11();
157+
let rmii_rxd1 = gpioc.pc5.into_alternate_af11();
158+
let rmii_tx_en = gpiob.pb11.into_alternate_af11();
159+
let rmii_txd0 = gpiob.pb12.into_alternate_af11();
160+
let rmii_txd1 = gpiob.pb13.into_alternate_af11();
162161

163162
// Initialise ethernet...
164163
assert_eq!(ccdr.clocks.hclk().0, 200_000_000); // HCLK 200MHz
@@ -168,10 +167,21 @@ const APP: () = {
168167

169168
let mac_addr = smoltcp::wire::EthernetAddress::from_bytes(&MAC_ADDRESS);
170169
let (eth_dma, eth_mac) = unsafe {
171-
ethernet::new_unchecked(
170+
ethernet::new(
172171
ctx.device.ETHERNET_MAC,
173172
ctx.device.ETHERNET_MTL,
174173
ctx.device.ETHERNET_DMA,
174+
(
175+
rmii_ref_clk,
176+
rmii_mdio,
177+
rmii_mdc,
178+
rmii_crs_dv,
179+
rmii_rxd0,
180+
rmii_rxd1,
181+
rmii_tx_en,
182+
rmii_txd0,
183+
rmii_txd1,
184+
),
175185
&mut DES_RING,
176186
mac_addr.clone(),
177187
ccdr.peripheral.ETH1MAC,

examples/ethernet-rtic-stm32h747i-disco.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use smoltcp::socket::{SocketSet, SocketSetItem};
2727
use smoltcp::time::Instant;
2828
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Cidr};
2929

30-
use gpio::Speed::*;
3130
use stm32h7xx_hal::gpio;
3231
use stm32h7xx_hal::hal::digital::v2::OutputPin;
3332
use stm32h7xx_hal::rcc::CoreClocks;
@@ -153,15 +152,15 @@ const APP: () = {
153152
let mut link_led = gpioi.pi14.into_push_pull_output(); // LED3
154153
link_led.set_high().ok();
155154

156-
let _rmii_ref_clk = gpioa.pa1.into_alternate_af11().set_speed(VeryHigh);
157-
let _rmii_mdio = gpioa.pa2.into_alternate_af11().set_speed(VeryHigh);
158-
let _rmii_mdc = gpioc.pc1.into_alternate_af11().set_speed(VeryHigh);
159-
let _rmii_crs_dv = gpioa.pa7.into_alternate_af11().set_speed(VeryHigh);
160-
let _rmii_rxd0 = gpioc.pc4.into_alternate_af11().set_speed(VeryHigh);
161-
let _rmii_rxd1 = gpioc.pc5.into_alternate_af11().set_speed(VeryHigh);
162-
let _rmii_tx_en = gpiog.pg11.into_alternate_af11().set_speed(VeryHigh);
163-
let _rmii_txd0 = gpiog.pg12.into_alternate_af11().set_speed(VeryHigh);
164-
let _rmii_txd1 = gpiog.pg13.into_alternate_af11().set_speed(VeryHigh);
155+
let rmii_ref_clk = gpioa.pa1.into_alternate_af11();
156+
let rmii_mdio = gpioa.pa2.into_alternate_af11();
157+
let rmii_mdc = gpioc.pc1.into_alternate_af11();
158+
let rmii_crs_dv = gpioa.pa7.into_alternate_af11();
159+
let rmii_rxd0 = gpioc.pc4.into_alternate_af11();
160+
let rmii_rxd1 = gpioc.pc5.into_alternate_af11();
161+
let rmii_tx_en = gpiog.pg11.into_alternate_af11();
162+
let rmii_txd0 = gpiog.pg13.into_alternate_af11();
163+
let rmii_txd1 = gpiog.pg12.into_alternate_af11();
165164

166165
// Initialise ethernet...
167166
assert_eq!(ccdr.clocks.hclk().0, 200_000_000); // HCLK 200MHz
@@ -171,10 +170,21 @@ const APP: () = {
171170

172171
let mac_addr = smoltcp::wire::EthernetAddress::from_bytes(&MAC_ADDRESS);
173172
let (eth_dma, eth_mac) = unsafe {
174-
ethernet::new_unchecked(
173+
ethernet::new(
175174
ctx.device.ETHERNET_MAC,
176175
ctx.device.ETHERNET_MTL,
177176
ctx.device.ETHERNET_DMA,
177+
(
178+
rmii_ref_clk,
179+
rmii_mdio,
180+
rmii_mdc,
181+
rmii_crs_dv,
182+
rmii_rxd0,
183+
rmii_rxd1,
184+
rmii_tx_en,
185+
rmii_txd0,
186+
rmii_txd1,
187+
),
178188
&mut DES_RING,
179189
mac_addr.clone(),
180190
ccdr.peripheral.ETH1MAC,

examples/ethernet-stm32h747i-disco.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod utilities;
1818

1919
use log::info;
2020

21-
use stm32h7xx_hal::gpio::Speed::*;
2221
use stm32h7xx_hal::hal::digital::v2::OutputPin;
2322
use stm32h7xx_hal::{ethernet, ethernet::PHY};
2423
use stm32h7xx_hal::{prelude::*, stm32, stm32::interrupt};
@@ -67,15 +66,15 @@ fn main() -> ! {
6766
let mut link_led = gpioi.pi14.into_push_pull_output(); // LED3
6867
link_led.set_high().ok();
6968

70-
let _rmii_ref_clk = gpioa.pa1.into_alternate_af11().set_speed(VeryHigh);
71-
let _rmii_mdio = gpioa.pa2.into_alternate_af11().set_speed(VeryHigh);
72-
let _rmii_mdc = gpioc.pc1.into_alternate_af11().set_speed(VeryHigh);
73-
let _rmii_crs_dv = gpioa.pa7.into_alternate_af11().set_speed(VeryHigh);
74-
let _rmii_rxd0 = gpioc.pc4.into_alternate_af11().set_speed(VeryHigh);
75-
let _rmii_rxd1 = gpioc.pc5.into_alternate_af11().set_speed(VeryHigh);
76-
let _rmii_tx_en = gpiog.pg11.into_alternate_af11().set_speed(VeryHigh);
77-
let _rmii_txd0 = gpiog.pg13.into_alternate_af11().set_speed(VeryHigh);
78-
let _rmii_txd1 = gpiog.pg12.into_alternate_af11().set_speed(VeryHigh);
69+
let rmii_ref_clk = gpioa.pa1.into_alternate_af11();
70+
let rmii_mdio = gpioa.pa2.into_alternate_af11();
71+
let rmii_mdc = gpioc.pc1.into_alternate_af11();
72+
let rmii_crs_dv = gpioa.pa7.into_alternate_af11();
73+
let rmii_rxd0 = gpioc.pc4.into_alternate_af11();
74+
let rmii_rxd1 = gpioc.pc5.into_alternate_af11();
75+
let rmii_tx_en = gpiog.pg11.into_alternate_af11();
76+
let rmii_txd0 = gpiog.pg13.into_alternate_af11();
77+
let rmii_txd1 = gpiog.pg12.into_alternate_af11();
7978

8079
// Initialise ethernet...
8180
assert_eq!(ccdr.clocks.hclk().0, 200_000_000); // HCLK 200MHz
@@ -85,10 +84,21 @@ fn main() -> ! {
8584

8685
let mac_addr = smoltcp::wire::EthernetAddress::from_bytes(&MAC_ADDRESS);
8786
let (_eth_dma, eth_mac) = unsafe {
88-
ethernet::new_unchecked(
87+
ethernet::new(
8988
dp.ETHERNET_MAC,
9089
dp.ETHERNET_MTL,
9190
dp.ETHERNET_DMA,
91+
(
92+
rmii_ref_clk,
93+
rmii_mdio,
94+
rmii_mdc,
95+
rmii_crs_dv,
96+
rmii_rxd0,
97+
rmii_rxd1,
98+
rmii_tx_en,
99+
rmii_txd0,
100+
rmii_txd1,
101+
),
92102
&mut DES_RING,
93103
mac_addr.clone(),
94104
ccdr.peripheral.ETH1MAC,

src/ethernet/eth.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use smoltcp::{
3434
wire::EthernetAddress,
3535
};
3636

37-
use crate::ethernet::StationManagement;
37+
use crate::{
38+
ethernet::{PinsRMII, StationManagement},
39+
gpio::Speed,
40+
};
3841

3942
// 6 DMAC, 6 SMAC, 4 q tag, 2 ethernet type II, 1500 ip MTU, 4 CRC, 2
4043
// padding
@@ -378,6 +381,40 @@ pub struct EthernetMAC {
378381
clock_range: u8,
379382
}
380383

384+
/// Create and initialise the ethernet driver.
385+
///
386+
/// You must move in ETH_MAC, ETH_MTL, ETH_DMA.
387+
///
388+
/// Sets up the descriptor structures, sets up the peripheral
389+
/// clocks and GPIO configuration, and configures the ETH MAC and
390+
/// DMA peripherals. Automatically sets slew rate to VeryHigh.
391+
/// If you wish to use another configuration, please see
392+
/// [new_unchecked](new_unchecked).
393+
///
394+
/// This method does not initialise the external PHY. However it does return an
395+
/// [EthernetMAC](EthernetMAC) which implements the
396+
/// [StationManagement](super::StationManagement) trait. This can be used to
397+
/// communicate with the external PHY.
398+
///
399+
/// # Safety
400+
///
401+
/// `EthernetDMA` shall not be moved as it is initialised here
402+
pub fn new<'a, const TD: usize, const RD: usize>(
403+
eth_mac: stm32::ETHERNET_MAC,
404+
eth_mtl: stm32::ETHERNET_MTL,
405+
eth_dma: stm32::ETHERNET_DMA,
406+
pins: impl PinsRMII,
407+
ring: &'a mut DesRing<TD, RD>,
408+
mac_addr: EthernetAddress,
409+
prec: rec::Eth1Mac,
410+
clocks: &CoreClocks,
411+
) -> (EthernetDMA<'a, TD, RD>, EthernetMAC) {
412+
pins.set_speed(Speed::VeryHigh);
413+
unsafe {
414+
new_unchecked(eth_mac, eth_mtl, eth_dma, ring, mac_addr, prec, clocks)
415+
}
416+
}
417+
381418
/// Create and initialise the ethernet driver.
382419
///
383420
/// You must move in ETH_MAC, ETH_MTL, ETH_DMA.

0 commit comments

Comments
 (0)