Skip to content

Commit 261a476

Browse files
committed
Update rtic-echo from branch master
1 parent 690a9d1 commit 261a476

File tree

1 file changed

+55
-93
lines changed

1 file changed

+55
-93
lines changed

examples/rtic-echo.rs

Lines changed: 55 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
use defmt_rtt as _;
1111
use panic_probe as _;
1212

13-
use smoltcp::{
14-
iface::{self, SocketStorage},
15-
wire::{self, IpAddress, Ipv4Address},
16-
};
13+
use smoltcp::wire::{IpAddress, Ipv4Address};
1714

1815
mod common;
1916

20-
const ADDRESS: (IpAddress, u16) = (IpAddress::Ipv4(Ipv4Address::new(10, 0, 0, 1)), 1337);
17+
const IP_ADDRESS: Ipv4Address = Ipv4Address::new(10, 0, 0, 1);
18+
const SOCKET_ADDRESS: (IpAddress, u16) = (IpAddress::Ipv4(IP_ADDRESS), 1337);
2119
const MAC: [u8; 6] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05];
2220

2321
#[rtic::app(device = stm32_eth::stm32, dispatchers = [SPI1])]
@@ -28,25 +26,20 @@ mod app {
2826
use ieee802_3_miim::{phy::PhySpeed, Phy};
2927
use systick_monotonic::Systick;
3028

31-
use stm32_eth::{
32-
dma::{EthernetDMA, RxDescriptor, RxDescriptorRing, TxDescriptor, TxDescriptorRing},
33-
mac::Speed,
34-
Parts,
35-
};
29+
use stm32_eth::{dma::EthernetDMA, mac::Speed, Parts};
3630

3731
use smoltcp::{
38-
iface::{self, Interface, SocketHandle},
39-
socket::TcpSocket,
40-
socket::{TcpSocketBuffer, TcpState},
41-
wire::EthernetAddress,
32+
iface::{self, Interface, SocketHandle, SocketSet, SocketStorage},
33+
socket::tcp::{Socket as TcpSocket, SocketBuffer as TcpSocketBuffer, State as TcpState},
34+
wire::{EthernetAddress, IpCidr, Ipv4Cidr},
4235
};
4336

44-
use crate::NetworkStorage;
45-
4637
#[local]
4738
struct Local {
48-
interface: Interface<'static, &'static mut EthernetDMA<'static, 'static>>,
39+
interface: Interface,
4940
tcp_handle: SocketHandle,
41+
dma: EthernetDMA<'static, 'static>,
42+
sockets: SocketSet<'static>,
5043
}
5144

5245
#[shared]
@@ -61,69 +54,70 @@ mod app {
6154
}
6255

6356
#[init(local = [
64-
rx_desc: [RxDescriptor; 2] = [RxDescriptor::new(); 2],
65-
tx_desc: [TxDescriptor; 2] = [TxDescriptor::new(); 2],
66-
rx_buffers: [[u8; 1522]; 2] = [[0u8; stm32_eth::MTU]; 2],
67-
tx_buffers: [[u8; 1522]; 2] = [[0u8; stm32_eth::MTU]; 2],
68-
storage: NetworkStorage = NetworkStorage::new(),
69-
dma: core::mem::MaybeUninit<EthernetDMA<'static, 'static>> = core::mem::MaybeUninit::uninit(),
57+
rx_storage: [u8; 512] = [0u8; 512],
58+
tx_storage: [u8; 512] = [0u8; 512],
59+
socket_storage: [SocketStorage<'static>; 1] = [SocketStorage::EMPTY; 1],
7060
])]
7161
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
7262
defmt::info!("Pre-init");
7363
let core = cx.core;
7464
let p = cx.device;
7565

76-
let rx_ring = RxDescriptorRing::new(cx.local.rx_desc, cx.local.rx_buffers);
77-
let tx_ring = TxDescriptorRing::new(cx.local.tx_desc, cx.local.tx_buffers);
66+
let (tx_ring, rx_ring) = crate::common::setup_rings();
7867

