Skip to content
New issue

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

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

Already on GitHub? # to your account

Adding MACRAW mode support #33

Merged
merged 5 commits into from
Apr 28, 2022

Conversation

ryan-summers
Copy link
Collaborator

@ryan-summers ryan-summers commented Apr 14, 2022

This PR adds in initial MACRAW operation mode support. This allows you to run a software-based network stack (e.g. smoltcp) on socket 0.

The current implementation does not support any type of hybrid model (e.g. TCP/UDP on sockets 1-7, raw on socket 0), but rather allocates the whole RX/TX buffers for socket 0 in raw mode and disables the other sockets.

Using this, raw ethernet frames can be read from and written to the W5500 MAC.

I have used this branch with Smoltcp to implement multiple MQTT clients over TCP to confirm operation.

@kellerkindt
Copy link
Owner

Could you provide an example usage? Not necessary runnable, but enough to grasp how to set it up...

@ryan-summers
Copy link
Collaborator Author

ryan-summers commented Apr 28, 2022

My specific use case is unfortunately closed-source currently, but the essence of these changes is as follows:

The general pattern is to first implement your necessary trait on the w5500::RawDevice. In this case, we want to implement smoltcp's smoltcp::phy::Device so that we can use the W5500 as a mechanism for smoltcp to send and receive ethernet frames:

impl smoltcp::phy::Device for w5500::RawDevice<SPI> {
   // Implementation details
}

Now that the trait is implemented, we can use the w5500::RawDevice for the network interface constructor in smoltcp, which just requires "anything that can send/receive ethernet frames":

// During hardware setup, construct the RawDevice
let external_mac = W5500::UninitializedDevice::new(w5500::bus::FourWire::new(spi, cs))
        .initialize_macraw(mac).unwrap();

// Pass the RawDevice to smoltcp, which then allows smoltcp to be used as in any other application
let interface = smoltcp::iface::InterfaceBuilder::new(external_mac, ...).finalize();

Once you have the smoltcp interface, you can use it however you'd like to make as many UDP/TCP sockets as you'd like. Smoltcp is a software-based network stack, so it's configurable for different operations and extendable (e.g. supports DHCP and maybe DNS in the future)

@kellerkindt
Copy link
Owner

You make it sound as if you plan further PRs? I'd love so see a mention somewhere (maybe in the README) that smoltcp is supported and how to set it up once all PRs are merged.

Re impl smoltcp::phy::Device for w5500::RawDevice wouldn't that violate the orphan rule?

@ryan-summers
Copy link
Collaborator Author

ryan-summers commented Apr 28, 2022

Re impl smoltcp::phy::Device for w5500::RawDevice wouldn't that violate the orphan rule?

Directly as written, yes. That's why I said (implementation details). In reality, there's some synchronization primitives (FIFOs, frame pools) etc. that are required. You can't implement Device on RawDevice directly, but you can make a software proxy that lives between smoltcp and the W5500 that handles synchronization and frame juggling. :) It's non-trivial code and I haven't fully explained it here for brevity.

You make it sound as if you plan further PRs? I'd love so see a mention somewhere (maybe in the README) that smoltcp is supported and how to set it up once all PRs are merged.

As I mentioned above, there's a fair amount more to implementing the smoltcp phy Device trait for just the RawDevice, as there's a lot of other resources that need to be managed. I'm trying to figure out where this code would best live, but I don't think any of it is actually specific to the W5500 - it works for any external PHY/MAC, so I don't think I planned on moving the code into this repo.

@kellerkindt
Copy link
Owner

Fine with me. Feel free to post follow ups anyway :)
Thanks

@kellerkindt kellerkindt merged commit aa5eb1d into kellerkindt:master Apr 28, 2022
@ryan-summers ryan-summers deleted the feature/macraw-support branch April 28, 2022 10:00
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants