Help me for start with w5500 in udp mode

Hello
I am an electronics student and amateur programmer :frowning_face:
To begin, i want in udp mode Send “Hello world!” to the computer and see it in hercules
I read all the forum and site content but we did not understand anything :cry:

I use the dsPIC33ep256mu814 microcontroller and MPLAB X compiler.

The steps I have taken are:

1- I visited https://github.com/Wiznet/ioLibrary_Driver and downloaded the ioLibrary_Driver-master.zip

2- I added libraries to the project. (wizchip_conf.c & .h / socke.c & .h / w5500.c & .h)

3- in wizchip_conf.c these changes in the library:

void wizchip_cs_select(void) {

TRISFbits.TRISF1 = 0;
PORTFbits.RF1 = 0;

}

//======================================================

void wizchip_cs_deselect(void) {

TRISFbits.TRISF1 = 0;
PORTFbits.RF1 = 1;

}

//======================================================

uint8_t wizchip_spi_readbyte(void) {

    uint8_t temp = 0;	

SPI1BUF = temp;		// write the data out to the SPI peripheral

// Wait for the RX buffer to be full
while(!SPI1STATbits.SPIRBF);

// Read the receive buffer
temp =  SPI1BUF;
SPI1STATbits.SPIROV=0;

return temp;

}

//======================================================

void wizchip_spi_writebyte(uint8_t wb) {

    uint8_t temp;	

SPI1BUF = wb;		// write the data out to the SPI peripheral

// Wait for the TX buffer to empty
    while(SPI1STATbits.SPITBF);

// Read the receive buffer
temp =  SPI1BUF;
SPI1STATbits.SPIROV = 0;  

}

//======================================================

void wizchip_spi_readburst(uint8_t* pBuf, uint16_t len) {

size_t for_i = 0; 

for(for_i = 0 ; for_i < len ; for_i++)
{
pBuf[for_i] = wizchip_spi_readbyte();
}

}

//======================================================

void wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) {

size_t for_i = 0; 

for(for_i = 0 ; for_i < len ; for_i++)
{
wizchip_spi_writebyte(pBuf[for_i]);
}

}

4- Init SPI hardware in main function.

I do not know the next steps :disappointed_relieved:
Please guide me in simple :roll_eyes:
thank you

Hi,

You should not make any changes to the Wiznet code. The option is to create your own w5500.c file with the w5500_init function. You call this once and then you can use the socket functions. See an example below for the Raspberry Pi. You need to provide the static functions with the SPI code for your CPU/SoC.

-Arjan

#include <stdint.h>
#include <stdio.h>
#include <assert.h>

#define _WIZCHIP_ 5500
#include "wizchip_conf.h"

#if defined(BARE_METAL)
 #include "bcm2835_spi.h"
#else
 #include "bcm2835.h"
 #define BCM2835_SPI_CLOCK_MIN	4000			///< 4kHz
 #define BCM2835_SPI_CLOCK_MAX	125000000		///< 125Mhz
#endif

#include "w5x00.h"

extern int bcm2835_prereq(void);
extern void w5x00_netinfo_set(wiz_NetInfo *netinfo, const uint8_t *pMacAddress, const struct ip_info *pIpInfo);
extern void w5x00_netinfo_dump(const wiz_NetInfo *netinfo);

static uint8_t w5500_spi_cs;
static uint16_t w5500_spi_clk_div;

static void w5500_cs_select(void) {
	__sync_synchronize();
	bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE);
	bcm2835_spi_setClockDivider(w5500_spi_clk_div);
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
	__sync_synchronize();
	bcm2835_gpio_clr(w5500_spi_cs);
	__sync_synchronize();
}

static void w5500_cs_deselect(void) {
	__sync_synchronize();
	bcm2835_gpio_set(w5500_spi_cs);
	__sync_synchronize();
}

static void w5500_read_burst(uint8_t *pBuf, uint16_t len) {
	bcm2835_spi_transfern((char*) pBuf, len);
	return;
}

static void w5500_write_burst(uint8_t *pBuf, uint16_t len) {
	bcm2835_spi_writenb((const char*) pBuf, len);
}