7968
let (clocks, gpio, ethernet) = crate::common::setup_peripherals(p);
8069
let mono = Systick::new(core.SYST, clocks.hclk().raw());
8170

71+
let (rx_storage, tx_storage, socket_storage) = (
72+
cx.local.rx_storage,
73+
cx.local.tx_storage,
74+
cx.local.socket_storage,
75+
);
76+
8277
defmt::info!("Setting up pins");
8378
let (pins, mdio, mdc, _) = crate::common::setup_pins(gpio);
8479

8580
defmt::info!("Configuring ethernet");
8681

8782
let Parts {
88-
dma,
83+
mut dma,
8984
mac,
9085
#[cfg(feature = "ptp")]
9186
ptp: _,
9287
} = stm32_eth::new_with_mii(ethernet, rx_ring, tx_ring, clocks, pins, mdio, mdc).unwrap();
9388

94-
let dma = cx.local.dma.write(dma);
95-
9689
defmt::info!("Enabling interrupts");
9790
dma.enable_interrupt();
9891

9992
defmt::info!("Setting up smoltcp");
100-
let store = cx.local.storage;
10193

102-
let mut routes = smoltcp::iface::Routes::new(&mut store.routes_cache[..]);
94+
let mut routes = smoltcp::iface::Routes::new();
10395
routes
10496
.add_default_ipv4_route(smoltcp::wire::Ipv4Address::UNSPECIFIED)
10597
.ok();
10698

107-
let neighbor_cache = smoltcp::iface::NeighborCache::new(&mut store.neighbor_cache[..]);
108-
109-
let rx_buffer = TcpSocketBuffer::new(&mut store.tcp_socket_storage.rx_storage[..]);
110-
let tx_buffer = TcpSocketBuffer::new(&mut store.tcp_socket_storage.tx_storage[..]);
99+
let rx_buffer = TcpSocketBuffer::new(&mut rx_storage[..]);
100+
let tx_buffer = TcpSocketBuffer::new(&mut tx_storage[..]);
111101

112102
let socket = TcpSocket::new(rx_buffer, tx_buffer);
113103

114-
let mut interface = iface::InterfaceBuilder::new(dma, &mut store.sockets[..])
115-
.hardware_addr(EthernetAddress::from_bytes(&crate::MAC).into())
116-
.neighbor_cache(neighbor_cache)
117-
.ip_addrs(&mut store.ip_addrs[..])
118-
.routes(routes)
119-
.finalize();
104+
let mut config = iface::Config::new();
105+
config.hardware_addr = Some(EthernetAddress::from_bytes(&crate::MAC).into());
120106

121-
let tcp_handle = interface.add_socket(socket);
107+
let mut interface = Interface::new(config, &mut &mut dma);
108+
interface.update_ip_addrs(|addr| {
109+
addr.push(IpCidr::Ipv4(Ipv4Cidr::new(crate::IP_ADDRESS, 24)))
110+
.ok();
111+
});
122112

123-
let socket = interface.get_socket::<TcpSocket>(tcp_handle);
124-
socket.listen(crate::ADDRESS).ok();
113+
let mut sockets = SocketSet::new(&mut socket_storage[..]);
125114

126-
interface.poll(now_fn()).unwrap();
115+
let tcp_handle = sockets.add(socket);
116+
117+
let socket = sockets.get_mut::<TcpSocket>(tcp_handle);
118+
socket.listen(crate::SOCKET_ADDRESS).ok();
119+
120+
interface.poll(now_fn(), &mut &mut dma, &mut sockets);
127121

