aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSatoshi Tanaka <[email protected]>2023-05-02 01:30:08 +0900
committerSatoshi Tanaka <[email protected]>2023-05-02 01:30:08 +0900
commit534cf7c618b0e93881b9757b5608a7ad67606fce (patch)
tree0f67fa535b24880d94c657bb734361072b8fa403 /examples
parent73cd016885fc6db38c71ca3c1941554c864670ce (diff)
Add AP mode example
Diffstat (limited to 'examples')
-rw-r--r--examples/rpi-pico-w/src/bin/tcp_server_ap.rs144
1 files changed, 144 insertions, 0 deletions
diff --git a/examples/rpi-pico-w/src/bin/tcp_server_ap.rs b/examples/rpi-pico-w/src/bin/tcp_server_ap.rs
new file mode 100644
index 000000000..e43412625
--- /dev/null
+++ b/examples/rpi-pico-w/src/bin/tcp_server_ap.rs
@@ -0,0 +1,144 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4#![feature(async_fn_in_trait)]
5#![allow(incomplete_features)]
6
7use core::str::from_utf8;
8
9use cyw43_pio::PioSpi;
10use defmt::*;
11use embassy_executor::Spawner;
12use embassy_net::tcp::TcpSocket;
13use embassy_net::{Config, Stack, StackResources};
14use embassy_rp::gpio::{Level, Output};
15use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_25};
16use embassy_rp::pio::{Pio0, PioPeripheral, PioStateMachineInstance, Sm0};
17use embedded_io::asynch::Write;
18use static_cell::StaticCell;
19use {defmt_rtt as _, panic_probe as _};
20
21macro_rules! singleton {
22 ($val:expr) => {{
23 type T = impl Sized;
24 static STATIC_CELL: StaticCell<T> = StaticCell::new();
25 STATIC_CELL.init_with(move || $val)
26 }};
27}
28
29#[embassy_executor::task]
30async fn wifi_task(
31 runner: cyw43::Runner<
32 'static,
33 Output<'static, PIN_23>,
34 PioSpi<PIN_25, PioStateMachineInstance<Pio0, Sm0>, DMA_CH0>,
35 >,
36) -> ! {
37 runner.run().await
38}
39
40#[embassy_executor::task]
41async fn net_task(stack: &'static Stack<cyw43::NetDriver<'static>>) -> ! {
42 stack.run().await
43}
44
45#[embassy_executor::main]
46async fn main(spawner: Spawner) {
47 info!("Hello World!");
48
49 let p = embassy_rp::init(Default::default());
50
51 let fw = include_bytes!("../../../../firmware/43439A0.bin");
52 let clm = include_bytes!("../../../../firmware/43439A0_clm.bin");
53
54 // To make flashing faster for development, you may want to flash the firmwares independently
55 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
56 // probe-rs-cli download 43439A0.bin --format bin --chip RP2040 --base-address 0x10100000
57 // probe-rs-cli download 43439A0_clm.bin --format bin --chip RP2040 --base-address 0x10140000
58 //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) };
59 //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
60
61 let pwr = Output::new(p.PIN_23, Level::Low);
62 let cs = Output::new(p.PIN_25, Level::High);
63
64 let (_, sm, _, _, _) = p.PIO0.split();
65 let dma = p.DMA_CH0;
66 let spi = PioSpi::new(sm, cs, p.PIN_24, p.PIN_29, dma);
67
68 let state = singleton!(cyw43::State::new());
69 let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
70 unwrap!(spawner.spawn(wifi_task(runner)));
71
72 control.init(clm).await;
73 control
74 .set_power_management(cyw43::PowerManagementMode::PowerSave)
75 .await;
76
77 // Use a link-local address for communication without DHCP server
78 let config = Config::Static(embassy_net::StaticConfig {
79 address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(169, 254, 1, 1), 16),
80 dns_servers: heapless::Vec::new(),
81 gateway: None,
82 });
83
84 // Generate random seed
85 let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random.
86
87 // Init network stack
88 let stack = &*singleton!(Stack::new(
89 net_device,
90 config,
91 singleton!(StackResources::<2>::new()),
92 seed
93 ));
94
95 unwrap!(spawner.spawn(net_task(stack)));
96
97 //control.start_ap_open("cyw43", 5).await;
98 control.start_ap_wpa2("cyw43", "password", 5).await;
99
100 // And now we can use it!
101
102 let mut rx_buffer = [0; 4096];
103 let mut tx_buffer = [0; 4096];
104 let mut buf = [0; 4096];
105
106 loop {
107 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
108 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
109
110 control.gpio_set(0, false).await;
111 info!("Listening on TCP:1234...");
112 if let Err(e) = socket.accept(1234).await {
113 warn!("accept error: {:?}", e);
114 continue;
115 }
116
117 info!("Received connection from {:?}", socket.remote_endpoint());
118 control.gpio_set(0, true).await;
119
120 loop {
121 let n = match socket.read(&mut buf).await {
122 Ok(0) => {
123 warn!("read EOF");
124 break;
125 }
126 Ok(n) => n,
127 Err(e) => {
128 warn!("read error: {:?}", e);
129 break;
130 }
131 };
132
133 info!("rxd {}", from_utf8(&buf[..n]).unwrap());
134
135 match socket.write_all(&buf[..n]).await {
136 Ok(()) => {}
137 Err(e) => {
138 warn!("write error: {:?}", e);
139 break;
140 }
141 };
142 }
143 }
144}