static void w5500_write(uint8_t b) {
	bcm2835_spi_writenb((const char*) &b, 1);
}

static uint8_t w5500_read(void) {
	uint8_t rbuf;
	bcm2835_spi_transfern((char*) &rbuf, 1);
	return rbuf;
}

W5X00_Init w5500_init(uint8_t nSpiChipSelect, uint32_t nSpiSpeedHz, const uint8_t *pMacAddress, struct ip_info *pIpInfo) {
	int rc = bcm2835_prereq();

	if (rc < 0) {
		return rc;
	}

	assert(pMacAddress != 0);
	assert(pIpInfo != 0);

	assert(nSpiChipSelect < 32);
	w5500_spi_cs = nSpiChipSelect;

	if ((nSpiSpeedHz < BCM2835_SPI_CLOCK_MIN) || (nSpiSpeedHz > BCM2835_SPI_CLOCK_MAX)) {
		w5500_spi_clk_div = (uint16_t) BCM2835_SPI_CLOCK_DIVIDER_64;
	} else {
		w5500_spi_clk_div = (uint16_t) ((uint32_t) BCM2835_CORE_CLK_HZ / nSpiSpeedHz);
	}

	bcm2835_spi_begin();

	bcm2835_gpio_fsel(w5500_spi_cs, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_set(w5500_spi_cs);

	reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect);
	reg_wizchip_spiburst_cbfunc(w5500_read_burst, w5500_write_burst);
	reg_wizchip_spi_cbfunc(w5500_read, w5500_write);

	wizphy_reset();

	uint8_t bufSize[2][8] = { {8,2,2,2,2,0,0,0},{8,2,2,2,2,0,0,0}};

	if (wizchip_init(bufSize[0], bufSize[1]) != 0) {
		return W5X00_INIT_WIZCHIP_INIT_FAILED;
	}

	do {
		rc = wizphy_getphylink();
		if (rc == -1) {
			return W5X00_INIT_WIZPHY_GET_FAILED;
		}
	} while (rc == PHY_LINK_OFF);

	wiz_NetInfo netInfo;

	w5x00_netinfo_set(&netInfo, pMacAddress, pIpInfo);

	wizchip_setnetinfo(&netInfo);

#ifndef NDEBUG
	wizchip_getnetinfo(&netInfo);
	w5x00_netinfo_dump(&netInfo);
#endif

	return W5X00_INIT_OK;
}

As it’s been stated by vanvught, you don’t have to change the library source code. The chip select/de-select functions and SPI read/write functions can all be registered as callbacks using reg_wizchip_cs_cbfunc() and reg_wizchip_spi_cbfunc(). For all PIC micros, remember to always read from a PORT and write to a LAT.

In the ioLibrary’s “Internet” folder, there are DHCP client and DNS client source code files if you plan to use them. They also require you to provide a 1-second tick function for various time-out operation.

In general, your project should start out with PHY detection. Once PHY is active, run DHCP. Once an IP address is acquired, you can then run other TCP/UPD socket functions.

Thanks for your response Dear friends
I want in UDP mode Send “Hello world!” by LAN port
My code is as follows
I could not do more than that
Please help me
thank you

#include <xc.h>
#define FCY 60000000UL
#include <libpic30.h>
#include <stdio.h>
#include <string.h>
#include “wizchip_conf.h”
#include “socket.h”
#include “w5500.h”
#include “uart1.h”

// FICD
#pragma config ICS = PGD1 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)

// FPOR
#pragma config ALTI2C1 = OFF // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)

// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FOSC
#pragma config POSCMD = XT // Primary Oscillator Mode Select bits (XT Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSECMD // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)

// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)

// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)