128122
if let Ok(mut phy) = EthernetPhy::from_miim(mac, 0) {
129123
defmt::info!(
@@ -154,28 +148,36 @@ mod app {
154148
defmt::info!("Not resetting unsupported PHY. Cannot detect link speed.");
155149
}
156150

157-
defmt::info!("Setup done. Listening at {}", crate::ADDRESS);
151+
defmt::info!("Setup done. Listening at {}", crate::SOCKET_ADDRESS);
158152

159153
(
160154
Shared {},
161155
Local {
162156
interface,
163157
tcp_handle,
158+
dma,
159+
sockets,
164160
},
165161
init::Monotonics(mono),
166162
)
167163
}
168164

169-
#[task(binds = ETH, local = [interface, tcp_handle, data: [u8; 512] = [0u8; 512]], priority = 2)]
165+
#[task(binds = ETH, local = [interface, tcp_handle, dma, sockets, data: [u8; 512] = [0u8; 512]], priority = 2)]
170166
fn eth_interrupt(cx: eth_interrupt::Context) {
171-
let (iface, tcp_handle, buffer) = (cx.local.interface, cx.local.tcp_handle, cx.local.data);
172-
173-
let interrupt_reason = iface.device_mut().interrupt_handler();
167+
let (iface, tcp_handle, buffer, sockets, mut dma) = (
168+
cx.local.interface,
169+
cx.local.tcp_handle,
170+
cx.local.data,
171+
cx.local.sockets,
172+
cx.local.dma,
173+
);
174+
175+
let interrupt_reason = dma.interrupt_handler();
174176
defmt::debug!("Got an ethernet interrupt! Reason: {}", interrupt_reason);
175177

176-
iface.poll(now_fn()).ok();
178+
iface.poll(now_fn(), &mut dma, sockets);
177179

178-
let socket = iface.get_socket::<TcpSocket>(*tcp_handle);
180+
let socket = sockets.get_mut::<TcpSocket>(*tcp_handle);
179181
if let Ok(recv_bytes) = socket.recv_slice(buffer) {
180182
if recv_bytes > 0 {
181183
socket.send_slice(&buffer[..recv_bytes]).ok();
@@ -185,50 +187,10 @@ mod app {
185187

186188
if !socket.is_listening() && !socket.is_open() || socket.state() == TcpState::CloseWait {
187189
socket.abort();
188-
socket.listen(crate::ADDRESS).ok();
190+
socket.listen(crate::SOCKET_ADDRESS).ok();
189191
defmt::warn!("Disconnected... Reopening listening socket.");
190192
}
191193

192-
iface.poll(now_fn()).ok();
193-
}
194-
}
195-
196-
/// All storage required for networking
197-
pub struct NetworkStorage {
198-
pub ip_addrs: [wire::IpCidr; 1],
199-
pub sockets: [iface::SocketStorage<'static>; 1],
200-
pub tcp_socket_storage: TcpSocketStorage,
201-
pub neighbor_cache: [Option<(wire::IpAddress, iface::Neighbor)>; 8],
202-
pub routes_cache: [Option<(wire::IpCidr, iface::Route)>; 8],
203-
}
204-
205-
impl NetworkStorage {
206-
const IP_INIT: wire::IpCidr =
207-
wire::IpCidr::Ipv4(wire::Ipv4Cidr::new(wire::Ipv4Address::new(10, 0, 0, 1), 24));
208-
209-
pub const fn new() -> Self {
210-
NetworkStorage {
211-
ip_addrs: [Self::IP_INIT],
212-
neighbor_cache: [None; 8],
213-
routes_cache: [None; 8],
214-
sockets: [SocketStorage::EMPTY; 1],
215-
tcp_socket_storage: TcpSocketStorage::new(),
216-
}
217-
}
218-
}
219-
220-
/// Storage of TCP sockets
221-
#[derive(Copy, Clone)]
222-
pub struct TcpSocketStorage {
223-
rx_storage: [u8; 512],
224-
tx_storage: [u8; 512],
225-
}
226-
227-
impl TcpSocketStorage {
228-
const fn new() -> Self {
229-
Self {
230-
rx_storage: [0; 512],
231-
tx_storage: [0; 512],
232-
}
194+
iface.poll(now_fn(), &mut dma, sockets);
233195
}
234196
}

0 commit comments

Comments
 (0)