---
title: "Ethernet controller W5300. Initialization. ping"
url: "https://maker.wiznet.io/eric_g/projects/ethernet-controller-w5300-initialization-ping/"
markdown_url: "https://maker.wiznet.io/eric_g/projects/ethernet-controller-w5300-initialization-ping/md"
type: "UCC: User Created Content"
author: "Donets Anton"
author_url: "https://blog.radiotech.kz/stm32/ethernet-kontroller-w5300-initsializatsiya-ping/"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Donets Anton"
original_url: "https://blog.radiotech.kz/stm32/ethernet-kontroller-w5300-initsializatsiya-ping/"
license: "Apache License 2.0 (Apache-2.0)"
published: "2023-01-02"
language: "en"
hardware: ["WIZnet W5300"]
likes: 4
views: 667
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Ethernet controller W5300. Initialization. ping

> Today is more interesting. We will try to initialize the W5300 and ping.

Original author: Donets Anton (source: https://blog.radiotech.kz/stm32/ethernet-kontroller-w5300-initsializatsiya-ping/)

## Components

- **WIZnet W5300** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5300))

## Article

Original link is <https://blog.radiotech.kz/stm32/ethernet-kontroller-w5300-initsializatsiya-ping/>

and posted in the Czech language.

What we need for this:
- a board with W5300 (own-made or ready-made, for example WIZ830MJ);
- control module (I have a STM32F4-Discovery handkerchief);
- development environment, debugger, compiler, depending on what you have chosen as a control module. (I have Keil. They have a free version with a code limit of up to 32 KB. Personally, this is more than enough for me. :-))) );
- sniffer. This item is not required, but we will need it later. Sniffer - a program that will show us packets on the network. I like WireShark. By the way, during the installation, WireShark will ask permission to install WinPcap. Agree. If WinPcap does not ask you to install it, then you will have to install it separately.

### **Scheme**

We access the W5300 memory using direct addressing and a 16-bit data bus. I will not draw a schematic diagram, I will simply describe which GPIOs go to which legs of the WIZ830MJ.

WIZ830MJ STM32F4-Discovery
Address bus ADDR [0:9] GPIOE [0:9]
Data bus DATA [0:15] GPIOD [0:15]
CS GPIOB 13
RD GPIOB 14
WR GPIOB 15
RES GPIOB 11

### **Create a project in Keil**

I will describe how to create a project in Keil, in case this may cause difficulties.

Project -> New uVision Project

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/04e3ee.jpg)

Choosing our microcontroller:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/33e0a4.jpg)

We agree to copy the startup file:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/3e469e.jpg)

Copy the CMSIS folder to the project folder, which in turn contains the files:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/b93fe1.jpg)

These files can be taken from [STM32CubeF4](http://www.st.com/web/en/catalog/tools/PF259243#)
And the system_stm32f4xx.c file is generated using the STM32F4xx_Clock_Configuration Excel [file](http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1533/PF257927) . Download, unpack and run the Excel file (UPD at the time of writing this was the easiest way to set up clocking, now it is recommended to use the CubeMX code generator).
Activate the macro by clicking the Options button.

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/0288bf.jpg)

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/3446d7.jpg)

We set the frequency value at the output and at the input, press the Run button, in the process we select the external HSE clock source. Next, click Generate and in the same folder in which the Excel file is located, we find system_stm32f4xx.c, generated for our frequency and for our divisor coefficients.

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/fad802.jpg)

Adding a CMSIS group to our project:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/5906df.jpg)

Add a sish file from the CMSIS folder to the CMSIS group:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/eac137.jpg)

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/e55d6f.jpg)

Now we create a new file, when saving we call it main.c.

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/cfa419.jpg)

Create a main group, add the file we just created to it.
The project structure should now look like this:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/e32b06.jpg)

Immediately in the project options, we make the following changes.
We set the frequency to 30 MHz:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/b04d36.jpg)

Set the Create HEX checkbox, otherwise we will not be able to flash.

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/b4fabd.jpg)

Select the ST-Link debugger, Settings -> Debug Port should be SW.

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/01f83d.jpg)

### **Define**

Further, already in the program, we prescribe the addresses of the registers of the ethernet controller, someone does it in the header, someone in the main file, more correctly, of course, in the header.
In general, we look in the datasheet for the W5300, where the addresses of the registers are written on pages 25 - 44. For now, we only need these registers:

#define MR 0x0000 //Mode Register

#define SHAR 0x0008 // MAC address

#define SHAR2 0x000A

#define SHAR4 0x000C

#define GAR 0x0010 // IP address

#define GAR2 0x0012

#define SUBR 0x0014 // subnet mask

#define SUBR2 0x0016

#define SIPR 0x0018 // Source IP address for PPPoE

#define SIPR2 0x001A

