|
#include "usart.h"
#include "device.h"
#include "spi2.h"
#include "ult.h"
#include "socket.h"
#include "w5500.h"
#include "24c16.h"
#include "md5.h"
#include "string.h"
extern uint8 txsize[]; // òyóÃía2¿±äá¿£¬éùÃ÷Socket·¢Ëí»o′æ′óD¡
extern uint8 rxsize[]; // òyóÃía2¿±äá¿£¬éùÃ÷Socket½óêÕ»o′æ′óD¡
uint8 buffer[2048]; // ¶¨òåò»¸ö2KBμÄêy×飬óÃà′′æ·ÅSocketμÄí¨DÅêy¾Y
int main(void)
{
uint8 server_ip[4]={192,168,1,151}; // ÅäÖÃÔ¶3ì·tÎñÆ÷IPμØÖ·
uint16 server_port=5000; // ÅäÖÃÔ¶3ì·tÎñÆ÷¶Ë¿ú
uint16 local_port=6000; // 3õê¼»ˉò»¸ö±¾μض˿ú
uint16 len=0;
/***** MCUê±Öó3õê¼»ˉ *****/
Systick_Init(72);
/***** ÖD¶Ï¿ØÖÆÆ÷ÅäÖà *****/
NVIC_Configuration();
/***** GPIO¡¢SPI3õê¼»ˉ *****/
GPIO_Configuration();
WIZ_SPI_Init();
/***** ′®¿ú3õê¼»ˉ *****/
USART1_Init();
/***** 3õê¼»ˉeeprom *****/
at24c16_init();
/***** ó2ÖØÆôW5500 *****/
Reset_W5500();
/***** W5500μÄIPDÅÏ¢3õê¼»ˉ *****/
set_default(); // éèÖÃĬèÏMAC¡¢IP¡¢GW¡¢SUB¡¢DNS
set_network(); // ÅäÖÃ3õê¼»ˉIPDÅÏ¢2¢′ò󡣬3õê¼»ˉ8¸öSocket
/***** ′ò¿aW5500μÄKeepalive1|Äü *****/
setkeepalive(0);
printf("TCP Server IP: %d.%d.%d.%d \r\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]);
printf("TCP Server Port: %d \r\n",server_port);
printf("W5500 Init Complete!\r\n");
printf("Start TCP Client Test!\r\n");
/*Socket×′쬻ú£¬MCUí¨1y¶áSn_SR(0)μÄÖμ½øDDÅD¶ÏSocketó|¸Ã′|óúoÎÖÖ×′ì¬
Sn_SR×′ì¬Ãèêö£o
0x00 SOCK_CLOSED
0x13 SOCK_INIT
0x14 SOCK_LISTEN
0x17 SOCK_ESTABLISHED
0x1C SOCK_CLOSE_WAIT
0x22 SOCK_UDP
*/
while(1) // Socket×′쬻ú
{
switch(getSn_SR(0)) // »ñè¡socket0μÄ×′ì¬
{
case SOCK_INIT: // Socket′|óú3õê¼»ˉíê3é(′ò¿a)×′ì¬
connect(0, server_ip,server_port); // ÅäÖÃSn_CRÎaCONNECT£¬2¢ÏòTCP·tÎñÆ÷·¢3öᬽóÇëÇó
break;
case SOCK_ESTABLISHED: // Socket′|óúᬽó½¨á¢×′ì¬
if(getSn_IR(0) & Sn_IR_CON)
{
setSn_IR(0, Sn_IR_CON); // Sn_IRμÄCONλÖÃ1£¬í¨ÖaW5500ᬽóòѽ¨á¢
}
// êy¾Y»Ø»·2aêÔ3ìDò£oêy¾Y′óéÏλ»ú·tÎñÆ÷·¢¸øW5500£¬W5500½óêÕμ½êy¾YoóÔù»Ø¸ø·tÎñÆ÷
len=getSn_RX_RSR(0); // len=Socket0½óêÕ»o′æÖDòѽóêÕoí±£′æμÄêy¾Y′óD¡
if(len>0)
{
recv(0,buffer,len); // W5500½óêÕà′×Ô·tÎñÆ÷μÄêy¾Y£¬2¢í¨1ySPI·¢Ëí¸øMCU
printf("%s\r\n",buffer); // ′®¿ú′òó¡½óêÕμ½μÄêy¾Y
send(0,buffer,len); // ½óêÕμ½êy¾YoóÔù»Ø¸ø·tÎñÆ÷£¬íê3éêy¾Y»Ø»·
}
// W5500′ó′®¿ú·¢êy¾Y¸ø¿í»§¶Ë3ìDò£¬êy¾YDèòÔ»Ø3μ½áêø
if(USART_RX_STA & 0x8000) // ÅD¶Ï′®¿úêy¾YêÇ·ñ½óêÕíê3é
{
len=USART_RX_STA & 0x3fff; // »ñè¡′®¿ú½óêÕμ½êy¾YμÄ3¤¶è
send(0,USART_RX_BUF,len); // W5500Ïò¿í»§¶Ë·¢Ëíêy¾Y
USART_RX_STA=0; // ′®¿ú½óêÕ×′쬱ê־λÇå0
memset(USART_RX_BUF,0,len+1); // ′®¿ú½óêÕ»o′æÇå0
}
break;
case SOCK_CLOSE_WAIT: // Socket′|óúμè′y1رÕ×′ì¬
close(0); // 1رÕSocket0
break;
case SOCK_CLOSED: // Socket′|óú1رÕ×′ì¬
socket(0,Sn_MR_TCP,local_port,Sn_MR_ND); // ′ò¿aSocket0£¬2¢ÅäÖÃÎaTCPÎTÑóê±Ä£ê½£¬′ò¿aò»¸ö±¾μض˿ú
break;
}
}
}
|
|