标题:
STM32+W5100S以太网芯片程序与资料
[打印本页]
作者:
雷劳谋
时间:
2021-1-19 21:05
标题:
STM32+W5100S以太网芯片程序与资料
stm控制W5100S的程序
51hei.png
(5.27 KB, 下载次数: 89)
下载附件
2021-1-19 21:47 上传
单片机源程序如下:
#include "socket.h"
static uint16 local_port;
extern uint16 sent_ptr;
#define __MACRAW__
/**
@brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5200 done it.
@return 1 for sucess else 0.
*/
void setkeepalive(SOCKET s);
uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag) // 2017-07-17
{
uint8 ret;
if (
((protocol&0x0F) == Sn_MR_TCP) ||
((protocol&0x0F) == Sn_MR_UDP) ||
((protocol&0x0F) == Sn_MR_IPRAW) ||
((protocol&0x0F) == Sn_MR_MACRAW)
)
{
close(s);
if((protocol&0x0F)==Sn_MR_TCP)
{
setkeepalive(s);
}
IINCHIP_WRITE(W5100S_Sn_MR(s) ,protocol | flag);
if (port != 0) {
IINCHIP_WRITE( W5100S_Sn_PORT0(s) ,(uint8)((port & 0xff00) >> 8));
IINCHIP_WRITE( W5100S_Sn_PORT1(s) ,(uint8)(port & 0x00ff));
} else {
local_port++; // if don't set the source port, set local_port number.
IINCHIP_WRITE(W5100S_Sn_PORT0(s) ,(uint8)((local_port & 0xff00) >> 8));
IINCHIP_WRITE(W5100S_Sn_PORT1(s) ,(uint8)(local_port & 0x00ff));
}
IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_OPEN); // run sockinit Sn_CR
/* wait to process the command... */
while( IINCHIP_READ(W5100S_Sn_CR(s)) )
;
/* ------- */
ret = 1;
}
else
{
ret = 0;
}
return ret;
}
/**
@brief This function close the socket and parameter is "s" which represent the socket number
*/
void close(SOCKET s)
{
IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_CLOSE); //SOCKET关闭
/* wait to process the command... */
while( IINCHIP_READ(W5100S_Sn_CR(s) ) )
;
/* ------- */
/* all clear */
IINCHIP_WRITE( W5100S_Sn_IR(s) , 0xFF);
}
/**
@brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
@return 1 for success else 0.
*/
uint8 listen(SOCKET s) //设置为等待客户端发出请求模式
{
uint8 ret;
if (IINCHIP_READ( W5100S_Sn_SR(s) ) == Sn_SR_INIT) //指示SOCKET打开并处于TCP模式
{
IINCHIP_WRITE(W5100S_Sn_CR(s) ,Sn_CR_LISTEN); //设置为等待客户端发出请求模式
/* wait to process the command... */
while( IINCHIP_READ(W5100S_Sn_CR(s) ) ) //等待设置完成
;
/* ------- */
ret = 1;
}
else
{
ret = 0;
}
return ret;
}
/**
@brief This function established the connection for the channel in Active (client) mode.
This function waits for the untill the connection is established.
@return 1 for success else 0.
*/
uint8 connect(SOCKET s, uint8 * addr, uint16 port)
{
uint8 ret; // ret定义为是否连接的标志位,ret=0 连接中断;ret=1连接成功
if
(
((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
(port == 0x00)
)
{
ret = 0; // 如果IP地址和Port无法获取,则连接中断
}
else // 如果目的IP和Port未设置,则进行设置
{
ret = 1;
IINCHIP_WRITE( W5100S_Sn_DIPR0(s), addr[0]);
IINCHIP_WRITE( W5100S_Sn_DIPR1(s), addr[1]);
IINCHIP_WRITE( W5100S_Sn_DIPR2(s), addr[2]);
IINCHIP_WRITE( W5100S_Sn_DIPR3(s), addr[3]);
IINCHIP_WRITE( W5100S_Sn_DPORT0(s), (uint8)((port & 0xff00) >> 8));
IINCHIP_WRITE( W5100S_Sn_DPORT1(s), (uint8)(port & 0x00ff));
IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_CONNECT); // Sn_CR数值设为0x04,并执行TCP连接请求命令
while ( IINCHIP_READ(W5100S_Sn_CR(s) ) ) ; // MCU读取Sn_CR(s)的数值
while ( IINCHIP_READ(W5100S_Sn_SR(s)) != Sn_SR_SYNSEND ) // 此时Sn_SR(s)寄存器应该处于SOCK_SYNSENT,下面排除不在该状态的几种情况
{
if(IINCHIP_READ(W5100S_Sn_SR(s)) == Sn_SR_ESTABLISHED) // Socket连接已经建立,正常连接
{
break;
}
if (getSn_IR(s) & Sn_IR_TIMEOUT) // 当ARPto或TCPto超时,异常
{
IINCHIP_WRITE(W5100S_Sn_IR(s), (Sn_IR_TIMEOUT)); // 通知MCU该中断,并清中断
ret = 0; // ret置0,连接中断
break;
}
}
}
return ret;
}
/**
@brief This function used for disconnect the socket and parameter is "s" which represent the socket number
@return 1 for success else 0.
*/
void disconnect(SOCKET s)
{
IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_DISCON);
/* wait to process the command... */
while( IINCHIP_READ(W5100S_Sn_CR(s) ) )
;
/* ------- */
}
/**
@brief This function used to send the data in TCP mode
@return 1 for success else 0.
*/
uint16 send(SOCKET s, const uint8 * buf, uint16 len)
{
uint8 status=0;
uint16 ret=0;
uint16 freesize=0;
if (len > getSn_TXBUF_SIZE( s)*1024)
ret = getSn_TXBUF_SIZE( s)*1024;
else ret = len;
do
{
freesize = getSn_TX_FSR(s);
status = IINCHIP_READ(W5100S_Sn_SR(s));
if ((status != Sn_SR_ESTABLISHED) && (status != Sn_SR_CLOSE_WAIT))
{
printf("status break\r\n");
ret = 0;
break;
}
}
while (freesize < ret);
send_data_processing(s, (uint8 *)buf, ret);
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
网络调试助手:
http://www.51hei.com/bbs/dpj-201792-1.html
所有资料51hei提供下载:
STM32F407通过W5100S进行网络通信.7z
(5.15 MB, 下载次数: 58)
2021-1-19 22:35 上传
点击文件名下载附件
STM32F407通过W5100S进行网络通信
下载积分: 黑币 -5
作者:
TobbyHH
时间:
2021-7-17 16:10
这个例程怎么用呀。。
作者:
chen707070yi
时间:
2022-12-22 10:37
你这个比世伟的驱动好多了,世伟的里面很多bug,在判断sock端口改变那里用了太多while等待,而且一些极端情况考虑的不是很周到导致经常莫名宕机,你这个比他们的好太多。
作者:
王富贵兒
时间:
2025-5-22 16:07
这个例
程太棒了
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1