Skip to content

Commit 922c6a1

Browse files
authored
Merge pull request #15 from thalesfragoso/pins
Better family support and easier configuration
2 parents 54dd611 + b61a6ad commit 922c6a1

File tree

13 files changed

+385
-186
lines changed

13 files changed

+385
-186
lines changed

.travis.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ language: rust
22
matrix:
33
include:
44
- rust: nightly
5-
env: FEATURES='stm32f4xx-hal/stm32f407'
5+
env: FEATURES='stm32f407'
66
- rust: nightly
7-
env: FEATURES='stm32f4xx-hal/stm32f417'
7+
env: FEATURES='stm32f417'
88
- rust: nightly
9-
env: FEATURES='stm32f4xx-hal/stm32f427'
9+
env: FEATURES='stm32f427'
1010
- rust: nightly
11-
env: FEATURES='stm32f4xx-hal/stm32f429'
11+
env: FEATURES='stm32f429'
1212
- rust: nightly
13-
env: FEATURES='stm32f4xx-hal/stm32f437'
13+
env: FEATURES='stm32f437'
1414
- rust: nightly
15-
env: FEATURES='stm32f4xx-hal/stm32f439'
15+
env: FEATURES='stm32f439'
1616
- rust: nightly
17-
env: FEATURES='stm32f4xx-hal/stm32f469'
17+
env: FEATURES='stm32f469'
1818
- rust: nightly
19-
env: FEATURES='stm32f4xx-hal/stm32f479'
19+
env: FEATURES='stm32f479'
2020
- rust: nightly
21-
env: FEATURES='stm32f4xx-hal/rt nucleo-f429zi' ARGS="--release --target=thumbv7em-none-eabihf --example=pktgen"
21+
env: FEATURES='stm32f429' ARGS="--release --target=thumbv7em-none-eabihf --example=pktgen"
2222
- rust: nightly
23-
env: FEATURES='stm32f4xx-hal/rt nucleo-f429zi smoltcp-phy' ARGS="--release --target=thumbv7em-none-eabihf --example=ip"
23+
env: FEATURES='stm32f429 smoltcp-phy smoltcp-log smoltcp-verbose' ARGS="--release --target=thumbv7em-none-eabihf --example=ip"
2424
script:
2525
- "cargo build --target=`uname -m`-unknown-linux-gnu --no-default-features --features \"$FEATURES\""

Cargo.toml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "stm32-eth"
44
description = "Embedded Rust Ethernet driver for the STM32 MCU series"
55
license = "Apache-2.0"
66
authors = ["Astro <[email protected]>"]
7-
version = "0.1.2"
7+
version = "0.2.0"
88
keywords = ["ethernet", "eth", "stm32", "stm32f4"]
99
repository = "https://github.com/stm32-rs/stm32-eth"
1010
documentation = "https://docs.rs/stm32-eth/"
@@ -15,32 +15,44 @@ travis-ci = { repository = "astro/stm32-eth", branch = "master" }
1515
maintenance = { status = "experimental" }
1616

1717
[package.metadata.docs.rs]
18-
features = [ "smoltcp-phy", "nucleo-f429zi" ]
18+
features = [ "smoltcp-phy", "stm32f429" ]
1919

2020
[dependencies]
2121
volatile-register = "0.2"
2222
aligned = "0.3"
23-
stm32f4xx-hal = "0.7"
23+
stm32f4xx-hal = "0.8.3"
2424

25-
smoltcp = { version = "0.6.0", default-features = false, features = ["proto-ipv4", "proto-ipv6", "socket-icmp", "socket-udp", "socket-tcp", "log", "verbose", "ethernet"], optional = true }
25+
smoltcp = { version = "0.6.0", default-features = false, features = ["proto-ipv4", "proto-ipv6", "socket-icmp", "socket-udp", "socket-tcp", "ethernet"], optional = true }
2626
log = { version = "0.4", optional = true }
2727

2828
[features]
29-
smoltcp-phy = ["smoltcp", "log"]
30-
default = []
31-
nucleo-f429zi = ["stm32f4xx-hal/stm32f429"]
29+
device-selected = []
30+
stm32f407 = ["stm32f4xx-hal/stm32f407", "device-selected"]
31+
stm32f417 = ["stm32f4xx-hal/stm32f417", "device-selected"]
32+
stm32f427 = ["stm32f4xx-hal/stm32f427", "device-selected"]
33+
stm32f429 = ["stm32f4xx-hal/stm32f429", "device-selected"]
34+
stm32f437 = ["stm32f4xx-hal/stm32f437", "device-selected"]
35+
stm32f439 = ["stm32f4xx-hal/stm32f439", "device-selected"]
36+
stm32f469 = ["stm32f4xx-hal/stm32f469", "device-selected"]
37+
stm32f479 = ["stm32f4xx-hal/stm32f479", "device-selected"]
38+
smoltcp-phy = ["smoltcp"]
39+
smoltcp-log = ["log", "smoltcp/log"]
40+
smoltcp-verbose = ["smoltcp/verbose"]
3241

