---
title: "STM32CubeMX series | Use BearPi hardware SPI to drive W5500 Ethernet module"
url: "https://maker.wiznet.io/WIZnet/projects/stm32cubemx-series-use-bearpi-hardware-spi-to-drive-w5500-ethernet-module/"
markdown_url: "https://maker.wiznet.io/WIZnet/projects/stm32cubemx-series-use-bearpi-hardware-spi-to-drive-w5500-ethernet-module/md"
type: "UCC: User Created Content"
author: "MCUlover666"
author_url: "http://www.mculover666.cn/posts/2656092005/"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "MCUlover666"
original_url: "http://www.mculover666.cn/posts/2656092005/"
published: "2023-02-22"
language: "en"
likes: 4
views: 470
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# STM32CubeMX series | Use BearPi hardware SPI to drive W5500 Ethernet module

> STM32CubeMX series | Use BearPi hardware SPI to drive W5500 Ethernet module

Original author: MCUlover666 (source: http://www.mculover666.cn/posts/2656092005/)

## Article

## **1. Preparation**

## **Hardware preparation**

- Development board

First, you need to prepare a development board, here I am preparing the STM32L4 development board (BearPi):

![](https://maker.wiznet.io/uploads/2021/03/image.png)

- W5500 Ethernet module

Here I use the common Ethernet module W5500:

![](https://maker.wiznet.io/uploads/2021/03/image-1.png)

- [W5500 official website](https://www.wiznet.io/product-item/w5500/)

- [W5500 Chinese Manual iEthernet W5500 Datasheet](https://wizwiki.net/wiki/doku.php?id=products:w5500:datasheet)

## **Software preparation**

- Need to install Keil-MDK and the corresponding package of the chip in order to compile and download the generated code;

- Prepare a serial port debugging assistant, here I am using `Serial Port Utility`;

- Prepare a network debugging assistant, here I am using `sockettool`;

## **2. Generate MDK project**

## **Select chip model**

Open STM32CubeMX, open the MCU selector:

![](https://maker.wiznet.io/uploads/2021/03/image-2-1024x598.png)

Search and select the chip `STM32L431RCT6`:

![](https://maker.wiznet.io/uploads/2021/03/image-3-1024x565.png)

Configure the clock source

- If you choose to use an external high-speed clock (HSE), you need to configure RCC in System Core;

- If the default internal clock (HSI) is used, this step can be skipped;

Here I use an external clock:

![](https://maker.wiznet.io/uploads/2021/03/image-4-1024x573.png)

## **Configure Ethernet module to control GPIO**

There are two GPIOs that need additional configuration for the Ethernet module:

| ETHERNET MODULE PIN NAME | GPIO | EFFECT |
| --- | --- | --- |
| RST | PC9 | Ethernet module hard reset |
| INT | PA0 | Interrupt pin |

The reset pin can be configured as output mode:

![](https://maker.wiznet.io/uploads/2021/03/image-5.png)

The interrupt pin needs to receive the interrupt from the Ethernet module, so the EXTI external interrupt pin needs to be configured:

![](https://maker.wiznet.io/uploads/2021/03/image-6.png)

## **Configure SPI1 interface**

In this experiment, I connected the Ethernet module to the SPI1 interface, and the pin correspondence table is as follows:

> ***It should be noted that the SPI chip select pins are not controlled by hardware SPI peripherals, but are configured as ordinary GPIOs and controlled manually** .*

| ETHERNET MODULE PINS | MCU PIN |
| --- | --- |
| MISO | PA6(SPI1_MISO) |
| MOSI | PA12(SPI1_MOSI) |
| SCS | PA4(SPI1_NSS) |
| SCLK | PA1(SPI1_SCK) |

There are three points to note when configuring the SPI interface:

① Frequency division coefficient;
② CPOL: When the CLK is idle, the level is high or low;
③ CPHA: Sampling on the first clock edge or sampling on the second clock edge;

Next, start to configure the SPI1 peripheral. First, configure the mode and pins of the SPI1 peripheral:

![](https://maker.wiznet.io/uploads/2021/03/image-7-1024x539.png)

Because you chose not to use the hardware SPI peripheral to control the chip select pin, you need to manually configure the chip select pin PA4:

![](https://maker.wiznet.io/uploads/2021/03/image-8.png)

The SPI bus clock given in the W5500 manual is 80Mhz:

| SYMBOL | DESCRIPTION | MIN | MAX | UNITS |
| --- | --- | --- | --- | --- |
| FSCK | SCLK Clock Frequency | – | 80 | MHZ |
| TWH | SCLK High duration | 6 | – | ns |
| TWL | SCLK Low duration | 6 | – | ns |
| TCS | nSCS High duration | 5 | – | ns |

However, it should be noted that the manual clearly indicates that the actual guarantee is at least 33.3Mhz, so for the sake of safety, **the SPI bus clock is configured as 20Mhz in this experiment** :

For CPOL, W5500 supports both modes. Select the LOW mode when idle. The first clock edge is given in the CPHA manual:

![The first clock edge is given in the CPHA manual](https://maker.wiznet.io/uploads/2021/03/image-9-1024x384.png)

In summary, the timing parameters are configured as follows:

## **Configure the serial port**

The development board has a CH340z for serial port, which is connected to USART1.

Next, start the configuration `USART1`:

![](https://maker.wiznet.io/uploads/2021/03/image-11-1024x513.png)

## **Configure the clock tree**

The highest frequency of STM32L4 is 80M, so configure PLL, and finally use `HCLK = 80Mhz`:

![](https://maker.wiznet.io/uploads/2021/03/image-12-1024x572.png)

## **Generate project settings**

![](https://maker.wiznet.io/uploads/2021/03/image-13-1024x531.png)

## **Code generation settings**

Finally set to generate a separate initialization file:

![](https://maker.wiznet.io/uploads/2021/03/image-14-1024x423.png)

## **Generate code**

Click `GENERATE CODE`to generate MDK-V5 project:

![](https://maker.wiznet.io/uploads/2021/03/image-15-1024x160.png)

## **3. Redirect printf function to USART1**

Reference: [\[STM32Cube_09\] Multiple methods to redirect printf function to serial port output](https://lgikdz6ovlmiewdvjiyww77g4i--www-mculover666-cn.translate.goog/posts/2251182441/) .

## **4. Porting W5500 official driver library**

## **4.1. Download the official driver library**

W5500 officially provides ioLibrary v2.0.0. ioLibrary is the Ethernet driver library of the WIZnet chip, which includes drivers and application protocols. This driver (ioLibrary) can be used for the application design of WIZnet TCP/IP chips, such as W5500, W5300, W5200, W5100, W5100S.

There are two download addresses:

- github open source warehouse address: [https://github.com/Wiznet/ioLibrary_Driver](https://translate.google.com/website?sl=zh-CN&tl=en&u=https://github.com/Wiznet/ioLibrary_Driver)

- gitee warehouse address (for faster download speed, the blogger synced to gitee): [https://gitee.com/mculover666/ioLibrary_Driver](https://translate.google.com/website?sl=zh-CN&tl=en&u=https://gitee.com/mculover666/ioLibrary_Driver)

The source code directory structure is shown in the figure:

![](https://maker.wiznet.io/uploads/2021/03/image-16.png)

- Ethernet: SOCKET API interface similar to BSD, and WIZCHIP (W5500 / W5300 / W5200 / W5100 / W5100S) driver

- Internet: Various application layer protocol stacks
  - DHCP client
  - DNS client
  - FTP client
  - FTP server
  - SNMP agent/trap
  - SNTP client
  - TFTP client
  - HTTP server
  - MQTT Client

## **4.2. Add the driver library to the project**

Create a new Hardware/W5500 under the project directory and copy all three folders in the driver library:

![](https://maker.wiznet.io/uploads/2021/03/image-17.png)

Note that **only the files under Ethernet are required,** and **the files in the** other two folders can be added optionally, which will be used in the test later.

Next, add files related to W5500 in the Ethernet directory to the MDK project:

….

Source: [mculover666.cn…](http://www.mculover666.cn/posts/2656092005/)

---

Source: https://maker.wiznet.io/WIZnet/projects/stm32cubemx-series-use-bearpi-hardware-spi-to-drive-w5500-ethernet-module/