void Init_SPI ( void )
{
// setup the SPI peripheral
SPI1STAT = 0x0; // disable the SPI module (just in case)

SPI1CON1bits.DISSCK = 0;
SPI1CON1bits.DISSDO = 0;
SPI1CON1bits.MODE16 = 0;
SPI1CON1bits.SMP    = 0;
SPI1CON1bits.CKE    = 1;
SPI1CON1bits.SSEN   = 0;
SPI1CON1bits.CKP    = 0;
SPI1CON1bits.MSTEN  = 1;
SPI1CON1bits.SPRE   = 0b110;
/*
bit 4-2 SPRE<2:0>: Secondary Prescale bits (Master mode)
111 = Secondary prescale 1:1
110 = Secondary prescale 2:1
...
000 = Secondary prescale 8:1
*/
SPI1CON1bits.PPRE   = 0b11;
/*
bit 1-0 PPRE<1:0>: Primary Prescale bits (Master mode)
11 = Primary prescale 1:1
10 = Primary prescale 4:1
01 = Primary prescale 16:1
00 = Primary prescale 64:1
*/

// SPI1CON1 = 0x0161; // FRAMEN = 0, SPIFSD = 0, DISSDO = 0, MODE16 = 0; SMP = 0; CKP = 1; CKE = 1; SSEN = 0; MSTEN = 1; SPRE = 0b000, PPRE = 0b01
// SPI1CON1bits.CKE = 0x00;
// SPI1CON1bits.CKP = 0x00;
SPI1STAT = 0x8000; // enable the SPI module

TRISGbits.TRISG0 = 1;	// RG0 input - spi1 pin
RPINR20bits.SDI1R = 0b1110000;	//  assigned to spi1 function

TRISFbits.TRISF0 = 0;	// RF0 output - SPI1CLK pin
RPOR7bits.RP96R = 0b000110;		//RP96 as SPI1CLK


TRISGbits.TRISG1 = 0;	// Rg1 output - SPI1 mosi pin
RPOR13bits.RP113R = 0b000101;		//RP96 as SPI1CLK

}

void Init_URAT (void)
{
TRISDbits.TRISD5 = 0; //TX1RS232 - RP69 - RD5 - GRP13
TRISDbits.TRISD4 = 0; //TX2RS232 - RP68 - RD4 - GRP12

RPOR2bits.RP69R = 0b000001; //RPn tied to UART1 Transmit
RPOR2bits.RP68R = 0b000011; //RPn tied to UART2 Transmit

TRISDbits.TRISD12 = 1; //RX1RS232 - RPI76 - RD12 - GRPI7
TRISDbits.TRISD13 = 1; //RX2RS232 - RPI77 - RD13 - GRPI8

RPINR18bits.U1RXR = 0b1001100; // RPI76
RPINR19bits.U2RXR = 0b1001101; // RPI77

uart1_open(9600);

}

static void w5500_cs_select(void) {
   
    TRISFbits.TRISF1 = 0;
    PORTFbits.RF1 = 0;
}

static void w5500_cs_deselect(void) {
    TRISFbits.TRISF1 = 0;
    PORTFbits.RF1 = 1;
}

static void w5500_write(uint8_t b) {
uint8_t temp;	

SPI1BUF = b;		// write the data out to the SPI peripheral

// Wait for the TX buffer to empty
while(SPI1STATbits.SPITBF);

// Read the receive buffer
temp =  SPI1BUF;
SPI1STATbits.SPIROV = 0; 
}

static uint8_t w5500_read(void) {

uint8_t temp = 0;	

SPI1BUF = temp;		// write the data out to the SPI peripheral
// Wait for the TX buffer to empty

// while(SPI1STATbits.SPITBF);

// Wait for the RX buffer to be full
while(!SPI1STATbits.SPIRBF);

// Read the receive buffer
temp =  SPI1BUF;
SPI1STATbits.SPIROV=0;

return temp;

}

static void w5500_read_burst(uint8_t *pBuf, uint16_t len) {

size_t for_i = 0; 

for(for_i = 0 ; for_i < len ; for_i++)
{
pBuf[for_i] = w5500_read();
}
}

static void w5500_write_burst(uint8_t *pBuf, uint16_t len) {
 size_t for_i = 0; 

for(for_i = 0 ; for_i < len ; for_i++)
{
w5500_write(pBuf[for_i]);
}
}

