Skip to content

He Says She Says – Incremental Success With the W5500

November 28, 2014

I’ve done low-level write and read routines for the W5500 and patched them into my test program. In theory it should be listening for a connection on socket 0 and printing the IP when a connection is established. At the first try it reports that it is going into listen mode but not establishing the session. I don’t think this will be hard to fix.
14-11-28 he says she says

I made one low level read and one write routine that mimic the way you access the W5500’s registers in variable length data mode. They can be a bit awkward to use because they expect you to supply the data area via a pointer rather than accepting a single character or returning a byte. They are mostly being used within the initialization routines so I’m living with them for now. I can always do wrappers to make them friendlier.

Included below is the source for the read and write routines, the overall, and socket 0 initialization which call them, and the main routine.

void wizWrite(unsigned int addr, unsigned char opcode, unsigned char * data, unsigned int len){
//variable length write to the wiznet W5500 common or socketN areas
//opcode is xxxx x100
// xxxxx is 00000 for common area, 00001 for socket 0 register, 00010 for socket 0 tx buffer
  enablewiz();   			// Activate the CS pin
  spiSend(addr >>8); 		// Send Wiznet Address High Byte
  spiSend(addr & 0x00FF);	// Send Wiznet Address Low Byte
  spiSend(opcode);   		// Send Wiznet Write OpCode
  spiSendN(data,len);		//Send the payload
  disablewiz();				// make CS pin not active
}

void wizRead(unsigned int addr,unsigned char opcode, unsigned char * data, unsigned int len){
//variable length read from the wiznet w5500 common or socketN areas
//opcode is xxxx x000
// xxxxx is 00000 for common area, 00001 for socket 0 register, 00011 for socket 0 rx buffer
  enablewiz();   			// Activate the CS pin
  spiSend(addr >>8); 		// Send Wiznet Address High Byte
  spiSend(addr & 0x00FF);	// Send Wiznet Address Low Byte
  spiSend(opcode);   		// Send Wiznet Read OpCode
  spiReceiveN(data,len);	// Send the data byte
  disablewiz();				// make CS pin not active
}
void wiz_Init(unsigned char ip_addr[]){// Ethernet Setup
  unsigned char mac_addr[] = {0xDE, 0xAD, 0xBE, 0xE5, 0xFE, 0xED};
  unsigned char sub_mask[] = {255,255,255,0};
  unsigned char gtw_addr[] = {192,168,0,1};
  unsigned char readback_ip[] = {1,2,3,4};
  unsigned char bsz0=0, bsz2=2, bsz4=4;	//
  wizWrite(GAR,WIZNET_WRITE_COMMON,gtw_addr,4);//set the wiznet gateway address register
  delay(1);
  wizWrite(SHAR,WIZNET_WRITE_COMMON,mac_addr,6);// Set the MAC address - Source Address Register
  delay(1);
  wizWrite(SUBR,WIZNET_WRITE_COMMON,sub_mask,4);// Set the Wiznet W5100 Sub Mask Address
  delay(1);
  wizWrite(SIPR,WIZNET_WRITE_COMMON,ip_addr,4);// Set the Wiznet W5100 IP Address
  delay(1);
  wizRead(SIPR,WIZNET_READ_COMMON,readback_ip,4); //read back the IP to make sure it "took"

  wizWrite(SnRX_BSZ,WIZNET_WRITE_S0R,&bsz4,1); //set receive buffer for soscket 0 to 4K
  wizWrite(SnTX_BSZ,WIZNET_WRITE_S0R,&bsz4,1); //set transmit buffer for soscket 0 to 4K
  wizWrite(SnRX_BSZ,WIZNET_WRITE_S7R,&bsz0,1); //set receive buffer for soscket 7 to 0K
  wizWrite(SnTX_BSZ,WIZNET_WRITE_S7R,&bsz0,1); //set transmit buffer for soscket 7 to 0K

  printf("Done Wiznet W5100 Initialization on IP address %d.%d.%d.%d\n\n",readback_ip[0],readback_ip[1],readback_ip[2],readback_ip[3]);
}

void socket0_init(){ //initialize socket 0 for http server
	unsigned char tcpmode=MR_TCP, cropen=CR_OPEN, crlisten=CR_LISTEN;
	unsigned char tcpport[]={0,80}; //port 80 for tcp
	wizWrite(SnMR,WIZNET_WRITE_S0R,&tcpmode,1);	//set mode register to tcp
	wizWrite(SnPORT,WIZNET_WRITE_S0R,tcpport,2);	//set tcp port to 0050
	wizWrite(SnCR,WIZNET_WRITE_S0R,&cropen,1); // Open Socket
	delay(10);
    wizWrite(SnCR,WIZNET_WRITE_S0R,&crlisten,1); //listen to socket
}

void main(void){
	unsigned char socket0status;
    unsigned char ip_addr[] = {169,254,180,2}; //{192,168,0,182}; //
	delay(100);
	printf("\nW5500 Test Mule V2\n");
    wiz_Init(ip_addr); //initialize the wiznet chip
    wizRead(SnSR,WIZNET_READ_S0R,&socket0status,1);
	printf("s0s=%cx ",socket0status);
	while(1){  // Loop forever
		wizRead(SnSR,WIZNET_READ_S0R,&socket0status,1);
		printf("s0s=%cx ",socket0status);
		switch (socket0status){
			case SOCK_CLOSED: //initial condition
				socket0_init();	//initialize socket 0
				break;
			case SOCK_ESTABLISHED: //someone wants to talk to the server
				handlesession();
				break;
			//following are cases where we have to reset and reopen the socket
			case SOCK_FIN_WAIT: case SOCK_CLOSING: case SOCK_TIME_WAIT:
			case SOCK_CLOSE_WAIT: case SOCK_LAST_ACK:
				close0();
				break;
		}
		delay(100);
	}
}
Advertisements

From → web server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: