working tcp socket
This commit is contained in:
parent
cdf31119fe
commit
bb7f643134
@ -1,5 +1,5 @@
|
|||||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||||
runner = "elf2uf2-rs -d -s"
|
runner = "elf2uf2-rs -d -s -t"
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||||
|
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -409,6 +409,7 @@ checksum = "0ef3bac31ec146321248a169e9c7b5799f1e0b3829c7a9b324cb4600a7438f59"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m",
|
"cortex-m",
|
||||||
"critical-section",
|
"critical-section",
|
||||||
|
"defmt",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -418,6 +419,7 @@ version = "0.6.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed041cc19a603d657124fddefdcbe5ef8bd60e77d972793ebb57de93394f5949"
|
checksum = "ed041cc19a603d657124fddefdcbe5ef8bd60e77d972793ebb57de93394f5949"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"defmt",
|
||||||
"document-features",
|
"document-features",
|
||||||
"embassy-net-driver",
|
"embassy-net-driver",
|
||||||
"embassy-sync",
|
"embassy-sync",
|
||||||
@ -434,6 +436,9 @@ name = "embassy-net-driver"
|
|||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "524eb3c489760508f71360112bca70f6e53173e6fe48fc5f0efd0f5ab217751d"
|
checksum = "524eb3c489760508f71360112bca70f6e53173e6fe48fc5f0efd0f5ab217751d"
|
||||||
|
dependencies = [
|
||||||
|
"defmt",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "embassy-net-driver-channel"
|
name = "embassy-net-driver-channel"
|
||||||
@ -457,6 +462,7 @@ dependencies = [
|
|||||||
"cortex-m",
|
"cortex-m",
|
||||||
"cortex-m-rt",
|
"cortex-m-rt",
|
||||||
"critical-section",
|
"critical-section",
|
||||||
|
"defmt",
|
||||||
"document-features",
|
"document-features",
|
||||||
"embassy-embedded-hal",
|
"embassy-embedded-hal",
|
||||||
"embassy-futures",
|
"embassy-futures",
|
||||||
@ -553,6 +559,9 @@ name = "embassy-usb-driver"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4fc247028eae04174b6635104a35b1ed336aabef4654f5e87a8f32327d231970"
|
checksum = "4fc247028eae04174b6635104a35b1ed336aabef4654f5e87a8f32327d231970"
|
||||||
|
dependencies = [
|
||||||
|
"defmt",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "embassy-usb-logger"
|
name = "embassy-usb-logger"
|
||||||
@ -818,6 +827,7 @@ version = "0.8.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
|
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"defmt",
|
||||||
"hash32",
|
"hash32",
|
||||||
"stable_deref_trait",
|
"stable_deref_trait",
|
||||||
]
|
]
|
||||||
@ -1407,6 +1417,7 @@ dependencies = [
|
|||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
|
"defmt",
|
||||||
"heapless",
|
"heapless",
|
||||||
"managed",
|
"managed",
|
||||||
]
|
]
|
||||||
|
@ -4,11 +4,11 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
embassy-executor = {version="*", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"]}
|
embassy-executor = {version="*", features = ["defmt", "task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt"]}
|
||||||
embassy-rp = {version ="*", features = ["rp2040", "time-driver", "critical-section-impl"] }
|
embassy-rp = {version ="*", features = ["defmt", "unstable-pac", "rp2040", "time-driver", "critical-section-impl"] }
|
||||||
embassy-time = "*"
|
embassy-time = "*"
|
||||||
embassy-usb-logger = "*"
|
embassy-usb-logger = "*"
|
||||||
embassy-net = {version = "*", features = ["proto-ipv4", "tcp", "dhcpv4", "dns"]}
|
embassy-net = {version = "*", features = ["defmt", "proto-ipv4", "tcp", "dhcpv4", "dns"]}
|
||||||
cyw43-pio = "*"
|
cyw43-pio = "*"
|
||||||
cyw43 = "*"
|
cyw43 = "*"
|
||||||
|
|
||||||
|
52
src/main.rs
52
src/main.rs
@ -2,8 +2,9 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
#![allow(async_fn_in_trait)]
|
#![allow(async_fn_in_trait)]
|
||||||
|
|
||||||
|
use core::str::from_utf8;
|
||||||
use cyw43_pio::{PioSpi, DEFAULT_CLOCK_DIVIDER};
|
use cyw43_pio::{PioSpi, DEFAULT_CLOCK_DIVIDER};
|
||||||
use defmt::*;
|
use log::{info, warn};
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_net::tcp::TcpSocket;
|
use embassy_net::tcp::TcpSocket;
|
||||||
use embassy_net::{Config, StackResources};
|
use embassy_net::{Config, StackResources};
|
||||||
@ -67,7 +68,7 @@ async fn main(spawner: Spawner) {
|
|||||||
static STATE: StaticCell<cyw43::State> = StaticCell::new();
|
static STATE: StaticCell<cyw43::State> = StaticCell::new();
|
||||||
let state = STATE.init(cyw43::State::new());
|
let state = STATE.init(cyw43::State::new());
|
||||||
let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
|
let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
|
||||||
unwrap!(spawner.spawn(cyw43_task(runner)));
|
spawner.spawn(cyw43_task(runner)).unwrap();
|
||||||
|
|
||||||
control.init(clm).await;
|
control.init(clm).await;
|
||||||
control
|
control
|
||||||
@ -76,7 +77,7 @@ async fn main(spawner: Spawner) {
|
|||||||
|
|
||||||
// Use a link-local address for communication without DHCP server
|
// Use a link-local address for communication without DHCP server
|
||||||
let config = Config::ipv4_static(embassy_net::StaticConfigV4 {
|
let config = Config::ipv4_static(embassy_net::StaticConfigV4 {
|
||||||
address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(169, 254, 1, 1), 16),
|
address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(192, 254, 0, 2), 16),
|
||||||
dns_servers: heapless::Vec::new(),
|
dns_servers: heapless::Vec::new(),
|
||||||
gateway: None,
|
gateway: None,
|
||||||
});
|
});
|
||||||
@ -88,8 +89,51 @@ async fn main(spawner: Spawner) {
|
|||||||
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
||||||
let (stack, runner) = embassy_net::new(net_device, config, RESOURCES.init(StackResources::new()), seed);
|
let (stack, runner) = embassy_net::new(net_device, config, RESOURCES.init(StackResources::new()), seed);
|
||||||
|
|
||||||
unwrap!(spawner.spawn(net_task(runner)));
|
spawner.spawn(net_task(runner)).unwrap();
|
||||||
|
|
||||||
//control.start_ap_open("cyw43", 5).await;
|
//control.start_ap_open("cyw43", 5).await;
|
||||||
control.start_ap_wpa2("cyw43", "password", 5).await;
|
control.start_ap_wpa2("cyw43", "password", 5).await;
|
||||||
|
|
||||||
|
let mut rx_buffer = [0; 4096];
|
||||||
|
let mut tx_buffer = [0; 4096];
|
||||||
|
let mut buf = [0; 4096];
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||||
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
||||||
|
|
||||||
|
control.gpio_set(0, false).await;
|
||||||
|
info!("Listening on TCP:1234...");
|
||||||
|
if let Err(e) = socket.accept(1234).await {
|
||||||
|
warn!("accept error: {:?}", e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Received connection from {:?}", socket.remote_endpoint());
|
||||||
|
control.gpio_set(0, true).await;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let n = match socket.read(&mut buf).await {
|
||||||
|
Ok(0) => {
|
||||||
|
warn!("read EOF");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(e) => {
|
||||||
|
warn!("read error: {:?}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
info!("rxd {}", from_utf8(&buf[..n]).unwrap());
|
||||||
|
|
||||||
|
match socket.write_all(&buf[..n]).await {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("write error: {:?}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user