int main(void)
{
// Configure Oscillator to operate the device at 60Mhz
// Fosc= FinM/(N1N2), Fcy=Fosc/2
// Fosc= 8M60/(22)=120Mhz for 8M input clock
PLLFBD = 58; // M=60
CLKDIVbits.PLLPOST = 0; // N1=2
CLKDIVbits.PLLPRE = 0; // N2=2
OSCTUN = 0; // Tune FRC oscillator, if FRC is used

// Disable Watch Dog Timer
RCONbits.SWDTEN = 0;

// Clock switching to incorporate PLL
__builtin_write_OSCCONH( 0x03 );            // Initiate Clock Switch to Primary

// Oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL( OSCCON || 0x01 );  // Start clock switching
while( OSCCONbits.COSC != 0b011 );

// Wait for Clock switch to occur
// Wait for PLL to lock
while( OSCCONbits.LOCK != 1 )
{ };



Init_SPI();
Init_URAT();


reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect);
reg_wizchip_spiburst_cbfunc(w5500_read_burst, w5500_write_burst);
reg_wizchip_spi_cbfunc(w5500_read, w5500_write);

wizphy_reset();

uint8_t bufSize[2][8] = { {8,2,2,2,2,0,0,0},{8,2,2,2,2,0,0,0}};

wiz_NetInfo netInfo = { .mac 	= {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef},			// MAC address
			.ip 	= {10, 4, 33, 244},					// IP address
			.sn 	= {255, 255, 255, 0},					// IP netmask
			.gw 	= {10, 4, 33, 1}};					// IP gateway

wizchip_setnetinfo(&netInfo);
wizchip_getnetinfo(&netInfo);
printf("test\n");

TRISCbits.TRISC13 = 0;



while( 1 )
{

    PORTCbits.RC13 = 1;
    __delay_ms(500);
    PORTCbits.RC13 = 0;
    __delay_ms(500);
    
    printf("Hello!\r\n");
}

}

If this is all you want to do, you can call the socket() API with Sn_MR_MACRAW, i.e., the MACRAW mode, and then call sendto() to send the “Hello world!” string.

Thanks, acpie360
I have two problems:

1- When using the command:
wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
.ip = {192, 168, 0, 226},
.sn = {255,255,255,0},
.gw = {192, 168, 0, 1},
.dns = {0,0,0,0},
.dhcp = NETINFO_STATIC };
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);

And then I want to read parameters above by:

ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);

I see a different amount in the COM port terminal for debug !!!

The picture below is at the 7.5MHz SPI clock:

The picture below is at the 30MHz SPI clock:

=======================================================================

2- When I use the command to open the port:
socket(0,Sn_MR_UDP,3000,0);

The program stops at the same line of code and does not go to the next step !!!

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

This is all my code:

#include <xc.h>
#define FCY 60000000UL
#include <libpic30.h>
#include <stdio.h>
#include <string.h>

#define WIZCHIP 5500

#include “wizchip_conf.h”
#include “socket.h”
#include “w5500.h”
#include “uart1.h”

// FICD
#pragma config ICS = PGD1 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)

// FPOR
#pragma config ALTI2C1 = OFF // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)

// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FOSC
#pragma config POSCMD = XT // Primary Oscillator Mode Select bits (XT Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSECMD // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)

// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config IESO = OFF // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)

// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)

