/*----------------------------------------------------
说明 :此例程为W5500 TCP服务器的程序 收什么就返回发送什么
目标服务器IP:192.168.0.12
目标PORT:6000
------------------------------------------------------*/
#include "main.h"
uint8 mac[6] = {0x00, 0x08, 0xdc, 0x11, 0x11, 0x11}; /*定义Mac变量*/
uint8 lip[4] = {192, 168, 1, 10}; /*定义lp变量*/
uint8 sub[4] = {255, 255, 255, 0}; /*定义subnet变量*/
uint8 gw[4] = {192, 168, 1, 1}; /*定义gateway变量*/
uint8 ip[4];
uint8 pc_ip[4] = {192, 168, 1, 2}; /*配置服务器的IP地址*/
uint16 DESPORT = 6000;
uint8 buffer[2048];/*定义一个2KB的缓存*/
uint8 send_buf[8192] = {0};
unsigned int send_cnt=0;
int main(void)
{
uint16 len = 0;
uint16 anyport = 7000; /*定义一个任意端口并初始化*/
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
Systick_Init(72);/* 初始化Systick工作时钟*/
USART1_Init(); /*初始化串口通信:9600@8-n-1*/
printf("STM32 initialization over.\r\n");
Reset_W5500();/*硬重启W5500*/
WIZ_SPI_Init();/*初始化SPI接口*/
printf("W5500 initialized!\r\n");
setSHAR(mac);/*配置Mac地址*/
setSUBR(sub);/*配置子网掩码*/
setGAR(gw);/*配置默认网关*/
setSIPR(lip);/*配置Ip地址*/
//Init. TX & RX Memory size of w5500
sysinit(txsize, rxsize); /*初始化8个socket*/
setRTR(2000);/*设置溢出时间值*/
setRCR(5);/*设置最大重新发送次数*/
getSIPR(ip);
printf("IP : %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
getSUBR(ip);
printf("SN : %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
getGAR(ip);
printf("GW : %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
printf("Network is ready.\r\n");
for (unsigned int cnt = 0; cnt < 8; cnt++) {
send_buf[cnt] = cnt;
}
while (1) {
switch (getSn_SR(0)) { /*获取socket0的状态*/
case SOCK_INIT:/*socket初始化完成*/
connect(0, pc_ip, DESPORT);/*在TCP模式下向服务器发送连接请求*/
break;
case SOCK_ESTABLISHED:/*socket连接建立*/
if (getSn_IR(0) & Sn_IR_CON) {
setSn_IR(0, Sn_IR_CON);/*Sn_IR的第0位置1*/
}
len = getSn_RX_RSR(0); /*len为已接收数据的大小*/
if (len > 0) {
recv(0, buffer, len); /*W5500接收来自Sever的数据*/
send(0, buffer, len); /*W5500向Server发送数据*/
}
for(send_cnt=0; send_cnt<100; send_cnt++)//一次性发25个,循环100次,一次发2500个数据
{
//HEAD
send_buf[send_cnt*8+0]='S';
send_buf[send_cnt*8+1]='T';
send_buf[send_cnt*8+2]='A';
send_buf[send_cnt*8+3]='R';
send_buf[send_cnt*8+4]='T';
//1
send_buf[send_cnt*8+5]=(ADCConvertedValue[0]>>8);
send_buf[send_cnt*8+6]=(ADCConvertedValue[0]%255);
//2
send_buf[send_cnt*8+7]=(ADCConvertedValue[1]>>8);
send_buf[send_cnt*8+8]=(ADCConvertedValue[1]%255);
//3
send_buf[send_cnt*8+9]=(ADCConvertedValue[2]>>8);
send_buf[send_cnt*8+10]=(ADCConvertedValue[2]%255);
//4
send_buf[send_cnt*8+11]=(ADCConvertedValue[3]>>8);
send_buf[send_cnt*8+12]=(ADCConvertedValue[3]%255);
//5
send_buf[send_cnt*8+13]=(ADCConvertedValue[4]>>8);
send_buf[send_cnt*8+14]=(ADCConvertedValue[4]%255);
//6
send_buf[send_cnt*8+15]=(ADCConvertedValue[5]>>8);
send_buf[send_cnt*8+16]=(ADCConvertedValue[5]%255);
//7
send_buf[send_cnt*8+17]=(ADCConvertedValue[6]>>8);
send_buf[send_cnt*8+18]=(ADCConvertedValue[6]%255);
//8
send_buf[send_cnt*8+19]=(ADCConvertedValue[7]>>8);
send_buf[send_cnt*8+20]=(ADCConvertedValue[7]%255);
//E
send_buf[send_cnt*8+21]='$';
send_buf[send_cnt*8+21]='E';
send_buf[send_cnt*8+22]='N';
send_buf[send_cnt*8+23]='D';
}
send(0, send_buf, 2500);
break;
case SOCK_CLOSE_WAIT:/*socket等待关闭状态*/
break;
case SOCK_CLOSED:/*socket关闭*/
socket(0, Sn_MR_TCP, anyport++, Sn_MR_ND); /*打开socket0的一个端口*/
break;
}
}
}
void RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
| RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1|RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LED;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, LED); // led off
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = USART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = USART1_RX;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// SPI
/* Configure SPIy pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = WIZ_SCLK | WIZ_MISO | WIZ_MOSI;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = WIZ_SCS;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, WIZ_SCS);
GPIO_InitStructure.GPIO_Pin = WIZ_RESET ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, WIZ_RESET);
GPIO_InitStructure.GPIO_Pin = WIZ_INT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
|