Skip to content

Ethernet controller W5300. IPRAW mode

IPRAW is data transfer using a layer lower than TCP and UDP.

WIZnet

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

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

Ethernet controller W5300. IPRAW mode

Components

Hardware components

x 1


Project description

Original link is https://blog.radiotech.kz/stm32/ethernet-kontroller-w5300-ipraw-rezhim/

and posted in the Czech language.

 

IPRAW is data transfer using a layer lower than TCP and UDP. IPRAW supports protocols such as ICMP (0x01), IGMP (0x02) which can be specified by protocol number (Sn_PROTOR). As I warn you last time, here we are considering an arbitrary package as an example. Of course, you choose your protocol.

What do you need:

— WIZ830MJ;
— STM32F4-Discovery;
— Keil;
- Wireshark (+ WinPcap);
— Colasoft Packet Builder.

The WIZ830MJ connection table to STM32F4-Discovery and part of the register addresses were given in the article  Part 3. W5300 Ethernet Controller. Initialization. Ping
Additional register addresses are listed in  Part 4: W5300 Ethernet Controller. MACRAW mode

Initializing STM32F4 and W5300

The STM32F4 initialization (GPIO and TIM6) and W5300 initialization (SHAR, GAR, SUBR, SIPR) procedures were shown in  Part 3. W5300 Ethernet Controller. Initialization. ping

socket initialization

We start, as usual, by opening a socket. I chose socket 0, you can choose any of the 8 sockets that will smile at you.
Before initializing the socket, you must specify the MAC address and IP address of the destination:

// Destination MAC address
WriteReg ( S0_DHAR, 0xD027 ) ; 
WriteReg ( S0_DHAR2, 0x8885 ) ;
WriteReg ( S0_DHAR4, 0x4D1D ) ;
// destination IP address
WriteReg ( S0_DIPR, 0xC0A8 ) ; 
WriteReg ( S0_DIPR2, 0x460B ) ;

Next, we write the S0_PROTOR register in accordance with the protocol chosen (ICMP - 0x01, IGMP - 0x02, TCP - 0x06, UDP - 0x11). Also, the protocol number must be set before the socket is opened.

do // ///////////// Initialization of the IPRAW socket /////////////////////////////// /////
{
WriteReg ( S0_PROTOR, 0x0008 ) ; // protocol number 
WriteReg ( S0_MR, 0x0003 ) ; // socket in IPRAW mode
WriteReg ( S0_CR, OPEN ) ; // open socket
Dat= ReadReg ( S0_SSR ) ;
}
while (( Dat & 0x00FF ) !=SOCK_IPRAW ) ; // socket 0 open in IPRAW mode? 

Broadcast

First, by reading the S0_TX_FSR2 register, we check for free memory in the W5300 transmitter memory. We open the data recording cycle. In the registers S0_TX_WRSR and S0_TX_WRSR2 we write the size of the data being sent. Attention!!! It must be specified in bytes. Write the SEND command to the command register.

while ( 1 )
{
///////////////////// TRANSFER //////////////////////////////////////////
do Dat= ReadReg ( S0_TX_FSR2 ) ; // check for free memory
while ( Dat < SizeB ) ;
 
do //write sent data
{
WriteReg ( S0_TX_FIFOR, SendData ) ;
SendData++;
}
while ( SendData < SizeW ) ;
WriteReg ( S0_TX_WRSR, 0x0000 ) ; // size of sent data
WriteReg ( S0_TX_WRSR2, SizeB ) ; // size of sent data
WriteReg ( S0_CR, SEND ) ; // sent
delay_us ( 1000 ) ; // just delay between packets
SendData = 0 ;
}

We catch packets on a computer with a sniffer.

Ethernet controller W5300. IPRAW mode, Broadcast

Reception

The received IP packet according to the datasheet has the following structure: 4 bytes IP address of the destination and 2 bytes data size.

Ethernet controller W5300. IPRAW mode, Reception

In order to receive an IPRAW packet, we monitor the S0_RX_RSR2 register, which signals to us that data has been received. We read 4 bytes of the IP address and the size of the data in bytes, we consider how much it will be in words. We read the data into an array and send the RECV command to the S0_CR command register.

while ( 1 )
{
////////////////////////////////////////////////////////////////////////////
if ( ReadReg ( S0_RX_RSR2 ) != 0x0000 ) //received data size is non-zero 
{
Dat = ReadReg ( S0_RX_FIFOR ) ; // read IP address
Dat = ReadReg ( S0_RX_FIFOR ) ;
 
pack_size = ReadReg ( S0_RX_FIFOR ) ; // counted data size
 
// determine how many times to read data
if ( ( pack_size% 2 ) == 0 ) { read_cnt = pack_size/ 2 ; } 
else { read_cnt = ( pack_size+ 1 ) / 2 ; }
 
for ( Cnt= 0 ; Cnt < read_cnt; Cnt++ ) //SizeW read received 
{
Rcv_data [ Cnt ] = ReadReg ( S0_RX_FIFOR ) ;
}
i++; // packet counter
WriteReg ( S0_CR, RECV ) ; // accepted
}
}

We send a package like this using the Packet Builder. To do this, click on the Send button.

Ethernet controller W5300. IPRAW mode, Reception

In the window that appears, select LoopSending, the number is 20 times.

Ethernet controller W5300. IPRAW mode, Reception

In debug mode, we look at the data in Rcv_data. The sniffer shows the same data.

Ethernet controller W5300. IPRAW mode, Reception

Comments

Comments