Skip to content

Commit ad3e893

Browse files
committed
Merge branch 'master' into h7
2 parents 486c2ad + b1616f7 commit ad3e893

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Unreleased
2-
No changes yet.
2+
* Update to `smoltcp` v0.9 [#71](https://github.com/stm32-rs/stm32-eth/pull/71).
33

44
## [0.4.0](https://github.com/stm32-rs/stm32-eth/tree/v0.4.0)
55
* Remove the `smi` feature and always enable miim/smi. Use `ieee802_3_miim` for SMI access [#45](https://github.com/stm32-rs/stm32-eth/pull/45)

examples/ip.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
//! For build and run instructions, see README.md
55
//!
66
//! This example starts a TCP listening server at the address 10.0.0.1/24, on port 80, that
7-
//! should transmit `Hello` to any connecting client, and then close the connection.
7+
//! should transmit `hello` to any connecting client, and then close the connection.
88
99
use defmt_rtt as _;
1010
use panic_probe as _;
1111

1212
use cortex_m_rt::{entry, exception};
13+
use smoltcp::iface::{Config, Interface, SocketSet, SocketStorage};
1314
use stm32_eth::stm32::{interrupt, CorePeripherals, Peripherals, SYST};
1415

1516
use core::cell::RefCell;
1617
use cortex_m::interrupt::Mutex;
1718

18-
use smoltcp::iface::{InterfaceBuilder, NeighborCache};
19-
use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
19+
use smoltcp::socket::tcp::{Socket as TcpSocket, SocketBuffer as TcpSocketBuffer};
2020
use smoltcp::time::Instant;
21-
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
21+
use smoltcp::wire::{EthernetAddress, IpCidr, Ipv4Address, Ipv4Cidr};
2222

2323
pub mod common;
2424

@@ -27,6 +27,7 @@ use stm32_eth::{
2727
Parts,
2828
};
2929

30+
const IP_ADDRESS: Ipv4Address = Ipv4Address::new(10, 0, 0, 1);
3031
const SRC_MAC: [u8; 6] = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
3132

3233
static TIME: Mutex<RefCell<u64>> = Mutex::new(RefCell::new(0));
@@ -62,27 +63,26 @@ fn main() -> ! {
6263
.unwrap();
6364
dma.enable_interrupt();
6465

65-
let local_addr = Ipv4Address::new(10, 0, 0, 1);
66-
let ip_addr = IpCidr::new(IpAddress::from(local_addr), 24);
67-
let mut ip_addrs = [ip_addr];
68-
let mut neighbor_storage = [None; 16];
69-
let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]);
7066
let ethernet_addr = EthernetAddress(SRC_MAC);
7167

72-
let mut sockets: [_; 1] = Default::default();
73-
let mut iface = InterfaceBuilder::new(&mut dma, &mut sockets[..])
74-
.hardware_addr(ethernet_addr.into())
75-
.ip_addrs(&mut ip_addrs[..])
76-
.neighbor_cache(neighbor_cache)
77-
.finalize();
68+
let mut config = Config::new();
69+
config.hardware_addr = Some(ethernet_addr.into());
70+
let mut iface = Interface::new(config, &mut &mut dma);
71+
72+
iface.update_ip_addrs(|addr| {
73+
addr.push(IpCidr::Ipv4(Ipv4Cidr::new(IP_ADDRESS, 24))).ok();
74+
});
75+
76+
let mut sockets = [SocketStorage::EMPTY];
77+
let mut sockets = SocketSet::new(&mut sockets[..]);
7878

7979
let mut server_rx_buffer = [0; 512];
8080
let mut server_tx_buffer = [0; 512];
8181
let server_socket = TcpSocket::new(
8282
TcpSocketBuffer::new(&mut server_rx_buffer[..]),
8383
TcpSocketBuffer::new(&mut server_tx_buffer[..]),
8484
);
85-
let server_handle = iface.add_socket(server_socket);
85+
let server_handle = sockets.add(server_socket);
8686

8787
loop {
8888
let time: u64 = cortex_m::interrupt::free(|cs| *TIME.borrow(cs).borrow());
@@ -91,31 +91,43 @@ fn main() -> ! {
9191
*eth_pending = false;
9292
});
9393

94-
iface.poll(Instant::from_millis(time as i64)).ok();
94+
iface.poll(
95+
Instant::from_millis(time as i64),
96+
&mut &mut dma,
97+
&mut sockets,
98+
);
9599

96-
let socket = iface.get_socket::<TcpSocket>(server_handle);
100+
let socket = sockets.get_mut::<TcpSocket>(server_handle);
97101

98102
if !socket.is_listening() && !socket.is_open() {
99103
socket.abort();
100104
if let Err(e) = socket.listen(80) {
101105
defmt::error!("TCP listen error: {:?}", e)
102106
} else {
103-
defmt::info!("Listening at {}:80...", ip_addr);
107+
defmt::info!("Listening at {}:80...", IP_ADDRESS);
104108
}
105109
} else {
106110
match socket.send_slice(b"hello\n") {
107111
Ok(_) => {
108-
while iface.get_socket::<TcpSocket>(server_handle).send_queue() != 0 {
112+
while sockets.get::<TcpSocket>(server_handle).send_queue() != 0 {
109113
// Poll to get the message out of the door
110-
iface.poll(Instant::from_millis(time as i64 + 1)).ok();
114+
iface.poll(
115+
Instant::from_millis(time as i64 + 1),
116+
&mut &mut dma,
117+
&mut sockets,
118+
);
111119
}
112120

113121
// Abort the connection
114-
let socket = iface.get_socket::<TcpSocket>(server_handle);
122+
let socket = sockets.get_mut::<TcpSocket>(server_handle);
115123
socket.abort();
116124
defmt::info!("Transmitted hello! Closing socket...");
117125

118-
iface.poll(Instant::from_millis(time as i64 + 1)).ok();
126+
iface.poll(
127+
Instant::from_millis(time as i64),
128+
&mut &mut dma,
129+
&mut sockets,
130+
);
119131
}
120132
Err(_) => {}
121133
}

src/dma/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,16 @@ impl<'rx, 'tx> EthernetDMA<'rx, 'tx> {
299299
self.tx_ring.running_state()
300300
}
301301

302-
/// Receive the next packet (if any is ready), or return `None`
302+
/// Receive the next packet (if any is ready), or return [`Err`]
303303
/// immediately.
304304
pub fn recv_next(&mut self, packet_id: Option<PacketId>) -> Result<RxPacket, RxError> {
305305
self.rx_ring.recv_next(packet_id.map(|p| p.into()))
306306
}
307307

308-
/// Check whether a frame is available for reception.
308+
/// Check if there is a packet available for reading.
309309
///
310-
/// If this function returns `true`, the next [`TxRing::recv_next`] is
311-
/// guaranteed to succeed.
310+
/// If this function returns true, it is guaranteed that the
311+
/// next call to [`EthernetDMA::recv_next`] will return [`Ok`].
312312
pub fn rx_available(&mut self) -> bool {
313313
self.rx_ring.available()
314314
}
@@ -328,10 +328,10 @@ impl<'rx, 'tx> EthernetDMA<'rx, 'tx> {
328328
self.tx_ring.send(length, packet_id.map(|p| p.into()), f)
329329
}
330330

331-
/// Check whether a descriptor is available for sending.
331+
/// Check if sending a packet now would succeed.
332332
///
333-
/// If this function returns `true`, the next [`EthernetDMA::send`] is
334-
/// guarantted to succeed.
333+
/// If this function returns true, it is guaranteed that
334+
/// the next call to [`EthernetDMA::send`] will return [`Ok`]
335335
pub fn tx_available(&mut self) -> bool {
336336
self.tx_ring.available()
337337
}

src/peripherals.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ mod pac_override_impl {
260260
impl ETHERNET_DMA {
261261
#[doc = r"Pointer to the register block"]
262262
pub const PTR: *const DmaRegisterBlock = 0x4002_9000 as *const _;
263+
264+
pub const fn ptr() -> *const DmaRegisterBlock {
265+
Self::PTR
266+
}
263267
}
264268

265269
impl core::ops::Deref for ETHERNET_DMA {

0 commit comments

Comments
 (0)