---
title: "Rust-based TCP socket communication in W5500-EVB-Pico (with embassy)"
url: "https://maker.wiznet.io/rena/projects/rust-based-implementation-of-tcp-socket-communication-in-w5500-evb-pico-with-embassy/"
markdown_url: "https://maker.wiznet.io/rena/projects/rust-based-implementation-of-tcp-socket-communication-in-w5500-evb-pico-with-embassy/md"
type: "WCC: WIZnet Created Content"
author: "rena"
author_url: "https://maker.wiznet.io/rena/"
published: "2023-07-19"
language: "en"
tags: ["W5500"]
hardware: ["WIZnet W5500-EVB-Pico"]
likes: 1
views: 5999
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Rust-based TCP socket communication in W5500-EVB-Pico (with embassy)

> I found out about a Rust-based framework called embassy, and since this framework provides the W5500-EVB-Pico library and examples, I deci

## Components

- **WIZnet W5500-EVB-Pico** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/w5500-evb-pico))

## Article

## **Introduction**

I found out about a Rust-based framework called embassy, ​​and since this framework provides the W5500-EVB-Pico library and examples, I decided to test it out.

Last June, I introduced the embassy-net-w5500 project.

- <https://maker.wiznet.io/rena/projects/embassy-net-w5500/>

When I searched for the keyword, the website of the embassy came up. So instead of searching for embassy, ​​you can find the page right away by searching for `embassy rust`.

