/*初始化 以太网SPI接口*/
ENC_SPI_Init();
/*初始化systick,用于定时轮询输入或给LWIP提供定时*/
SysTick_Init();
/* 初始化LWIP协议栈*/
LwIP_Init();
while ( 1 )
{
TCP_Client_Init();
Delay1(1000000UL);
Delay1(1000000UL);
Delay1(1000000UL);
Delay1(1000000UL);
}
具体的子函数:
/******* 这是一个回调函数,当TCP客户端请求的连接建立时被调用********/
err_t TcpCli_Connected(void *arg,struct tcp_pcb *pcb,err_t err)
{
tcp_write(pcb,TCP_TestData,sizeof(TCP_TestData),0); //发送数据
tcp_close(pcb);
return ERR_OK;
}
void TCP_Client_Init()
{
struct tcp_pcb *Clipcb;
struct ip_addr ipaddr;
IP4_ADDR(&ipaddr,192,168,1,16);
Clipcb = tcp_new(); // 建立通信的TCP控制块(Clipcb)
tcp_bind(Clipcb,IP_ADDR_ANY,23); // 绑定本地IP地址和端口号
iris_pcb=Clipcb; //the mathod is wrong //by iris 2013.6.3
tcp_connect(Clipcb,&ipaddr,23,TcpCli_Connected);
}
unsigned long iris_IP[5]={192, 168, 1, 18};
void LwIP_Init( void )
{
// struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
/*调用LWIP初始化函数,
初始化网络接口结构体链表、内存池、pbuf结构体*/
lwip_init();
#if LWIP_DHCP //若使用DHCP协议
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else //
/* //by iris 2013.6.6 //
IP4_ADDR(&ipaddr, 192, 168, 0, 18); //设置网络接口的ip地址
IP4_ADDR(&netmask, 255, 255, 255, 0); //子网掩码
IP4_ADDR(&gw, 192, 168, 0, 1); //网关
*/ //by iris 2013.6.6 //
// IP4_ADDR(&ipaddr, 192, 168, 1, 18); //设置网络接口的ip地址
IP4_ADDR(&ipaddr, iris_IP[0], iris_IP[1], iris_IP[2], iris_IP[3]); //设置网络接口的ip地址
IP4_ADDR(&netmask, 255, 255, 255, 0); //子网掩码
IP4_ADDR(&gw, 192, 168, 1, 1); //网关
#endif
/*初始化enc28j60与LWIP的接口,参数为网络接口结构体、ip地址、
子网掩码、网关、网卡信息指针、初始化函数、输入函数*/
netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/*把enc28j60设置为默认网卡 .*/
netif_set_default(&enc28j60);
#if LWIP_DHCP //若使用了DHCP
/* Creates a new DHCP client for this interface on the first call.
Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
the predefined regular intervals after starting the client.
You can peek in the netif->dhcp struct for the actual DHCP status.*/
dhcp_start(&enc28j60); //启动DHCP
#endif
/* When the netif is fully configured this function must be called.*/
netif_set_up(&enc28j60); //使能enc28j60接口
}
#define ENC_SPI_Init SPI1_Init
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIOA clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, F_ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, F_ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //CS
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOC, &GPIO_InitStructure); //BY IRIS 2013.6.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure SPI1 pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//by iris 2013.6.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
//by iris 2013.6.3
/* SPI1 configuration */
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_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, F_ENABLE);
}
|