### **STM32F407 initialization**

///////////////////////////////////////// Initialization ///////// ////////////////////////////////

**void **Init ( **void **)

{ // ports

RCC- > AHB1ENR |= RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOEEN; // enable clocking of ports B, E and D

GPIOB- > ODR= *0x0000F800 *; // set all WRC and Res to 1

// exits

GPIOB- > MODER= *0x55400280 *; // 0-2(WRC wiznet_1), 3(RST wiznet_1)

GPIOD- > MODER= *0x55555555 *; // Data wiznet_1

GPIOE- > MODER = *0x00055555 *; // Adr wiznet 1

// pull up to the ground

GPIOD- > PUPDR= *0xAAAAAAAA *;

// speed 50 MHz

GPIOB- > OSPEEDR= *0xAA8000C0 *;

GPIOD- > OSPEEDR = *0xAAAAAAAA *;

GPIOE- > OSPEEDR= *0x000AAAAA *;

//////////////////timer/////////////////////////////// //////////////////////////////////

RCC- > APB1ENR |=RCC_APB1ENR_TIM6EN; // enable timer clock

TIM6- > PSC= *0x0000001E *; //30, timer frequency 1 MHz,

TIM6- > CR1|=TIM_CR1_CEN; // turn on the timer

TIM6- > EGR|=TIM_EGR_UG; // call update event

__NOP () ;

}

### **W5300 Write, Read Register Functions**

The datasheet contains the following diagram explaining to us the process of writing to the W5300 registers:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/8fc319.jpg)

In accordance with it, we write the code:

// write to register W5300

**void **WriteReg ( uint16_t Addr, uint16_t Data )

{

GPIOE- > ODR=Addr;

GPIOD- > ODR=Data;

GPIOB- > ODR= *0x00005800 *; // WRC

__NOP () ;

GPIOB- > ODR= *0x0000F800 *; // WRC

GPIOE- > ODR= *0x0000 *;

GPIOD- > ODR= *0x0000 *;

}

Exactly the same diagram is given for reading from registers.

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/e82af1.jpg)

// read from register W5300

uint16_t ReadReg ( uint16_t Addr )

{

uint16_tdata;

GPIOD- > MODER = *0x00000000 *; // make data port an input

GPIOE- > ODR=Addr;

GPIOB- > ODR= *0x00009800 *; // WRC

data=GPIOD- > IDR;

GPIOB- > ODR= *0x0000F800 *; // WRC

GPIOE- > ODR=Addr;

GPIOD- > MODER= *0x55555555 *; // make port D an output

**return **( data ) ;

}

Here I think, nothing complicated.

### **Delay function**

// delay

**void **delay_us ( uint16_t us )

{

TIM6- > CR1|=TIM_CR1_CEN; // turn on the timer

TIM6- > EGR|=TIM_EGR_UG; // call update event

**while **(( TIM6- > CNT ) &lt; us ) ;

TIM6- > CR1 &= ~TIM_CR1_CEN; // turn off the timer

TIM6- > CNT= 0 ; // reset the timer

}

W5300 initialization and reset signal

What should be the duration of the reset signal is also indicated in the datasheet:

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/8a0d7f.jpg)

Next ... We write down the MAC address, IP address, subnet mask of our device, IP address of the main gateway:

int main ( **void **)

{

init () ;

// signal reset to Wiznet

GPIOB- > ODR &= ~GPIO_ODR_ODR_11;

delay_us (( uint16_t ) *0x0005 *) ; //reset signal must be at least 2us

GPIOB- > ODR |= GPIO_ODR_ODR_11;

delay_us (( uint16_t ) *0x00C8 *) ; //after reset time from 50us to 10ms

//MAC address W5300: 00.08.220.17.02.03

WriteReg ( SHAR, *0x0008 *) ;

WriteReg ( SHAR2, *0xDC11 *) ;

WriteReg ( SHAR4, *0x0203 *) ;

// IP address of the default gateway

WriteReg ( GAR, *0xC0A8 *) ; //192.168.70.1

WriteReg ( GAR2, *0x4601 *) ;

//subnet mask indicates that the class C IP address is 255.255.255.0

WriteReg ( SUBR, *0xFFFF *) ;

WriteReg ( SUBR2, *0xFF00 *) ;

// W5300 IP address

WriteReg ( SIPR, *0xC0A8 *) ; //192.168.70.12

WriteReg ( SIPR2, *0x460C *) ;

**while **( 1 ) {}

}

Compiling, flashing.

### **ping**

Ping went and our ICMP packets too!

![](http://blog.radiotech.kz/wp-content/uploads/2017/07/e9d967.jpg)

---

Source: https://maker.wiznet.io/eric_g/projects/ethernet-controller-w5300-initialization-ping/
