下面是我对一个简单W5100的TCP协议固件的注释,
在此我们使用了W5100的开发板(W5100E01-ARV),开发和更新非常简单,AVR Studio的使用示意图放在最后。
这个程序实现了简单的socket建立,以太网收发数据的程序。
可以调用device terminal来进行测试。(在wiznet主页中可以搜索到这个小软件,可轻松实现串口和以太网的数据互通)
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include "../header/types.h"
#include "../header/serial.h"
#include "../header/socket.h"
#include "../header/w5100.h"
#define MAX_SIZE 2048
#define RX_BUF 0x1100
#define TX_BUF 0x1900
void main()
{
//initialize
cli(); // disable interrupt
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;
EIFR=0x00;
MCUCR = 0x80; // enable external ram
XMCRA=0x40; //define lower sector of memory for EXternal ram and upper sector for W5100C and others
sei(); // enable interrupts
uart_init(0,7);
lcd_init();
//set IP address
uint8 addr[6];
iinchip_init();
sysinit(0x55,0x55); //
addr[0] = 192; addr[1] = 168; addr[2] = 1; addr[3] = 20;
setSIPR(addr);
addr[0] = 192; addr[1] = 168; addr[2] = 1; addr[3] = 1;
setGAR(addr);
addr[0] = 255; addr[1] = 255; addr[2] = 255; addr[3] = 0;
setSUBR(addr);
memcpy(addr, "\x00\x08\xDC\x00\x00\x00",6);
setSHAR(addr);
uint8 lcd[50];
// IINCHIP_READ 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));
lcd_puts(lcd); //print LCD string into LCD
uint8 len;
//Create socket 0 for TCP mode at port 5000
//#define Sn_MR_TCP 0x01
socket(0, Sn_MR_TCP, 5000, 0);
//extern uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag);
//listen the socket 0
listen(0);
//check the status of Listen status (if entered), print to serial
if( getSn_SR(0) == SOCK_LISTEN)
{
printf("Listen Mode");
}
// if data is received
// extern uint16 getSn_RX_RSR(SOCKET s); // get socket RX recv buf size
while(getSn_RX_RSR(0) ==0 );
printf("got data \r\n");
//print data length
len = getSn_RX_RSR(0);
printf("the size is %u \r\n",len);
//set Send(TX) and Receive(RX) buffer pointer
uint8 * TX_BUFFER = (uint8 *) TX_BUF;
uint8 * RX_BUFFER = (uint8 *) RX_BUF;
if( len > MAX_SIZE)
len = MAX_SIZE;
//put the received data into RX buffer, save length to len
len = recv(0,RX_BUFFER,len);
//send data from RX Buffer to socket 0
send(0,RX_BUFFER,len);
}
使用AVR STUDIO编译,生成hex文件,然后烧录到MCU芯片中(Atmega128)
图片如下:
在下一节,我们将再次介绍一下UDP协议的简单实现。
如果您有什么疑问请留言或者来信:wiznetbj@wiznettechnology.com,希望本篇文章可以给您带来帮助,谢谢。
文章评论(0条评论)
登录后参与讨论