aboutsummaryrefslogtreecommitdiff
path: root/embassy-net-driver-channel/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-net-driver-channel/README.md')
-rw-r--r--embassy-net-driver-channel/README.md96
1 files changed, 96 insertions, 0 deletions
diff --git a/embassy-net-driver-channel/README.md b/embassy-net-driver-channel/README.md
new file mode 100644
index 000000000..dd90e7ad2
--- /dev/null
+++ b/embassy-net-driver-channel/README.md
@@ -0,0 +1,96 @@
1# embassy-net-driver-channel
2
3This crate provides a toolkit for implementing [`embassy-net`](https://crates.io/crates/embassy-net) drivers in a
4higher level way than implementing the [`embassy-net-driver`](https://crates.io/crates/embassy-net-driver) trait directly.
5
6The `embassy-net-driver` trait is polling-based. To implement it, you must write the packet receive/transmit state machines by
7hand, and hook up the `Waker`s provided by `embassy-net` to the right interrupt handlers so that `embassy-net`
8knows when to poll your driver again to make more progress.
9
10With `embassy-net-driver-channel`
11
12## A note about deadlocks
13
14When implementing a driver using this crate, it might be tempting to write it in the most straightforward way:
15
16```rust,ignore
17loop {
18 // Wait for either..
19 match select(
20 // ... the chip signaling an interrupt, indicating a packet is available to receive, or
21 irq_pin.wait_for_low(),
22 // ... a TX buffer becoming available, i.e. embassy-net wants to send a packet
23 tx_chan.tx_buf(),
24 ).await {
25 Either::First(_) => {
26 // a packet is ready to be received!
27 let buf = rx_chan.rx_buf().await; // allocate a rx buf from the packet queue
28 let n = receive_packet_over_spi(buf).await;
29 rx_chan.rx_done(n);
30 }
31 Either::Second(buf) => {
32 // a packet is ready to be sent!
33 send_packet_over_spi(buf).await;
34 tx_chan.tx_done();
35 }
36 }
37}
38```
39
40However, this code has a latent deadlock bug. The symptom is it can hang at `rx_chan.rx_buf().await` under load.
41
42The reason is that, under load, both the TX and RX queues can get full at the same time. When this happens, the `embassy-net` task stalls trying to send because the TX queue is full, therefore it stops processing packets in the RX queue. Your driver task also stalls because the RX queue is full, therefore it stops processing packets in the TX queue.
43
44The fix is to make sure to always service the TX queue while you're waiting for space to become available in the TX queue. For example, select on either "tx_chan.tx_buf() available" or "INT is low AND rx_chan.rx_buf() available":
45
46```rust,ignore
47loop {
48 // Wait for either..
49 match select(
50 async {
51 // ... the chip signaling an interrupt, indicating a packet is available to receive
52 irq_pin.wait_for_low().await;
53 // *AND* the buffer is ready...
54 rx_chan.rx_buf().await
55 },
56 // ... or a TX buffer becoming available, i.e. embassy-net wants to send a packet
57 tx_chan.tx_buf(),
58 ).await {
59 Either::First(buf) => {
60 // a packet is ready to be received!
61 let n = receive_packet_over_spi(buf).await;
62 rx_chan.rx_done(n);
63 }
64 Either::Second(buf) => {
65 // a packet is ready to be sent!
66 send_packet_over_spi(buf).await;
67 tx_chan.tx_done();
68 }
69 }
70}
71```
72
73## Examples
74
75These `embassy-net` drivers are implemented using this crate. You can look at them for inspiration.
76
77- [`cyw43`](https://github.com/embassy-rs/embassy/tree/main/cyw43) for WiFi on CYW43xx chips, used in the Raspberry Pi Pico W
78- [`embassy-usb`](https://github.com/embassy-rs/embassy/tree/main/embassy-usb) for Ethernet-over-USB (CDC NCM) support.
79- [`embassy-net-w5500`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-w5500) for Wiznet W5500 SPI Ethernet MAC+PHY chip.
80- [`embassy-net-esp-hosted`](https://github.com/embassy-rs/embassy/tree/main/embassy-net-esp-hosted) for using ESP32 chips with the [`esp-hosted`](https://github.com/espressif/esp-hosted) firmware as WiFi adapters for another non-ESP32 MCU.
81
82
83## Interoperability
84
85This crate can run on any executor.
86
87
88## License
89
90This work is licensed under either of
91
92- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
93 http://www.apache.org/licenses/LICENSE-2.0)
94- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
95
96at your option.