Thursday, September 24, 2009

W5100 simple TCP communication protocol development and firmware update

Here is my W5100's TCP protocol with a simple firmware notes, we use the W5100 development board (W5100E01-ARV), the development and updating is very simple, AVR Studio using schematic at the end.

This socket program to achieve a simple build, Ethernet sends and receives data procedure.
You can call the device terminal to test. (In WIZnet the home page you can search for this small software, can easily exchange data in serial and Ethernet)

Procedures are as follows:


#include # include
#include # include
#include # include
#include # include
#include # include
#include "../header/types.h" # include ".. / header / types.h"
#include "../header/serial.h" # include ".. / header / serial.h"
#include "../header/socket.h" # include ".. / header / socket.h"
#include "../header/w5100.h" # include "../header/w5100.h"
#define MAX_SIZE 2048 # define MAX_SIZE 2048
#define RX_BUF 0x1100 # define RX_BUF 0x1100
#define TX_BUF 0x1900 # define TX_BUF 0x1900
void main() void main ()
{ (
//initialize / / initialize
cli(); // disable interrupt cli (); / / disable interrupt
EICRA=0x00; EICRA = 0x00;
EICRB=0x00; EICRB = 0x00;
EIMSK=0x00; EIMSK = 0x00;
EIFR=0x00; EIFR = 0x00;
MCUCR = 0x80; MCUCR = 0x80; // enable external ram / / Enable external ram
XMCRA=0x40; //define lower sector of memory for EXternal ram and upper sector for W5100C and others XMCRA = 0x40; / / define lower sector of memory for EXternal ram and upper sector for W5100C and others
sei(); sei (); // enable interrupts / / Enable interrupts
uart_init(0,7); uart_init (0,7);
lcd_init(); lcd_init ();
//set IP address / / set IP address
uint8 addr[6]; uint8 addr [6];
iinchip_init(); iinchip_init ();
sysinit(0x55,0x55); // sysinit (0x55, 0x55); / /
addr[0] = 192; addr[1] = 168; addr[2] = 1; addr[3] = 20; addr [0] = 192; addr [1] = 168; addr [2] = 1; addr [3] = 20;
setSIPR(addr); setSIPR (addr);
addr[0] = 192; addr[1] = 168; addr[2] = 1; addr[3] = 1; addr [0] = 192; addr [1] = 168; addr [2] = 1; addr [3] = 1;
setGAR(addr); setGAR (addr);
addr[0] = 255; addr[1] = 255; addr[2] = 255; addr[3] = 0; addr [0] = 255; addr [1] = 255; addr [2] = 255; addr [3] = 0;
setSUBR(addr); setSUBR (addr);
memcpy(addr, "\x00\x08\xDC\x00\x00\x00",6); memcpy (addr, "\ x00 \ x08 \ xDC \ x00 \ x00 \ x00", 6);
setSHAR(addr); setSHAR (addr);
uint8 lcd[50]; uint8 lcd [50];
// IINCHIP_READ / / IINCHIP_READ get register value from SIPR(Source IP register) get register value from SIPR (Source IP register)
sprintf(lcd,"IP: %d.%d.%d.%d",IINCHIP_READ(SIPR0),IINCHIP_READ(SIPR0+1),IINCHIP_READ(SIPR0+2),IINCHIP_READ(SIPR0+3)); sprintf (lcd, "IP:% d.% d.% d.% d", IINCHIP_READ (SIPR0), IINCHIP_READ (SIPR0 +1), IINCHIP_READ (SIPR0 +2), IINCHIP_READ (SIPR0 +3));
lcd_puts(lcd); lcd_puts (lcd); //print LCD string into LCD / / print LCD string into LCD
uint8 len; uint8 len;
//Create socket 0 for TCP mode / / Create socket 0 for TCP mode at port 5000 at port 5000
//#define Sn_MR_TCP / / # define Sn_MR_TCP 0x01 0x01
socket(0, Sn_MR_TCP, 5000, 0); socket (0, Sn_MR_TCP, 5000, 0);
//extern uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag); / / extern uint8 socket (SOCKET s, uint8 protocol, uint16 port, uint8 flag);
//listen the socket 0 / / listen the socket 0
listen(0); listen (0);
//check the status of Listen status (if entered), print to serial / / check the status of Listen status (if entered), print to serial
if( getSn_SR(0) == SOCK_LISTEN) if (getSn_SR (0) == SOCK_LISTEN)
{ (
printf("Listen Mode"); printf ( "Listen Mode");
} )
// if data is received / / If data is received
// extern uint16 getSn_RX_RSR(SOCKET s); // get socket RX recv buf size / / Extern uint16 getSn_RX_RSR (SOCKET s); / / get socket RX recv buf size
while(getSn_RX_RSR(0) ==0 ); while (getSn_RX_RSR (0) == 0);
printf("got data \r\n"); printf ( "got data \ r \ n");
//print data length / / print data length
len = getSn_RX_RSR(0); len = getSn_RX_RSR (0);
printf("the size is %u \r\n",len); printf ( "the size is% u \ r \ n", len);
//set Send(TX) and Receive(RX) buffer pointer / / set Send (TX) and Receive (RX) buffer pointer
uint8 * TX_BUFFER = (uint8 *) TX_BUF; uint8 * TX_BUFFER = (uint8 *) TX_BUF;
uint8 * RX_BUFFER = uint8 * RX_BUFFER = (uint8 *) RX_BUF; (uint8 *) RX_BUF;
if( len > MAX_SIZE) if (len> MAX_SIZE)
len = MAX_SIZE; len = MAX_SIZE;
//put the received data into RX buffer, save length to len / / put the received data into RX buffer, save length to len
len = recv(0,RX_BUFFER,len); len = recv (0, RX_BUFFER, len);
//send data from RX Buffer to socket 0 / / send data from RX Buffer to socket 0
send(0,RX_BUFFER,len); send (0, RX_BUFFER, len);
} )

Compiled using AVR STUDIO, generate hex file, and then burn to the MCU chip (Atmega128)
Images are as follows:

http://photo.blog.sina.com.cn/blogpic/61b952280100f4ou/61b95228g740500163fad

In the next blog, we will try to introduce how to use the UDP protocol.
I hope you can enjoy it!

Johnny



The blog was translated from Johnny Yan on 20/09/2009

Direct link(In Chinese Version): http://blog.sina.com.cn/s/blog_61b952280100f4ou.html

1 comment:

  1. it is difficult to read the code.
    Some formatting is needed. ask for that..

    ReplyDelete