Skip to content

Ethernet controller W5300. Initialization. ping

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

WIZnet

Published January 02, 2023 · © Apache License 2.0 (Apache-2.0)

Original author: Donets Anton · CzechOriginal source (new tab)

Ethernet controller W5300. Initialization. ping

Components

Hardware components

Project description

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

Create a project in Keil

Choosing our microcontroller:

Create a project in Keil

We agree to copy the startup file:

Create a project in Keil

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

Create a project in Keil

These files can be taken from  STM32CubeF4
And the system_stm32f4xx.c file is generated using the STM32F4xx_Clock_Configuration Excel  file . 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.

Create a project in Keil
Create a project in Keil

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.

Create a project in Keil

Adding a CMSIS group to our project:

Create a project in Keil

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

Create a project in Keil
Create a project in Keil

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

Create a project in Keil

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

Create a project in Keil

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

Create a project in Keil

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

Create a project in Keil

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

Create a project in Keil

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:

W5300 Write, Read Register Functions

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.

W5300 Write, Read Register Functions
// 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 ) < 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:

Ethernet controller W5300. Initialization. ping, Delay function

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!

Ethernet controller W5300. Initialization. ping, ping

Comments

Comments