WIZnet Developer Forum

Error in starting the server in pic18f4520

Hi,

I am using the example given for pic18f45k22 to implement the server in the pic18f4520. i was not getting any error while, but server was not starting when controller was programmed.

Here is the code:

#include "Wizchip_Conf.h"
#include "W5500.h"
#include "Socket.h"
#include <stdio.h>


// ETH WIZ click connections
sbit Wizchip_Rst at LATE1_bit;
sbit Wizchip_CS  at LATE0_bit;
sbit Wizchip_Rst_Direction at TRISE1_bit;
sbit Wizchip_CS_Direction  at TRISE0_bit;
// end of ETH WIZ click connections


// Socket & Port number definition
#define SOCK_ID_TCP       0
#define SOCK_ID_UDP       1
#define PORT_TCP          5000
#define PORT_UDP          10001

#define DATA_BUF_SIZE     1024
uint8_t gDATABUF[DATA_BUF_SIZE];

int32_t WebServer(uint8_t sn, uint8_t* buf, uint16_t port);
char proc_http(uint8_t sn, char * buf);
void sendHeader(uint8_t sn);
void sendData(uint8_t sn);
uint16_t calcu_len(void);


volatile wiz_NetInfo gWIZNETINFO =
{
  {0x00, 0x14, 0xA3, 0x72, 0x17, 0x3f},    // Source Mac Address
  {192, 168,  0, 111 },                      // Source IP Address
  {255, 255, 255, 0},                      // Subnet Mask
  {192, 168,  0, 1},                       // Gateway IP Address
  {8, 8,  8, 8},                      // DNS server IP Address
  NETINFO_STATIC
 };

uint8_t destip[4] = {192, 168, 0, 103};
uint16_t destport = 5000;

volatile wiz_PhyConf phyConf =
{
  PHY_CONFBY_HW,       // PHY_CONFBY_SW
  PHY_MODE_MANUAL,     // PHY_MODE_AUTONEGO
  PHY_SPEED_10,        // PHY_SPEED_100
  PHY_DUPLEX_FULL,     // PHY_DUPLEX_HALF
};

volatile wiz_NetInfo pnetinfo;

// brief Call back function for WIZCHIP select.
void CB_ChipSelect(void)
{
    Wizchip_CS = 0;
}

// brief Call back function for WIZCHIP deselect.
void CB_ChipDeselect(void)
{
    Wizchip_CS = 1;
}

// brief Callback function to read byte usig SPI.
uint8_t CB_SpiRead(void)
{
    return SPI1_Read(0xAA);
}

// brief Callback function to write byte usig SPI.
void CB_SpiWrite(uint8_t wb)
{
    SPI1_Write(wb);
}

// brief Handle TCP socket state.
void TCP_Server(void)
{
    int32_t ret;
    uint16_t size = 0, sentsize = 0;

    // Get status of socket
    switch(getSn_SR(SOCK_ID_TCP))
    {
        // Connection established
        case SOCK_ESTABLISHED :
        {
            // Check interrupt: connection with peer is successful
            if(getSn_IR(SOCK_ID_TCP) & Sn_IR_CON)
            {
                // Clear corresponding bit
                setSn_IR(SOCK_ID_TCP,Sn_IR_CON);
            }

            // Get received data size
            if((size = getSn_RX_RSR(SOCK_ID_TCP)) > 0)
            {
                // Cut data to size of data buffer
                if(size > DATA_BUF_SIZE)
                {
                    size = DATA_BUF_SIZE;
                }

                // Get received data
                ret = recv(SOCK_ID_TCP, gDATABUF, size);

                // Check for error
                if(ret <= 0)
                {
                    return;
                }
                proc_http(sn, (char *)buf);
            }
            break;
        }

        // Socket received the disconnect-request (FIN packet) from the connected peer
        case SOCK_CLOSE_WAIT :
        {
            // Disconnect socket
            if((ret = disconnect(SOCK_ID_TCP)) != SOCK_OK)
            {
                return;
            }

            break;
        }

        // Socket is opened with TCP mode
        case SOCK_INIT :
        {
            // Listen to connection request
            if( (ret = listen(SOCK_ID_TCP)) != SOCK_OK)
            {
                return;
            }

            break;
        }

        // Socket is released
        case SOCK_CLOSED:
        {
            // Open TCP socket
            if((ret = socket(SOCK_ID_TCP, Sn_MR_TCP, PORT_TCP, 0x00)) != SOCK_ID_TCP)
            {
                return;
            }

           break;
        }

        default:
        {
            break;
        }
    }
}

