实现原理
嵌入式程序跑在STM32微控制器,通过片上SPI控制器与W5500进行通信,配置所需网络参数并与远端服务器建立链接(TCP客户端)之后发送接收数据;又或者是建立服务器(TCP服务器)等待建立链接之后收发数据。抑或是不用建立连接直接发送报文(UDP)。PHY信号变压之后通过RJ45接口收发至网线。
嵌入式程序
嵌入式程序分两个部分,第一部分是一些底层通用接口。第二部分W5500抽象供上层调用的接口。
底层通用接口
这部分实现主要包括IO口控制器初始化,以及MCU不同位宽配置芯片接口和Socket批量发送接收数据接口,代码片如下(中文注释帮助您更好的理解实现):
//IO口控制器初始化
uint8 w55_IoInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(SPI_CS_RCC|SPI_SCLK_RCC|SPI_SO_RCC|SPI_SI_RCC|W5500_RESET_RCC|LINK_RCC|RCC_APB2Periph_AFIO,ENABLE);
/*使能AFIO时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/*只保留SWD模式*/
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
RCC_APB1PeriphClockCmd(W5500_SPI_CLK ,ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = W5500_RESET_pin;
GPIO_Init(W5500_RESET_GPIO, &GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =SPI_CS_pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_CS_GPIO, &GPIO_InitStructure);
GPIO_SetBits(SPI_CS_GPIO,SPI_CS_pin);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =SPI_SCLK_pin|SPI_SI_pin|SPI_SO_pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_SCLK_GPIO, &GPIO_InitStructure);
GPIO_SetBits(SPI_SCLK_GPIO,SPI_SCLK_pin|SPI_SI_pin|SPI_SO_pin);
RCC_APB1PeriphClockCmd(LINK_RCC ,ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LINK_pin;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LINK_GPIO, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_Init(W5500_SPI, &SPI_InitStructure);
SPI_Cmd(W5500_SPI,ENABLE);
return 1;
}
//spi发送一个字节
void SPI_Send_Byte(unsigned char dat)
{
while (SPI_I2S_GetFlagStatus(W5500_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(W5500_SPI, dat);
}
//spi接收一个字节
unsigned char SPI_Recv_Byte(void)
{
return (uint8)(SPI_I2S_ReceiveData(W5500_SPI));
}
//spi发送两个字节
void SPI_Send_Short(unsigned short dat)
{
SPI_Send_Byte(dat/256);
SPI_Send_Byte(dat);
}
//通过spi向指定地址寄存器写n个字节数据
uint8 w55_WritenByte(uint16 reg, uint8 *dat_ptr, uint16 size)
{
unsigned short i;
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(reg);
SPI_Send_Byte(VDM|RWB_WRITE|COMMON_R);
for(i=0;i<size;i++)
{
SPI_Send_Byte(*dat_ptr++);
}
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
return 1;
}
//读取W5500指定地址1Byte数据
uint8 w55_Read1Byte(uint16 reg)
{
unsigned char i;
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(reg);
SPI_Send_Byte(FDM1|RWB_READ|COMMON_R);
i=SPI_Recv_Byte();
SPI_Send_Byte(0x00);
i=SPI_Recv_Byte();
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
return i;
}
///
//华丽的分割线
///
//单字节配置socket
uint8 w55_WriteSock1Byte(SOCKET s, uint16 reg, uint8 dat)
{
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(reg);
SPI_Send_Byte(FDM1|RWB_WRITE|(s*0x20+0x08));
SPI_Send_Byte(dat);
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
return 1;
}
//两字节配置socket
uint8 w55_WriteSock2Byte(SOCKET s, uint16 reg, uint16 dat)
{
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(reg);
SPI_Send_Byte(FDM2|RWB_WRITE|(s*0x20+0x08));
SPI_Send_Short(dat);
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
return 1;
}
//四字节配置socket
uint8 w55_WriteSock4Byte(SOCKET s, uint16 reg, uint8 *dat_ptr)
{
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);
SPI_Send_Byte(FDM4|RWB_WRITE|(s*0x20+0x08));
SPI_Send_Byte(*dat_ptr++);
SPI_Send_Byte(*dat_ptr++);
SPI_Send_Byte(*dat_ptr++);
SPI_Send_Byte(*dat_ptr++);
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
return 1;
}
//读取Socket1字节信息
uint8 w55_ReadSock1Byte(SOCKET s, uint16 reg)
{
unsigned char i;
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(reg);
SPI_Send_Byte(FDM1|RWB_READ|(s*0x20+0x08));
//i=SPI_I2S_ReceiveData(W5500_SPI);
i=SPI_Recv_Byte();
SPI_Send_Byte(0x00);
//i=SPI_I2S_ReceiveData(W5500_SPI);
i=SPI_Recv_Byte();
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
return i;
}
//读取2字节socket信息
uint16 w55_ReadSock2Byte(SOCKET s, uint16 reg)
{
unsigned short i;
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(reg);
SPI_Send_Byte(FDM2|RWB_READ|(s*0x20+0x08));
i=SPI_Recv_Byte();
SPI_Send_Byte(0x00);
i=SPI_Recv_Byte();
SPI_Send_Byte(0x00);
i*=256;
i+=SPI_Recv_Byte();
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
return i;
}
W5500抽象接口
这部分主要实现了芯片以及Socket初始化,模拟中断处理循环函数等,示例使用Socket0实现TCP客户端,您可以在此代码基础上增加配置别的Socket实现TCP服务器以及UDP等。代码片(会有中文注释帮助您更好地理解程序)如下,你会通过这部分代码片体会到C51单片机程序的编程风格(尤其是开始的两个接口):
//从Socket接收数据缓存区读取数据
uint16 w55_ReadSockToBuffer(SOCKET s, uint8 *dat_ptr)
{
unsigned short rx_size;
unsigned short offset, offset1;
unsigned short i;
unsigned char j;
rx_size=w55_ReadSock2Byte(s,W5500_Sn_RX_RSR);
if(rx_size==0) return 0;//没接收到数据则返回
if(rx_size>S_RX_SIZE) rx_size=S_RX_SIZE;
offset=w55_ReadSock2Byte(s,W5500_Sn_RX_RD);
offset1=offset;
offset&=(S_RX_SIZE-1);//计算实际的物理地址
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(offset);//写16位地址
SPI_Send_Byte(VDM|RWB_READ|(s*0x20+0x18));//写控制字节,N字节数据长度,读数据,选择端口
j=SPI_Recv_Byte();
if((offset+rx_size)<S_RX_SIZE)//如果最大地址未超过w5500接收缓冲区寄存器的最大地址
{
for(i=0;i<rx_size;i++)//循环读取rx_size个字节数据
{
SPI_Send_Byte(0x00);//发送一个哑数据
j=SPI_Recv_Byte();
*dat_ptr=j;
dat_ptr++;
}
}
else//如果最大地址超过w5500接收缓冲区寄存器最大地址
{
offset=S_RX_SIZE-offset;
for(i=0;i<offset;i++)//循环读出前offset个字节数据
{
SPI_Send_Byte(0x00);
j=SPI_Recv_Byte();
*dat_ptr=j;
dat_ptr++;
}
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(0x00);//写16位地址
SPI_Send_Byte(VDM|RWB_READ|(s*0x20+0x18));//写控制字
j=SPI_Recv_Byte();
for(;i<rx_size;i++)//循环读取后rx_size-offset个字节数据
{
SPI_Send_Byte(0x00);
j=SPI_Recv_Byte();
*dat_ptr=j;
dat_ptr++;
}
}
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
offset1+=rx_size;//更新实际物理地址,即下次读取接收到的数据的起始地址
w55_WriteSock2Byte(s, W5500_Sn_RX_RD, offset1);
w55_WriteSock1Byte(s, W5500_Sn_CR, RECV);//发送启动接收命令
return rx_size;//返回接收到的数据长度
}
//将数据写入W5500的数据发送缓冲区
uint8 w55_WriteSockToBuffer(SOCKET s, uint8 *dat_ptr, uint16 size)
{
unsigned short offset,offset1;
unsigned short i;
offset=w55_ReadSock2Byte(s,W5500_Sn_TX_WR);
offset1=offset;
offset&=(S_TX_SIZE-1);//计算实际物理地址
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(offset);
SPI_Send_Byte(VDM|RWB_WRITE|(s*0x20+0x10));
if((offset+size)<S_TX_SIZE)//如果最大地址未超过W5500发送缓冲区寄存器的最大地址
{
for(i=0;i<size;i++)
{
SPI_Send_Byte(*dat_ptr++);
}
}
else//如果最大地址超过w5500发送缓冲区寄存器的最大地址
{
offset=S_TX_SIZE-offset;
for(i=0;i<offset;i++)
{
SPI_Send_Byte(*dat_ptr++);
}
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);
SPI_Send_Short(0x00);
SPI_Send_Byte(VDM|RWB_WRITE|(s*0x20+0x10));
for(;i<size;i++)
{
SPI_Send_Byte(*dat_ptr++);
}
}
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);
offset1+=size;//更新实际物理地址,即下次写待发送数据到发送数据缓冲区起始地址
w55_WriteSock2Byte(s, W5500_Sn_TX_WR, offset1);
w55_WriteSock1Byte(s, W5500_Sn_CR, SEND);//发送启动发送指令
}
//芯片初始化
uint8 w55_StartUp(void)
{
uint8 i;
w55_IoInit();
w55_HardwareReset();
w55_Write1Byte(W5500_MODE,MODE_RST); //软复位
delay(1000);
//设置网关的IP地址
w55_WritenByte(W5500_GAR, s_app_w55_para.u8Gateway, 4);
//设置子网掩码值
w55_WritenByte(W5500_SUBR,s_app_w55_para.u8SubMask,4);
//设置MAC地址
w55_WritenByte(W5500_SHAR,s_app_w55_para.u8MacAddr,6);
//设置w5500的IP地址
w55_WritenByte(W5500_SIPR,s_app_w55_para.u8LocalIp,4);
//设置发送缓冲区以及接收缓冲区大小
for(i=0;i<8;i++)
{
w55_WriteSock1Byte(i,W5500_Sn_RXBUF_SIZE, 0x02);//Socket Rx memory size=2k
w55_WriteSock1Byte(i,W5500_Sn_TXBUF_SIZE, 0x02);//Socket Tx mempry size=2k
}
//设置重连时间为200ms
w55_Write2Byte(W5500_RTR, 0x07d0);
//设置重连次数为8次
w55_Write1Byte(W5500_RCR,8);
w55_Write1Byte(W5500_IMR,IM_IR7|IM_IR6);
w55_WriteSock1Byte(W55_SOCKETSERVER,W5500_Sn_IMR,(IMR_SENDOK|IMR_TIMEOUT|IMR_RECV|IMR_DISCON|IMR_CON));
w55_WriteSock1Byte(W55_SOCKETCLIENT,W5500_Sn_IMR,(IMR_SENDOK|IMR_TIMEOUT|IMR_RECV|IMR_DISCON|IMR_CON));
w55_WriteSock1Byte(W55_SOCKETUDP,W5500_Sn_IMR,(IMR_SENDOK|IMR_TIMEOUT|IMR_RECV|IMR_DISCON|IMR_CON));
w55_WriteSock1Byte(W55_SOCKETLISTEN,W5500_Sn_IMR,(IMR_SENDOK|IMR_TIMEOUT|IMR_RECV|IMR_DISCON|IMR_CON));
}
//Socket初始化
uint8 w55_SocketInit(SOCKET s)
{
switch(s)
{
case 0: //将socket0用作Tcp客户端
w55_WriteSock2Byte(s, W5500_Sn_MSSR, 1460);//设置最大字节分片数
//设置源端口号
w55_WriteSock2Byte(s, W5500_Sn_PORT, s_app_w55_para.u16UpPort);
//设置目标(远程)端口号
w55_WriteSock2Byte(1, W5500_Sn_DPORTR, s_app_w55_para.u16UpPort);
//设置目标(远程)IP地址
w55_WriteSock4Byte(s, W5500_Sn_DIPR, s_app_w55_para.u8UpIp);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
break;
}
}
//Tcp客户端连接
uint8 u8_app_w55_SocketConnect(SOCKET s)
{
u8_drv_w55_WriteSock1Byte(s,W5500_Sn_MR,MR_TCP);
u8_drv_w55_WriteSock1Byte(s,W5500_Sn_CR,OPEN);//打开Socket
delay(5);//ÑÓʱ5ms
if(u8_drv_w55_ReadSock1Byte(s,W5500_Sn_SR)!=SOCK_INIT)
{
u8_drv_w55_WriteSock1Byte(s,W5500_Sn_CR,CLOSE);
return FALSE;//·µ>>ØFALSE(0x00)
}
u8_drv_w55_WriteSock1Byte(s,W5500_Sn_CR,CONNECT);//设置Socket尝试连接
return TRUE;
}
//循环模拟中断处理函数
uint8 w55_InterruptProcess(void)
{
uint8 i,j;
uint8 u8Data[50];
uint16 u16Length;
uint16 k;
while(1)
{
//读取当前中断标志
i = w55_Read1Byte(W5500_IR);
//清除中断标志
w55_Write1Byte(W5500_IR, (i&0xf0));
if((i & IR_CONFLICT) == IR_CONFLICT)
{
//IP冲突处理代码添加
return 0;
}
if((i & IR_UNREACH) == IR_UNREACH)
{
//报文无法到达
return 0;
}
i=w55_Read1Byte(W5500_SIR);
if((i & S1_INT) == S1_INT) //Socket1用作TCP客户端
{
//读取socket1中断标志
j=w55_ReadSock1Byte(W55_SOCKETCLIENT,W5500_Sn_IR);
//清除socket1中断标志
w55_WriteSock1Byte(W55_SOCKETCLIENT,W5500_Sn_IR,j);
if(j&IR_CON)//TCP客户端模式下,成功建立连接
{
//一般添加发送设备信息
}
if(j&IR_DISCON) //TCP模式下socket断开连接处理
{
w55_WriteSock1Byte(W55_SOCKETCLIENT,W5500_Sn_CR,CLOSE);//关闭
w55_SocketInit(W55_SOCKETCLIENT);// 初始化TCP客户端socket
w55_SocketConnect(W55_SOCKETCLIENT);//尝试建立连接
}
if(j&IR_SEND_OK) //数据包发送成功
{
//添加发送成功相关代码
}
if(j&IR_RECV) //数据包接收
{
memset(w55_DataTmpRC,'\0',sizeof(MAX_CIRCBUFF_SIZE_1+10));
u16Length = w55_ReadSockToBuffer(W55_SOCKETCLIENT,w55_DataTmpRC);
//通知处理接收数据
}
if(j&IR_TIMEOUT)//Socket连接或数据传输超时处理
{
w55_WriteSock1Byte(W55_SOCKETCLIENT,W5500_Sn_CR,CLOSE);//关闭
w55_SocketInit(W55_SOCKETCLIENT);//初始化
w55_SocketConnect(W55_SOCKETCLIENT); //重连
}
}
w55_WriteSockToBuffer(7,u8test1,2);
if(w55_Read1Byte(W5500_SIR) != 0)
{
continue;
}
else
{
break;
}
}
}
官网例程补充说明
W5500官网提供了包括开发板,原理图,以及参考代码和常见问题与技术服务等。参考代码又针对STM32,DSP,C8051以及基于操作系统分别提供了作为TCP客户端/服务器,UDP收发等例程。下面我们对STM32F407+W5500例程中的W5500作为TCP客户端工程进行简单的分析,具体代码可以去官网下载,我这边就不贴了。
工程中包含了DHCP/DNS客户端的实现代码,示例中并没有使用,我也就不详述了,有兴趣的可以去看看。W5500.c包含了主要的驱动实现,包含所有寄存器的定义以及用以上层调用的初始化,建立连接,发送接收等接口。考虑到MCU可能不通过SPI控制器与W5500进行通信,W5500.c实现了注册平台提供的MCU与W5500直接数据交互的函数。下面是示例工程的流程图: