ENC28J60 TCP/IP Communication of 51 Single Chip Microcomputer

Keywords: network Web Server

Links to the original text: https://blog.csdn.net/a_a666/article/details/78277628

Reprint address
https://blog.csdn.net/a_a666/article/details/78277628

ENC28J60 TCP/IP Communication of 51 Single Chip Microcomputer

  • SCM: STC90C516RD+
  • ENC28J60 module: mini(3.3V power supply)
  • Equipment: PC, Development Board, Two Networks, Router

Firstly, this paper introduces the transplantation of TCP Server for MCU and TCP Client for PC. It realizes the communication between MCU and PC (network debugging assistant). It does not involve http and remote port service.

Configuration of ENC28J60
1. Introduction of ENC28J60 module
VCC,GND VCC3.3V,5V 4 SPI 4
2. The function details of ENC28J60 can be found in the blog on CSDN. The routines of the modules bought include ENC28J60.c and ENC28J60.h files, which basically need not be changed. The unnecessary parts can be deleted to reduce the code capacity.
3. Call it in the main function.
Here my calling function is dev_init(); this is because the initialization function is encapsulated in the ENC28J60.c file.

//Principal function
dev_init();//Initialization of network card by calling this function
//ENC28J60.c
void dev_init(void)
{
    enc28j60_init();
}

II. Transplantation of UIP
This is because the TCP protocol is hierarchical. The protocol stack of UIP is at the bottom, which is equivalent to the underlying program. It does not need to be written by itself. There are routines in general. According to the needs of my project, I transplant uip.c, uipopt.h, uip_arp.c and uip_arch.c into the project. This file is mainly used to configure IP address (MCU), gateway and network mask. These can enter the command window, input configip, press Enter key, IP parameters will be there. Note: The IP of the board is different from that of the computer.
Here we mainly introduce the important use of uip

//Use is to note that the UIP_FIXEDADDR in uip_popt.h is changed to 0, otherwise error will occur. This is the active configuration of IP.
    uip_ipaddr(ipaddr,192,168,1,113);  //Configuration board ip
    uip_sethostaddr(ipaddr);//  uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr,192,168,1,1);    //configure gateway
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr,255,255,255,0);   //Configure subnet mask
    uip_setnetmask(ipaddr);
uip_send(neirong,len); first of all, this function is quite special. At the beginning of using this function, I thought it was no different from the ordinary sending function. After calling it, I could send it, but I didn't realize that this function can not send data actively. In the TCP event processing function appcall(), we can use sending data. This function can only send out the last sent content. Baidu online, found that there is no good way for MCU to actively send, and finally, find a framework that can actively send content TCP protocol, etc. specific to the later introduction of TCP services in detail.
Uip_listen (HTONS (8000); //listen on local port 8000, which port setting of TCP Client on TCP network debugging assistant
 Let's start with something that must be used.
 III. TCP Sever Program
 This part of the content can be said to be the configuration of IP to the upper layer.
 The main thing is the Allcall function.

//This is a TCP server application callback function.
//This function is called by UIP_APPCALL(tcp_demo_appcall) to realize the function of Web Server.
//When a uip event occurs, the UIP_APPCALL function is called to determine whether to execute the function based on the port (1200).
//For example, when a TCP connection is created, new data arrives, data has been answered, and data needs to be retransmitted.
void tcp_server_demo_appcall(void)
{
    struct tcp_demo_appstate *s = (struct tcp_demo_appstate *)&uip_conn->appstate;
    if(uip_aborted())tcp_server_aborted();      //Connection termination
    if(uip_timedout())tcp_server_timedout();    //connection timed out   
    if(uip_closed())tcp_server_closed();        //Connection closure     
    if(uip_connected())tcp_server_connected();  //Successful connection      
    if(uip_acked())tcp_server_acked();          //Successful delivery of transmitted data 
    //Receive a new TCP packet 
    if (uip_newdata())//Receive data from client
    {
        if((tcp_server_sta&(1<<6))==0)//No data has been received yet.
        {
            if(uip_len>199)
            {          
                ((unsigned char*)uip_appdata)[199]=0;
            }   
            strcpy((char*)tcp_server_databuf,uip_appdata);                        
            tcp_server_sta|=1<<6;//Represents receipt of client data
        }
    }else if(tcp_server_sta&(1<<5))//Data needs to be sent
    {
        s->textptr=tcp_server_databuf;
        s->textlen=strlen((const char*)tcp_server_databuf);
        tcp_server_sta&=~(1<<5);//Clearance markers
    }   
    //Inform uip to send data when it is necessary to retransmit, new data arrival, data packet delivery, connection establishment 
    if(uip_rexmit()||uip_newdata()||uip_acked()||uip_connected()||uip_poll())
    {
        tcp_server_senddata();
    }
}

Finally, the program with the main function

#include "uip.h" 
#include "uip_arp.h"
#include "enc28j60.h"
#include "tcp_demo.h"
#include "USART.h"
#include "stdio.h"
#include <string.h>
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])

#ifndef NULL
#define NULL (void *)0
#endif /* NULL */
/*-----------------------------------------------------------------------------------*/
int
main(void)
{
    unsigned char tcnt=0;
    unsigned char tcp_server_tsta=0XFF;
    idata u8_t i, arptimer;
    idata u16_t j;
    idata u16_t ipaddr[2];
    USART_Init();
    SendString("ENC28J60 Test Start...\r\n");
    /* Initialize the device driver. */ 
    dev_init();
    uip_arp_init();
    /* Initialize the uIP TCP/IP stack. */
    uip_init();
    SendString("http://ag-embeded.taobao.com\r\n");
    /* Initialize the HTTP server. */
    uip_ipaddr(ipaddr,192,168,1,113);  //Configure ip
    uip_sethostaddr(ipaddr);//  uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr,192,168,1,1);    //configure gateway
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr,255,255,255,0);   //Configure subnet mask
    uip_setnetmask(ipaddr);
    uip_listen(HTONS(8000));//tcp_server_init();
    arptimer = 0;
    SendString("http://shop64454242.taobao.com\r\n");
thanks"));
  while(1) {
    /* Let the tapdev network device driver read an entire IP packet
       into the uip_buf. If it must wait for more than 0.5 seconds, it
       will return with the return value 0. If so, we know that it is
       time to call upon the uip_periodic(). Otherwise, the tapdev has
       received an IP packet that is to be processed by uIP. */
    uip_len = dev_poll();
    for(j=0;j<500;j++);
    if(uip_len == 0) {
      for(i = 0; i < UIP_CONNS; i++) {
    uip_periodic(i);
    /* If the above function invocation resulted in data that
       should be sent out on the network, the global variable
       uip_len is set to a value > 0. */
    if(uip_len > 0) {
      uip_arp_out();
      dev_send();
    }
      }

#if UIP_UDP
      for(i = 0; i < UIP_UDP_CONNS; i++) {
    uip_udp_periodic(i);
    /* If the above function invocation resulted in data that
       should be sent out on the network, the global variable
       uip_len is set to a value > 0. */
    if(uip_len > 0) {
      uip_arp_out();
      dev_send();
    }
      }
#endif /* UIP_UDP */

      /* Call the ARP timer function every 10 seconds. */
      if(++arptimer == 20) 
      { 
            uip_arp_timer();
            arptimer = 0;
      }
      } else {
      if(BUF->type == htons(UIP_ETHTYPE_IP)) {
    uip_arp_ipin();
    uip_input();
    /* If the above function invocation resulted in data that
       should be sent out on the network, the global variable
       uip_len is set to a value > 0. */
    if(uip_len > 0) {
      uip_arp_out();
      dev_send();
    }
      } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
    uip_arp_arpin();
    /* If the above function invocation resulted in data that
       should be sent out on the network, the global variable
       uip_len is set to a value > 0. */    
    if(uip_len > 0) {   
      dev_send();
    }
      }
    }
    if(tcp_server_tsta!=tcp_server_sta)//TCP Server State Change
    {                                                            
        if(tcp_server_sta&(1<<7))   SendString("TCP Server Connected   ");
        else    SendString("TCP Server Disconnected");
        if(tcp_server_sta&(1<<6))   //Receiving new data
        {
            SendString("in up");//print data
            tcp_server_sta&=~(1<<6);       //Markup data has been processed 
        }
        tcp_server_tsta=tcp_server_sta;
    }   
  }
    if(Button == 0)//TCP Server Request Send Data Button Press Send Data
    {
        if(tcp_server_sta&(1<<7))   //Connections still exist
        {
            sprintf((char*)tcp_server_databuf,"TCP Server OK \r\n");     
            tcp_server_sta|=1<<5;//Marked data needs to be sent
            tcnt++;
        }
    }

  return 0;
}

Reprint some information
http://blog.csdn.net/kjlrzzyffmx/article/details/47292135
http://www.360doc.com/content/14/1127/15/20642619_428506515.shtml
http://m.eeworld.com.cn/ic_article/267/33638.html

Posted by Jack Sparrow on Tue, 10 Sep 2019 03:09:40 -0700