---
title: "[Recuration] w5500-rs Rust Support for W5500"
url: "https://maker.wiznet.io/josephsr/projects/%5Brecuration%5D-w5500-rs-rust-support-for-w5500/"
markdown_url: "https://maker.wiznet.io/josephsr/projects/%5Brecuration%5D-w5500-rs-rust-support-for-w5500/md"
type: "UCC: User Created Content"
author: "Alex Martens"
author_url: "https://maker.wiznet.io/eric_g/projects/w5500-rs-mqtt%2C-tls/"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Alex Martens"
original_url: "https://maker.wiznet.io/eric_g/projects/w5500-rs-mqtt%2C-tls/"
license: "MIT license (MIT)"
published: "2023-09-04"
language: "en"
tags: ["Rust"]
hardware: ["WIZnet W5500"]
likes: 0
views: 664
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# [Recuration] w5500-rs Rust Support for W5500

> Embedded rust support for the Wiznet W5500 SPI internet offload chip.

Original author: Alex Martens (source: https://maker.wiznet.io/eric_g/projects/w5500-rs-mqtt%2C-tls/)

## Components

- **WIZnet W5500** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500))
- Software: **Mozilla Foundation Rust Programming Language**: Rust

WIZnet parts: W5500 ([Datasheet](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/datasheet?utm_source=maker&utm_medium=project&utm_campaign=w5500), [product hub](https://maker.wiznet.io/products/w5500/))

## Documents and links

- [w5500-rs](https://github.com/newAM/w5500-rs) (code): w5500 Rust Support

## Article

Embedded rust support for the [Wiznet W5500](https://www.wiznet.io/product-item/w5500/) SPI internet offload chip.

- [`w5500-ll`](https://github.com/newAM/w5500-rs/tree/main/ll) contains low-level drivers, register setters & getters.

- [`w5500-hl`](https://github.com/newAM/w5500-rs/tree/main/hl) contains higher-level drivers.

- [`w5500-regsim`](https://github.com/newAM/w5500-rs/tree/main/regsim) contains a simulation of the [`w5500-ll`](https://github.com/newAM/w5500-rs/tree/main/ll) `Registers` trait.

- Other crates contain protocol implementations.
  - [`w5500-dhcp`](https://github.com/newAM/w5500-rs/tree/main/dhcp)
  - [`w5500-dns`](https://github.com/newAM/w5500-rs/tree/main/dns)
  - [`w5500-mqtt`](https://github.com/newAM/w5500-rs/tree/main/mqtt)
  - [`w5500-sntp`](https://github.com/newAM/w5500-rs/tree/main/sntp)
  - [`w5500-tls`](https://github.com/newAM/w5500-rs/tree/main/tls)

functions in the W5500-LL library:

W5500::reset(&mut self): Resets the W5500 chip.
W5500::configure(&mut self, config: &Configure) -> Result&lt;(), Error>: Configure Ethernet settings. You can set IP address, subnet mask, gateway address and MAC address.
W5500::set_mac(&mut self, mac: [u8; 6]): Sets the MAC address.
W5500::set_ip(&mut self, ip: IpAddress): Sets the IP address.
W5500::set_subnet_mask(&mut self, mask: IpAddress): Sets the subnet mask.
W5500::set_gateway_ip(&mut self, gateway: IpAddress): Sets the gateway address.

functions in the W5500-HL library:

W5500::set_mode(&mut self, mode: SocketMode) -> Result&lt;(), Error>: Sets the socket mode. Usually choose TCP or UDP.
Socket::new(w5500: &mut W5500) -> Result&lt;Socket, Error>: Creates a new socket.
Socket::listen(&mut self, port: u16) -> Result&lt;(), Error>: Listens for a connection on the specified port.
Socket::connect(&mut self, remote_ip: IpAddress, remote_port: u16) -> Result&lt;(), Error>: Connect to the remote host.
Socket::send(&mut self, data: &[u8]) -> Result&lt;usize, Error>: Send data.
Socket::receive(&mut self, buffer: &mut [u8]) -> Result&lt;usize, Error>: Receive data.

functions in the W5500-RegSim library:

Simulator::new() -> Simulator: Creates a simulator instance.
Simulator::set_mac(&mut self, mac: [u8; 6]): Sets the MAC address.
Simulator::set_ip(&mut self, ip: [u8; 4]): Sets the IP address.
Simulator::set_subnet_mask(&mut self, mask: [u8; 4]): Sets the subnet mask.
Simulator::set_gateway_ip(&mut self, gateway: [u8; 4]): Sets the gateway address.

example

```plaintext
use w5500_ll::{W5500, Configure, IpAddress, Error};
use std::error::Error as StdError;
use std::thread::sleep;
use std::time::Duration;

fn main() -> Result<(), Box<dyn StdError>> {
    // SPI 및 다른 하드웨어 설정을 초기화합니다.
    let spi = setup_spi(); // 본 예제에서는 실제 SPI 설정을 가정합니다.
    let cs = setup_cs();   // CS 핀 설정
    let reset = setup_reset(); // Reset 핀 설정

    // W5500을 초기화합니다.
    let mut w5500 = W5500::new(spi, cs, reset);
    w5500.configure(|c| {
        c.mac_address([0x00, 0x01, 0x02, 0x03, 0x04, 0x05])
         .ip_address(IpAddress::new(192, 168, 1, 100))
         .subnet_mask(IpAddress::new(255, 255, 255, 0))
         .gateway_ip(IpAddress::new(192, 168, 1, 1))
    })?;

    // TCP 서버 소켓을 엽니다.
    let server_socket = w5500.socket(0, Tcp)?;
    server_socket.listen(80)?;

    loop {
        if let Ok(mut client_socket) = server_socket.accept() {
            // 클라이언트와 연결된 경우, 데이터를 수신하고 다시 보냅니다.
            let mut buffer = [0u8; 512];
            let bytes_received = client_socket.receive(&mut buffer)?;
            if bytes_received > 0 {
                // 수신한 데이터를 화면에 출력
                println!("Received: {:?}", &buffer[..bytes_received]);

                // 클라이언트에게 데이터를 다시 보냅니다.
                client_socket.send(&buffer[..bytes_received])?;
            }
        }
        
        // 간격을 두고 루프를 실행 (실제로는 무한 루프에서 종료 조건을 설정해야 함)
        sleep(Duration::from_secs(1));
    }
}
```

---

Source: https://maker.wiznet.io/josephsr/projects/%5Brecuration%5D-w5500-rs-rust-support-for-w5500/