void Init_SPI ( void )
{
// setup the SPI peripheral
SPI1STAT = 0x0; // disable the SPI module (just in case)

SPI1CON1bits.DISSCK = 0;
SPI1CON1bits.DISSDO = 0;
SPI1CON1bits.MODE16 = 0;
SPI1CON1bits.SMP    = 0;
SPI1CON1bits.CKE    = 1;
SPI1CON1bits.SSEN   = 0;
SPI1CON1bits.CKP    = 0;
SPI1CON1bits.MSTEN  = 1;
SPI1CON1bits.SPRE   = 0b110;
/*
bit 4-2 SPRE<2:0>: Secondary Prescale bits (Master mode)
111 = Secondary prescale 1:1
110 = Secondary prescale 2:1
...
000 = Secondary prescale 8:1
*/
SPI1CON1bits.PPRE   = 0b10;
/*
bit 1-0 PPRE<1:0>: Primary Prescale bits (Master mode)
11 = Primary prescale 1:1
10 = Primary prescale 4:1
01 = Primary prescale 16:1
00 = Primary prescale 64:1
*/

// SPI1CON1 = 0x0161; // FRAMEN = 0, SPIFSD = 0, DISSDO = 0, MODE16 = 0; SMP = 0; CKP = 1; CKE = 1; SSEN = 0; MSTEN = 1; SPRE = 0b000, PPRE = 0b01
// SPI1CON1bits.CKE = 0x00;
// SPI1CON1bits.CKP = 0x00;
SPI1STAT = 0x8000; // enable the SPI module

TRISGbits.TRISG0 = 1;	// RG0 input - spi1 pin
RPINR20bits.SDI1R = 0b1110000;	//  assigned to spi1 function

TRISFbits.TRISF0 = 0;	// RF0 output - SPI1CLK pin
RPOR7bits.RP96R = 0b000110;		//RP96 as SPI1CLK


TRISGbits.TRISG1 = 0;	// Rg1 output - SPI1 mosi pin
RPOR13bits.RP113R = 0b000101;		//RP96 as SPI1CLK

}

void Init_URAT (void)
{
TRISDbits.TRISD5 = 0; //TX1RS232 - RP69 - RD5 - GRP13
TRISDbits.TRISD4 = 0; //TX2RS232 - RP68 - RD4 - GRP12

RPOR2bits.RP69R = 0b000001; //RPn tied to UART1 Transmit
RPOR2bits.RP68R = 0b000011; //RPn tied to UART2 Transmit

TRISDbits.TRISD12 = 1; //RX1RS232 - RPI76 - RD12 - GRPI7
TRISDbits.TRISD13 = 1; //RX2RS232 - RPI77 - RD13 - GRPI8

RPINR18bits.U1RXR = 0b1001100; // RPI76
RPINR19bits.U2RXR = 0b1001101; // RPI77

uart1_open(9600);

}

////////////////////////////////////////////////
// Shared Buffer Definition for LOOPBACK TEST //
////////////////////////////////////////////////

    wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
                        .ip = {192, 168, 0, 226},
                        .sn = {255,255,255,0},
                        .gw = {192, 168, 0, 1},
                        .dns = {0,0,0,0},
                        .dhcp = NETINFO_STATIC };
    


static void w5500_cs_select(void) {
   
    TRISFbits.TRISF1 = 0;
    PORTFbits.RF1 = 0;
}

static void w5500_cs_deselect(void) {
    TRISFbits.TRISF1 = 0;
    PORTFbits.RF1 = 1;
}

static void w5500_write(uint8_t wb) {
uint8_t temp;	

SPI1BUF = wb;		// write the data out to the SPI peripheral

// Wait for the TX buffer to empty
while(SPI1STATbits.SPITBF);

// Read the receive buffer
temp =  SPI1BUF;
SPI1STATbits.SPIROV = 0; 
}

static uint8_t w5500_read(void) {

uint8_t temp = 0;	

SPI1BUF = temp;		// write the data out to the SPI peripheral
// Wait for the TX buffer to empty

// while(SPI1STATbits.SPITBF);

// Wait for the RX buffer to be full
while(!SPI1STATbits.SPIRBF);

// Read the receive buffer
temp =  SPI1BUF;
SPI1STATbits.SPIROV=0;

return temp;

}



static void w5500_read_burst(uint8_t *pBuf, uint16_t len) {

size_t for_i = 0; 

for(for_i = 0 ; for_i < len ; for_i++)
{
pBuf[for_i] = w5500_read();
}
}

static void w5500_write_burst(uint8_t *pBuf, uint16_t len) {
 size_t for_i = 0; 

for(for_i = 0 ; for_i < len ; for_i++)
{
w5500_write(pBuf[for_i]);
}
}

void network_init(void)
{
uint8_t tmpstr[6];
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);

// Display Network Information
ctlwizchip(CW_GET_ID,(void*)tmpstr);
printf("\r\n=== %s SET NET CONF ===\r\n",(char*)tmpstr);
printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
        gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