3342
[dev-dependencies]
34-
cortex-m = "0.5"
43+
cortex-m = "0.6.2"
3544
cortex-m-rt = "0.6"
3645
panic-itm = "0.4"
46+
cortex-m-semihosting = "0.3.5"
47+
stm32f4xx-hal = {version = "0.8.3", features = ["rt"] }
3748

3849
[[example]]
3950
name = "pktgen"
51+
required-features = ["stm32f429"]
4052

4153
[[example]]
4254
name = "ip"
43-
required-features = ["smoltcp-phy"]
55+
required-features = ["stm32f429", "smoltcp-phy", "smoltcp-log", "smoltcp-verbose"]
4456

4557
[profile.release]
4658
debug = 2

README.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Please send pull requests.
1313

1414
Add to the `[dependencies]` section in your `Cargo.toml`:
1515
```rust
16-
stm32f4xx-hal = { version = "*", features = ["stm32f429"] }
17-
stm32-eth = { version = "0.1.1", features = ["nucleo-f429zi"] }
16+
stm32f4xx-hal = { version = "0.8.3", features = ["stm32f429"] }
17+
stm32-eth = { version = "0.2.0", features = ["stm32f429"] }
1818
```
1919

2020
In `src/main.rs` add:
@@ -28,33 +28,45 @@ use stm32_eth::{Eth, RingEntry};
2828
fn main() {
2929
let p = Peripherals::take().unwrap();
3030

31-
// Setup pins and initialize clocks.
32-
stm32_eth::setup(&p.RCC, &p.SYSCFG);
31+
let rcc = p.RCC.constrain();
32+
// HCLK must be between 25MHz and 168MHz to use the ethernet peripheral
33+
let clocks = rcc.cfgr.sysclk(32.mhz()).hclk(32.mhz()).freeze();
34+
3335
let gpioa = p.GPIOA.split();
3436
let gpiob = p.GPIOB.split();
3537
let gpioc = p.GPIOC.split();
3638
let gpiog = p.GPIOG.split();
37-
stm32_eth::setup_pins(
38-
gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1,
39-
gpioc.pc4, gpioc.pc5, gpiog.pg11, gpiog.pg13
40-
);
41-
// Allocate the ring buffers
42-
let mut rx_ring: [RingEntry<_>; 8] = Default::default();
43-
let mut tx_ring: [RingEntry<_>; 2] = Default::default();
44-
// Instantiate driver
45-
let mut eth = Eth::new(
46-
p.ETHERNET_MAC, p.ETHERNET_DMA,
47-
&mut rx_ring[..], &mut tx_ring[..]
48-
);
49-
// If you have a handler, enable interrupts
50-
eth.enable_interrupt(&mut cp.NVIC);
5139

40+
let eth_pins = EthPins {
41+
ref_clk: gpioa.pa1,
42+
md_io: gpioa.pa2,
43+
md_clk: gpioc.pc1,
44+
crs: gpioa.pa7,
45+
tx_en: gpiog.pg11,
46+
tx_d0: gpiog.pg13,
47+
tx_d1: gpiob.pb13,
48+
rx_d0: gpioc.pc4,
49+
rx_d1: gpioc.pc5,
50+
};
51+
52+
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
53+
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
54+
let mut eth = Eth::new(
55+
p.ETHERNET_MAC,
56+
p.ETHERNET_DMA,
57+
&mut rx_ring[..],
58+
&mut tx_ring[..],
59+
PhyAddress::_0,
60+
clocks,
61+
eth_pins,
62+
)
63+
.unwrap();
64+
eth.enable_interrupt();
5265

5366
if let Ok(pkt) = eth.recv_next() {
5467
// handle received pkt
5568
}
5669

57-
5870
eth.send(size, |buf| {
5971
// write up to `size` bytes into buf before it is being sent
6072
}).expect("send");

examples/ip.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cortex_m::asm;
77
use cortex_m_rt::{entry, exception};
88
use stm32f4xx_hal::{
99
gpio::GpioExt,
10+
prelude::*,
1011
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
1112
};
1213

@@ -22,7 +23,7 @@ use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
2223
use smoltcp::time::Instant;
2324
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
2425

25-
use stm32_eth::{Eth, RingEntry};
26+
use stm32_eth::{Eth, EthPins, PhyAddress, RingEntry};
2627

2728
static mut LOGGER: HioLogger = HioLogger {};
2829

@@ -59,18 +60,29 @@ fn main() -> ! {
5960
let p = Peripherals::take().unwrap();
6061
let mut cp = CorePeripherals::take().unwrap();
6162

63+
let rcc = p.RCC.constrain();
64+
// HCLK must be between 25MHz and 168MHz to use the ethernet peripheral
65+
let clocks = rcc.cfgr.sysclk(32.mhz()).hclk(32.mhz()).freeze();
66+
6267
setup_systick(&mut cp.SYST);
6368

6469
writeln!(stdout, "Enabling ethernet...").unwrap();
65-
stm32_eth::setup(&p.RCC, &p.SYSCFG);
6670
let gpioa = p.GPIOA.split();
6771
let gpiob = p.GPIOB.split();
6872
let gpioc = p.GPIOC.split();
6973
let gpiog = p.GPIOG.split();
70-
stm32_eth::setup_pins(
71-
gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1, gpioc.pc4, gpioc.pc5, gpiog.pg11,
72-
gpiog.pg13,
73-
);
74+
75+
let eth_pins = EthPins {
76+
ref_clk: gpioa.pa1,
77+
md_io: gpioa.pa2,
78+
md_clk: gpioc.pc1,
79+
crs: gpioa.pa7,
80+
tx_en: gpiog.pg11,
81+
tx_d0: gpiog.pg13,
82+
tx_d1: gpiob.pb13,
83+
rx_d0: gpioc.pc4,
84+
rx_d1: gpioc.pc5,
85+
};
7486

7587
let mut rx_ring: [RingEntry<_>; 8] = Default::default();
7688
let mut tx_ring: [RingEntry<_>; 2] = Default::default();
@@ -79,8 +91,12 @@ fn main() -> ! {
7991
p.ETHERNET_DMA,
8092
&mut rx_ring[..],
8193
&mut tx_ring[..],
82-
);
83-
eth.enable_interrupt(&mut cp.NVIC);
94+
PhyAddress::_0,
95+
clocks,
96+
eth_pins,
97+
)
98+
.unwrap();
99+
eth.enable_interrupt();
84100

85101
let local_addr = Ipv4Address::new(10, 0, 0, 1);
86102
let ip_addr = IpCidr::new(IpAddress::from(local_addr), 24);

examples/pktgen.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ use cortex_m::asm;
1111
use cortex_m::interrupt::Mutex;
1212
use stm32f4xx_hal::{
1313
gpio::GpioExt,
14+
prelude::*,
1415
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
1516
};
1617

1718
use core::fmt::Write;
1819
use cortex_m_semihosting::hio;
1920

20-
use stm32_eth::{Eth, RingEntry, TxError};
21+
use stm32_eth::{Eth, EthPins, PhyAddress, RingEntry, TxError};
2122

2223
const SRC_MAC: [u8; 6] = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
2324
const DST_MAC: [u8; 6] = [0x00, 0x00, 0xBE, 0xEF, 0xDE, 0xAD];
@@ -33,18 +34,29 @@ fn main() -> ! {
3334
let p = Peripherals::take().unwrap();
3435
let mut cp = CorePeripherals::take().unwrap();
3536

37+
let rcc = p.RCC.constrain();
38+
// HCLK must be between 25MHz and 168MHz to use the ethernet peripheral
39+
let clocks = rcc.cfgr.sysclk(32.mhz()).hclk(32.mhz()).freeze();
40+
3641
setup_systick(&mut cp.SYST);
3742

3843
writeln!(stdout, "Enabling ethernet...").unwrap();
39-
stm32_eth::setup(&p.RCC, &p.SYSCFG);
4044
let gpioa = p.GPIOA.split();
4145
let gpiob = p.GPIOB.split();
4246
let gpioc = p.GPIOC.split();
4347
let gpiog = p.GPIOG.split();
44-
stm32_eth::setup_pins(
45-
gpioa.pa1, gpioa.pa2, gpioa.pa7, gpiob.pb13, gpioc.pc1, gpioc.pc4, gpioc.pc5, gpiog.pg11,
46-
gpiog.pg13,
47-
);
48+
49+
let eth_pins = EthPins {
50+
ref_clk: gpioa.pa1,
51+
md_io: gpioa.pa2,
52+
md_clk: gpioc.pc1,
53+
crs: gpioa.pa7,
54+
tx_en: gpiog.pg11,
55+
tx_d0: gpiog.pg13,
56+
tx_d1: gpiob.pb13,
57+
rx_d0: gpioc.pc4,
58+
rx_d1: gpioc.pc5,
59+
};
4860

4961
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
5062
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
@@ -53,8 +65,12 @@ fn main() -> ! {
5365
p.ETHERNET_DMA,
5466
&mut rx_ring[..],
5567
&mut tx_ring[..],
56-
);
57-
eth.enable_interrupt(&mut cp.NVIC);
68+
PhyAddress::_0,
69+
clocks,
70+
eth_pins,
71+
)
72+
.unwrap();
73+
eth.enable_interrupt();
5874

5975
// Main loop
6076
let mut last_stats_time = 0usize;

run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if [[ "$EXAMPLE" = "" ]]; then
1313
fi
1414

1515
cargo build --target=thumbv7em-none-eabihf --release \
16-
--features="stm32f4xx-hal/rt nucleo-f429zi smoltcp-phy" \
16+
--features="stm32f429 smoltcp-phy smoltcp-log smoltcp-verbose" \
1717
--example=$EXAMPLE \
1818
|| exit 1
1919

src/desc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct Descriptor {
1111
impl Clone for Descriptor {
1212
fn clone(&self) -> Self {
1313
Descriptor {
14-
desc: Aligned((*self.desc).clone()),
14+
desc: Aligned(*self.desc),
1515
}
1616
}
1717
}

0 commit comments

Comments
 (0)