static void W5500_Init(void)
{
    // Set Tx and Rx buffer size to 2KB
    uint8_t buffsize[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };

    Wizchip_Rst_Direction = 0;                           // Set Rst pin to be output
    Wizchip_CS_Direction = 0;                         // Set CS pin to be output

    CB_ChipDeselect();                                                          // Deselect module

    // Reset module
    Wizchip_Rst = 0;
    Delay_ms(1);
    Wizchip_Rst = 1;
    Delay_ms(1);

    SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

    // Wizchip initialize
    wizchip_init(buffsize, buffsize, 0, 0, CB_ChipSelect, CB_ChipDeselect, 0, 0, CB_SpiRead, CB_SpiWrite);

    // Wizchip netconf
    ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);
    ctlwizchip(CW_SET_PHYCONF, (void*) &phyConf);
}

void Move_Delay() {
Delay_ms(500);
}

void sendHeader(uint8_t sn)
{
    uint8_t dat[128]={0,};
    uint8_t dat_t[128]={0,};
    uint8_t *dat_temp;
    dat_temp = "HTTP/1.1 200 OK\r\n";                       strcat(dat, dat_temp);
    dat_temp = "Content-Type: text/html\r\n";                   strcat(dat, dat_temp);
    dat_temp = "Connection: close\r\n";                     strcat(dat, dat_temp);
    //sprintf(dat, "%sContent-Length: %d", dat, calcu_len());
    dat_temp = "\r\n\r\n";                              strcat(dat, dat_temp);
    send(sn, (uint8_t *)dat, strlen(dat));
   // printf("%s", dat);
}
uint8_t send_dat[1024]={0,};

uint16_t calcu_len(void)
{
    uint8_t *dat_temp;

    dat_temp = "\r\n";                                              strcat(send_dat, dat_temp);
    dat_temp = "\r\n";                                                  strcat(send_dat, dat_temp);
    dat_temp = "\r\n";                                                      strcat(send_dat, dat_temp);
    dat_temp = "\r\n";                                                  strcat(send_dat, dat_temp);
    dat_temp = "\r\n";                  strcat(send_dat, dat_temp);
    dat_temp = "W5500 WebServer Test!!\r\n";                                    strcat(send_dat, dat_temp);
    dat_temp = "\r\n";                              strcat(send_dat, dat_temp);
    dat_temp = "\r\n";                                                  strcat(send_dat, dat_temp);
    dat_temp = "\r\n";                                                      strcat(send_dat, dat_temp);
    dat_temp = "\r\n";              strcat(send_dat, dat_temp);
    dat_temp = "W5500-EVB WebServer Test!!\r\n";                                      strcat(send_dat, dat_temp);

    return strlen(send_dat);
}

void sendData(uint8_t sn)
{
    uint16_t i;
    send(sn, (uint8_t *)send_dat, strlen(send_dat));
    //printf("%s", send_dat);
    for(i=0;i<1024;i++){
        send_dat[i]=0;
    }
}


void main() {
    ADCON0 = 0;                             // set PORTA as digital
    ADCON1 = 0;                             // set PORTB as digital
    ADCON2 = 0;                             // set PORTD as digital

    W5500_Init();
    wizchip_getnetinfo(&pnetinfo);

    while(1)
    {
       TCP_Server();
       
        }

Thanks much,
Dhanunjay

Is this function modified by you?
It is different from ioDriver’s function form.

I want you to re-check the code with the following information.
W5500 TCP Function Application Guide

And,

Please refer to other topic below.

Looking at your initialization I noticed you assert the Hardware Reset line, apply 1 ms delay, then de-assert the Reset line and apply another 1 ms delay.
I would suggest to toggle the Reset line without using any delays, then start polling the PHY Configuration Register (Common Block, Offset 0x2E) for RST Bit (0x08) cleared. This happens in less than 1 ms.
From this point you can apply Software Reset and start Register initialization.
Below is some pseudo code reflecting this approach.
When your initialization is complete, your device should be responding to pinging its IP Address.
Good Luck!

void InitW5500(void)
{
   // W5500 Hardware Reset
   PORTCbits.RC1 = LOW;       // Put W5500 in Reset (Active Low)
   PORTCbits.RC1 = HIGH;      // Bring W5500 Out of Reset

   // Waiting for PHY Ready
   while (!(ReadPHYCFG() & PHY_RST));

   // W5500 Software Reset
   InitComBlkMr();

   // Set Network Attributes
   SetMacAddr(NetAttrs.pMacAddress);
   SetIpAddr(NetAttrs.pIpAddress);
   SetSbnMask(NetAttrs.pSubnetMask);
   SetGateway(NetAttrs.pDefGateway);

   // Set Socket 0 PORT
   SetS0PORT(PortNumber);       // Default Port Number

   // Enable Socket 0 Interrupts
   WriteSIMR(SIMR_S0);

   // Connection Alive
   WriteS0KA(KA_TIME);

   // Set TCP Protocol
   WriteS0MR(TCP);
}
1 Like

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.