printf("IP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
printf("GW: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);

printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
printf("======================\r\n");

__delay_ms(300);


ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);




// Display Network Information
ctlwizchip(CW_GET_ID,(void*)tmpstr);
printf("\r\n=== %s GET NET CONF ===\r\n",(char*)tmpstr);
printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
        gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
printf("IP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
printf("GW: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);

printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
printf("======================\r\n");

}

int main(void)
{
// Configure Oscillator to operate the device at 60Mhz
// Fosc= FinM/(N1N2), Fcy=Fosc/2
// Fosc= 8M60/(22)=120Mhz for 8M input clock
PLLFBD = 58; // M=60
CLKDIVbits.PLLPOST = 0; // N1=2
CLKDIVbits.PLLPRE = 0; // N2=2
OSCTUN = 0; // Tune FRC oscillator, if FRC is used

// Disable Watch Dog Timer
RCONbits.SWDTEN = 0;

// Clock switching to incorporate PLL
__builtin_write_OSCCONH( 0x03 );            // Initiate Clock Switch to Primary

// Oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL( OSCCON || 0x01 );  // Start clock switching
while( OSCCONbits.COSC != 0b011 );

// Wait for Clock switch to occur
// Wait for PLL to lock
while( OSCCONbits.LOCK != 1 )
{ };



Init_SPI();
Init_URAT();

/*
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // MAC address
.ip = {10, 4, 33, 244}, // IP address
.sn = {255, 255, 255, 0}, // IP netmask
.gw = {10, 4, 33, 1}}; // IP gateway
*/

TRISCbits.TRISC13 = 0;

uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
uint8_t txsize[8] = {2,2,2,2,2,2,2,2};
uint8_t rxsize[8] = {2,2,2,2,2,2,2,2};

reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect);
reg_wizchip_spiburst_cbfunc(w5500_read_burst, w5500_write_burst);
reg_wizchip_spi_cbfunc(w5500_read, w5500_write);
////////////////////////////////////////////////////////////////////////

/* WIZCHIP SOCKET Buffer initialize */

wizchip_init(txsize,rxsize);

/* Network initialization */
network_init();

printf("test\n");





socket(0, Sn_MR_UDP, 3000, SF_IO_NONBLOCK);

while( 1 )
{
// Write_SPI(0x07);
PORTCbits.RC13 = 1;
__delay_ms(500);
PORTCbits.RC13 = 0;
__delay_ms(500);

    printf("Hello!\r\n");

// WIZCHIP_WRITE(0x0402 ,0xff);

// WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb );

// result = socket(0, Sn_MR_UDP, 3000, SF_IO_NONBLOCK);
// printf(“socket Result: %d\r\n”, result);

// sendto(0, testBuffer, strlen(testBuffer), address, 300);
// printf(“sendto Result: %d\r\n”, result);

// __delay_ms(1000);

//
// loopback_udps(1,testBuffer,3000);

// __delay_ms(1000);
}
}

What are my code bugs?
please tell me

It would be interesting to know the result of the wizchip_init. For example:

wizphy_reset();

uint8_t bufSize[2][8] = { {8,2,2,2,2,0,0,0},{8,2,2,2,2,0,0,0}};

if (wizchip_init(bufSize[0], bufSize[1]) != 0) {
	return W5X00_INIT_WIZCHIP_INIT_FAILED;
}

do {
	rc = wizphy_getphylink();
	if (rc == -1) {
		return W5X00_INIT_WIZPHY_GET_FAILED;
	}
} while (rc == PHY_LINK_OFF);

Please note issue #44

I’d check the SPI RX buffer flag before reading the buffer, e.g.,

static void w5500_write(uint8_t b) {
	uint8_t temp;	

	SPI1BUF = b;		// write the data out to the SPI peripheral

	// Wait for the TX buffer to empty
	while(SPI1STATbits.SPITBF);
	
	// Wait for the RX buffer ready
	while (SPI1STATbits.SPIRBF != 1);

	// Read the receive buffer
	temp =  SPI1BUF;
	SPI1STATbits.SPIROV = 0; 
}