An organization called [embassy-rs](https://github.com/embassy-rs) has been formed on Github, and updates are actively being made.

In this article, configure the environment using W5500-EVB-Pico and run the TCP socket communication example.

## **Prerequisite**

- Linux environment
  - Using Virtual Machine

- W5500-EVB-Pico

- Extra Pico boards of any kind
  - Used as a picoprobe
  - We used WizFi360-EVB-Pico here.

- Breadboard

- Hercules Tool
  - Used for socket communication test

## **Development environment**

I tried to use it in the WSL2 environment, but it was done in a VM environment due to USB port problems.

There was a way to enable USB ports in WSL, but I didn't want to touch it, so I used a VM I had configured.

VirtualBox (free) or VMware (paid) programs allow you to pull hardware devices connected to the Host Windows into a VM.

![](https://blog.kakaocdn.net/dn/lGlbM/btsn9ayt3Vz/h6rBUHsscwDanLFLZ7Q5z1/img.png)

## **Ubuntu 20.04 VM**

When checking with the `lsusb` command, W5500-EVB-Pico is recognized as follows.

![](https://blog.kakaocdn.net/dn/XQZzk/btsn7wCl3YQ/UjReOyCcmCxysI0gH8NFbk/img.png)

For reference, after installing `picoprobe.uf2` into Pico board, the recognized name changes slightly as follows.

![](https://blog.kakaocdn.net/dn/kOYEK/btsn7OJsdAq/Crx5Xr7I1CECd3Ra2llkc1/img.png)

![](https://blog.kakaocdn.net/dn/NIrYn/btsn7t6Hdab/aSCVEWkHHOBMFYobGFrwgk/img.png)

## **Picoprobe configuration**

Referring to the official guide document, I connected the board as follows.

- **Official Pico Guide link: **[**https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf**](https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf)

![](https://blog.kakaocdn.net/dn/bgAV4v/btsn3jQ6pGf/AlWOdkug6ZxTA7jVZzVee1/img.png)

![](https://blog.kakaocdn.net/dn/dIwUzA/btsn3kJhF6U/gc9BZ0sNvpF4xO3K9G3uSK/img.png)

### **Build and flash picoprobe**

- It seems to be possible to omit because the already built image is provided, but I proceeded directly.
  - <https://github.com/raspberrypi/picoprobe>

If Toolchain is not installed, install the package first.

```plaintext
$ sudo apt update
$ sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential
```

Clone the picoprobe repository and build it.

The commands below are referenced from the Raspberry Pi Pico official guide document.

```plaintext
cd ~/pico
git clone https://github.com/raspberrypi/picoprobe.git 
cd picoprobe
mkdir build 
cd build
cmake ..
make -j4
```

## **Cargo**

Cargo is the official package manager for the Rust, serving as the build system and package manager for the Rust project.

It seems to be considered to **include** the same role as Python's pip.

The official guide explains the development environment configuration and test method well.

- [The Cargo Book - Installation](https://doc.rust-lang.org/cargo/getting-started/installation.html#install-rust-and-cargo)

The environment setup was carried out according to the guide.

### **Install Rust and Cargo**

```plaintext
curl https://sh.rustup.rs -sSf | sh
```

![](https://blog.kakaocdn.net/dn/yZi6C/btsobaSkWdg/Eop9VhHwUFRHBb00aQkETk/img.png)

[**First Steps with Cargo**](https://doc.rust-lang.org/cargo/getting-started/first-steps.html#first-steps-with-cargo)

After installing Cargo, proceed with a simple operation test.

![](https://blog.kakaocdn.net/dn/CBEiM/btsobbcEiNC/kUYaa3vyFhWLDhTdFX18O1/img.png)

![](https://blog.kakaocdn.net/dn/tsQFC/btsn3kCuNCQ/OlAyyXtB0ZWUI0ngKGG4ok/img.png)

## **Embassy**

The official website of the Embassy also serves as a guide, so we proceeded accordingly.

: <https://embassy.dev/book/dev/getting_started.html>

## **Prerequisite**

Here's what you need:

![](https://blog.kakaocdn.net/dn/btSCZR/btsn7ukepz3/xtwwicpKDttgfVRKj6cEk1/img.png)

### **rustup**

<https://rustup.rs/>

```plaintext
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

### **probe-run**

```plaintext
sudo apt install -y libusb-1.0-0-dev libudev-dev
```

```plaintext
cargo install probe-rs-cli
```

![](https://blog.kakaocdn.net/dn/GLEqF/btsoeH3mnEj/h3nZdmCfvwraEFzl2k5bF1/img.png)

```plaintext
source "$HOME/.cargo/env"
```

## **embassy download**

Now, let's download the embassy source code and run the example.

```plaintext
git clone https://github.com/embassy-rs/embassy
cd embassy
```

### **Blinky**

```plaintext
cd examples/rp
cargo run --release --bin blinky
# This takes a few minutes
```

![](https://maker.wiznet.io/upload/ckeditor5/41694456%5F1689837881%2Epng)

**There is a problem that Cargo does not recognize the probe board here.**

It wasn't easy to solve, so I searched quite a bit, and there seemed to be quite a few people experiencing similar problems to me, but I couldn't find a clear solution.

As a result, Github Issues and blog posts were very helpful.

The blog post referenced the middle content.

- [Getting started with rust on a Raspberry Pi Pico](https://reltech.substack.com/p/getting-started-with-rust-on-a-raspberry)

- [Github - picoprobe issue page](https://github.com/raspberrypi/picoprobe/issues/48#issuecomment-1336719133)

Open two terminals and run:

![](https://blog.kakaocdn.net/dn/bkXSil/btsn6NdiOdF/nPwV1h4trcxADXX4cLzd10/img.png)

**Terminal 1**

```plaintext
$ cd ~/pico/openocd
$ src/openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -s tcl -c "adapter speed 5000"
```

![](https://blog.kakaocdn.net/dn/FJN52/btsn7OW2BY4/F0a0odMyZRotnaKF8Qp50k/img.png)

**Terminal 2**

```plaintext
$ cd ~/pico/rp2040-project-template
$ gdb-multiarch -q -ex "target extended-remote :3333" target/thumbv6m-none-eabi/debug/rp2040-project-template
```

You can load and run it using the following command in gdb.

```plaintext
(gdb)

load
continue
```

Up to this point, we have verified the operation of the board connection.

Back in embassy, ​​run the example in the same way.

We started with the basic example, the blinky example.

```plaintext
cd ~/pico/cargo/embassy/examples/rp
cargo run --release --bin blinky
```

![](https://blog.kakaocdn.net/dn/bQxgwg/btsobIBuOqZ/rLZ8XFJfrMrJAfQ4aHSJO1/img.png)

It works! :)

## **W5500 example**

The TCP server and client examples using the Ethernet function of W5500-EVB-Pico were executed and tested.

### **ethernet_w5500_tcp_server**

First, the TCP server application test was conducted.

I modified and referenced the following in the application code.

- Modify mac address to identify

- IP is set to be assigned by DHCP

- LED is set to turn on when TCP connection is established

![](https://blog.kakaocdn.net/dn/9mspe/btsn7NRldsn/xq6Snrnokhl4XJjrT20q41/img.png)

```plaintext
$ vi src/bin/ethernet_w5500_tcp_server.rs

let mac_addr = [0x00, 0x08, 0xdc, 0x01, 0x02, 0x03];

$ **cargo run --release --bin ethernet_w5500_tcp_server**
```

![](https://blog.kakaocdn.net/dn/bRTrA3/btsn7LeWDxb/jiITzmaQmKRk6wq9BlI6V0/img.png)

![](https://blog.kakaocdn.net/dn/bf0Xiu/btsoezqK7EG/K8j6FVeWLq45QeFSISlRq1/img.png)

![](https://blog.kakaocdn.net/dn/dBd8kj/btsn1JJeBQX/Swyig3cp0F3XhLbDRYFuSk/img.png)

I checked the MAC address and assigned IP address on the router to which the board is connected as below. (192.168.50.100, 00-08-dc-01-02-03)

![](https://blog.kakaocdn.net/dn/bGEvdx/btsoezj86br/UVKanwrqXhtTbpOYlLd8cK/img.png)

By default, it operates in Loopback mode.

![](https://blog.kakaocdn.net/dn/msaFQ/btsn7LMK1BF/ayIMZjTt63RS0rMIowvcY0/img.png)

### **ethernet_w5500_tcp_client**

The following is the TCP client application.

Execute after modifying the code as follows.

- `host_addr` I set the value to the Host PC IP address value of my environment.

```plaintext
$ vi src/bin/ethernet_w5500_tcp_client.rs

        let **host_addr** = embassy_net::Ipv4Address::from_str("192.168.50.81").unwrap();

$ cargo run --release --bin ethernet_w5500_tcp_client
```

![](https://blog.kakaocdn.net/dn/MWx6J/btsn7J2upJv/Ftg7smi4JbuhcGkwb0XMyk/img.png)

![](https://blog.kakaocdn.net/dn/rr7zI/btsn7ruiqio/Kc3tcfd1vm96knxIJ5Qvx1/img.png)

It is implemented so that the client is executed immediately and sends a hello world message periodically.

Here, the second run stopped and it seemed that the board was disconnected.

In this case, use reset to recognize the board again.

### **ethernet_w5500_multisocket**

Lastly, it is a multiple socket operation test.

```plaintext
$ cargo run --release --bin ethernet_w5500_multisocket
```

![](https://blog.kakaocdn.net/dn/3KhO7/btsobJmQziZ/rkFqJKmKw9AGcMyrb49DO1/img.png)

![](https://blog.kakaocdn.net/dn/0Su8p/btsoa96X1vb/ykNMBkSWMexwLVKkZqMOd0/img.png)

**⚠️ Log of cmsis-dap unrecognized issue**

During the test, there are cases in which OpenOCD suddenly does not recognize the USB. At this time, if you use the lsusb command to check, the device is recognized.

![](https://blog.kakaocdn.net/dn/bBIVJw/btsn6HD8q5j/9X2WgQObuZIVCVVATZbFFK/img.png)

![](https://blog.kakaocdn.net/dn/AGItP/btsn9TwvzLX/z6unO9ZhsMR33zIPqNw3v0/img.png)

If openocd does not work even though it is recognized as the same when checked with lsusb, after resetting the board, connect the USB to the VM again in VirtualBox.

Reset should be done on the side of the debug board connected to USB power. It is reset by pressing the RUN button.

Perhaps there is an unstable part because of using VirtualBox, but if there is a chance to test it in a real Linux environment later, I will try it.

## **Concluding**

So far, I have tested TCP socket communication with the W5500-EVB-Pico board based on the Rust environment, and it works well.

Now that the basic operation has been tested, I would like to proceed with a test that connects something by attaching a function.

## **Reference**

W5500-EVB-Pico example

<https://github.com/embassy-rs/embassy-net-w5500/tree/main/examples>

---

Source: https://maker.wiznet.io/rena/projects/rust-based-implementation-of-tcp-socket-communication-in-w5500-evb-pico-with-embassy/
