Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple sockets, UDP and multicast #82

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[target.riscv32imc-unknown-none-elf]
runner = "espflash --speed 921600 --monitor"
runner = "espflash --speed 2000000 --monitor"

rustflags = [
"-C", "link-arg=-Tlinkall.x",
Expand Down Expand Up @@ -46,4 +46,4 @@ rustflags = [
target = "riscv32imc-unknown-none-elf"

[unstable]
build-std = [ "core", "alloc" ]
build-std = [ "core" ]
28 changes: 11 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ opt-level = 3
embedded-hal = "0.2.3"
nb = "1.0.0"
void = { version = "1.0.2", default-features = false }
esp32c3-hal = { version = "0.2.0", optional = true }
esp32-hal = { version = "0.5.0", optional = true, features = [ "rt" ] }
esp32s3-hal = { version = "0.2.0", optional = true, features = [ "rt" ] }
esp32s2-hal = { version = "0.2.0", optional = true, features = [ "rt" ] }
riscv-rt = { version = "0.9.0", optional = true }
riscv = { version = "0.8.0", optional = true }
esp32c3-hal = { version = "0.3.0", optional = true }
esp32-hal = { version = "0.6.0", optional = true, features = [ "rt" ] }
esp32s3-hal = { version = "0.3.0", optional = true, features = [ "rt" ] }
esp32s2-hal = { version = "0.3.0", optional = true, features = [ "rt" ] }
riscv-rt = { version = "0.10.0", optional = true }
riscv = { version = "0.10.0", optional = true }
xtensa-lx-rt = { version = "0.13.0", optional = true }
xtensa-lx = { version = "0.7.0", optional = true }
smoltcp = { version = "0.8.0", default-features=false, features = ["proto-igmp", "proto-ipv4", "socket-tcp", "socket-icmp", "socket-udp", "medium-ethernet", "proto-dhcpv4", "socket-raw", "socket-dhcpv4"] }
critical-section = "1.1.0"
critical-section = "1.1.1"
atomic-polyfill = "1.0.1"
log = "0.4.17"
embedded-svc = { version = "0.22.1", default-features = false, features = [], optional = true }
Expand All @@ -39,19 +39,19 @@ bleps-macros = { git = "https://github.com/bjoernQ/bleps", package = "bleps-macr

[target.xtensa-esp32-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32", "log" ] }
esp-backtrace = { version = "0.2.0", features = [ "esp32", "panic-handler", "exception-handler", "print-uart" ] }
esp-backtrace = { version = "0.3.0", features = [ "esp32", "panic-handler", "exception-handler", "print-uart" ] }

[target.riscv32imc-unknown-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32c3", "log" ] }
esp-backtrace = { version = "0.2.0" , features = [ "esp32c3", "panic-handler", "exception-handler", "print-uart" ] }
esp-backtrace = { version = "0.3.0", features = [ "esp32c3", "panic-handler", "exception-handler", "print-uart" ] }

[target.xtensa-esp32s3-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32s3", "log" ] }
esp-backtrace = { version = "0.2.0", features = [ "esp32s3", "panic-handler", "exception-handler", "print-uart" ] }
esp-backtrace = { version = "0.3.0", features = [ "esp32s3", "panic-handler", "exception-handler", "print-uart" ] }

[target.xtensa-esp32s2-none-elf.dev-dependencies]
esp-println = { version = "0.3.1", features = [ "esp32s2", "log", "critical-section" ] }
esp-backtrace = { version = "0.2.0", features = [ "esp32s2", "panic-handler", "exception-handler", "print-uart" ] }
esp-backtrace = { version = "0.3.0", features = [ "esp32s2", "panic-handler", "exception-handler", "print-uart" ] }
xtensa-atomic-emulation-trap = "0.2.0"

[features]
Expand All @@ -67,9 +67,3 @@ enumset = []
embedded-svc = [ "dep:enumset", "dep:embedded-svc", "utils" ]
wifi = []
ble = [ "esp32-hal?/bluetooth" ]

# currently published versions don't contain all relevant adjustments - using git dependencies for now
[patch.crates-io]
esp32s3-hal = { git = "https://github.com/esp-rs/esp-hal/", package = "esp32s3-hal", rev = "9b3e644e309e0ab3332fd007f4ac2fb4881abc4c" }
esp32s2-hal = { git = "https://github.com/esp-rs/esp-hal/", package = "esp32s2-hal", rev = "9b3e644e309e0ab3332fd007f4ac2fb4881abc4c" }
esp32c3-hal = { git = "https://github.com/esp-rs/esp-hal/", package = "esp32c3-hal", rev = "9b3e644e309e0ab3332fd007f4ac2fb4881abc4c" }
9 changes: 6 additions & 3 deletions examples/coex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn main() -> ! {

rtc.rwdt.disable();

let mut storage = create_network_stack_storage!(3, 8, 1);
let mut storage = create_network_stack_storage!(3, 8, 1, 1);
let ethernet = create_network_interface(network_stack_storage!(storage));
let mut wifi_interface = esp_wifi::wifi_interface::Wifi::new(ethernet);

Expand Down Expand Up @@ -162,8 +162,11 @@ fn main() -> ! {

println!("Start busy loop on main");

let mut network = Network::new(wifi_interface, current_millis);
let mut socket = network.get_socket();
let network = Network::new(wifi_interface, current_millis);

let mut rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let mut socket = network.get_socket(&mut rx_buffer, &mut tx_buffer);

loop {
println!("Making HTTP request");
Expand Down
9 changes: 6 additions & 3 deletions examples/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> ! {

rtc.rwdt.disable();

let mut storage = create_network_stack_storage!(3, 8, 1);
let mut storage = create_network_stack_storage!(3, 8, 1, 1);
let ethernet = create_network_interface(network_stack_storage!(storage));
let mut wifi_interface = esp_wifi::wifi_interface::Wifi::new(ethernet);

Expand Down Expand Up @@ -136,8 +136,11 @@ fn main() -> ! {

println!("Start busy loop on main");

let mut network = Network::new(wifi_interface, current_millis);
let mut socket = network.get_socket();
let network = Network::new(wifi_interface, current_millis);

let mut rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let mut socket = network.get_socket(&mut rx_buffer, &mut tx_buffer);

loop {
println!("Making HTTP request");
Expand Down
2 changes: 1 addition & 1 deletion src/timer_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use critical_section::Mutex;
use esp32_hal::{
interrupt,
pac::{self, TIMG1},
prelude::_embedded_hal_timer_CountDown,
prelude::{Instance, _embedded_hal_timer_CountDown},
timer::{Timer, Timer0},
};
use log::trace;
Expand Down
33 changes: 15 additions & 18 deletions src/wifi/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use smoltcp::{
iface::{Interface, InterfaceBuilder, Neighbor, NeighborCache, Route, Routes, SocketStorage},
socket::{Dhcpv4Socket, TcpSocket, TcpSocketBuffer},
wire::{EthernetAddress, IpAddress, IpCidr},
socket::Dhcpv4Socket,
wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address},
};

use crate::wifi::get_sta_mac;
Expand All @@ -10,7 +10,7 @@ use super::WifiDevice;

#[macro_export]
macro_rules! create_network_stack_storage {
($socket_count:literal , $cache_count:literal , $routes_count:literal) => {{
($socket_count:literal , $cache_count:literal , $routes_count:literal, $multicast_storage_size:literal) => {{
use smoltcp::iface::{Neighbor, NeighborCache, Route, SocketStorage};
use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address};

Expand All @@ -20,20 +20,28 @@ macro_rules! create_network_stack_storage {
let mut routes_storage: [Option<(IpCidr, Route)>; $routes_count] = Default::default();
let ip_addr = IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0);
let mut ip_addrs = [ip_addr];
let mut ipv4_multicast_storage = [None; $multicast_storage_size];

(
socket_set_entries,
neighbor_cache_storage,
routes_storage,
ip_addrs,
ipv4_multicast_storage,
)
}};
}

#[macro_export]
macro_rules! network_stack_storage {
($param:ident) => {{
(&mut $param.0, &mut $param.1, &mut $param.2, &mut $param.3)
(
&mut $param.0,
&mut $param.1,
&mut $param.2,
&mut $param.3,
&mut $param.4,
)
}};
}

Expand All @@ -45,12 +53,14 @@ pub fn create_network_interface<'a>(
&'a mut [Option<(IpAddress, Neighbor)>],
&'a mut [Option<(IpCidr, Route)>],
&'a mut [IpCidr; 1],
&'a mut [Option<(Ipv4Address, ())>],
),
) -> Interface<WifiDevice> {
let socket_set_entries = storage.0;
let neighbor_cache_storage = storage.1;
let routes_storage = storage.2;
let ip_addrs = storage.3;
let ipv4_multicast_groups = storage.4;

let mut mac = [0u8; 6];
get_sta_mac(&mut mac);
Expand All @@ -61,27 +71,14 @@ pub fn create_network_interface<'a>(
let neighbor_cache = NeighborCache::new(&mut neighbor_cache_storage[..]);
let routes = Routes::new(&mut routes_storage[..]);

let sockets_to_add = socket_set_entries.len() - 1;
let mut ethernet = InterfaceBuilder::new(device, socket_set_entries)
.hardware_addr(smoltcp::wire::HardwareAddress::Ethernet(hw_address))
.neighbor_cache(neighbor_cache)
.ip_addrs(&mut ip_addrs[..])
.ipv4_multicast_groups(ipv4_multicast_groups)
.routes(routes)
.finalize();

for _ in 0..sockets_to_add {
let rx_tx_socket1 = {
static mut TCP_SERVER_RX_DATA: [u8; 1536] = [0; 1536];
static mut TCP_SERVER_TX_DATA: [u8; 1536] = [0; 1536];

let tcp_rx_buffer = unsafe { TcpSocketBuffer::new(&mut TCP_SERVER_RX_DATA[..]) };
let tcp_tx_buffer = unsafe { TcpSocketBuffer::new(&mut TCP_SERVER_TX_DATA[..]) };

TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer)
};
ethernet.add_socket(rx_tx_socket1);
}

let dhcp_socket = Dhcpv4Socket::new();
ethernet.add_socket(dhcp_socket);

Expand Down
Loading